Skip to content

Commit 92e81e9

Browse files
zeripathlunny
andauthored
Detect conflicts with 3way merge (#18536)
* Detect conflicts with 3way merge 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]> * and three-way failed Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 5469e61 commit 92e81e9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

services/pull/patch.go

+13
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,10 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
343343
if prConfig.IgnoreWhitespaceConflicts {
344344
args = append(args, "--ignore-whitespace")
345345
}
346+
is3way := false
346347
if git.CheckGitVersionAtLeast("2.32.0") == nil {
347348
args = append(args, "--3way")
349+
is3way = true
348350
}
349351
args = append(args, patchPath)
350352
pr.ConflictedFiles = make([]string, 0, 5)
@@ -383,6 +385,9 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
383385

384386
const prefix = "error: patch failed:"
385387
const errorPrefix = "error: "
388+
const threewayFailed = "Failed to perform three-way merge..."
389+
const appliedPatchPrefix = "Applied patch to '"
390+
const withConflicts = "' with conflicts."
386391

387392
conflictMap := map[string]bool{}
388393

@@ -394,6 +399,8 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
394399
conflict = true
395400
filepath := strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0])
396401
conflictMap[filepath] = true
402+
} else if is3way && line == threewayFailed {
403+
conflict = true
397404
} else if strings.HasPrefix(line, errorPrefix) {
398405
conflict = true
399406
for _, suffix := range patchErrorSuffices {
@@ -405,6 +412,12 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
405412
break
406413
}
407414
}
415+
} else if is3way && strings.HasPrefix(line, appliedPatchPrefix) && strings.HasSuffix(line, withConflicts) {
416+
conflict = true
417+
filepath := strings.TrimPrefix(strings.TrimSuffix(line, withConflicts), appliedPatchPrefix)
418+
if filepath != "" {
419+
conflictMap[filepath] = true
420+
}
408421
}
409422
// only list 10 conflicted files
410423
if len(conflictMap) >= 10 {

0 commit comments

Comments
 (0)