Tim Landscheidt
Tim Landscheidt

Reputation: 1400

Pipe output of git show through a filter?

We use Gerrit and Bugzilla, so our commit messages have footers à la:

commit c557164627b6a53017a2b6ea5122393415445d43
Author: Tim Landscheidt <[email protected]>
Date:   Mon Jun 23 22:09:21 2014 +0000

    Tools: Install xsltproc

    Bug: 66962
    Change-Id: I01cfb2f72c3a7de39a5ac2b3439022122fdfbb15

In Gerrit's web interface, the "Bug" and "Change-Id" footers have autolinks to the corresponding Gerrit and Bugzilla pages. On the command line, I have to copy & paste & search instead.

I would like to filter the commit messages so that the above example is displayed by git show (1.8.3.1) as:

commit c557164627b6a53017a2b6ea5122393415445d43
Author: Tim Landscheidt <[email protected]>
Date:   Mon Jun 23 22:09:21 2014 +0000

    Tools: Install xsltproc

    Bug: https://bugzilla.wikimedia.org/66962
    Change-Id: https://gerrit.wikimedia.org/r/#q,I01cfb2f72c3a7de39a5ac2b3439022122fdfbb15,n,z

allowing my Konsole window to make those links clickable.

But looking at git show, the formats there don't seem to allow calling external programs.

Is there no way to do that in Git internally so that I would need to resort to setting up a shell alias instead?

Upvotes: 3

Views: 461

Answers (2)

Guildenstern
Guildenstern

Reputation: 3754

This will output most of what you want except that the body between the subject and the trailers is missing (git format can’t distinguish between them).

git show --format='commit %H%nAuthor: %aN <%ae>%nDate: %n%n%s%n%n%(trailers:key=Bug,keyonly,separator=): https://bugzilla.wikimedia.org/%(trailers:key=Bug,valueonly)%(trailers:key=Change-Id,keyonly,separator=): https://gerrit.wikimedia.org/r/#q,%(trailers:key=Change-Id,valueonly)'

This will give you something like this:

commit c557164627b6a53017a2b6ea5122393415445d43
Author: Tim Landscheidt <[email protected]>
Date: Mon Jun 23 22:09:21 2014 +0000

Tools: Install xsltproc

Bug: https://bugzilla.wikimedia.org/66962
Change-Id: https://gerrit.wikimedia.org/r/#q,I01cfb2f72c3a7de39a5ac2b3439022122fdfbb15

Notes

  • Each expected trailer is dealt with by fetching the key and value separately:
    • Key: %(trailers:key=Bug,keyonly,separator=)
    • Value: %(trailers:key=Bug,valueonly)
  • The separator= is so that the separator between the trailers is set to nothing (a newline is added by default)

Upvotes: 0

Matthew Strawbridge
Matthew Strawbridge

Reputation: 20600

Although you can use git show's --format parameter to change the order of fields and to add extra text between them, you can't modify the contents of the fields. The Gerrit and Bugzilla messages are added to the commit message by the looks of it, so you can't reformat them from within Git.

However, you can set up an alias in Git without needing a separate shell file, something like this:

git config alias.shw '!git show | sed "s/Bug: /Bug: https:\/\/bugzilla.wikimedia.org\//" | sed "s/Change-Id: \(.*\)/Change-Id: https:\/\/gerrit.wikimedia.org\/r\/#q,\1,n,z/"'

So then you'd be able to use git shw instead of git show to get your adjusted output.

Upvotes: 1

Related Questions