-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParse-GitCommit.ps1
133 lines (123 loc) · 4.03 KB
/
Parse-GitCommit.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Commit,
[Parameter()]
[switch]$OnlyMetadata = $false
)
begin {
}
process {
$parsedPatch = @{
Files = @()
Commit = $null
AuthorName = $null
AuthorEmail = $null
Date = $null
Subject = $null
Description = @()
}
$currentFile = $null
if ($null -ne $Commit) {
$PatchContent = (git show $Commit)
}
foreach ($line in $PatchContent) {
if ($line -match '^\s*commit (.*)$') {
Write-Verbose "Recognized commit line:$line"
$parsedPatch.Commit = $matches[1]
}
elseif ($line -match '^\s*Author: (.*)$') {
Write-Verbose "Recognized author line:$line"
$parsedPatch.AuthorName = $matches[1]
}
elseif ($line -match '^\s*Date:\s+(.*)$') {
Write-Verbose "Recognized date line:$line"
$parsedPatch.Date = [DateTime]::ParseExact($matches[1], 'ddd MMM d HH:mm:ss yyyy', $null)
}
elseif ($line -match '^\s*Subject: (.*)$') {
Write-Verbose "Recognized subject line:$line"
$parsedPatch.Subject = $matches[1]
}
elseif ($line -match '^diff --git a\/(.+) b\/(.+)$') {
Write-Verbose "Recognized diff line:$line"
$currentFile = @{
OldPath = $matches[1]
NewPath = $matches[2]
Hunks = @()
}
$parsedPatch.Files += $currentFile
}
elseif ($line -match '^\s*index \S+..(\S+) (\d+)$') {
Write-Verbose "Recognized index line: $line"
$currentFile.Index = $matches[1]
$currentFile.Mode = $matches[2]
}
elseif ($line -match '^\s*--- (.*)$') {
Write-Verbose "Recognized original file path: $line"
if (-Not $currentFile) {
$currentFile = @{
OldPath = $matches[1]
NewPath = $matches[1]
Hunks = @()
}
} else {
$currentFile.OldPath = $matches[1]
}
}
elseif ($line -match '^\s*\+\+\+ (.*)$') {
Write-Verbose "Recognized new file path: $line"
$currentFile.NewPath = $matches[1]
}
elseif ($line -match '^@@ -(\d+),(\d+) \+(\d+),(\d+) @@(.*)$') {
Write-Verbose "Recognized hunk line: $line"
$hunk = @{
OldStart = $matches[1]
OldLength = $matches[2]
NewStart = $matches[3]
NewLength = $matches[4]
Context = $matches[5]
Lines = @()
}
$currentFile.Hunks += $hunk
}
elseif ($line -match '^([\+|\-])(.*)$') {
Write-Verbose "Recognized change line:$line"
if (-Not $currentFile) {
Write-Error "Current file is null"
}
$hunk = @{
Lines = @{
Prefix = $matches[1]
Content = $matches[2]
}
}
$currentFile.Hunks += $hunk
}
elseif ($currentFile -eq $null) {
Write-Verbose "Recognized description line: $line"
if ($null -eq $parsedPatch.Subject -and $line.Length -gt 2) {
$parsedPatch.Subject = $line.Trim(" ")
}
$parsedPatch.Description += $line
}
else {
Write-Verbose "Unrecognized line:$line"
}
if ($OnlyMetadata -and
$parsedPatch.Subject -and
$parsedPatch.AuthorName -and
$parsedPatch.Date -and
$parsedPatch.Commit) {
break;
}
}
if ($parsedPatch.AuthorName -match "^(.*)\s<(.+)>$") {
$parsedPatch.AuthorName = $matches[1]
$parsedPatch.AuthorEmail = $matches[2]
}
# Join description lines into a single string
$parsedPatch.Description = $parsedPatch.Description -join "`n"
Write-Output $parsedPatch
}
end {
}