Task IConfigurableComponent.UnconfigureAsync( IEnvironment environment, EnvironmentVariableTarget environmentTarget, IGit git, GitConfigurationLevel configurationLevel) { string helperKey = $"{Constants.GitConfiguration.Credential.SectionName}.{Constants.GitConfiguration.Credential.Helper}"; string gitConfigAppName = GetGitConfigAppName(); IGitConfiguration targetConfig = git.GetConfiguration(configurationLevel); Context.Trace.WriteLine("Removing Git credential helper configuration..."); // Clear any blank 'reset' entries targetConfig.UnsetAll(helperKey, Constants.RegexPatterns.Empty); // Clear GCM executable entries targetConfig.UnsetAll(helperKey, Regex.Escape(gitConfigAppName)); // NOTE: We currently only update the PATH in Windows installations and leave removing the GCM executable // on the PATH on other platform to their installers. // Remove GCM executable from the PATH if (PlatformUtils.IsWindows()) { Context.Trace.WriteLine("Removing application from the PATH..."); string directoryPath = Path.GetDirectoryName(_appPath); environment.RemoveDirectoryFromPath(directoryPath, environmentTarget); } return(Task.CompletedTask); }
Task IConfigurableComponent.ConfigureAsync( IEnvironment environment, EnvironmentVariableTarget environmentTarget, IGit git, GitConfigurationLevel configurationLevel) { // NOTE: We currently only update the PATH in Windows installations and leave putting the GCM executable // on the PATH on other platform to their installers. if (PlatformUtils.IsWindows()) { string directoryPath = Path.GetDirectoryName(_appPath); if (!environment.IsDirectoryOnPath(directoryPath)) { Context.Trace.WriteLine("Adding application to PATH..."); environment.AddDirectoryToPath(directoryPath, environmentTarget); } else { Context.Trace.WriteLine("Application is already on the PATH."); } } string helperKey = $"{Constants.GitConfiguration.Credential.SectionName}.{Constants.GitConfiguration.Credential.Helper}"; string gitConfigAppName = GetGitConfigAppName(); IGitConfiguration targetConfig = git.GetConfiguration(configurationLevel); /* * We are looking for the following to be considered already set: * * [credential] * ... # any number of helper entries * helper = # an empty value to reset/clear any previous entries * helper = {gitConfigAppName} # the expected executable value in the last position & directly following the empty value * */ string[] currentValues = targetConfig.GetRegex(helperKey, Constants.RegexPatterns.Any).ToArray(); if (currentValues.Length < 2 || !string.IsNullOrWhiteSpace(currentValues[currentValues.Length - 2]) || // second to last entry is empty currentValues[currentValues.Length - 1] != gitConfigAppName) // last entry is the expected executable { Context.Trace.WriteLine("Updating Git credential helper configuration..."); // Clear any existing entries in the configuration. targetConfig.UnsetAll(helperKey, Constants.RegexPatterns.Any); // 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. targetConfig.SetValue(helperKey, string.Empty); targetConfig.ReplaceAll(helperKey, Constants.RegexPatterns.None, gitConfigAppName); } else { Context.Trace.WriteLine("Credential helper configuration is already set correctly."); } return(Task.CompletedTask); }
Task IConfigurableComponent.UnconfigureAsync(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($"Unconfiguring 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) // // We should remove the {appPath} entry, and any blank entries immediately preceding IFF there are no more entries following. // Context.Trace.WriteLine("Removing Git credential helper configuration..."); string[] currentValues = config.GetAll(helperKey).ToArray(); int appIndex = Array.FindIndex(currentValues, x => Context.FileSystem.IsSamePath(x, appPath)); if (appIndex > -1) { // Check for the presence of a blank entry immediately preceding an app entry in the last position if (appIndex > 0 && appIndex == currentValues.Length - 1 && string.IsNullOrWhiteSpace(currentValues[appIndex - 1])) { // Clear the blank entry config.UnsetAll(helperKey, Constants.RegexPatterns.Empty); } // Clear app entry config.UnsetAll(helperKey, Regex.Escape(appPath)); } return(Task.CompletedTask); }
public void GitConfiguration_UnsetAll_All_ThrowsException() { string repoPath = CreateRepository(out _); string gitPath = GetGitPath(); var trace = new NullTrace(); var git = new GitProcess(trace, gitPath, repoPath); IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.All); Assert.Throws <InvalidOperationException>(() => config.UnsetAll("core.foobar", Constants.RegexPatterns.Any)); }
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); }
public void GitConfiguration_UnsetAll_UnsetsAllConfig() { string repoPath = CreateRepository(out string workDirPath); Git(repoPath, workDirPath, "config --local --add core.foobar foo1").AssertSuccess(); Git(repoPath, workDirPath, "config --local --add core.foobar foo2").AssertSuccess(); Git(repoPath, workDirPath, "config --local --add core.foobar bar1").AssertSuccess(); string gitPath = GetGitPath(); var trace = new NullTrace(); var git = new GitProcess(trace, gitPath, repoPath); IGitConfiguration config = git.GetConfiguration(GitConfigurationLevel.Local); config.UnsetAll("core.foobar", "foo*"); GitResult result = Git(repoPath, workDirPath, "config --local --get-all core.foobar"); Assert.Equal("bar1", result.StandardOutput.Trim()); }