Task IConfigurableComponent.ConfigureAsync(ConfigurationTarget target) { string helperKey = $"{Constants.GitConfiguration.Credential.SectionName}.{Constants.GitConfiguration.Credential.Helper}"; string appPath = GetGitConfigAppPath(); GitConfigurationLevel configLevel = target == ConfigurationTarget.System ? GitConfigurationLevel.System : GitConfigurationLevel.Global; Context.Trace.WriteLine($"Configuring for config level '{configLevel}'."); IGitConfiguration config = Context.Git.GetConfiguration(configLevel); // We are looking for the following to be set in the config: // // [credential] // ... # any number of helper entries (possibly none) // helper = # an empty value to reset/clear any previous entries (if applicable) // helper = {appPath} # the expected executable value & directly following the empty value // ... # any number of helper entries (possibly none, but not the empty value '') // string[] currentValues = config.GetAll(helperKey).ToArray(); // Try to locate an existing app entry with a blank reset/clear entry immediately preceding, // and no other blank empty/clear entries following (which effectively disable us). int appIndex = Array.FindIndex(currentValues, x => Context.FileSystem.IsSamePath(x, appPath)); int lastEmptyIndex = Array.FindLastIndex(currentValues, string.IsNullOrWhiteSpace); if (appIndex > 0 && string.IsNullOrWhiteSpace(currentValues[appIndex - 1]) && lastEmptyIndex < appIndex) { Context.Trace.WriteLine("Credential helper configuration is already set correctly."); } else { Context.Trace.WriteLine("Updating Git credential helper configuration..."); // Clear any existing app entries in the configuration config.UnsetAll(helperKey, Regex.Escape(appPath)); // Add an empty value for `credential.helper`, which has the effect of clearing any helper value // from any lower-level Git configuration, then add a second value which is the actual executable path. config.Add(helperKey, string.Empty); config.Add(helperKey, appPath); } return(Task.CompletedTask); }