-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathGitUtilities.groovy
448 lines (392 loc) · 13.6 KB
/
GitUtilities.groovy
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
@groovy.transform.BaseScript com.ibm.dbb.groovy.ScriptLoader baseScript
import com.ibm.dbb.metadata.*
import com.ibm.dbb.dependency.*
import com.ibm.dbb.build.*
import groovy.transform.*
import java.util.regex.Matcher
import java.util.regex.Pattern
@Field BuildProperties props = BuildProperties.getInstance()
@Field MetadataStore metadataStore
/*
* Tests if directory is in a local git repository
*
* @param String dir Directory to test
* @return boolean
*/
def isGitDir(String dir) {
String cmd = "git -C $dir rev-parse --is-inside-work-tree"
StringBuffer gitResponse = new StringBuffer()
StringBuffer gitError = new StringBuffer()
boolean isGit = false
Process process = cmd.execute()
process.waitForProcessOutput(gitResponse, gitError)
if (gitError) {
String warningMsg = "*? Warning executing isGitDir($dir). Git command: $cmd error: $gitError"
println(warningMsg)
updateBuildResult(warningMsg:warningMsg)
}
else if (gitResponse) {
isGit = gitResponse.toString().trim().toBoolean()
}
return isGit
}
/*
* Returns the current Git branch
*
* @param String gitDir Local Git repository directory
* @return String gitBranch The current Git branch
*/
def getCurrentGitBranch(String gitDir) {
String cmd = "git -C $gitDir rev-parse --abbrev-ref HEAD"
StringBuffer gitBranch = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitBranch, gitError)
if (gitError) {
println("*! Error executing Git command: $cmd error: $gitError")
}
return gitBranch.toString().trim()
}
/*
* Returns the current Git branch in detached HEAD state
*
* @param String gitDir Local Git repository directory
* @return String gitBranch The current Git branch
*/
def getCurrentGitDetachedBranch(String gitDir) {
String cmd = "git -C $gitDir show -s --pretty=%D HEAD"
StringBuffer gitBranch = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute();
process.waitForProcessOutput(gitBranch, gitError)
if (gitError) {
println("*! Error executing Git command: $cmd error: $gitError")
}
String gitBranchString = gitBranch.toString()
String[] gitBranchesArray = gitBranchString.split(',')
def solution = ""
// expecting references with "origin" as segment
def origin = "origin/"
if (gitBranchesArray.count {it.contains(origin)} > 1 ) {
String warningMsg = "*! (GitUtils.getCurrentGitDetachedBranch) Issue when obtaining the current branch for ($gitDir): multiple references ($gitBranchString) point to the same commit. Consider passing the current Git branch with the '--applicationCurrentBranch' CLI parameter."
println(warningMsg)
updateBuildResult(warningMsg:warningMsg)
}
// substring the branch name
for (i = 0; i < gitBranchesArray.length; i++) {
if (gitBranchesArray[i].contains(origin)) {
solution = gitBranchesArray[i].replaceAll(".*?${origin}", "").trim()
}
}
if (solution != "") { // return branch name
return solution
} else {
String errorMsg = "*! (GitUtils.getCurrentGitDetachedBranch) Error extracting current branch name: $gitBranch. Expects a origin/ segment."
println(errorMsg)
props.error = "true"
updateBuildResult(errorMsg:errorMsg)
}
}
/*
* Returns the current Git branch
*
* @param String gitGit git directory
* @return List list of remote branches
*/
def getRemoteGitBranches(String gitDir) {
Set<String> remoteBranches = new HashSet<String>()
String cmd = "git -C $gitDir branch -r"
StringBuffer gitOut = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitOut, gitError)
if (gitError) {
println("*! Error executing Git command: $cmd error: $gitError")
} else {
for (line in gitOut.toString().split("\n")) {
remoteBranches.add(line.replaceAll(".*?/", ""))
}
}
return remoteBranches
}
/*
* Returns true if this is a detached HEAD
*
* @param String gitDir Local Git repository directory
*/
def isGitDetachedHEAD(String gitDir) {
String cmd = "git -C $gitDir status"
StringBuffer gitStatus = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitStatus, gitError)
if (gitError) {
println("*! Error executing Git command: $cmd error $gitError")
}
return gitStatus.toString().contains("HEAD detached at")
}
/*
* Returns the current Git hash
*
* @param String gitDir Local Git repository directory
* @return String gitHash The current Git hash
*/
def getCurrentGitHash(String gitDir, boolean abbrev) {
String cmd = "git -C $gitDir rev-parse HEAD"
if (abbrev) cmd = "git -C $gitDir rev-parse --short=8 HEAD"
StringBuffer gitHash = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitHash, gitError)
if (gitError) {
String errorMsg = "*! Error executing Git command: $cmd error: $gitError"
println(errorMsg)
props.error = "true"
updateBuildResult(errorMsg:errorMsg)
}
return gitHash.toString().trim()
}
/*
* Returns the current Git hash for this file path
*
* @param String gitDir Local Git repository directory
* @param String filePath filePath relative to gitDir
* @return String gitHash The current Git hash
*/
def getFileCurrentGitHash(String gitDir, String filePath) {
String cmd = "git -C $gitDir rev-list -1 HEAD " + filePath
StringBuffer gitHash = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitHash, gitError)
if (gitError) {
print("*! Error executing Git command: $cmd error: $gitError")
}
return gitHash.toString().trim()
}
/*
* Returns the current Git url
*
* @param String gitDir Local Git repository directory
* @return String gitUrl The current masked Git url
*/
def getCurrentGitUrl(String gitDir) {
String maskedGitUrl = new String()
String cmd = "git -C $gitDir config --get remote.origin.url"
StringBuffer gitUrl = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitUrl, gitError)
if (gitError) {
print("*! Error executing Git command: $cmd error: $gitError")
}
// Mask credentials that may be added by the pipeline orchestrator
// into the git remote.origin.url configuration
// applies only for http/https configs
// Find // and then take all until the @ char. This is where the PAT is
// stored
String regex = "(?<=.\\/\\/)[^@]*(?=@)";
String subst = "***";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(gitUrl.toString().trim());
maskedGitUrl = matcher.replaceAll(subst);
return maskedGitUrl
}
/*
* Returns the lst previous Git commit hash
*
* @param String gitDir Local Git repository directory
* @return String gitHash The previous Git commit hash
*/
def getPreviousGitHash(String gitDir) {
String cmd = "git -C $gitDir --no-pager log -n 1 --skip=1"
StringBuffer gitStdout = new StringBuffer()
StringBuffer gitError = new StringBuffer()
Process process = cmd.execute()
process.waitForProcessOutput(gitStdout, gitError)
if (gitError) {
print("*! Error executing Git command: $cmd error: $gitError")
}
else {
return gitStdout.toString().minus('commit').trim().split()[0]
}
}
/*
* getChangedFiles - assembles a git diff command to support the impactBuild for a given directory
* returns the changed, deleted and renamed files.
*
*/
def getChangedFiles(String gitDir, String baseHash, String currentHash) {
String gitCmd = "git -C $gitDir --no-pager diff --name-status $baseHash $currentHash"
return getChangedFiles(gitCmd)
}
/*
* getMergeChanges - assembles a git triple-dot diff command to support mergeBuild scenarios
* returns the changed, deleted and renamed files between current HEAD and the provided baseline.
*
*/
def getMergeChanges(String gitDir, String baselineReference) {
String gitCmd = "git -C $gitDir --no-pager diff --name-status remotes/origin/$baselineReference...HEAD"
return getChangedFiles(gitCmd)
}
/*
* getMergeChanges - assembles a git triple-dot diff command to support mergeBuild scenarios
* returns the changed, deleted and renamed files between current HEAD and the provided baseline.
*
*/
def getConcurrentChanges(String gitDir, String baselineReference) {
String gitCmd = "git -C $gitDir --no-pager diff --name-status HEAD...remotes/origin/$baselineReference"
return getChangedFiles(gitCmd)
}
/*
* getChangedFiles - internal method to submit the a gitDiff command and calucate and classify the idenfified changes
*/
def getChangedFiles(String cmd) {
def git_diff = new StringBuffer()
def git_error = new StringBuffer()
def changedFiles = []
def deletedFiles = []
def renamedFiles = []
def process = cmd.execute()
process.waitForProcessOutput(git_diff, git_error)
// handle command error
if (git_error.size() > 0) {
String errorMsg = "*! Error executing Git command: $cmd error: $git_error \n *! Attempting to parse unstable git command for changed files..."
println(errorMsg)
props.error = "true"
updateBuildResult(errorMsg:errorMsg)
}
for (line in git_diff.toString().split("\n")) {
// process files from git diff
try {
gitDiffOutput = line.split()
action = gitDiffOutput[0]
file = gitDiffOutput[1]
if (action == "M" || action == "A") { // handle changed and new added files
changedFiles.add(file)
} else if (action == "D") {// handle deleted files
deletedFiles.add(file)
} else if (action.startsWith("R")) { // handle renamed file
renamedFile = gitDiffOutput[1]
newFileName = gitDiffOutput[2]
changedFiles.add(newFileName) // will rebuild file
renamedFiles.add(renamedFile)
//evaluate similarity score
similarityScore = action.substring(1) as int
if (similarityScore < 50){
println ("*! (GitUtils.getChangedFiles - Renaming Scenario) Low similarity score for renamed file $renamedFile : $similarityScore with new file $newFileName. ")
}
}
else {
println ("*! (GitUtils.getChangedFiles) Error in determining action in git diff. ")
println ("*! (GitUtils.getChangedFiles) Git diff output: $line. ")
}
}
catch (Exception e) {
// no changes or unhandled format
}
}
return [
changedFiles,
deletedFiles,
renamedFiles
]
}
def getCurrentChangedFiles(String gitDir, String currentHash, String verbose) {
if (verbose) println "** Running git command: git -C $gitDir show --pretty=format: --name-status $currentHash"
String cmd = "git -C $gitDir show --pretty=format: --name-status $currentHash"
def gitDiff = new StringBuffer()
def gitError = new StringBuffer()
def changedFiles = []
def deletedFiles = []
def renamedFiles = []
Process process = cmd.execute()
process.waitForProcessOutput(gitDiff, gitError)
// handle command error
if (gitError.size() > 0) {
println("*! Error executing Git command: $cmd error: $gitError")
println ("*! Attempting to parse unstable git command for changed files...")
}
for (line in gitDiff.toString().split("\n")) {
if (verbose) println "** Git command line: $line"
// process files from git diff
try {
gitDiffOutput = line.split()
action = gitDiffOutput[0]
file = gitDiffOutput[1]
if (action == "M" || action == "A") { // handle changed and new added files
changedFiles.add(file)
} else if (action == "D") {// handle deleted files
deletedFiles.add(file)
} else if (action == "R100") { // handle renamed file
renamedFile = gitDiffOutput[1]
newFileName = gitDiffOutput[2]
changedFiles.add(newFileName) // will rebuild file
renamedFiles.add(renamedFile)
}
else {
println ("*! (GitUtils.getChangedFiles) Error in determining action in git diff. ")
println ("*! (GitUtils.getChangedFiles) Git diff output: $line. ")
}
}
catch (Exception e) {
// no changes or unhandled format
}
}
return [
changedFiles,
deletedFiles,
renamedFiles
]
}
def getChangedProperties(String gitDir, String baseline, String currentHash, String propertiesFile) {
String cmd = "git -C $gitDir diff --ignore-all-space --no-prefix -U0 $baseline $currentHash $propertiesFile"
def gitDiff = new StringBuffer()
def gitError = new StringBuffer()
Properties changedProperties = new Properties()
Process process = cmd.execute()
process.waitForProcessOutput(gitDiff, gitError)
for (line in gitDiff.toString().split("\n")) {
if (line.startsWith("+") && line.contains("=")){
try {
gitDiffOutput = line.substring(1)
changedProperties.load(new StringReader(gitDiffOutput));
}
catch (Exception e) {
// no changes or unhandled format
}
}
}
return changedProperties.propertyNames()
}
/** helper methods **/
def getMetadataStore() {
if (!metadataStore)
metadataStore = MetadataStoreFactory.getMetadataStore()
return metadataStore
}
/*
* updateBuildResult - for git cmd related issues
*/
def updateBuildResult(Map args) {
// args : errorMsg / warningMsg
MetadataStore metadataStore = MetadataStoreFactory.getMetadataStore()
// update build results only in non-userbuild scenarios
if (metadataStore && !props.userBuild) {
def buildResult = metadataStore.getBuildResult(props.applicationBuildGroup, props.applicationBuildLabel)
if (!buildResult) {
println "*! No build result found for BuildGroup '${props.applicationBuildGroup}' and BuildLabel '${props.applicationBuildLabel}'"
return
}
// add error message
if (args.errorMsg) {
buildResult.setStatus(buildResult.ERROR)
buildResult.addProperty("error", args.errorMsg)
}
// add warning message, but keep result status
if (args.warningMsg) {
// buildResult.setStatus(buildResult.WARNING)
buildResult.addProperty("warning", args.warningMsg)
}
}
}