Skip to content

Instantly share code, notes, and snippets.

@benyanke
Last active April 28, 2022 12:34
Show Gist options
  • Save benyanke/031a8d31cc263f5b95cbcdc38ed1f331 to your computer and use it in GitHub Desktop.
Save benyanke/031a8d31cc263f5b95cbcdc38ed1f331 to your computer and use it in GitHub Desktop.
BASH: Reminder to 'git pull'. This function hooks into CD to remind you to 'git pull' when entering a repository directory. It will only alert you every X seconds.
cd() {
# Seconds between alerts: raise this
# number if you want to get fewer alerts
# Default: 10 min
alertDelay=600
# Alert text: this is displayed when you
# CD into a git repo
alertText="\n##################################################\n This is a git repo. Don't forget to 'git pull'\n##################################################\n"
######
## Nothing to change beyond here
######
# Run CD command
builtin cd $1
# Check if current dir is repo
git status >/dev/null 2>&1
if [ $? == 0 ]; then
# Make sure there's at least one commit before continuing
git rev-parse HEAD &> /dev/null
if [ $? != 0 ]; then
return
fi
# If current dir is a git repo,
# then set up tools for alert
repoRoot="$(git rev-parse --show-toplevel)"
# This is a repository identifer
# used to track when alerts are
# displayed for each repo.
repoId="$(git rev-list --parents HEAD | tail -1)"
# This is the file containing the
# Unix timestamp of the last alert
statusFile="/tmp/git-cd-alerter/$repoId"
# Get the unix timestamp
timestamp=$(date +%s)
if [ -s $statusFile ]; then
fileTime=$(cat $statusFile);
timeDiff=`expr $timestamp - $fileTime`
if [ "$timeDiff" -gt "$alertDelay" ]; then
echo -e $alertText;
echo $timestamp > /tmp/git-cd-alerter/$repoId
fi
else
echo -e $alertText;
mkdir -p /tmp/git-cd-alerter/
echo $timestamp > /tmp/git-cd-alerter/$repoId
fi
else
# If not, exit function now
return
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment