Created
December 31, 2020 18:34
-
-
Save nick87720z/219d2cfb269b51792e3031d1006a0346 to your computer and use it in GitHub Desktop.
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 | |
# https://stackoverflow.com/a/37105443 | |
fatal() { | |
printf '%s\n' "$1" | |
exit 1 | |
} | |
path= | |
depth=1 | |
commit= | |
if [ $# -eq 0 ]; then | |
cat <<- endhelp | |
Usage: $(basename $0) [options] [path] | |
Truncate history in git repository at <path> either by depth or by commit. | |
Options: | |
-c, --commit COMMIT: shallow since COMMIT (default none, overrides depth if set) | |
-d, --depth N: shallow to depth N (default 1) | |
endhelp | |
exit 1 | |
fi | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
'-d'|'--depth') | |
depth="$2" | |
shift ;; | |
'-c'|'--commit') | |
commit="$2" | |
shift ;; | |
* ) path="$1" | |
esac | |
shift | |
done | |
if [ -z "$path" ]; then | |
path='.' | |
fi | |
path="$( realpath "$path" )" | |
if ! [ -d "$path" ]; then | |
fatal "$path : Not a directory" | |
fi | |
if ! [ -w "$path" ]; then | |
fatal "'Can't write to $path'" | |
fi | |
if ! [ -f "$path/HEAD" -o -d "$path/.git" ]; then | |
fatal "$path : Not a git repository" | |
fi | |
printf '%s\n' "Repository: $path" | |
cd "$path" | |
if [ -n "$commit" ]; then | |
git fetch --shallow-since $commit | |
else | |
git fetch --depth=1 | |
fi | |
git reflog expire --expire-unreachable=now --all | |
git gc --aggressive --prune=all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment