Skip to content

Instantly share code, notes, and snippets.

@lann
Created December 13, 2021 15:10
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 lann/8c6830dd086104ecefbbfee291c516ac to your computer and use it in GitHub Desktop.
Save lann/8c6830dd086104ecefbbfee291c516ac to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eu
case "${1:-now}" in
now) ;;
log) git reflog 'AUTOSAVE@{now}'; exit 0 ;;
expire) git reflog expire AUTOSAVE; exit 0 ;;
*) echo "git autosave [now,log,expire]"; exit 1 ;;
esac
orig_index="$(git rev-parse --git-path index)"
export GIT_INDEX_FILE="$(git rev-parse --git-path autosave-index)"
[ -e "${GIT_INDEX_FILE}" ] \
|| cp "${orig_index}" "${GIT_INDEX_FILE}"
git add -u
tree="$(git write-tree)"
[ -e "$(git rev-parse --git-path AUTOSAVE)" ] \
&& git diff AUTOSAVE --quiet --exit-code \
&& exit 0 \
|| true
commit="$(git commit-tree -m autosave -p HEAD "${tree}")"
git update-ref AUTOSAVE "${commit}" --create-reflog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment