Get list of task numbers from git log based on message

Task number = JIRA release number = **** (e.g .: 7600)

Suppose I have a list of commits with 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 is for the oldest fixation.

Required Conclusion:

 1001 7600 8283 

I have 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 indicated to ignore comments that were automatically generated ("Merge branch ...") (alternative to --no-merges )
  • --pretty=format:"%s" shows only the message (removes the hash)

Actual conclusion:

 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 these numbers (perhaps using a regular expression or something like a substring), showing them only once?

Details:

  • Windows 7
  • git 1.9.5 (msysgit) -> used from cmd, not from Git Bash console
+5
source share
1 answer

This will run in both bash and git bash:

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

So, creating the first section posted in the question, the following is added:

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

this channel sorts the output into cut , which extracts the second field, separated by a hyphen “-”, and the result then passes to the uniq pipe to display various 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. The best solution would be to use a regular expression search (instead of cut ) for the problem key ("/PRJ-.+\s/", I think ...) and output the part of the number.

EDIT

So, after a little digging, you can do it more reliably using grep to find the key of the element ( PRJ in this case):

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

-o tells grep to print only the matched part of the string
-P uses PCRE (perl / PHP) regex flavor, which allows us to use \K , which eliminates the preceding matches (to this point)

+6
source

All Articles