-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-change-log.ps1
238 lines (186 loc) · 5.69 KB
/
generate-change-log.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#How to use:
# .\generate-change-log.ps1 -generateVersion $true -autoCommitAndPush $true -autoGetLatestDevelop $true
Param
(
[Parameter(Mandatory = $true)]
[bool]$generateVersion,
[Parameter(Mandatory = $true)]
[bool]$autoCommitAndPush,
[Parameter(Mandatory = $true)]
[bool]$autoGetLatestDevelop
);
#region [Get comment/id/hash]
<#
.SYNOPSIS
Get comment from current commit
.DESCRIPTION
Get comment added by user on commit changes
.PARAMETER commitItem
A description of the commitItem parameter.
.EXAMPLE
PS C:\> Get-CommitMessage
.NOTES
#>
function Get-CommitMessage
{
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$commitItem
)
return $commitItem.Substring(43, $commitItem.Length - 43);
}
<#
.SYNOPSIS
Get commit hash
.DESCRIPTION
Get commit hash assigned to user on add new changes
.PARAMETER commitItem
A description of the commitItem parameter.
.EXAMPLE
PS C:\> Get-CommitHash
.NOTES
#>
function Get-CommitHash
{
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$commitItem
)
return $commitItem.Substring(2, 7);
}
<#
.SYNOPSIS
Get commit identifier
.DESCRIPTION
Get current commit identifier generated when user add new changes
.PARAMETER commitItem
A description of the commitItem parameter.
.EXAMPLE
PS C:\> Get-CommitId
.NOTES
#>
function Get-CommitId
{
[OutputType([string])]
param
(
[Parameter(Mandatory = $true)]
[string]$commitItem
)
return $commitItem.Substring(2, 40);
}
#endregion
#region [Change log]
<#
.SYNOPSIS
Add/Append cahnge log
.DESCRIPTION
Append to current change log file new modification
.PARAMETER commitHash
Commit hash code.
.PARAMETER commitComment
Commit comment.
.PARAMETER changeType
0 => Add empty row;
1 => Add Version;
2 => Add new change row.
.EXAMPLE
PS C:\> Add-ChangeLog
.NOTES
#>
function Add-ChangeLog
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0)]
[int16]$changeType,
[Parameter(Mandatory = $false, Position = 1)]
[string]$commitHash,
[Parameter(Mandatory = $false, Position = 2)]
[string]$commitComment,
[Parameter(Mandatory = $false, Position = 3)]
[string]$version
)
$changeLogPath = "..\CHANGELOG.md";
If ($changeType -eq 0)
{ @("") + (Get-Content $changeLogPath) | Set-Content $changeLogPath; }
ElseIf ($changeType -eq 1)
{
$userName = git config user.name;
$userEmail = git config user.email;
@("# v$version [$userName](mailto:$userEmail)") + (Get-Content $changeLogPath) | Set-Content $changeLogPath;
}
Else
{
$userCommit = git show -s --format='%an' $commitHash;
$commitRecord = "* [$commitHash] ($userCommit) -> " + $commitComment;
@($commitRecord) + (Get-Content $changeLogPath) | Set-Content $changeLogPath;
}
}
#endregion
#region [Main Execution]
$brachToCheck = "develop";
$currentBranch = git branch --show-current;
If ($autoGetLatestDevelop -eq $true)
{
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "# Get the latest develop branch and commit to the current" -ForegroundColor Green;
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "";
# Auto get the latest 'develop' branch version and merge to the current branch
git checkout $brachToCheck;
git pull origin $brachToCheck;
git checkout $currentBranch;
git merge $brachToCheck;
}
# Generate commit changes
$brachDiffCommits = git cherry -v $brachToCheck $currentBranch;
If (($brachDiffCommits -ne $null -and ([Array]$brachDiffCommits).Length -gt 0) -or ($generateVersion -eq $true))
{
# Add empty row before all commits
Add-ChangeLog -changeType 0;
}
If ($brachDiffCommits -ne $null -and ([Array]$brachDiffCommits).Length -gt 0)
{
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "# Generate changelog (code comments) of the current branch" -ForegroundColor Green;
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "";
# Add new commit log
foreach ($commitItem in $brachDiffCommits)
{
$commitHash = Get-CommitHash -commitItem $commitItem;
$commitComment = Get-CommitMessage -commitItem $commitItem;
Add-ChangeLog -changeType 2 -commitHash $commitHash -commitComment $commitComment;
}
}
If ($generateVersion -eq $true)
{
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "# Generate new application version" -ForegroundColor Green;
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "";
# TODO: Find the best solution when to generate a new app version and append it to the changelog file
# Generate new application version
$appVersion = .\generate-new-version.ps1;
Add-ChangeLog -changeType 1 -version $appVersion;
}
If ($autoCommitAndPush -eq $true) # Auto commit and push to origin
{
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "# Auto commit and push the current branch to the origin" -ForegroundColor Green;
Write-Host "#-------------------------------------------------------------------#" -ForegroundColor DarkGreen;
Write-Host "";
$autoCommitMessage = "";
If ($generateVersion -eq $true) { $autoCommitMessage = "Generate new application version and changelog from commits."; }
Else { $autoCommitMessage = "Generate new application changelog from commits."; }
git add --all;
git commit -m $autoCommitMessage;
git push -u origin $currentBranch;
}
#endregion