Skip to content

Instantly share code, notes, and snippets.

@antoninkriz
Created April 24, 2018 18:21
Show Gist options
  • Save antoninkriz/e2e27cbedaf7ad2b79a6d26591a8f118 to your computer and use it in GitHub Desktop.
Save antoninkriz/e2e27cbedaf7ad2b79a6d26591a8f118 to your computer and use it in GitHub Desktop.
Generates shell script that merges multiple GIT repositories into subfolders into a new repository
/**
* Generates shell script that merges multiple GIT repositories into subfolders into a new repository
* This script will output only a shell script which will automatically do all this booring git magic for you
* This code is not mean to be effective as-a-code in any way
* This code is under WTFPL Licence: http://www.wtfpl.net/
* Tested on .Net Core 2.1.0 preview
*
*
* **EXAMPLE**
* You have <repos> A, B, C. You want to create a <new repo> X which will contain <repos> in subfolders:
* /<new repo>
* ./A
* ./B
* ./C
*
*
* **USAGE**
* cd /directory/where/you/want/all/things/to/happen
* wget https://gist.github.com/tonakriz/e2e27cbedaf7ad2b79a6d26591a8f118 -O GitMergeReposInSubfolders.cs
* nano GitMergeReposInSubfolders.cs
* # change **SETTINGS** variables according to your own needs and save
* chmod +x GitMergeReposInSubfolders.cs
* eval "$( dotnet run GitMergeReposInSubfolders.cs )"
* # wait... after script finishes, your all-in-one repo is in folder ./<NewRepoName>
*
*
* **SETTINGS**
* NewRepoName
* - name of the new repository
* Url
* - url to GIT server with {0} instead of repo name
* - example: https://github.com/USERNAME/{0}.git
* RepoNames
* - list of names of repositories you want to merge
*
* **COMMANDS**
* - Templates of shell commands that do all the magic you need
*
* Not-proud author: Antonin Kriz, https://antoninkriz.eu, https://github.com/tonakriz
*/
using System;
using System.Text;
namespace GitScriptGenerator
{
internal static class Program
{
//** SETTINGS **//
private const string NewRepoName = "NewRepoName";
private const string Url = "https://github.com/USERNAME/{0}.git";
private static readonly string[] RepoNames = {
"A",
"B",
"C"
};
//** COMMANDS **//
private const string TeplateInit = "git init {0};";
private const string TemplateClone = "git clone " + Url + ";";
private const string TemplateCd = "cd {0};";
private const string TemplateFilterBranch = "git filter-branch -f --index-filter 'git ls-files -s | awk -v prefix=\"{0}/\" \"{{ sub(/\\t/, \\\"\\t\\\" prefix); print}}\" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv \"$GIT_INDEX_FILE.new\" \"$GIT_INDEX_FILE\"' HEAD;";
private const string TemplateRemoteAddPull = "git remote add {0} ../{0};git pull {0} master;";
private const string TemplateRmRf = "rm -rf {0};";
//** DO MAGIC STUFF **//
private static void Main()
{
var sb = new StringBuilder();
sb.AppendFormat(TeplateInit, NewRepoName);
foreach (var s in RepoNames)
{
sb.AppendFormat(TemplateClone, s);
sb.AppendFormat(TemplateCd, s);
sb.AppendFormat(TemplateFilterBranch, s);
sb.AppendFormat(TemplateCd, "..");
}
sb.AppendFormat(TemplateCd, NewRepoName);
foreach (var s in RepoNames)
{
sb.AppendFormat(TemplateRemoteAddPull, s);
}
sb.AppendFormat(TemplateCd, "..");
foreach (var s in RepoNames)
{
sb.AppendFormat(TemplateRmRf, s);
}
Console.Write(sb.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment