private async Task RemoveGitConfig(RunnerActionPluginExecutionContext executionContext, GitCliManager gitCommandManager, string targetPath, string configKey, string configValue) { int exitCode_configUnset = await gitCommandManager.GitConfigUnset(executionContext, targetPath, configKey); if (exitCode_configUnset != 0) { // if unable to use git.exe unset http.extraheader or core.askpass, modify git config file on disk. make sure we don't left credential. if (!string.IsNullOrEmpty(configValue)) { executionContext.Warning("An unsuccessful attempt was made using git command line to remove \"http.extraheader\" from the git config. Attempting to modify the git config file directly to remove the credential."); string gitConfig = Path.Combine(targetPath, ".git/config"); if (File.Exists(gitConfig)) { string gitConfigContent = File.ReadAllText(Path.Combine(targetPath, ".git", "config")); if (gitConfigContent.Contains(configKey)) { string setting = $"extraheader = {configValue}"; gitConfigContent = Regex.Replace(gitConfigContent, setting, string.Empty, RegexOptions.IgnoreCase); setting = $"askpass = {configValue}"; gitConfigContent = Regex.Replace(gitConfigContent, setting, string.Empty, RegexOptions.IgnoreCase); File.WriteAllText(gitConfig, gitConfigContent); } } } else { executionContext.Warning($"Unable to remove \"{configKey}\" from the git config. To remove the credential, execute \"git config --unset - all {configKey}\" from the repository root \"{targetPath}\"."); } } }
private async Task RemoveGitConfig(RunnerActionPluginExecutionContext executionContext, GitCliManager gitCommandManager, string targetPath, string configKey, string configValue) { int exitCode_configUnset = await gitCommandManager.GitConfigUnset(executionContext, targetPath, configKey); if (exitCode_configUnset != 0) { // if unable to use git.exe unset http.extraheader or core.askpass, modify git config file on disk. make sure we don't left credential. if (!string.IsNullOrEmpty(configValue)) { executionContext.Warning("An unsuccessful attempt was made using git command line to remove \"http.extraheader\" from the git config. Attempting to modify the git config file directly to remove the credential."); string gitConfig = Path.Combine(targetPath, ".git/config"); if (File.Exists(gitConfig)) { List <string> safeGitConfig = new List <string>(); var gitConfigContents = File.ReadAllLines(gitConfig); foreach (var line in gitConfigContents) { if (!line.Contains(configValue)) { safeGitConfig.Add(line); } } File.WriteAllLines(gitConfig, safeGitConfig); } } else { executionContext.Warning($"Unable to remove \"{configKey}\" from the git config. To remove the credential, execute \"git config --unset - all {configKey}\" from the repository root \"{targetPath}\"."); } } }