Skip to content

Instantly share code, notes, and snippets.

@bdrupieski
Created March 11, 2017 18:39
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 bdrupieski/dc00ad1672bba7276788c37444f5e42f to your computer and use it in GitHub Desktop.
Save bdrupieski/dc00ad1672bba7276788c37444f5e42f to your computer and use it in GitHub Desktop.
Update origin for all git repositories in a folder
using System;
using System.Diagnostics;
using System.IO;
namespace App
{
public static class MigrateOrigins
{
public static void DoIt(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("This program updates all git repositories in a directory to a new URL.");
Console.WriteLine("There must be two arguments:");
Console.WriteLine("1. The base origin URL. Each repository name will be appended to the end.");
Console.WriteLine("2. Your directory of git repositories to update.");
}
else
{
string baseVsoGitUrl = args[0];
string directoryWithClonedRepoFolders = args[1];
var repoDirectories = new DirectoryInfo(directoryWithClonedRepoFolders).EnumerateDirectories();
foreach (var repoDirectory in repoDirectories)
{
var outcome = ExecuteGitMigrateOrigin(directoryWithClonedRepoFolders, baseVsoGitUrl, repoDirectory.Name);
Console.WriteLine(outcome);
}
}
}
private static string ExecuteGitMigrateOrigin(string parentPath, string baseUrl, string repoName)
{
var insideRepoFolder = Path.Combine(parentPath, repoName);
Process cmd = new Process
{
StartInfo =
{
FileName = "cmd.exe",
WorkingDirectory = insideRepoFolder,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false,
}
};
cmd.Start();
var command = $"git remote set-url origin {baseUrl}{repoName}";
cmd.StandardInput.WriteLine(command);
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
return cmd.StandardOutput.ReadToEnd();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment