Skip to content

Instantly share code, notes, and snippets.

@andrewbolster
Last active June 23, 2020 14:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andrewbolster/2ed60be3592c41c9123b5c0b764dea4c to your computer and use it in GitHub Desktop.
Git Merge Hellscape
#!/bin/bash
usage() {
cat << EOF
This script imports a git repo (accessible from https://\$origin/\$user/\$repo) and all its history as subdirectory of a destination (available locally at \$dest)
It is designed for non-production, archival processes and may destroy everything you've ever loved because you looked at it funny. You have been warned.
The structure of the destination will end up something like this:
~/src
- \$dest
- origins
- \$origin
- \$user
- \$repo
Required Arguments:
-u|--user: The user that owns the repo to be imported
-r|--repo: The name of the repository to be imported
-d|--dest: The local name of the destination repository (assumed to be under ~/src)
-o|--origin: The git server that is the origin of the repo to be imported
EOF
}
if [ $# -le 6 ]; then
usage
exit 1
fi
while [[ "$#" -gt 0 ]]; do
case $1 in
-u|--user) user="$2"; shift ;;
-r|--repo) repo="$2"; shift ;;
-d|--dest) dest="$2"; shift ;;
-o|--origin) origin="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; usage; exit 1 ;;
esac
shift
done
tmp="/tmp/_${dest}_tmp"
echo "Importing $origin/$user/$repo into $dest"
rm -rf ~/src/$repo
cd ~/src
git clone https://$origin/$user/$repo
cd $repo
git filter-branch \
--tree-filter "mkdir -p $tmp/origin; git ls-files | cpio -pdumB $tmp/origin; git ls-files | xargs -d '\n' rm -r; find . -type d -empty -delete; mkdir -p origins/$origin/$user; mv $tmp/origin origins/$origin/$user/$repo/"\
--tag-name-filter cat --prune-empty -- --all
if [ $? -eq 0 ]; then
## WAIT PATIENTLY
cd ../$dest
git remote add $repo ../$repo
git fetch $repo --tags
git merge --allow-unrelated-histories $repo/master #Youre on your own if you want a different / multiple branch(es)...
git remote remove $repo
else
echo failed for $user/$repo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment