Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created December 15, 2014 10:26
Show Gist options
  • Save chgeuer/d7f1d8e2b960eae2f226 to your computer and use it in GitHub Desktop.
Save chgeuer/d7f1d8e2b960eae2f226 to your computer and use it in GitHub Desktop.
Changes the hard-coded origin https password for all repos in a directory
namespace ChangePasswordForOrigin
{
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
var repoRoot = @"C:\Users\chgeuer\github\chgeuer";
var githubUser = "chgeuer";
var oldPass = "secret123";
var newPass = "deadbeef1234512345123451234512345"; // https://github.com/settings/tokens/new
Func<string, string, string> run = (dir, args) => {
var process = new Process
{
StartInfo = new ProcessStartInfo
{
WorkingDirectory = dir,
FileName = "git.exe",
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
},
EnableRaisingEvents = true
};
var sb = new StringBuilder();
Action<DataReceivedEventArgs> outputDataReceived = _ => sb.AppendLine(_.Data);
Action<DataReceivedEventArgs> errorDataReceived = _ => sb.AppendLine(_.Data);
process.OutputDataReceived += (s, a) => outputDataReceived(a);
process.ErrorDataReceived += (s, a) => errorDataReceived(a);
process.Start();
if (process.StartInfo.RedirectStandardOutput) process.BeginOutputReadLine();
if (process.StartInfo.RedirectStandardError) process.BeginErrorReadLine();
process.WaitForExit();
return sb.ToString();
};
new DirectoryInfo(repoRoot)
.EnumerateDirectories("*.*", SearchOption.TopDirectoryOnly).ToList()
.Select(_ => _.FullName).ToList()
.ForEach(dir =>
{
var r = run(dir, "remote -v");
try
{
var lines = r.Split('\r', '\n').Where(_ => !string.IsNullOrEmpty(_)).ToList();
var url = Regex.Split(lines.First(), "\\s+").ToList()[1];
if (url.StartsWith(string.Format("https://{0}:{1}@github.com/", githubUser, oldPass)))
{
Console.WriteLine(url);
run(dir, "remote remove origin");
run(dir, "remote add origin " + url.Replace(":" + oldPass, ":" + newPass));
}
}
catch (Exception)
{
// Console.WriteLine(dir);
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment