Skip to content

Instantly share code, notes, and snippets.

@jcayzac
Last active August 29, 2015 14:17
Show Gist options
  • Save jcayzac/258213c9ed0a4b0fc962 to your computer and use it in GitHub Desktop.
Save jcayzac/258213c9ed0a4b0fc962 to your computer and use it in GitHub Desktop.
git: import remote tags into a local bare repository
#!/usr/bin/env bash
set -e -u -o pipefail
# Similar to git clone --shallow, except it creates a local shallow
# tag for every remote tag and doesn't clone any branch
#
# @param $1 [Required] Git remote
r="$1"
p="${r##*/}"
p="${p%.git*}.git"
w=".${p}.$$"
a='The Team <[email protected]>'
printf 'Importing "%s" into "%s"...\n' "$r" "$p"
# Create a temporary repo
mkdir -p "$w"
cd "$w"
git init -q
# Add a README to the master branch
echo 'Use "git tag -l" to list revisions' >README
git add README
git commit -q --author="$a" -m "Added README"
# Loop thru all the remote tags
while read t
do
printf 'Importing tag "%s"...\n' "$t"
# Checkout the tag
git fetch -q "$r" "refs/tags/$t" 2>&1 | (grep -v 'no common commits' >&2 || true)
git checkout -q FETCH_HEAD
# Squash all the commits together
git reset -q --soft $(git rev-list --max-parents=0 HEAD)
git commit -q --amend --author="$a" -m "v$t"
# Create a local tag
git tag -a "$t" -m "v$t"
done < <(git ls-remote --tags "$r" | grep -v '\^{}$' | sed -E 's/^.*refs\/tags\///g')
git reset -q --hard master
git checkout -q master
# Repack everything
printf 'Packing...\n'
git gc --quiet --aggressive
# Make the repository bare
printf 'Exporting...\n'
cd ..
[ ! -d "$p" ] || rm -rf "$p"
git clone -q --bare "$w" "$p"
rm -rf "$w"
printf 'Done\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment