@@ -270,6 +270,10 @@ func AttemptThreeWayMerge(ctx context.Context, gitPath string, gitRepo *git.Repo
270
270
}
271
271
272
272
func checkConflicts (ctx context.Context , pr * models.PullRequest , gitRepo * git.Repository , tmpBasePath string ) (bool , error ) {
273
+ // 1. checkConflicts resets the conflict status - therefore - reset the conflict status
274
+ pr .ConflictedFiles = nil
275
+
276
+ // 2. AttemptThreeWayMerge first - this is much quicker than plain patch to base
273
277
description := fmt .Sprintf ("PR[%d] %s/%s#%d" , pr .ID , pr .BaseRepo .OwnerName , pr .BaseRepo .Name , pr .Index )
274
278
conflict , _ , err := AttemptThreeWayMerge (ctx ,
275
279
tmpBasePath , gitRepo , pr .MergeBase , "base" , "tracking" , description )
@@ -290,16 +294,14 @@ func checkConflicts(ctx context.Context, pr *models.PullRequest, gitRepo *git.Re
290
294
if treeHash == baseTree .ID .String () {
291
295
log .Debug ("PullRequest[%d]: Patch is empty - ignoring" , pr .ID )
292
296
pr .Status = models .PullRequestStatusEmpty
293
- pr .ConflictedFiles = []string {}
294
- pr .ChangedProtectedFiles = []string {}
295
297
}
296
298
297
299
return false , nil
298
300
}
299
301
300
- // OK read-tree has failed so we need to try a different thing - this might actually succeed where the above fails due to whitespace handling.
302
+ // 3. OK read-tree has failed so we need to try a different thing - this might actually succeed where the above fails due to whitespace handling.
301
303
302
- // 1 . Create a plain patch from head to base
304
+ // 3a . Create a plain patch from head to base
303
305
tmpPatchFile , err := os .CreateTemp ("" , "patch" )
304
306
if err != nil {
305
307
log .Error ("Unable to create temporary patch file! Error: %v" , err )
@@ -322,34 +324,29 @@ func checkConflicts(ctx context.Context, pr *models.PullRequest, gitRepo *git.Re
322
324
patchPath := tmpPatchFile .Name ()
323
325
tmpPatchFile .Close ()
324
326
325
- // 1a . if the size of that patch is 0 - there can be no conflicts!
327
+ // 3b . if the size of that patch is 0 - there can be no conflicts!
326
328
if stat .Size () == 0 {
327
329
log .Debug ("PullRequest[%d]: Patch is empty - ignoring" , pr .ID )
328
330
pr .Status = models .PullRequestStatusEmpty
329
- pr .ConflictedFiles = []string {}
330
- pr .ChangedProtectedFiles = []string {}
331
331
return false , nil
332
332
}
333
333
334
334
log .Trace ("PullRequest[%d].testPatch (patchPath): %s" , pr .ID , patchPath )
335
335
336
- // 2. preset the pr.Status as checking (this is not save at present)
337
- pr .Status = models .PullRequestStatusChecking
338
-
339
- // 3. Read the base branch in to the index of the temporary repository
336
+ // 4. Read the base branch in to the index of the temporary repository
340
337
_ , err = git .NewCommand (gitRepo .Ctx , "read-tree" , "base" ).RunInDir (tmpBasePath )
341
338
if err != nil {
342
339
return false , fmt .Errorf ("git read-tree %s: %v" , pr .BaseBranch , err )
343
340
}
344
341
345
- // 4 . Now get the pull request configuration to check if we need to ignore whitespace
342
+ // 5 . Now get the pull request configuration to check if we need to ignore whitespace
346
343
prUnit , err := pr .BaseRepo .GetUnit (unit .TypePullRequests )
347
344
if err != nil {
348
345
return false , err
349
346
}
350
347
prConfig := prUnit .PullRequestsConfig ()
351
348
352
- // 5 . Prepare the arguments to apply the patch against the index
349
+ // 6 . Prepare the arguments to apply the patch against the index
353
350
args := []string {"apply" , "--check" , "--cached" }
354
351
if prConfig .IgnoreWhitespaceConflicts {
355
352
args = append (args , "--ignore-whitespace" )
@@ -360,9 +357,8 @@ func checkConflicts(ctx context.Context, pr *models.PullRequest, gitRepo *git.Re
360
357
is3way = true
361
358
}
362
359
args = append (args , patchPath )
363
- pr .ConflictedFiles = make ([]string , 0 , 5 )
364
360
365
- // 6 . Prep the pipe:
361
+ // 7 . Prep the pipe:
366
362
// - Here we could do the equivalent of:
367
363
// `git apply --check --cached patch_file > conflicts`
368
364
// Then iterate through the conflicts. However, that means storing all the conflicts
@@ -380,7 +376,7 @@ func checkConflicts(ctx context.Context, pr *models.PullRequest, gitRepo *git.Re
380
376
_ = stderrWriter .Close ()
381
377
}()
382
378
383
- // 7 . Run the check command
379
+ // 8 . Run the check command
384
380
conflict = false
385
381
err = git .NewCommand (gitRepo .Ctx , args ... ).
386
382
RunWithContext (& git.RunContext {
@@ -448,7 +444,7 @@ func checkConflicts(ctx context.Context, pr *models.PullRequest, gitRepo *git.Re
448
444
},
449
445
})
450
446
451
- // 8 . If there is a conflict the `git apply` command will return a non-zero error code - so there will be a positive error.
447
+ // 9 . If there is a conflict the `git apply` command will return a non-zero error code - so there will be a positive error.
452
448
if err != nil {
453
449
if conflict {
454
450
pr .Status = models .PullRequestStatusConflict
@@ -518,6 +514,11 @@ func CheckUnprotectedFiles(repo *git.Repository, oldCommitID, newCommitID string
518
514
519
515
// checkPullFilesProtection check if pr changed protected files and save results
520
516
func checkPullFilesProtection (pr * models.PullRequest , gitRepo * git.Repository ) error {
517
+ if pr .Status == models .PullRequestStatusEmpty {
518
+ pr .ChangedProtectedFiles = nil
519
+ return nil
520
+ }
521
+
521
522
if err := pr .LoadProtectedBranch (); err != nil {
522
523
return err
523
524
}
0 commit comments