Skip to content

Commit

Permalink
fix: crash with old profiles.
Browse files Browse the repository at this point in the history
feat: migrate old profiles to new
  • Loading branch information
onlyutkarsh committed Feb 13, 2024
1 parent d0f2e63 commit cbbd561
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
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.

2 changes: 0 additions & 2 deletions src/commands/EditUserProfileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Profile } from "../models";
import * as util from "../util";
import * as gm from "../util/gitManager";
import { ICommand, Result } from "./ICommand";

export class EditUserProfileCommand implements ICommand<boolean> {
private static instance: EditUserProfileCommand | null = null;

Expand Down Expand Up @@ -36,7 +35,6 @@ export class EditUserProfileCommand implements ICommand<boolean> {
if (updatedProfile.id) {
await saveVscProfile(updatedProfile, updatedProfile.id);
} else {
// backward compatibility
await saveVscProfile(updatedProfile, updatedProfile.label);
}
vscode.commands.executeCommand(constants.CommandIds.GET_USER_PROFILE, "edited profile");
Expand Down
1 change: 0 additions & 1 deletion src/commands/PickUserProfileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Profile } from "../models";
import * as util from "../util";
import * as gm from "../util/gitManager";
import { ICommand, Result } from "./ICommand";

export class PickUserProfileCommand implements ICommand<Profile> {
private static instance: PickUserProfileCommand | null = null;

Expand Down
12 changes: 6 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ConfigurationTarget, workspace } from "vscode";
import * as vscode from "vscode";
import { Profile } from "./models";
import * as util from "./util";

export function getProfilesInSettings(): Profile[] {
const profiles = workspace.getConfiguration("gitConfigUser").get<Profile[]>("profiles");
const profiles = vscode.workspace.getConfiguration("gitConfigUser").get<Profile[]>("profiles");

if (profiles) {
if (profiles && profiles.length > 0) {
// map all profiles in to profiles entity and return
return profiles;
}
Expand All @@ -21,7 +21,7 @@ export async function saveVscProfile(profile: Profile, oldProfileId?: string): P
// user is updating existing profile, no need to make changes to selected field
existingProfileIndex = profiles.findIndex((x) => {
if (x.id) {
return x.id?.toLowerCase() === oldProfileId.toLowerCase();
return x.id === oldProfileId;
} else {
// for backward compatibility with old profiles without id
return x.label.toLowerCase() === oldProfileId.toLowerCase();
Expand All @@ -31,7 +31,7 @@ export async function saveVscProfile(profile: Profile, oldProfileId?: string): P
// user is making a selection of profile (not updating the profile), so set selected to false
existingProfileIndex = profiles.findIndex((x) => {
if (x.id) {
return x.id?.toLowerCase() === profile.id?.toLowerCase();
return x.id === profile.id;
} else {
// for backward compatibility with old profiles without id
return x.label.toLowerCase() === profile.label.toLowerCase();
Expand All @@ -50,7 +50,7 @@ export async function saveVscProfile(profile: Profile, oldProfileId?: string): P
} else {
profiles.push(profile);
}
await workspace.getConfiguration("gitConfigUser").update("profiles", profiles, ConfigurationTarget.Global);
await vscode.workspace.getConfiguration("gitConfigUser").update("profiles", profiles, true);
}

export function getVscProfile(profileName: string): Profile | undefined {
Expand Down
17 changes: 17 additions & 0 deletions src/util/gitManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { basename } from "path";
import { simpleGit, SimpleGit } from "simple-git";
import { v4 as uuidv4 } from "uuid";
import * as vscode from "vscode";
import { Result } from "../commands/ICommand";
import { getProfilesInSettings } from "../config";
Expand Down Expand Up @@ -161,6 +162,22 @@ export async function getWorkspaceStatus(): Promise<{
};
}
const profilesInVscConfig = getProfilesInSettings();
//migrate all old profiles to new format
let saveConfig = false;
profilesInVscConfig.forEach((x) => {
if (x.id === undefined || x.id === "" || x.signingKey == undefined || x.signingKey == null) {
saveConfig = true;
}
if (!x.id) {
x.id = uuidv4();
}
if (!x.signingKey) {
x.signingKey = "";
}
});
if (saveConfig) {
await vscode.workspace.getConfiguration("gitConfigUser").update("profiles", profilesInVscConfig, true);
}
const selectedProfileInVscConfig = profilesInVscConfig.filter((x) => x.selected === true) || [];
const selectedVscProfile: Profile | undefined = selectedProfileInVscConfig.length > 0 ? selectedProfileInVscConfig[0] : undefined;
const currentGitConfig = await getCurrentGitConfig(folder);
Expand Down
30 changes: 21 additions & 9 deletions src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,32 @@ export function trimProperties(profile: Profile): Profile {
selected: profile.selected,
detail: undefined,
id: profile.id,
signingKey: profile.signingKey.trim(),
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;
}
return (
!isNameAndEmailEmpty(profile1) &&
!isNameAndEmailEmpty(profile2) &&
profile1.email.toLowerCase() === profile2.email.toLowerCase() &&
profile1.userName.toLowerCase() === profile2.userName.toLowerCase() &&
profile1.signingKey.toLowerCase() === profile2.signingKey.toLowerCase()
);
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();
}
if (profile1.signingKey && profile2.signingKey) {
signingKeySame = profile1.signingKey.toLowerCase() === profile2.signingKey.toLowerCase();
}
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;
}

return userNameSame && emailSame && signingKeySame;
}

export function isNameAndEmailEmpty(profile: { email: string; userName: string }): boolean {
Expand Down Expand Up @@ -128,7 +140,7 @@ export async function loadProfileInWizard(preloadedProfile: Profile): Promise<Pr
profileEmail: preloadedProfile.email,
profileUserName: preloadedProfile.userName,
profileName: preloadedProfile.label || "",
profileId: preloadedProfile.id,
profileId: preloadedProfile.id || "",
profileSelected: preloadedProfile.selected,
profileSigningKey: preloadedProfile.signingKey,
};
Expand Down

0 comments on commit cbbd561

Please sign in to comment.