Skip to content

Commit

Permalink
Detect conflicts with 3way merge (go-gitea#18536)
Browse files Browse the repository at this point in the history
Backport go-gitea#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 go-gitea#18514

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Feb 1, 2022
1 parent f7606de commit d85bb24
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions services/pull/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,10 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
if prConfig.IgnoreWhitespaceConflicts {
args = append(args, "--ignore-whitespace")
}
is3way := false
if git.CheckGitVersionAtLeast("2.32.0") == nil {
args = append(args, "--3way")
is3way = true
}
args = append(args, patchPath)
pr.ConflictedFiles = make([]string, 0, 5)
Expand Down Expand Up @@ -379,6 +381,9 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath

const prefix = "error: patch failed:"
const errorPrefix = "error: "
const threewayFailed = "Failed to perform three-way merge..."
const appliedPatchPrefix = "Applied patch to '"
const withConflicts = "' with conflicts."

conflictMap := map[string]bool{}

Expand All @@ -401,6 +406,12 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
break
}
}
} else if is3way && strings.HasPrefix(line, appliedPatchPrefix) && strings.HasSuffix(line, withConflicts) {
conflict = true
filepath := strings.TrimPrefix(strings.TrimSuffix(line, withConflicts), appliedPatchPrefix)
if filepath != "" {
conflictMap[filepath] = true
}
}
// only list 10 conflicted files
if len(conflictMap) >= 10 {
Expand Down

0 comments on commit d85bb24

Please sign in to comment.