Skip to content

Commit

Permalink
fix: logic of comparing of git config
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyutkarsh committed Feb 15, 2024
1 parent 6726d02 commit 5bf97cd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dbaeumer.vscode-eslint",
"kavod-io.vscode-jest-test-adapter",
"hbenl.vscode-test-explorer",
"esbenp.prettier-vscode"
"esbenp.prettier-vscode",
"usernamehw.errorlens"
],
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@
"@types/glob": "^8.1.0",
"@types/jest": "^29.5.12",
"@types/node": "~20.11.17",
"@types/uuid": "^9.0.8",
"@types/vscode": "^1.83.0",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"@vscode/vsce": "^2.23.0",
"esbuild": "^0.20.0",
"eslint": "^8.56.0",
"fs-extra": "^11.2.0",
Expand All @@ -79,9 +81,7 @@
"simple-get": "^4.0.1",
"source-map-support": "^0.5.21",
"ts-jest": "^29.1.2",
"typescript": "~5.3.3",
"@types/uuid": "^9.0.8",
"@vscode/vsce": "^2.23.0"
"typescript": "~5.3.3"
},
"dependencies": {
"simple-git": "~3.22.0",
Expand Down
19 changes: 15 additions & 4 deletions src/util/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export async function isGitRepository(path: string): Promise<boolean> {
}

export enum WorkspaceStatus {
ConfigOutofSync,
FieldsMissing,
NoProfilesInConfig,
NoSelectedProfilesInConfig,
Expand Down Expand Up @@ -188,7 +189,7 @@ export async function getWorkspaceStatus(): Promise<{
message: "No profiles found in settings.",
profilesInVSConfigCount: 0,
selectedProfile: selectedVscProfile,
configInSync: configInSync,
configInSync: configInSync.result,
currentFolder: folder,
};
}
Expand All @@ -198,7 +199,7 @@ export async function getWorkspaceStatus(): Promise<{
message: "No profiles selected in settings.",
profilesInVSConfigCount: profilesInVscConfig.length,
selectedProfile: selectedVscProfile,
configInSync: configInSync,
configInSync: configInSync.result,
currentFolder: folder,
};
}
Expand All @@ -208,7 +209,17 @@ export async function getWorkspaceStatus(): Promise<{
message: "One of label, userName or email properties is missing in the config. Please verify.",
profilesInVSConfigCount: profilesInVscConfig.length,
selectedProfile: selectedVscProfile,
configInSync: configInSync,
configInSync: configInSync.result,
currentFolder: folder,
};
}
if (!configInSync.result) {
return {
status: WorkspaceStatus.ConfigOutofSync,
message: configInSync.message || "Git config is not in sync with the selected profile.",
profilesInVSConfigCount: profilesInVscConfig.length,
selectedProfile: selectedVscProfile,
configInSync: configInSync.result,
currentFolder: folder,
};
}
Expand All @@ -217,7 +228,7 @@ export async function getWorkspaceStatus(): Promise<{
message: "",
profilesInVSConfigCount: profilesInVscConfig.length,
selectedProfile: selectedVscProfile,
configInSync: configInSync,
configInSync: configInSync.result,
currentFolder: folder,
};
}
58 changes: 38 additions & 20 deletions src/util/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as vscode from "vscode";
import { window } from "vscode";
import { Result } from "../commands/ICommand";
import { getProfilesInSettings, getVscProfile } from "../config";
import { Messages } from "../constants";
import * as constants from "../constants";
import * as controls from "../controls";
import { Profile } from "../models";
import * as util from "../util";
Expand All @@ -24,7 +25,7 @@ export function isBlank(str: string) {

export function validateProfileName(input: string, checkForDuplicates = true) {
if (isEmpty(input) || isBlank(input)) {
return Messages.ENTER_A_VALID_STRING;
return constants.Messages.ENTER_A_VALID_STRING;
}
if (checkForDuplicates) {
const existingProfile = getVscProfile(input);
Expand All @@ -37,14 +38,14 @@ export function validateProfileName(input: string, checkForDuplicates = true) {

export function validateUserName(input: string) {
if (isEmpty(input) || isBlank(input)) {
return Messages.ENTER_A_VALID_STRING;
return constants.Messages.ENTER_A_VALID_STRING;
}
return undefined;
}
export function validateEmail(input: string) {
const validEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!validEmail.test(input)) {
return Messages.NOT_A_VALID_EMAIL;
return constants.Messages.NOT_A_VALID_EMAIL;
}
return undefined;
}
Expand All @@ -60,29 +61,46 @@ export function trimProperties(profile: Profile): Profile {
signingKey: profile.signingKey?.trim(),
};
}
export function isConfigInSync(profile1?: { email: string; userName: string; signingKey: string }, profile2?: { email: string; userName: string; signingKey: string }): boolean {
if (!profile1 || !profile2) {
return false;
export function isConfigInSync(
profile1?: { email: string; userName: string; signingKey: string },
profile2?: { email: string; userName: string; signingKey: string }
): Result<boolean> {
if (profile1 === null || profile1 === undefined || profile2 === null || profile2 === undefined) {
return {
result: false,
message: "One of the profiles is undefined. Cannot compare.",
};
}
let userNameSame = false;
let emailSame = false;
let signingKeySame = false;
if (profile1.userName && profile2.userName) {
userNameSame = profile1.userName.toLowerCase() === profile2.userName.toLowerCase();
}
if (profile1.email && profile2.email) {
emailSame = profile1.email.toLowerCase() === profile2.email.toLowerCase();

userNameSame = profile1.userName === profile2.userName;
if (!userNameSame) {
return {
result: false,
message: `User names are different.`,
};
}
if (profile1.signingKey && profile2.signingKey) {
signingKeySame = profile1.signingKey.toLowerCase() === profile2.signingKey.toLowerCase();

emailSame = profile1.email.toLowerCase() === profile2.email.toLowerCase();
if (!emailSame) {
return {
result: false,
message: `Emails are different.`,
};
}
if (profile1.signingKey === undefined || profile2.signingKey === undefined || profile1.signingKey === "" || profile2.signingKey === "") {
// backward compatibility with old profiles without signingKey
// if any profile does not have signing key, user is comparing old vs new profile, dont compare signing key
signingKeySame = true;
signingKeySame = profile1.signingKey.toLowerCase() === profile2.signingKey.toLowerCase();
if (!signingKeySame) {
return {
result: false,
message: `Signing keys are different.`,
};
}

return userNameSame && emailSame && signingKeySame;
return {
result: userNameSame && emailSame && signingKeySame,
message: `Profiles are in sync.`,
};
}

export function isNameAndEmailEmpty(profile: { email: string; userName: string }): boolean {
Expand Down

0 comments on commit 5bf97cd

Please sign in to comment.