gitrebase

git append "Reviewed-by" to commits


Apparently not the most popular situation, but I have a number of commits to whose messages I want to append a line Reviewed-by: user<mail>.

So far I only found this command which fails for me with Invalid line: 10: Reviewed-by: User <mail>

GIT_EDITOR='git interpret-trailers --trailer "Reviewed-by: User <mail>" --in-place' git rebase -i HEAD~8

I also asked on IRC, to no avail.

Any other suggestions are welcome.


Solution

  • git rebase solution

    Let's break down the second command (which is a complicated version of the 1st one):

    1. git rebase HEAD~2 -x … does rebase of n commits (where n = 2), to which you wanted to append Reviewed-by, and for every commit it stops and executes the shell command that follows.
    2. git commit --amend -m… amends the commit, and replaces its message with the one that follows.
    3. git log --format=%B -n1 prints message of the "current" commit.
    4. echo -ne \\nReviewed-by: User \<email\>. this one is a bit tricky. The obvious part is that it adds the Reviewed-by text. The less obvious one is that inline command execution removes trailing whitespace, i.e. the output of the previous command don't have newlines. So echo here adds a newline, and then the text.

    You can use that as is by getting it from command history, or you can wrap it to a bash/zsh function, e.g.:

    # adds reviwed-by to n commits
    function git_rb() {
        # export arguments, otherwise they're not visible to inline shell executions
        export who=$1
        export email=$2
        export n=$3
        git rebase HEAD~$n -x 'git commit --amend -m"$(git log --format=%B -n1)$(echo -e \\nReviewed-by: ${who} \<${email}\>.)"'
    }
    

    and then use it as

    $ git_rb "Holy Moly" "Holy@example.com" 5
    

    git filter-branch solution

    A bit limited solution that I found in this article. Turns out, you can use git filter-branch to do the same. It prints a warning though that says it can mangle up the history, and recommends to use git filter-repo instead. But it's a separate from git project, I didn't test it. From reading "gotchas" I got an impression that for modifying commit messages git filter-branch is fine. But one problem I find with this method: you can't use a HEAD~n notation, it would bail out with You must specify a ref to rewrite error. Instead you have to use a notation like master..HEAD, which does limit its usefulness a bit. An example:

    git filter-branch -f --msg-filter 'cat && echo -e "\nReviewed-by: User<Holy Moly>"' master..HEAD