From 0855320ccdec494b9b3789a6aaea945e011d12ea Mon Sep 17 00:00:00 2001 From: "adrian.buerli" Date: Fri, 21 Feb 2020 17:31:34 +0100 Subject: [PATCH 1/6] Add option to select branch to merge --- README.md | 12 +++++++----- git-join-repos | 11 +++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c17b407..3421a41 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Git Repository Join -Combines separate git repositories into a single monolithic git repository, prepending the source repo name to every commit message. It only includes master branches from each repository, but otherwise complete version history is maintained. Mainly just a convenient bash wrapper around git commands. +Combines separate git repositories into a single monolithic git repository, prepending the source repo name to every commit message. It only includes one selectable branche from each repository (default is master), but otherwise complete version history is maintained. Mainly just a convenient bash wrapper around git commands. ## Usage @@ -31,9 +31,10 @@ where repo_data.txt is in the format: repo1 git@github.com:username/repo1.git newname git@github.com:username/repo2.git repo3 /path/locally/repo3 +repo4 /path/locally/repo3 custom-branch ``` -each row is separated by a newline and the name and url are separated by a space character (tabs not supported!). +each row is separated by a newline and the name, the url and optionally a branch name are separated by spaces (tabs not supported!). ## Expected Result @@ -43,9 +44,10 @@ Your new top-level working directory will contain: repo1 newname repo3 +repo4 ``` -Where each subfolder contains the HEAD master commit from the corresponding source repository. n.b. old commit ids are lost in this process - so don't count on being able to reference your new repository with old commit ids. +Where each subfolder contains the HEAD commit of the selected branch from the corresponding source repository. n.b. old commit ids are lost in this process - so don't count on being able to reference your new repository with old commit ids. Each commit message will be retained, but prepended with the name of the source repository. @@ -53,13 +55,13 @@ Each commit message will be retained, but prepended with the name of the source ``` * c91baa3 (HEAD -> master, origin/master) repo3: git-join-repos monolithic conversion merge -|\ +|\ | * 106a74c repo3: add keyed message storage | * c48e9f2 repo3: add build/.npmignore | * 2766e11 repo3: fix build/.gitignore | * dcef023 repo3: initial commit of repo3 * b7530e7 newname: git-join-repos monolithic conversion merge -|\ +|\ | * 02f3536 newname: update sublibrary to versionf 1.4 ... ``` diff --git a/git-join-repos b/git-join-repos index 237e080..efb69ab 100755 --- a/git-join-repos +++ b/git-join-repos @@ -25,6 +25,7 @@ data_file="$1" name_urls="$(cat $data_file)" repo_name= repo_url= +repo_branch= function empty_repo_or_die { total="$(git count-objects -v | cut -d' ' -f2 | awk '{s+=$1}END{print s}')" @@ -36,8 +37,10 @@ function empty_repo_or_die { } function split_line { - repo_name="${a% *}" - repo_url="${a#* }" + readarray -d" " -t parts < <(echo -n "$a") + repo_name="${parts[0]}" + repo_url="${parts[1]}" + repo_branch="${parts[2]:-master}" } function repo_master_to_subdir { @@ -59,7 +62,7 @@ for a in $name_urls; do git rm -qrf --ignore-unmatch . git remote add "$repo_name" "$repo_url" git fetch "$repo_name" - git merge "$repo_name/master" + git merge "$repo_name/${repo_branch}" repo_master_to_subdir git remote remove "$repo_name" done @@ -70,6 +73,6 @@ git rm -qrf --ignore-unmatch . for a in $name_urls; do split_line "$a" - git merge --allow-unrelated-histories --ff -m "$repo_name: $self_name monolithic conversion merge" "$repo_name" + git merge --allow-unrelated-histories --ff -m "$self_name: merge ${repo_name}[${repo_branch}]" "$repo_name" git branch -D "$repo_name" done From 1eed8d568fcdfd189f580de5f2310a0507d08b1f Mon Sep 17 00:00:00 2001 From: "adrian.buerli" Date: Fri, 21 Feb 2020 17:45:02 +0100 Subject: [PATCH 2/6] Fix README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3421a41..c85a941 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Git Repository Join -Combines separate git repositories into a single monolithic git repository, prepending the source repo name to every commit message. It only includes one selectable branche from each repository (default is master), but otherwise complete version history is maintained. Mainly just a convenient bash wrapper around git commands. +Combines separate git repositories into a single monolithic git repository, prepending the source repo name to every commit message. It only includes one selectable branch ('master' by default) from each repository, but otherwise complete version history is maintained. Mainly just a convenient bash wrapper around git commands. ## Usage From 9584072ab8e8e3d01e9ac02d6a4706ab53f273d5 Mon Sep 17 00:00:00 2001 From: "adrian.buerli" Date: Fri, 21 Feb 2020 17:31:34 +0100 Subject: [PATCH 3/6] Include tags in history-rewriting --- git-join-repos | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-join-repos b/git-join-repos index efb69ab..b9af10f 100755 --- a/git-join-repos +++ b/git-join-repos @@ -44,12 +44,12 @@ function split_line { } function repo_master_to_subdir { - git filter-branch -f --index-filter \ + git filter-branch -f --tag-name-filter cat --index-filter \ "git ls-files -s | ${sed} \"s|\t\\\"*|&${repo_name}/|\" | GIT_INDEX_FILE=\$GIT_INDEX_FILE.new git update-index --index-info && mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" HEAD - git filter-branch -f --msg-filter "/bin/echo -n \"${repo_name}: \" && cat" + git filter-branch -f --tag-name-filter cat --msg-filter "/bin/echo -n \"${repo_name}: \" && cat" } From bcb31ec513d6ce3bd16fc960775099ee597979d8 Mon Sep 17 00:00:00 2001 From: "adrian.buerli" Date: Fri, 21 Feb 2020 17:31:34 +0100 Subject: [PATCH 4/6] Prune before history rewrite It doesn't hurt and seems to resolve rare cases where the "mv GIT_INDEX..." fails. --- git-join-repos | 1 + 1 file changed, 1 insertion(+) diff --git a/git-join-repos b/git-join-repos index b9af10f..7def46e 100755 --- a/git-join-repos +++ b/git-join-repos @@ -44,6 +44,7 @@ function split_line { } function repo_master_to_subdir { + git filter-branch -f --prune-empty --tag-name-filter cat -- --all git filter-branch -f --tag-name-filter cat --index-filter \ "git ls-files -s | ${sed} \"s|\t\\\"*|&${repo_name}/|\" | GIT_INDEX_FILE=\$GIT_INDEX_FILE.new git update-index --index-info && From 038520a5eee0d39acbbffc30fc043aba58352e1f Mon Sep 17 00:00:00 2001 From: "adrian.buerli" Date: Mon, 24 Feb 2020 11:11:03 +0100 Subject: [PATCH 5/6] Correct README: single space as separator --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c85a941..c9f734b 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ repo3 /path/locally/repo3 repo4 /path/locally/repo3 custom-branch ``` -each row is separated by a newline and the name, the url and optionally a branch name are separated by spaces (tabs not supported!). +each row is separated by a newline and the name, the url and optionally a branch name are separated a space character (tabs not supported!). ## Expected Result From a19c69525832b0cf9fb0c1a63cd2650a63a27fb6 Mon Sep 17 00:00:00 2001 From: "adrian.buerli" Date: Mon, 24 Feb 2020 11:14:13 +0100 Subject: [PATCH 6/6] README: fix grammar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c9f734b..0b46952 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ repo3 /path/locally/repo3 repo4 /path/locally/repo3 custom-branch ``` -each row is separated by a newline and the name, the url and optionally a branch name are separated a space character (tabs not supported!). +each row is separated by a newline and contains the name, the url and optionally a branch name separated by a space character (tabs not supported!). ## Expected Result