Skip to content

Instantly share code, notes, and snippets.

@Schoaf
Forked from detunized/join.sh
Last active April 23, 2021 14:58
Show Gist options
  • Save Schoaf/f1d3fdf73852a19979a0aaf8dd3fc8aa to your computer and use it in GitHub Desktop.
Save Schoaf/f1d3fdf73852a19979a0aaf8dd3fc8aa to your computer and use it in GitHub Desktop.
Join repos into subfolders with flat history
#!/bin/bash
set -euo pipefail
REPO_DIR="/c/_WORK/eclipse-workspace"
repos="activation-code-service advisor-service contract-revenue-service email-service email-template-service file-service login-service soap-client soap-service translation-service vehicle-registration-service weather-alert-service"
# pull all repos
(
for repo in $repos; do
echo $repo
cd $REPO_DIR/$repo
git pull -v --ff
done
)
# merge
(
rm -rf joined
mkdir joined
cd joined
git init .
for repo in $repos; do
git remote add $repo $REPO_DIR/$repo
git fetch $repo
git checkout -b $repo $repo/master
echo -n "$repo: " > prefix
git filter-branch \
-f \
--tree-filter "mkdir -p .original/$repo && rsync -a --remove-source-files ./ .original/$repo/" \
--msg-filter "cat $(pwd)/prefix -" \
--env-filter 'GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"' \
$repo
rm prefix
done
# create master
git checkout --orphan master
git rm -rf .
# cherry pick into the master
for i in $(git log --pretty='%H' --author-date-order --reverse $repos); do
GIT_COMMITTER_DATE=$(git log -1 --pretty='%at' $i) \
git cherry-pick $i
done
# cleanup
for repo in $repos; do
git branch -D $repo
git remote remove $repo
git update-ref -d refs/original/refs/heads/$repo
done
git gc --aggressive
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment