From 9b3e25f407d129597ab6960439d35404fb899865 Mon Sep 17 00:00:00 2001 From: Damien Tardy-Panis Date: Tue, 2 Aug 2016 19:41:56 +0200 Subject: [PATCH] Add git reauthor (#548) Based on https://help.github.com/articles/changing-author-info/ --- AUTHORS | 1 + Commands.md | 38 ++++++++ bin/git-reauthor | 161 +++++++++++++++++++++++++++++++++ etc/bash_completion.sh | 13 +++ man/git-extras.md | 1 + man/git-reauthor.1 | 198 +++++++++++++++++++++++++++++++++++++++++ man/git-reauthor.html | 183 +++++++++++++++++++++++++++++++++++++ man/git-reauthor.md | 87 ++++++++++++++++++ man/index.txt | 1 + 9 files changed, 683 insertions(+) create mode 100755 bin/git-reauthor create mode 100644 man/git-reauthor.1 create mode 100644 man/git-reauthor.html create mode 100644 man/git-reauthor.md diff --git a/AUTHORS b/AUTHORS index e11ca65..73f8a71 100644 --- a/AUTHORS +++ b/AUTHORS @@ -99,6 +99,7 @@ Patches and Suggestions - Carl Casbolt - Ciro Nunes - Craig MacGregor +- Damien Tardy-Panis - Dan Jackson - Daniel Schildt - Dave James Miller diff --git a/Commands.md b/Commands.md index 90ddc98..4b6ab20 100644 --- a/Commands.md +++ b/Commands.md @@ -38,6 +38,7 @@ - [`git psykorebase`](#git-psykorebase) - [`git pull-request`](#git-pull-request) - [`git rebase-patch`](#git-rebase-patch) + - [`git reauthor`](#git-reauthor) - [`git release`](#git-release) - [`git rename-tag`](#git-rename-tag) - [`git repl`](#git-repl) @@ -347,6 +348,7 @@ Does the following: - Push the branch / tags - Executes _.git/hooks/post-release.sh_ (if present) + ## git rename-tag Rename a tag (locally and remotely). @@ -370,6 +372,42 @@ $ git tag test2 ``` + +## git reauthor + +Rewrite history to change author's identity. + +Replace the personal email and name of Jack to his work ones +```bash +$ git reauthor --old-email jack@perso.me --correct-email jack@work.com --correct-name 'Jack Foobar' +``` + +Replace the email and name of Jack to the ones defined in the Git config +```bash +$ git reauthor --old-email jack@perso.me --use-config +``` + +Replace only the email of Jack (keep the name already used) +```bash +$ git reauthor --old-email jack@perso --correct-email jack@perso.me +``` + +Change only the committer email of Jack (keep the author email already used) +```bash +$ git reauthor --old-email jack@perso.me --correct-email jack@work.com --type committer +``` + +Set Jack's identity as the only one of the whole repository +```bash +$ git reauthor --all --correct-email jack@perso.me --correct-name Jack +``` + +Set Jack as the only committer of the whole repository (keeps authors) +```bash +$ git reauthor --all --correct-email jack@perso.me --correct-name Jack --type committer +``` + + ## git alias Define, search and show aliases. diff --git a/bin/git-reauthor b/bin/git-reauthor new file mode 100755 index 0000000..a516b5f --- /dev/null +++ b/bin/git-reauthor @@ -0,0 +1,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 + -a, --all rewrite all identities in commits and tags + -c, --use-config define correct values from user Git config + -e, --correct-email define the correct email to set + -n, --correct-name define the correct name to set + -o, --old-email rewrite identities matching old email in commits and tags + -t, --type 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 diff --git a/etc/bash_completion.sh b/etc/bash_completion.sh index 237b215..9478e26 100644 --- a/etc/bash_completion.sh +++ b/etc/bash_completion.sh @@ -113,6 +113,19 @@ _git_psykorebase(){ __gitcomp "$(__git_heads) --continue --no-ff" } +_git_reauthor(){ + local prev="${COMP_WORDS[COMP_CWORD-1]}" + local comp + + if [[ "${prev}" == '--type' ]] || [[ "${prev}" == '-t' ]]; then + comp='author committer full' + else + comp='--all --correct-email --correct-name --old-email --type --use-config' + fi + + __gitcomp "${comp}" +} + _git_refactor(){ __git_extras_workflow "refactor" } diff --git a/man/git-extras.md b/man/git-extras.md index 1f21d63..3514e99 100644 --- a/man/git-extras.md +++ b/man/git-extras.md @@ -57,6 +57,7 @@ git-extras(1) -- Awesome GIT utilities - **git-missing(1)** Show commits missing from another branch - **git-pr(1)** Checks out a pull request locally - **git-psykorebase(1)** Rebase a branch with a merge commit + - **git-reauthor(1)** Rewrite history to change author's identity - **git-rebase-patch(1)** Rebases a patch - **git-refactor(1)** Create refactor branch - **git-release(1)** Commit, tag and push changes to the repository diff --git a/man/git-reauthor.1 b/man/git-reauthor.1 new file mode 100644 index 0000000..0511b25 --- /dev/null +++ b/man/git-reauthor.1 @@ -0,0 +1,198 @@ +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "GIT\-REAUTHOR" "1" "July 2016" "" "" +. +.SH "NAME" +\fBgit\-reauthor\fR \- Rewrite history to change author\'s identity +. +.SH "SYNOPSIS" +\fBgit reauthor []\fR +. +.SH "DESCRIPTION" +Lets you replace the author and/or committer identities in commits and tags\. +. +.P +The command goes through all existing commits and tags in all local branches to selectively modify the identities present in those objects\. All the other informations such as dates, messages,\.\. are preserved\. +. +.P +You can rewrite all the identities in the commits and tags objects by using the \-\-all flag, or only replace the identities whose email matches the value of the \-\-old\-email option\. It is also possible to limit the rewrite to a certain type of identity: the author or the committer identity\. By default, both of them are affected\. +. +.br +For each of those identities to update, the command will replace the name and/or email with the new correct values as defined via the options\. If the new identity name to set is not defined, the current one will be kept (and vice\-versa with the email)\. +. +.P +\fBWARNING!\fR This command rewrites history and as a result you will not able to push your branch to the remote without using the \-\-force option\. +. +.br +See more information with \fBgit help filter\-branch\fR\. +. +.SH "OPTIONS" +\-a, \-\-all +. +.IP "" 4 +. +.nf + +Rewrite ALL identities in commits and tags\. +. +.fi +. +.IP "" 0 +. +.P +\-c, \-\-use\-config +. +.IP "" 4 +. +.nf + +Define correct values from user Git config +Values of \-\-correct\-email and \-\-correct\-name options take precedence over the ones from the config if specified as well +. +.fi +. +.IP "" 0 +. +.P +\-e, \-\-correct\-email <\fIemail\fR> +. +.IP "" 4 +. +.nf + +Define the correct email to set +Empty email \'\' is allowed +. +.fi +. +.IP "" 0 +. +.P +\-n, \-\-correct\-name <\fIname\fR> +. +.IP "" 4 +. +.nf + +Define the correct name to set +Empty name \'\' is not allowed +. +.fi +. +.IP "" 0 +. +.P +\-o, \-\-old\-email <\fIemail\fR> +. +.IP "" 4 +. +.nf + +Rewrite identities matching old email in commits and tags +Empty email \'\' is allowed +. +.fi +. +.IP "" 0 +. +.P +\-t, \-\-type <\fIid\fR> +. +.IP "" 4 +. +.nf + +Define the type of identities affected by the rewrite +Possible type identifiers are: author, committer, both (default) +. +.fi +. +.IP "" 0 +. +.SH "EXAMPLES" +Replace the personal email and name of Jack to his work ones +. +.IP "" 4 +. +.nf + +$ git reauthor \-\-old\-email jack@perso\.me \-\-correct\-email jack@work\.com \-\-correct\-name \'Jack Foobar\' +. +.fi +. +.IP "" 0 +. +.P +Replace the email and name of Jack to the ones defined in the Git config +. +.IP "" 4 +. +.nf + +$ git reauthor \-\-old\-email jack@perso\.me \-\-use\-config +. +.fi +. +.IP "" 0 +. +.P +Replace only the email of Jack (keep the name already used) +. +.IP "" 4 +. +.nf + +$ git reauthor \-\-old\-email jack@perso \-\-correct\-email jack@perso\.me +. +.fi +. +.IP "" 0 +. +.P +Change only the committer email of Jack (keep the author email already used) +. +.IP "" 4 +. +.nf + +$ git reauthor \-\-old\-email jack@perso\.me \-\-correct\-email jack@work\.com \-\-type committer +. +.fi +. +.IP "" 0 +. +.P +Set Jack\'s identity as the only one of the whole repository +. +.IP "" 4 +. +.nf + +$ git reauthor \-\-all \-\-correct\-email jack@perso\.me \-\-correct\-name Jack +. +.fi +. +.IP "" 0 +. +.P +Set Jack as the only committer of the whole repository (keeps authors) +. +.IP "" 4 +. +.nf + +$ git reauthor \-\-all \-\-correct\-email jack@perso\.me \-\-correct\-name Jack \-\-type committer +. +.fi +. +.IP "" 0 +. +.SH "AUTHOR" +Written by Damien Tardy\-Panis <\fIdamien@tardypad\.me\fR> +. +.SH "REPORTING BUGS" +<\fIhttp://github\.com/tj/git\-extras/issues\fR> +. +.SH "SEE ALSO" +<\fIhttps://github\.com/tj/git\-extras\fR> diff --git a/man/git-reauthor.html b/man/git-reauthor.html new file mode 100644 index 0000000..5d6a14c --- /dev/null +++ b/man/git-reauthor.html @@ -0,0 +1,183 @@ + + + + + + git-reauthor(1) - Rewrite history to change author's identity + + + + +
+ + + +
    +
  1. git-reauthor(1)
  2. +
  3. +
  4. git-reauthor(1)
  5. +
+ +

NAME

+

+ git-reauthor - Rewrite history to change author's identity +

+ +

SYNOPSIS

+ +

git reauthor [<options>]

+ +

DESCRIPTION

+ +

Lets you replace the author and/or committer identities in commits and tags.

+ +

The command goes through all existing commits and tags in all local branches to selectively modify the identities present in those objects. All the other informations such as dates, messages,.. are preserved.

+ +

You can rewrite all the identities in the commits and tags objects by using the --all flag, or only replace the identities whose email matches the value of the --old-email option. It is also possible to limit the rewrite to a certain type of identity: the author or the committer identity. By default, both of them are affected.
+For each of those identities to update, the command will replace the name and/or email with the new correct values as defined via the options. If the new identity name to set is not defined, the current one will be kept (and vice-versa with the email).

+ +

WARNING! This command rewrites history and as a result you will not able to push your branch to the remote without using the --force option.
+See more information with git help filter-branch.

+ +

OPTIONS

+ +

-a, --all

+ +
Rewrite ALL identities in commits and tags.
+
+ +

-c, --use-config

+ +
Define correct values from user Git config
+Values of --correct-email and --correct-name options take precedence over the ones from the config if specified as well
+
+ +

-e, --correct-email <email>

+ +
Define the correct email to set
+Empty email '' is allowed
+
+ +

-n, --correct-name <name>

+ +
Define the correct name to set
+Empty name '' is not allowed
+
+ +

-o, --old-email <email>

+ +
Rewrite identities matching old email in commits and tags
+Empty email '' is allowed
+
+ +

-t, --type <id>

+ +
Define the type of identities affected by the rewrite
+Possible type identifiers are: author, committer, both (default)
+
+ +

EXAMPLES

+ +

Replace the personal email and name of Jack to his work ones

+ +
$ git reauthor --old-email jack@perso.me --correct-email jack@work.com --correct-name 'Jack Foobar'
+
+ +

Replace the email and name of Jack to the ones defined in the Git config

+ +
$ git reauthor --old-email jack@perso.me --use-config
+
+ +

Replace only the email of Jack (keep the name already used)

+ +
$ git reauthor --old-email jack@perso --correct-email jack@perso.me
+
+ +

Change only the committer email of Jack (keep the author email already used)

+ +
$ git reauthor --old-email jack@perso.me --correct-email jack@work.com --type committer
+
+ +

Set Jack's identity as the only one of the whole repository

+ +
$ git reauthor --all --correct-email jack@perso.me --correct-name Jack
+
+ +

Set Jack as the only committer of the whole repository (keeps authors)

+ +
$ git reauthor --all --correct-email jack@perso.me --correct-name Jack --type committer
+
+ +

AUTHOR

+ +

Written by Damien Tardy-Panis <damien@tardypad.me>

+ +

REPORTING BUGS

+ +

<http://github.com/tj/git-extras/issues>

+ +

SEE ALSO

+ +

<https://github.com/tj/git-extras>

+ + +
    +
  1. +
  2. July 2016
  3. +
  4. git-reauthor(1)
  5. +
+ +
+ + diff --git a/man/git-reauthor.md b/man/git-reauthor.md new file mode 100644 index 0000000..00eda78 --- /dev/null +++ b/man/git-reauthor.md @@ -0,0 +1,87 @@ +git-reauthor(1) -- Rewrite history to change author's identity +============================================================== + +## SYNOPSIS + +`git reauthor []` + +## DESCRIPTION + +Lets you replace the author and/or committer identities in commits and tags. + +The command goes through all existing commits and tags in all local branches to selectively modify the identities present in those objects. All the other informations such as dates, messages,.. are preserved. + +You can rewrite all the identities in the commits and tags objects by using the --all flag, or only replace the identities whose email matches the value of the --old-email option. It is also possible to limit the rewrite to a certain type of identity: the author or the committer identity. By default, both of them are affected. +For each of those identities to update, the command will replace the name and/or email with the new correct values as defined via the options. If the new identity name to set is not defined, the current one will be kept (and vice-versa with the email). + +`WARNING!` This command rewrites history and as a result you will not able to push your branch to the remote without using the --force option. +See more information with `git help filter-branch`. + +## OPTIONS + + -a, --all + + Rewrite ALL identities in commits and tags. + + -c, --use-config + + Define correct values from user Git config + Values of --correct-email and --correct-name options take precedence over the ones from the config if specified as well + + -e, --correct-email <> + + Define the correct email to set + Empty email '' is allowed + + -n, --correct-name <> + + Define the correct name to set + Empty name '' is not allowed + + -o, --old-email <> + + Rewrite identities matching old email in commits and tags + Empty email '' is allowed + + -t, --type <> + + Define the type of identities affected by the rewrite + Possible type identifiers are: author, committer, both (default) + +## EXAMPLES + +Replace the personal email and name of Jack to his work ones + + $ git reauthor --old-email jack@perso.me --correct-email jack@work.com --correct-name 'Jack Foobar' + +Replace the email and name of Jack to the ones defined in the Git config + + $ git reauthor --old-email jack@perso.me --use-config + +Replace only the email of Jack (keep the name already used) + + $ git reauthor --old-email jack@perso --correct-email jack@perso.me + +Change only the committer email of Jack (keep the author email already used) + + $ git reauthor --old-email jack@perso.me --correct-email jack@work.com --type committer + +Set Jack's identity as the only one of the whole repository + + $ git reauthor --all --correct-email jack@perso.me --correct-name Jack + +Set Jack as the only committer of the whole repository (keeps authors) + + $ git reauthor --all --correct-email jack@perso.me --correct-name Jack --type committer + +## AUTHOR + +Written by Damien Tardy-Panis <> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<> diff --git a/man/index.txt b/man/index.txt index f21d5f6..304b50b 100644 --- a/man/index.txt +++ b/man/index.txt @@ -36,6 +36,7 @@ git-merge-repo(1) git-merge-repo git-missing(1) git-missing git-pr(1) git-pr git-psykorebase(1) git-psykorebase +git-reauthor(1) git-reauthor git-rebase-patch(1) git-rebase-patch git-refactor(1) git-refactor git-release(1) git-release