I am trying perform a git log using this git log "Version_10.1.18.1...Version_10.1.18" --format="%s" for just the` subject in the commit messages like these
Merged PR 17055: 94683: Add back in Treat Warnings as Errors
Merged PR 17054: #94658: Add OwnerInfo to address data when multiple locations returned by LPS.
TIA
I am using the following regex to strip out the colons and Text Merged PR
$regex = '[:]|(Merged PR)' # Regex to match commit hashes
$log= $gitHist -replace $regex, ""
so that it looks like this
17055 94683 Add back in Treat Warnings as Errors
17054 #94658 Add OwnerInfo to address data when multiple locations returned by LPS.
Next, I would like to grab just the Work item number i.e. 94658, etc. and place into a variable. Is this possible with regex in powershell?
Why not parse the output into an array of objects where it becomes easy to get any part for each item you like and also store the results in (for instance) a CSV file you can open with Excel for later use?
Something like:
$result = git log Version_10.1.18.1...Version_10.1.18 --format=%s |
Where-Object {$_ -match '^.*?(\d+):[\s#]*?(\d+):(.*)' } |
ForEach-Object {
[PsCustomObject]@{
CommitNumber = $matches[1] # not sure what this number is...
WorkItem = $matches[2]
Subject = $matches[3].Trim()
}
}
Using your example lines:
#output on screen
$result | Format-Table -AutoSize
CommitNumber WorkItem Subject
------------ -------- -------
17055 94683 Add back in Treat Warnings as Errors
17054 94658 Add OwnerInfo to address data when multiple locations returned by LPS.
# or save as CSV file
$result | Export-Csv -Path 'C:\SomePath\SomeFolder\GitLog.csv' -NoTypeInformation -UseCulture
To offer a conceptually simpler alternative to The fourth bird's helpful answer, based on the -split operator:
$workItemNumbers =
git log Version_10.1.18.1...Version_10.1.18 --format=%s |
ForEach-Object { ($_ -split ':')[1].Trim(' #') }
Note:
This assumes that all log entries have commit messages formatted like the ones shown in your question.
If this assumption doesn't hold, consider Theo's helpful answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With