Skip to content

Instantly share code, notes, and snippets.

@lambda-fairy
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save lambda-fairy/bd63627ed69d9919f6de to your computer and use it in GitHub Desktop.

Select an option

Save lambda-fairy/bd63627ed69d9919f6de to your computer and use it in GitHub Desktop.
git-retime: edit the timestamp of the HEAD commit
#!/bin/sh
set -e
USAGE=''
SUBDIRECTORY_OK=1
. "$(git --exec-path)/git-sh-setup"
if [ $# -gt 0 ]
then
usage
fi
date_file="$GIT_DIR/GIT_RETIME"
# Ensure only one instance of git-retime is running at a time
if ( set -o noclobber; echo 'locked' > "$date_file" ) 2>/dev/null
then
# Ensure the lock file is deleted on exit
trap "rm -f \"$date_file\"; exit $?" 0
# Write the existing date to the file
git show -s --format='%ad' > "$date_file"
echo "# Please edit the timestamp above. Lines starting with '#' are ignored," >> "$date_file"
echo "# and an empty date aborts the retime." >> "$date_file"
# Open an editor
git_editor "$date_file"
# Remove comment lines
# We need '|| true' here, because grep returns non-zero status on empty files
new_date="$(grep -v '^#' "$date_file" || true)"
if [ "z$new_date" != 'z' ]
then
# Amend the commit with the new date
GIT_COMMITTER_DATE="$new_date" \
git commit --amend --date="$new_date" --no-edit
else
echo 'aborting retime due to empty timestamp'
fi
else
die 'cannot obtain lock (is git-retime already running?)'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment