Skip to content

Instantly share code, notes, and snippets.

@jorupp
Created May 19, 2017 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorupp/fdaf773d8a0573fc894bdf4d8370e27c to your computer and use it in GitHub Desktop.
Save jorupp/fdaf773d8a0573fc894bdf4d8370e27c to your computer and use it in GitHub Desktop.
Find all git repos under a directory and check what branches they have that aren't merged into origin/master
string target = ".git";
string[] ignored = new [] { "node_modules", "bower_components" };
void Main()
{
var start = @"c:\projects";
var maxDepth = 4;
var dirs = Scan(start, maxDepth).ToList();
var results = dirs.AsParallel().Select(i => new { dir = i, data = GitStatus(i) }).ToDictionary(i => i.dir, i => i.data);
results.Dump();
}
string GitStatus(string directory) {
var proc = Process.Start(new ProcessStartInfo() {
RedirectStandardOutput = true,
WorkingDirectory = directory,
FileName = @"C:\Program Files\Git\cmd\git.exe",
Arguments = "branch --no-merge origin/master",
UseShellExecute = false,
});
var data = "";
proc.OutputDataReceived += (s, e) => {
data += e.Data;
};
proc.BeginOutputReadLine();
proc.WaitForExit();
return data;
}
IEnumerable<string> Scan(string start, int depth) {
string[] dirs = new string[0];
try {
dirs = Directory.GetDirectories(start);
} catch {
}
foreach(var dir in dirs) {
var name = Path.GetFileName(dir);
if(name == target) {
yield return start;
}
if(ignored.Contains(name)) {
continue;
}
if(depth > 1) {
foreach(var x in Scan(dir, depth-1)) {
yield return x;
}
}
}
}
@jorupp
Copy link
Author

jorupp commented May 19, 2017

The .Dump() is for LinqPad - if you use this elsewhere, just replace that with some calls to write out to console or wherever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment