Last active
September 23, 2020 12:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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