Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract changeid using git log --format=?

Tags:

git

I have gone through the documentation of

git log --format

but I dont see a way to extract Change-Id (as in example below Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b) using the command git log --format.

$ git log -1

  commit 29a6bb1a059aef021ac39d342499191278518d1d

  Author: A. U. Thor <[email protected]>

  Date: Thu Aug 20 12:46:50 2009 -0700

      Improve foo widget by attaching a bar.
      We want a bar, because it improves the foo by providing more
      wizbangery to the dowhatimeanery.
      Bug: #42
      Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
      Signed-off-by: A. U. Thor <[email protected]>
      CC: R. E. Viewer <[email protected]>

Is there a way to do this? Thanks in advance!

like image 767
spitfire88 Avatar asked Mar 02 '15 20:03

spitfire88


Video Answer


2 Answers

As far as git is concerned, there is no change ID. (It's just one line in the message body, and git can give you the message body, or suppress the message body, but it won't give you individual lines out of the message body.1)

To limit the output from git log to just the message body, use %b. To limit the output to just the message subject, use %s. To get the "raw body" (subject + body) use %B.

In any case, you'll then need some external program to snip out the interesting line(s) from the message body. As everyone else has already commented, grep is highly suited to this.


1Unless you count the first line (the subject, %s, part) and mean the "raw body" (%B part) when you say "body". Then %s will give you the first line. These descriptions, as usual, are somewhat vague and loose so that when someone says "the message body" it's often worth clarifying what that means. "Did you mean the raw subject-and-body, or the body-minus-subject?" This is where using the actual format directive (%s, %b, %B) really shines, as those are unambiguous.

like image 82
torek Avatar answered Sep 24 '22 03:09

torek


You want to start with http://blog.lost-theory.org/post/how-to-parse-git-log-output/

Then you add the "%b" for body flag to the format in the script to obtain the body of the commit message.

Finally, you split the body entry into lines and you locate a line starting with Change-Id and parse it apart.

#!/usr/bin/python

GIT_COMMIT_FIELDS = ['id', 'author_name', 'author_email', 'date', 'message', 'body']
GIT_LOG_FORMAT = ['%H', '%an', '%ae', '%ad', '%s', '%b']
GIT_LOG_FORMAT = '%x1f'.join(GIT_LOG_FORMAT) + '%x1e'

p = Popen('git log --format="%s"' % GIT_LOG_FORMAT, shell=True, stdout=PIPE)
(log, _) = p.communicate()
log = log.strip('\n\x1e').split("\x1e")
log = [row.strip().split("\x1f") for row in log]
log = [dict(zip(GIT_COMMIT_FIELDS, row)) for row in log]

for row in log:
    for line in row['body'].split("\n"):
        if line.startswith('Change-Id:'):
            row['changeId'] = line.split(':')[1].strip()
like image 37
Christian Goetze Avatar answered Sep 24 '22 03:09

Christian Goetze