Skip to content

Instantly share code, notes, and snippets.

@andreabedini
Created August 3, 2022 14:38
Show Gist options
  • Save andreabedini/9647abb8b3d647a4020fd07e568a85c4 to your computer and use it in GitHub Desktop.
Save andreabedini/9647abb8b3d647a4020fd07e568a85c4 to your computer and use it in GitHub Desktop.
git wrapper to make cabal and hls cache git checkouts more efficiently
#!/usr/bin/env bash
# only subvert cabal
PARENT_COMMAND=$(ps -o comm= $PPID)
if [ "$PARENT_COMMAND" != "cabal" ]; then
exec /usr/bin/git "$@"
fi
echo "$PWD /usr/bin/git $*" >> ~/git-log
case $1 in
clone)
srcurl="$3"
# normalise url by stripping .git suffix (perhaps only appropriate for
# github?
srcurl=${srcurl%.git}
dstdir="$4"
cachedir="$HOME/.cache/git/${srcurl#*//}"
if [ ! -d "$cachedir" ]; then
/usr/bin/git clone --mirror "$srcurl" "$cachedir"
else
# Automatic garbage collection
/usr/bin/git -C "$cachedir" worktree prune
fi
# Set up the worktree
/usr/bin/git -C "$cachedir" worktree add --force --no-checkout "$dstdir"
;;
checkout)
# do the thing, if it fails try to fetch new refs and try again
if /usr/bin/git "$@"; then
echo "all good" >> ~/git-log
else
echo "trying git fetch" >> ~/git-log
/usr/bin/git fetch origin --force --recurse-submodules --tags --prune-tags 'refs/heads/*:refs/heads/*'
/usr/bin/git "$@"
fi
;;
fetch)
# ignore fetches, we do it opportunistically as part of checkout
;;
*)
/usr/bin/git "$@"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment