Skip to content

Instantly share code, notes, and snippets.

@dansimau
Last active March 31, 2020 11:48
Show Gist options
  • Save dansimau/0dc4d1060d9ff098704042edcb946d3e to your computer and use it in GitHub Desktop.
Save dansimau/0dc4d1060d9ff098704042edcb946d3e to your computer and use it in GitHub Desktop.
Prints a reminder if you haven't fetched from a git repo for some period of time
#!/bin/bash
#
# Print a reminder if a fetch has not been done in n hours.
#
# There are multiple ways you could invoke this; for example, to have it run
# whenever you change directory you could do:
#
# cd() { command cd "$@"; ~/bin/git-fetch-reminder.sh 2>/dev/null; }
#
declare -r fetch_threshold_hours=6
main() {
local fetch_head
local repo_root
local verbose=false
if [ "${1:-}" == "-v" ]; then
verbose=true
fi
repo_root=$(git rev-parse --show-toplevel 2> /dev/null)
if [ -z "$repo_root" ]; then
echo "ERROR: Not in a repo" >&2
return 1
fi
fetch_head="${repo_root}/.git/FETCH_HEAD"
if [ -z "fetch_head" ]; then
echo "${fetch_head} is missing. You may want to do a \`git fetch\`."
return 0
fi
local last_fetch
local last_fetch_readable
local fetch_threshold
case $OSTYPE in
darwin*)
last_fetch="$(stat -f '%m' $fetch_head)"
last_fetch_readable="$(date -r ${last_fetch})"
fetch_threshold="$(date -v-${fetch_threshold_hours}H +%s)"
;;
*)
last_fetch="$(stat -c %Y $fetch_head)"
last_fetch_readable="$(date -d ${last_fetch})"
fetch_threshold="$(date -d'${fetch_threshold_hours} hours ago' +%s)"
;;
esac
if $verbose; then
echo "Info: Last fetch: ${last_fetch_readable}"
fi
if [ $last_fetch -lt $fetch_threshold ]; then
echo "Warning: Last fetch was at ${last_fetch_readable}; you may want to do a \`git fetch\`."
fi
}
# Run main if not being sourced
if [ "$0" == "$BASH_SOURCE" ]; then
main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment