Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of tasks numbers from git log based on message

Tags:

git

git-log

Task number = JIRA issue number = **** (E.g.: 7600)

Let's suppose that I have a list of commits having the following messages:

PRJ-7600 - first message
PRJ-8283 - second message
PRJ-8283 - third message
PRJ-1001 - fourth message
PRJ-8283 - fifth message
PRJ-7600 - sixth message

where the first one is for the oldest commit.

Wanted output:

1001
7600
8283

I listed my commits using the following command:

git log --author="First Last" --oneline --grep=PRJ --pretty=format:"%s" | sort

where

  • committer = author (in this case)
  • --grep=PRJ is specified to ignore the comments that were automatically generated ("Merge branch ...") (alternative to --no-merges)
  • --pretty=format:"%s" shows only the message (removes the hash)

Actual output:

PRJ-1001 - fourth message
PRJ-7600 - first message
PRJ-7600 - sixth message
PRJ-8283 - fifth message
PRJ-8283 - second message
PRJ-8283 - third message

Is it possible to extract those numbers (probably using regex or something like substring) showing them only once?

Details:

  • Windows 7
  • git 1.9.5 (msysgit) -> used from cmd, not from Git Bash console
like image 997
ROMANIA_engineer Avatar asked May 12 '15 08:05

ROMANIA_engineer


1 Answers

This will do it in either bash or git bash:

git log --author="First Last" --oneline --grep=PRJ --pretty=format:"%s" | sort | cut --delimiter='-' --fields=2 | uniq

So building on the first section posted in the question, the extra is:

| cut --delimiter='-' --fields=2 | uniq

this pipes the sorted output to cut which extracts the 2nd field delimited by a hyphen "-" and the result is then pipes to uniq to display the distinct values.

This solution has a weakness in the form of the delimiter used for cut - if the format of the log message changes, then it may break. A better solution would be to use a regex search (instead of cut) for the issue key ("/PRJ-.+\s/" I think...) and output the number part.

EDIT

So after a bit of digging, it is possible to do this a little more reliably using grep to find the item key (PRJ in this case):

git log ... | grep -oP --regexp="PRJ-\K\d+" | uniq

-o tells grep to output only the matched part of the line
-P is use the PCRE (perl/PHP) flavour of regex, thus enabling us to use the
\K option which causes the matches prior (to that point) to be excluded

like image 53
Raad Avatar answered Nov 27 '22 08:11

Raad