diff --git a/package-lock.json b/package-lock.json index fe69ce7..e50553b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "git-config-user-profiles", - "version": "1.3.1", + "version": "1.3.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "git-config-user-profiles", - "version": "1.3.1", + "version": "1.3.4", "license": "MIT", "dependencies": { "simple-git": "~3.22.0", diff --git a/src/commands/EditUserProfileCommand.ts b/src/commands/EditUserProfileCommand.ts index 3b4236c..fc454ec 100644 --- a/src/commands/EditUserProfileCommand.ts +++ b/src/commands/EditUserProfileCommand.ts @@ -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 { private static instance: EditUserProfileCommand | null = null; @@ -36,7 +35,6 @@ export class EditUserProfileCommand implements ICommand { 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"); diff --git a/src/commands/PickUserProfileCommand.ts b/src/commands/PickUserProfileCommand.ts index 7d4a5c3..9160e52 100644 --- a/src/commands/PickUserProfileCommand.ts +++ b/src/commands/PickUserProfileCommand.ts @@ -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 { private static instance: PickUserProfileCommand | null = null; diff --git a/src/config.ts b/src/config.ts index 819d796..8fa1194 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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("profiles"); + const profiles = vscode.workspace.getConfiguration("gitConfigUser").get("profiles"); - if (profiles) { + if (profiles && profiles.length > 0) { // map all profiles in to profiles entity and return return profiles; } @@ -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(); @@ -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(); @@ -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 { diff --git a/src/util/gitManager.ts b/src/util/gitManager.ts index 279851d..99eca38 100644 --- a/src/util/gitManager.ts +++ b/src/util/gitManager.ts @@ -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"; @@ -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); diff --git a/src/util/utils.ts b/src/util/utils.ts index b70dbba..872b233 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -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 { @@ -128,7 +140,7 @@ export async function loadProfileInWizard(preloadedProfile: Profile): Promise