Skip to content

Commit 97eb37e

Browse files
authored
Fix rebase conflict detection in git 2.26 (#10929)
* Fix rebase conflict detection in git 2.26 Git changed the technique used in rebase from simple apply-patches to use merge. This breaks our conflict detection code. Signed-off-by: Andrew Thornton <[email protected]> * As per @techknowlogick reduce copying Signed-off-by: Andrew Thornton <[email protected]>
1 parent 57cca44 commit 97eb37e

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

services/pull/merge.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,34 @@ func rawMerge(pr *models.PullRequest, doer *models.User, mergeStyle models.Merge
263263
if err := git.NewCommand("rebase", baseBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil {
264264
// Rebase will leave a REBASE_HEAD file in .git if there is a conflict
265265
if _, statErr := os.Stat(filepath.Join(tmpBasePath, ".git", "REBASE_HEAD")); statErr == nil {
266-
// The original commit SHA1 that is failing will be in .git/rebase-apply/original-commit
267-
commitShaBytes, readErr := ioutil.ReadFile(filepath.Join(tmpBasePath, ".git", "rebase-apply", "original-commit"))
268-
if readErr != nil {
269-
// Abandon this attempt to handle the error
266+
var commitSha string
267+
ok := false
268+
failingCommitPaths := []string{
269+
filepath.Join(tmpBasePath, ".git", "rebase-apply", "original-commit"), // Git < 2.26
270+
filepath.Join(tmpBasePath, ".git", "rebase-merge", "stopped-sha"), // Git >= 2.26
271+
}
272+
for _, failingCommitPath := range failingCommitPaths {
273+
if _, statErr := os.Stat(filepath.Join(failingCommitPath)); statErr == nil {
274+
commitShaBytes, readErr := ioutil.ReadFile(filepath.Join(failingCommitPath))
275+
if readErr != nil {
276+
// Abandon this attempt to handle the error
277+
log.Error("git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
278+
return "", fmt.Errorf("git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
279+
}
280+
commitSha = strings.TrimSpace(string(commitShaBytes))
281+
ok = true
282+
break
283+
}
284+
}
285+
if !ok {
286+
log.Error("Unable to determine failing commit sha for this rebase message. Cannot cast as models.ErrRebaseConflicts.")
270287
log.Error("git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
271288
return "", fmt.Errorf("git rebase staging on to base [%s:%s -> %s:%s]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
272289
}
273-
log.Debug("RebaseConflict at %s [%s:%s -> %s:%s]: %v\n%s\n%s", strings.TrimSpace(string(commitShaBytes)), pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
290+
log.Debug("RebaseConflict at %s [%s:%s -> %s:%s]: %v\n%s\n%s", commitSha, pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String())
274291
return "", models.ErrRebaseConflicts{
275292
Style: mergeStyle,
276-
CommitSHA: strings.TrimSpace(string(commitShaBytes)),
293+
CommitSHA: commitSha,
277294
StdOut: outbuf.String(),
278295
StdErr: errbuf.String(),
279296
Err: err,

0 commit comments

Comments
 (0)