Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
Created April 19, 2020 19:20
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 gigamonkey/2d9bd900d32edc64b3e9523dcd6c981b to your computer and use it in GitHub Desktop.
Save gigamonkey/2d9bd900d32edc64b3e9523dcd6c981b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x
set -e
here=$(dirname $(realpath "$0"))
dir="$1"
mirrors="$2"
function incorporate {
name=$1
url=$2
before=$(git log --all --oneline | wc -l)
git remote add "$name" "$url"
git config --local "remote.$name.fetch" "+refs/heads/*:refs/heads/$name/*"
git config --local --add "remote.$name.fetch" "+refs/tags/*:refs/tags/$name/*"
git fetch --no-tags "$name"
git remote remove "$name"
after=$(git log --all --oneline | wc -l)
newlogs=$((after - before))
echo "After incorporating $name $newlogs changes."
}
function pushdown {
name=$1
branch=$2
if [ ! -z "$(git show-ref "$branch")" ]; then
echo "Pushing $branch into $name."
git switch -c pushdown "$branch"
dir=$(mktemp -d tmp.XXXX)
moved_something="false"
for f in * .*; do
if [[ "$f" != .git && "$f" != "$dir" && "$f" != "." && "$f" != ".." ]]; then
git mv "$f" "$dir"
moved_something="true"
fi
done
if [ "$moved_something" == "true" ]; then
git mv "$dir" "$name"
else
mv "$dir" "$name"
echo "No files in master branch." > "$name/empty-master.txt"
git add "$name/empty-master.txt"
fi
git commit --allow-empty -m "Moving $name to subdir."
git checkout master
git merge --allow-unrelated-histories -s recursive -X no-renames --no-ff -m "Merging $name to master." pushdown
git branch -d pushdown
echo "Done pushing $branch into $name."
sleep 1
else
echo "No branch $branch to merge into master."
git show-ref | grep "refs/heads/$name"
fi
}
mkdir -p "$dir"
cd "$dir"
if [ ! -d ".git" ]; then
git init
git commit --allow-empty -m "Creating combined repo."
sleep 1
fi
# Incorporate constituent repos and combine into master branch
ls -d "$here"/"$mirrors"/*.git | sort | while read -r repo; do
if git --git-dir "$repo" show-ref; then
name=$(basename "$repo" .git)
echo "Incorporating $name ..."
incorporate "$name" "$repo"
pushdown "$name" "$name"/master
else
echo "Skipping empty repo $repo."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment