Skip to content

Commit 0dfe5fa

Browse files
authored
Detect conflicts with 3way merge (#18536) (#18537)
Backport #18536 Unforunately git apply --3way reports conflicts differently than standard patches resulting in conflicts being missed. Adjust the conflict detection code to account for this different error reporting. Fix #18514 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 1d17313 commit 0dfe5fa

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

services/pull/patch.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,10 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
339339
if prConfig.IgnoreWhitespaceConflicts {
340340
args = append(args, "--ignore-whitespace")
341341
}
342+
is3way := false
342343
if git.CheckGitVersionAtLeast("2.32.0") == nil {
343344
args = append(args, "--3way")
345+
is3way = true
344346
}
345347
args = append(args, patchPath)
346348
pr.ConflictedFiles = make([]string, 0, 5)
@@ -379,6 +381,9 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
379381

380382
const prefix = "error: patch failed:"
381383
const errorPrefix = "error: "
384+
const threewayFailed = "Failed to perform three-way merge..."
385+
const appliedPatchPrefix = "Applied patch to '"
386+
const withConflicts = "' with conflicts."
382387

383388
conflictMap := map[string]bool{}
384389

@@ -390,6 +395,8 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
390395
conflict = true
391396
filepath := strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0])
392397
conflictMap[filepath] = true
398+
} else if is3way && line == threewayFailed {
399+
conflict = true
393400
} else if strings.HasPrefix(line, errorPrefix) {
394401
conflict = true
395402
for _, suffix := range patchErrorSuffices {
@@ -401,6 +408,12 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
401408
break
402409
}
403410
}
411+
} else if is3way && strings.HasPrefix(line, appliedPatchPrefix) && strings.HasSuffix(line, withConflicts) {
412+
conflict = true
413+
filepath := strings.TrimPrefix(strings.TrimSuffix(line, withConflicts), appliedPatchPrefix)
414+
if filepath != "" {
415+
conflictMap[filepath] = true
416+
}
404417
}
405418
// only list 10 conflicted files
406419
if len(conflictMap) >= 10 {

0 commit comments

Comments
 (0)