Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pasela
Last active May 14, 2021 19:40
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 pasela/7821942 to your computer and use it in GitHub Desktop.
Save pasela/7821942 to your computer and use it in GitHub Desktop.
git-fix-author-committer - Apply filter-branch --commit-filter easily
#!/bin/bash
#
# Apply filter-branch --commit-filter easily
#
# Author: Yuki <paselan@gmail.com>
# License: MIT License
#
PROGNAME=$(basename $0)
SUBCMDNAME=${PROGNAME#git-}
usage() {
echo "usage: git $SUBCMDNAME [options] [rev-list options]"
echo
echo " --old-email=[EMAIL] old email address"
echo " --new-name=[NAME] new user name"
echo " --new-email=[EMAIL] new email address"
echo
exit 1
}
for OPT in "$@"
do
case "$OPT" in
'-h'|'--help' )
usage
exit 1
;;
'--old-email='* )
OLD_EMAIL=${1#--old-email=}
if [[ -z "$OLD_EMAIL" ]]; then
echo "option requires an argument -- --old-email" 1>&2
exit 1
fi
shift 1
;;
'--old-email' )
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "option requires an argument -- $1" 1>&2
exit 1
fi
OLD_EMAIL="$2"
shift 2
;;
'--new-name='* )
NEW_NAME=${1#--new-name=}
if [[ -z "$NEW_NAME" ]]; then
echo "option requires an argument -- --new-name" 1>&2
exit 1
fi
shift 1
;;
'--new-name' )
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "option requires an argument -- $1" 1>&2
exit 1
fi
NEW_NAME="$2"
shift 2
;;
'--new-email='* )
NEW_EMAIL=${1#--new-email=}
if [[ -z "$NEW_EMAIL" ]]; then
echo "option requires an argument -- --new-email" 1>&2
exit 1
fi
shift 1
;;
'--new-email' )
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "option requires an argument -- $1" 1>&2
exit 1
fi
NEW_EMAIL="$2"
shift 2
;;
'--'|'-' )
shift 1
param+=( "$@" )
break
;;
-*)
echo "illegal option -- '$(echo $1 | sed 's/^-*//')'" 1>&2
exit 1
;;
*)
if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
#param=( ${param[@]} "$1" )
param+=( "$1" )
shift 1
fi
;;
esac
done
if [ -z "$OLD_EMAIL" ]; then
echo -n "Old email> "
read OLD_EMAIL
[ -n "$OLD_EMAIL" ] || exit 1
fi
if [ -z "$NEW_NAME" ]; then
echo -n "New name> "
read NEW_NAME
[ -n "$NEW_NAME" ] || exit 1
fi
if [ -z "$NEW_EMAIL" ]; then
echo -n "New email> "
read NEW_EMAIL
[ -n "$NEW_EMAIL" ] || exit 1
fi
export OLD_EMAIL NEW_NAME NEW_EMAIL
git filter-branch -f --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]; then
GIT_AUTHOR_NAME=$NEW_NAME
GIT_AUTHOR_EMAIL=$NEW_EMAIL
fi
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]; then
GIT_COMMITTER_NAME=$NEW_NAME
GIT_COMMITTER_EMAIL=$NEW_EMAIL
fi
git commit-tree "$@"
' "${param[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment