Annotate to help learn how it works.

This is an interesting script. I learned a lot from reading it.
Maybe these comments make the script more approachable for people trying to learn how it works.
This commit is contained in:
Thomas Steven 2021-11-02 22:30:41 -04:00 committed by GitHub
parent 8d854c2d78
commit 456bd53faf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,19 +1,47 @@
#!/usr/bin/env bash
# Check that the user has passed in exactly 2 arguments, the AUTHOR and the COMMIT.
# If they number of arguments is wrong, print out a usage message.
if [ $# -ne 2 ]; then
>&2 echo "Usage: $0 <author> <commit>"
exit 1
fi
# Assign the author input to the author variable.
# Author must be formatted like: John Smith <johnsmith@example.com>
AUTHOR=$1
# Use perl to find the author name.
# This is using a regex capture group.
# Ex: John Smith
AUTHOR_NAME=$(echo $AUTHOR | perl -wlne '/^(.*?)\s*<.*>$/ and print $1')
# Use perl to find the author email.
# This is using a regex capture group.
# Ex: johnsmith@example.com
AUTHOR_EMAIL=$(echo $AUTHOR | perl -wlne '/^.*\s*<(.*)>$/ and print $1')
# Gets the short version of the commit hash
# This is needed below so that we can replace the correct commit
# when editing.
# Ex: b3d501f
COMMIT=$(git rev-parse --short $2)
{
# Sets the editor to use for updating the sequence of commits.
# Normally the sequence editor is your editor (ex: vim).
# Here we are using a command that will change pick to edit.
# This chooses the right commit for the rebase.
GIT_SEQUENCE_EDITOR="sed -i -e 's/^pick $COMMIT/edit $COMMIT/'" git rebase -i $COMMIT~1^^
# Set the committer name and email, AND the author.
# Git tracks both committer and author as they could be different people.
# Then we amend the commit in question with the new author and committer.
GIT_COMMITTER_NAME="$AUTHOR_NAME" GIT_COMMITTER_EMAIL="$AUTHOR_EMAIL" git commit --amend --no-edit --author="$AUTHOR"
# Finish up the rebase
git rebase --continue
} &> /dev/null
# Success message.
echo "$AUTHOR_NAME is now the author of $COMMIT. You're officially an asshole.";