Feb 16, 2022 git · development · pairing

Add Co-Authored-By to all commits after pairing

TL;DR

Here's the full solution:

shell git filter-branch --msg-filter "ruby -e'puts [$stdin.read,nil,*ARGV].join(%{\\n})' -- 'Co-Authored-By: John Doe <[email protected]>'" origin/master..HEAD

Now let's break it down, piece by piece.

The commits

This will list all commits that are in your branch but not on origin/master

shell origin/master..HEAD

The filter

The simplest ruby script that will:

  1. read the original commit message from $stdin
  2. append any provided argument with a new line

ruby puts [ $stdin.read, nil, # extra empty line, for good measure *ARGV ].join(%{\n})

And we use -- to separate file names to execute from CLI arguments:

shell $ ruby -e 'p ARGV' -- "anything after '--'" "ends up in ARGV" ["anything after '--'", "ends up in ARGV"]

Putting it all together

We're lucky git filter-branch has an option for changing commit messages, we'll use that to append Co-Authored-By: … information to every commit in the current branch.

The --msg-filter option will execute a script that will take the old message as its standard input and use the standard output as the new message.

shell git filter-branch --msg-filter "ruby -e'puts [$stdin.read,nil,*ARGV].join(%{\\n})' -- 'Co-Authored-By: John Doe <[email protected]>'" origin/master..HEAD