Skip to content

Instantly share code, notes, and snippets.

@sethwebster
Last active May 15, 2017 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethwebster/9026313 to your computer and use it in GitHub Desktop.
Save sethwebster/9026313 to your computer and use it in GitHub Desktop.
Script to rewrite a list of authors in git history
#!/bin/bash
#
# +-----------------------+
# | Update Authors Script |
# +-----------------------+-------------------------------------------------------------------------+
# | |
# | Updates wrong authors and maps them to the new Git authors |
# | |
# | Dependent on https://github.com/sethwebster/git-rewrite-author/blob/master/git-rewrite-author |
# | forked from https://github.com/davidfokkema/git-rewrite-author/blob/master/git-rewrite-author |
# | |
# +-------------------------------------------------------------------------------------------------+
#
#
# Notes (example below in code, please edit)
#
# 1. Define the correct authors (examples below: correct_author_*)
# 2. Define the old authors we should remap
# 3. Define the new authors the old author should map to (via position in the array)
# 4. Run via command line
#
# Note: a Git rewrite takes a long time, and you are rewriting history.
#
# Anyone using this repository should:
#
# git fetch --all
# git reset --hard origin/master
#
# Download git-rewrite-author
if [ ! -f "git-rewrite-author" ]; then
curl https://raw2.github.com/sethwebster/git-rewrite-author/master/git-rewrite-author -o git-rewrite-author
chmod +x git-rewrite-author;
fi
# map out our current correct users
correct_author_1="Seth Webster <nospam-seth@noemail.me>"
correct_author_2="Jane Doe <nospam-jane@noemail.me>"
correct_author_3="John Moose <nospam-john@noemail.me>"
# old authors
oldauthors[1]="jdoe <jdoe@c0f60b75-22a0-4989-a491-524eb590045c>";
oldauthors[2]="jmoose-admin <jdoe-admin@c0f60b75-22a0-4989-a491-524eb590045c>";
oldauthors[3]="Seth-Admin <sadmin@localhost>";
oldauthors[4]="Jane Doe <none@devuser.me>";
# map to new authors
newauthors[1]=$correct_author_2;
newauthors[2]=$correct_author_3;
newauthors[3]=$correct_author_1;
newauthors[4]=$correct_author_2;
# indexing function (find old, which maps to new)
find_correct_author ()
{
for index in ${!oldauthors[*]}
do
if [ "$1" == "${oldauthors[$index]}" ] ; then
echo $index;
break;
fi
done
return 0;
}
# load the authors who've contributed to this repository
list=$(./git-rewrite-author -l);
# split them into an array
IFS=$'\n' authors=($list)
# iterate over the authors and map them to our new ones
for index in ${!authors[*]}
do
#we skip our first element because it's the
# instructional text from output
if [[ $index -eq 0 ]] ; then
continue;
fi
# find our old author index (0 = not found)
item=$(find_correct_author "${authors[$index]}");
# check if we found old author
if [[ $item -ne 0 ]] ; then
# bingo, let's rewrite
echo "Rewrite ${authors[$index]} to ${newauthors[$item]}"
./git-rewrite-author -w "${oldauthors[$index]}" "${newauthors[$index]}"
# if our exit code is non-zero, it failed
if [[ $? -ne 0 ]] ; then
echo "Rewrite failed."
break;
fi
fi
done
echo
echo "Complete."
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment