threads / discuss / 58089

Non-interactively rewording commit messages

Subject: Non-interactively rewording commit messages

## tl;dr

11 messages between Jun 30, 2022 and Jul 1, 2022.

replies: 10people: 8as markdown or json

Konstantin Ryabitsev· Jun 30, 2022, 15:27 UTC · lore
Hello, all:

What's the best approach to non-interactively rewrite specific commit messages? In this particular case, I am trying to automatically retrieve code review trailers sent to the mailing list and put them into corresponding commits.

For example, I have a set of commits:

abcabc: This commit does foo bcdbcd: This commit does bar cdecde: This commit does baz

They were all sent to the mailing list and a maintainer sent a "Reviewed-by" to the second commit. In a usual interactive rebase session this would be:

pick abcabc reword bcdbcd pick cdecde

When the edit screen comes up for the bcdbcd commit, the author would manually stick the new trailer into the commit message. However, I can automate all that away with b4 -- just need a sane strategy for non-interactively rewriting entire commit messages at arbitrary points in the recent history.

Any pointers?
-Konstantin
Carlo Marcelo Arenas Belón· Jun 30, 2022, 17:32 UTC · re: Konstantin Ryabitsev · lore

Re: Non-interactively rewording commit messages

On Thu, Jun 30, 2022 at 11:27:57AM -0400, Konstantin Ryabitsev wrote:
> just need a sane strategy for non-interactively rewriting
> entire commit messages at arbitrary points in the recent history.
assuming you are still doing it by calling (albeit programmatically):
  rebase --autosquash -i

then you could prepare the new message by also programmatically doing a `git commit --fixup reword:<commit>` IMHO

Carlo
Christian Couder· Jun 30, 2022, 17:34 UTC · re: Konstantin Ryabitsev · lore

Re: Non-interactively rewording commit messages

Hi,

On Thu, Jun 30, 2022 at 5:35 PM Konstantin Ryabitsev <konstantin@linuxfoundation.org> wrote:

Show 27 quoted lines
>
> Hello, all:
>
> What's the best approach to non-interactively rewrite specific commit
> messages? In this particular case, I am trying to automatically retrieve code
> review trailers sent to the mailing list and put them into corresponding
> commits.
>
> For example, I have a set of commits:
>
> abcabc: This commit does foo
> bcdbcd: This commit does bar
> cdecde: This commit does baz
>
> They were all sent to the mailing list and a maintainer sent a "Reviewed-by"
> to the second commit. In a usual interactive rebase session this would be:
>
> pick abcabc
> reword bcdbcd
> pick cdecde
>
> When the edit screen comes up for the bcdbcd commit, the author would manually
> stick the new trailer into the commit message. However, I can automate all
> that away with b4 -- just need a sane strategy for non-interactively rewriting
> entire commit messages at arbitrary points in the recent history.
>
> Any pointers?
Have you tried `git interpret-trailers`?
Konstantin Ryabitsev· Jun 30, 2022, 17:54 UTC · re: Christian Couder · lore

Re: Non-interactively rewording commit messages

On Thu, Jun 30, 2022 at 07:34:54PM +0200, Christian Couder wrote:
Show 28 quoted lines
> > Hello, all:
> >
> > What's the best approach to non-interactively rewrite specific commit
> > messages? In this particular case, I am trying to automatically retrieve code
> > review trailers sent to the mailing list and put them into corresponding
> > commits.
> >
> > For example, I have a set of commits:
> >
> > abcabc: This commit does foo
> > bcdbcd: This commit does bar
> > cdecde: This commit does baz
> >
> > They were all sent to the mailing list and a maintainer sent a "Reviewed-by"
> > to the second commit. In a usual interactive rebase session this would be:
> >
> > pick abcabc
> > reword bcdbcd
> > pick cdecde
> >
> > When the edit screen comes up for the bcdbcd commit, the author would manually
> > stick the new trailer into the commit message. However, I can automate all
> > that away with b4 -- just need a sane strategy for non-interactively rewriting
> > entire commit messages at arbitrary points in the recent history.
> >
> > Any pointers?
> 
> Have you tried `git interpret-trailers`?

I'm aware of interpret-trailers, but unless I'm missing something large, it's just a way of analyzing standalone text files to retrieve or insert trailers. What I'm looking for is a way to amend arbitrary commit messages within recent git history.

-K
Ævar Arnfjörð Bjarmason· Jun 30, 2022, 18:10 UTC · re: Konstantin Ryabitsev · lore

Re: Non-interactively rewording commit messages

On Thu, Jun 30 2022, Konstantin Ryabitsev wrote:
Show 34 quoted lines
> On Thu, Jun 30, 2022 at 07:34:54PM +0200, Christian Couder wrote:
>> > Hello, all:
>> >
>> > What's the best approach to non-interactively rewrite specific commit
>> > messages? In this particular case, I am trying to automatically retrieve code
>> > review trailers sent to the mailing list and put them into corresponding
>> > commits.
>> >
>> > For example, I have a set of commits:
>> >
>> > abcabc: This commit does foo
>> > bcdbcd: This commit does bar
>> > cdecde: This commit does baz
>> >
>> > They were all sent to the mailing list and a maintainer sent a "Reviewed-by"
>> > to the second commit. In a usual interactive rebase session this would be:
>> >
>> > pick abcabc
>> > reword bcdbcd
>> > pick cdecde
>> >
>> > When the edit screen comes up for the bcdbcd commit, the author would manually
>> > stick the new trailer into the commit message. However, I can automate all
>> > that away with b4 -- just need a sane strategy for non-interactively rewriting
>> > entire commit messages at arbitrary points in the recent history.
>> >
>> > Any pointers?
>> 
>> Have you tried `git interpret-trailers`?
>
> I'm aware of interpret-trailers, but unless I'm missing something large, it's
> just a way of analyzing standalone text files to retrieve or insert trailers.
> What I'm looking for is a way to amend arbitrary commit messages within recent
> git history.

I think what's being suggested is that once you have a program that can munge a commit message on stdin, you can combine it with rebase, git commit --amend etc. to change existing commits.q

The t/t7513-interpret-trailers.sh test has some examples of munging existing content.

Junio C Hamano· Jun 30, 2022, 21:35 UTC · re: Christian Couder · lore

Re: Non-interactively rewording commit messages

Christian Couder <christian.couder@gmail.com> writes:
Show 11 quoted lines
>> For example, I have a set of commits:
>>
>> abcabc: This commit does foo
>> bcdbcd: This commit does bar
>> cdecde: This commit does baz
>>
>> They were all sent to the mailing list and a maintainer sent a "Reviewed-by"
>> to the second commit. In a usual interactive rebase session this would be:
> ...
>
> Have you tried `git interpret-trailers`?
Do you mean a
	git fast-export |
	git interpret-trailers |
	git fast-import
pipeline?
Christian Couder· Jul 1, 2022, 11:10 UTC · re: Junio C Hamano · lore

Re: Non-interactively rewording commit messages

On Thu, Jun 30, 2022 at 11:35 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Christian Couder <christian.couder@gmail.com> writes:
Show 9 quoted lines
> > Have you tried `git interpret-trailers`?
>
> Do you mean a
>
>         git fast-export |
>         git interpret-trailers |
>         git fast-import
>
> pipeline?

I was thinking about using ways that are described a bit in the `git interpret-trailers` doc. And I was thinking about using ̀git rebase -i` and either adding "exec ..." lines to the todo list (possibly with `--exec ...` ) or maybe using "reword ..."

About ways that are described in the `git interpret-trailers` doc, there is:
* the following example with `git format-patch`:
      •   Extract the last commit as a patch, and add a Cc and a
Reviewed-by trailer to it:
              $ git format-patch -1
              0001-foo.patch
              $ git interpret-trailers --trailer 'Cc: Alice
<alice@example.com>' --trailer 'Reviewed-by: Bob <bob@example.com>'
0001-foo.patch >0001-bar.patch
* the example with a commit-msg hook that uses git interpret-trailers

BTW using interpret-trailers' --in-place option might be better than handling files the way it's done in both these examples.

brian m. carlson· Jun 30, 2022, 21:17 UTC · re: Konstantin Ryabitsev · lore

Re: Non-interactively rewording commit messages

On 2022-06-30 at 15:27:57, Konstantin Ryabitsev wrote:
Show 19 quoted lines
> Hello, all:
> 
> What's the best approach to non-interactively rewrite specific commit
> messages? In this particular case, I am trying to automatically retrieve code
> review trailers sent to the mailing list and put them into corresponding
> commits.
> 
> For example, I have a set of commits:
> 
> abcabc: This commit does foo
> bcdbcd: This commit does bar
> cdecde: This commit does baz
> 
> They were all sent to the mailing list and a maintainer sent a "Reviewed-by"
> to the second commit. In a usual interactive rebase session this would be:
> 
> pick abcabc
> reword bcdbcd
> pick cdecde

You can set GIT_SEQUENCE_EDITOR to a command which does this in-place on the file...

> When the edit screen comes up for the bcdbcd commit, the author would manually
> stick the new trailer into the commit message. However, I can automate all
> that away with b4 -- just need a sane strategy for non-interactively rewriting
> entire commit messages at arbitrary points in the recent history.

...and you can set GIT_EDITOR to modify the commit message in-place in whatever way you want, and then use git rebase -i to rebase.

-- 
brian m. carlson (he/him or they/them)
Toronto, Ontario, CA
Elijah Newren· Jul 1, 2022, 04:01 UTC · re: Konstantin Ryabitsev · lore

Re: Non-interactively rewording commit messages

On Thu, Jun 30, 2022 at 8:35 AM Konstantin Ryabitsev <konstantin@linuxfoundation.org> wrote:

Show 27 quoted lines
>
> Hello, all:
>
> What's the best approach to non-interactively rewrite specific commit
> messages? In this particular case, I am trying to automatically retrieve code
> review trailers sent to the mailing list and put them into corresponding
> commits.
>
> For example, I have a set of commits:
>
> abcabc: This commit does foo
> bcdbcd: This commit does bar
> cdecde: This commit does baz
>
> They were all sent to the mailing list and a maintainer sent a "Reviewed-by"
> to the second commit. In a usual interactive rebase session this would be:
>
> pick abcabc
> reword bcdbcd
> pick cdecde
>
> When the edit screen comes up for the bcdbcd commit, the author would manually
> stick the new trailer into the commit message. However, I can automate all
> that away with b4 -- just need a sane strategy for non-interactively rewriting
> entire commit messages at arbitrary points in the recent history.
>
> Any pointers?

One possibility would be to tweak https://github.com/newren/git-filter-repo/blob/main/contrib/filter-repo-demos/signed-off-by, modifying the line setting the 'trailer' variable to make it be a Reviewed-by rather than a Signed-off-by. You could tweak the script to also make other automated commit message changes unrelated to any trailers, if you wanted.

(There's also the slightly less careful way of adding trailers shown over at https://github.com/newren/git-filter-repo/blob/main/Documentation/converting-from-filter-branch.md#rewriting-commit-messages----adding-text, but the literal appending instead of determining whether to also first add one or more newlines before the trailer might not be exactly what you want.)

Konstantin Ryabitsev· Jul 1, 2022, 18:18 UTC · re: Elijah Newren · lore

Re: Non-interactively rewording commit messages

On Thu, Jun 30, 2022 at 09:01:33PM -0700, Elijah Newren wrote:
Show 6 quoted lines
> One possibility would be to tweak
> https://github.com/newren/git-filter-repo/blob/main/contrib/filter-repo-demos/signed-off-by,
> modifying the line setting the 'trailer' variable to make it be a
> Reviewed-by rather than a Signed-off-by.  You could tweak the script
> to also make other automated commit message changes unrelated to any
> trailers, if you wanted.

Oh, this is neat, I was unaware of git-filter-repo. It looks like exactly what I need for my purposes -- maybe I can just use it as a subproject. Thanks for the suggestion!

-K
Johannes Schindelin· Jul 1, 2022, 13:37 UTC · re: Konstantin Ryabitsev · lore

Re: Non-interactively rewording commit messages

Hi Konstantin,
On Thu, 30 Jun 2022, Konstantin Ryabitsev wrote:
Show 24 quoted lines
> What's the best approach to non-interactively rewrite specific commit
> messages? In this particular case, I am trying to automatically retrieve code
> review trailers sent to the mailing list and put them into corresponding
> commits.
>
> For example, I have a set of commits:
>
> abcabc: This commit does foo
> bcdbcd: This commit does bar
> cdecde: This commit does baz
>
> They were all sent to the mailing list and a maintainer sent a "Reviewed-by"
> to the second commit. In a usual interactive rebase session this would be:
>
> pick abcabc
> reword bcdbcd
> pick cdecde
>
> When the edit screen comes up for the bcdbcd commit, the author would manually
> stick the new trailer into the commit message. However, I can automate all
> that away with b4 -- just need a sane strategy for non-interactively rewriting
> entire commit messages at arbitrary points in the recent history.
>
> Any pointers?

The quickest (in terms of script size) might be to craft an `amend!` commit, something like this:

	git commit --allow-empty -m "$(printf 'amend! bcdbcd\n\n' &&
		git cat-file commit bcdbcd |
		sed -e '1,/^$/d' \
		 -e '{:1;${s/^/Reviewed-by: M A Intainer <maintainer@example.org>\n/;b};N;/\n$/b;b1}')"
and then running an "interactive" rebase with `:` as the editor:
	GIT_SEQUENCE_EDITOR=: GIT_EDITOR=: git rebase -i --autosquash HEAD~3

(The `sed` call is admittedly a bit horrible, it prepends the `Reviewed-by:` footer to the last paragraph, if it needs to be _appended_, it can be greatly simplified.)

Ciao, Dscho

← back to recent threads