0

I'm trying to create a more useful list of commits for reporting, and would like to format commit messages made in the following format:

Merged PR 5678: [12345] here's my awesome git commit message

Into the this format:

12345 - 2020-06-02 - Merged PR 5678: [12345] here's my awesome git commit message - Joe Bloggs

What i have so far:

git log --grep=Merged --pretty="%ad - %s - %an" --date=short

Which returns:

2020-06-02 - Merged PR 5678: [12345] here's my awesome git commit message - Joe Bloggs

But i don't know where to start in extracting the number from brackets and adding it as a variable to the pretty mask, is this possible?

1 Answer 1

1

No, Git doesn't provide general commit message parsing capabilities like this. It does have support for trailers and for distinguishing titles and bodies, but since this is neither of those, you'll need to resort to something like sed, ruby, or perl.

If your commit message may contain other arbitrary brackets, you may find it convenient to use the multiline capabilities of git log --format (e.g., with %n) to create a set of key-value pairs for each commit and then parse each stanza with something like ruby:

$ git log --grep=Merged --pretty="date: %ad%nsummary: %s%nauthor: %an%n" --date=short | \
  ruby -ne 'x ||= {}
$_.chomp!
if $_.empty?
  x[:id] = Regexp.last_match[1] if x[:summary] =~ /^.*?\[(\d+)\]/
  puts [:date, :summary, :id, :author].map { |s| x[s] }.join(" - ")
  x = {}
else
  pair = $_.split(": ", 2)
  x[pair[0].to_sym] = pair[1]
end'

That will print a line like this:

2020-06-02 - Merged PR 5678: [12345] here's my awesome git commit message - 12345 - Joe Bloggs
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i'm getting a syntax error, unexpected end-of-input, expecting ')' error on this line: puts [:date, :summary, :id, :author].map { |s| x[s] }.join(" - ") Either way though, thanks for confirming that it has to be done as a script and not a command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.