Grepping the commit messages

Now we know how to list and sort files that we make frequent changes to and vice versa, but we are also interested in finding out the bugs we are fixing, the features we are implementing, and perhaps who is signing the code. All this information is usually available in the commit message. Some companies have a policy that you need to have a referral to a bug, a feature, or some other reference in the commit message. By having this information in the commit message, it is a lot easier to produce a nice release note as well.

Getting ready

As we will mostly be grepping the Git database in these examples, we really don't need to check something out or be at a specific commit for this example. So, if you are still lurking around in the chapter6 folder, we can continue.

How to do it...

Let's see how many commits in the repository are referring to a bug:

  1. First of all, we need to know the pattern for bugs referred to in the commit messages. I did this by looking in the commits, and the pattern for Jgit is to use Bug: 6 digits; so, to find all of these commits, we use the --grep option for git log, and we can grep for "[Bb][Uu][gG]: [0-9]+":
    $ git log --all --grep="^[bB][uU][gG]: [0-9]"
    commit 3db6e05e52b24e16fbe93376d3fd8935e5f4fc9b
    Author: Stefan Lay <[email protected]>
    Date:   Wed Jan 15 13:23:49 2014 +0100
    
        Fix fast forward rebase with rebase.autostash=true
    
        The folder .git/rebase-merge was not removed in this case. The
        repository was then still in rebase state, but neither abort nor
        continue worked.
    
        Bug: 425742
        Change-Id: I43cea6c9e5f3cef9d6b15643722fddecb40632d9
    
  2. You should get a lot of commits as output, but you should notice all the commits have a referral to a bug ID. So what was the grep doing? The ^[Bb][Uu][gG]: part matches any combination of lowercase and uppercase bugs. The ^ character means from the beginning of the line. The : character is matching :. Then, we have [0-9]+, which will match any number between zero and nine, and the + part means one or more occurrences. But enough with regular expressions for grep. We have a lot of output (which is valuable), but for now, we just want to count the commits. We can do this by piping it to wc -l (wordcount -l is to count the lines):
    $ git log --all --oneline --grep="^[bB][uU][gG]: [0-9]+" | wc -l
        366
    
  3. Before piping it to wc, remember to use --oneline to limit the output to one line for each commit. As you can see, when I was writing this, Jgit has reference to 366 bugs that have all been fixed and released into the repository. If you are used to using regular expressions in another scripting or programming language, you will see that using --grep does not support everything. You can enable a more extensive regular expression support using the --extended-regexp option for git log; however, the pattern still has to be used with --grep:
    $ git log --all --oneline  --extended-regexp --grep="^[bB][uU][gG]: [0-9]{6}"
    3db6e05 Fix fast forward rebase with rebase.autostash=true
    c6194c7 Update com.jcraft.jsch to 0.1.50 in Kepler target platform
    1def0a1 Fix for core.autocrlf=input resulting in modified file and unsmudge
    0ce61ca Canonicalize worktree path in BaseRepositoryBuilder if set via config
    e90438c Fix aborting rebase with detached head
    2e0d178 Add recursive variant of Config.getNames() methods
    
  4. I have used it in the preceding example, and you can see we are getting the same commits. I have used a slightly different expression, and have now added {6} instead of + the {6}, which searches for six occurrences of the associated pattern; in our case, it is six digits as it is next to the [0-9] pattern. We can verify by counting the lines or commits again with wc -l:
    $ git log --all --oneline  --extended-regexp --grep="^[bB][uU][gG]: [0-9]{6}" | wc -l
        366
    
  5. We get the same number. To shrink the regular expression even more, we can use --regexp-ignore-case, which will ignore the case for the pattern:
    $ git log --all --oneline  --regexp-ignore-case --extended-regexp --grep="^bug: [0-9]{6}"
    3db6e05 Fix fast forward rebase with rebase.autostash=true
    c6194c7 Update com.jcraft.jsch to 0.1.50 in Kepler target platform
    1def0a1 Fix for core.autocrlf=input resulting in modified file and unsmudge
    0ce61ca Canonicalize worktree path in BaseRepositoryBuilder if set via config
    e90438c Fix aborting rebase with detached head
    2e0d178 Add recursive variant of Config.getNames() methods
    
  6. Now we have the exact same output, and we no longer have [bB][uU][Gg] but just bug.

Now you know how to grep the commit messages for information, and you can grep for anything in the commit message and list all the commits where the regular expression matches.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset