1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#!/usr/bin/env bash
init_variables() {
COMMAND=${0#*-}
CONFIG=false
ALL=false
unset OLD_EMAIL
unset CORRECT_EMAIL
unset CORRECT_NAME
TYPE='both'
}
usage() {
cat << EOF
usage: git ${COMMAND} [<options>]
Options
-a, --all rewrite all identities in commits and tags
-c, --use-config define correct values from user Git config
-e, --correct-email <email> define the correct email to set
-n, --correct-name <name> define the correct name to set
-o, --old-email <email> rewrite identities matching old email in commits and tags
-t, --type <id> define the type of identities affected by the rewrite
author, committer, both (default)
EOF
}
error() {
if [[ -n "$1" ]]; then
local msg=$( echo "error: $1" | sed 's/\\n/\\n /g' )
echo -e "${msg}" >&2
fi
usage
exit 1
}
reauthor() {
local author='
if ${ALL} || [ "${GIT_AUTHOR_EMAIL}" = "${OLD_EMAIL}" ]; then
[ -z "${CORRECT_EMAIL+x}" ] || export GIT_AUTHOR_EMAIL="${CORRECT_EMAIL}"
[ -z "${CORRECT_NAME+x}" ] || export GIT_AUTHOR_NAME="${CORRECT_NAME}"
fi
'
local committer='
if ${ALL} || [ "${GIT_COMMITTER_EMAIL}" = "${OLD_EMAIL}" ]; then
[ -z "${CORRECT_EMAIL+x}" ] || export GIT_COMMITTER_EMAIL="${CORRECT_EMAIL}"
[ -z "${CORRECT_NAME+x}" ] || export GIT_COMMITTER_NAME="${CORRECT_NAME}"
fi
'
local filter
case "${TYPE}" in
author) filter="${author}" ;;
committer) filter="${committer}" ;;
both) filter="${author} ${committer}" ;;
esac
export ALL
export OLD_EMAIL
export CORRECT_EMAIL
export CORRECT_NAME
git filter-branch --force --env-filter "${filter}" \
--tag-name-filter cat -- --branches --tags
}
parse_options() {
while [[ "$#" -gt 0 ]]; do
case "$1" in
--all|-a)
ALL=true
shift
;;
--correct-email|-e)
[[ -n "${2+x}" ]] || error 'Missing correct-email value'
CORRECT_EMAIL="$2"
shift 2
;;
-h)
usage
exit 0
;;
--correct-name|-n)
[[ -n "${2+x}" ]] || error 'Missing correct-name value'
CORRECT_NAME="$2"
shift 2
;;
--old-email|-o)
[[ -n "${2+x}" ]] || error 'Missing old-email value'
OLD_EMAIL="$2"
shift 2
;;
--type|-t)
[[ -n "${2+x}" ]] || error 'Missing type value'
TYPE="$2"
shift 2
;;
--use-config|-c)
CONFIG=true
shift
;;
*)
error "invalid option '$1'"
;;
esac
done
if ${CONFIG}; then
# use config values if not explicitly already defined
[[ -n "${CORRECT_EMAIL+x}" ]] || CORRECT_EMAIL=$( git config user.email )
[[ -n "${CORRECT_NAME+x}" ]] || CORRECT_NAME=$( git config user.name )
fi
}
validate_options() {
# Either OLD_EMAIL should be set or ALL should be true
if [[ -z "${OLD_EMAIL+x}" ]] && ! ${ALL}; then
msg="missing target of the rewrite"
msg="${msg}\nuse either --old-email option or --all flag"
error "${msg}"
fi
# OLD_EMAIL shouldn't be set if ALL is true as well to prevent misuse
if [[ -n "${OLD_EMAIL+x}" ]] && ${ALL}; then
msg="ambiguous target of the rewrite"
msg="${msg}\nuse either --old-email option or --all flag"
error "${msg}"
fi
# CORRECT_NAME should be either unset or set to non-empty string
[[ -n "${CORRECT_NAME-x}" ]] || error "empty name is not allowed"
# Either CORRECT_EMAIL or CORRECT_NAME should be set
if [[ -z "${CORRECT_EMAIL+x}" ]] && [[ -z "${CORRECT_NAME+x}" ]]; then
msg="missing correct email and/or name to set"
msg="${msg}\nuse --correct-email and/or --correct-name options"
msg="${msg}\nor --use-config flag with user values set in Git config"
error "${msg}"
fi
# TYPE should be a valid identifier
if [[ "${TYPE}" != 'both' ]] \
&& [[ "${TYPE}" != 'author' ]] \
&& [[ "${TYPE}" != 'committer' ]]; then
error "invalid type '${TYPE}'"
fi
}
init_variables
parse_options "$@"
validate_options
reauthor
|