Skip to content

Instantly share code, notes, and snippets.

@mohd-akram
Last active September 23, 2020 12:54
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 mohd-akram/37d3631e567f9570911541bbba334583 to your computer and use it in GitHub Desktop.
Save mohd-akram/37d3631e567f9570911541bbba334583 to your computer and use it in GitHub Desktop.
A script to shallow clone a repository up to the most recent version tag, or a specified tag/ref.
#!/bin/sh
# usage: shallow-clone <url> [ref]
url=$1
ref=$2
repo=$(basename "$url" .git)
head=$(git ls-remote "$url" HEAD | tail -n1 | cut -f1)
if [ ! "$head" ]; then exit 1; fi
if [ "$ref" ]; then
id=$(git ls-remote "$url" "$ref" "$ref^{}" | tail -n1 | cut -f1)
else
tr=$(git -c 'versionsort.suffix=-' \
ls-remote -t --sort="v:refname" "$url" | tail -n2)
ref=$(echo "$tr" | head -n1 | cut -f2)
id=$(echo "$tr" | tail -n1 | cut -f1)
fi
if [ ! "$id" ]; then
echo "`basename $0`: ref does not exist" >&2
exit 1
fi
if [ "$head" = "$id" ]; then
git clone --depth=1 "$url"
else
git clone --shallow-exclude="$ref" "$url" && (
cd "$repo" && git fetch --deepen=1
)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment