Skip to content

Commit 9a7f59e

Browse files
ethantkoenigappleboy
authored andcommitted
Fix fast-forward PR bug (#2137)
* Fix fast-forward PR bug * Don't ignore error in getMergeCommit (#1843)
1 parent 6a6f061 commit 9a7f59e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

models/pull.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,15 @@ func (pr *PullRequest) getMergeCommit() (*git.Commit, error) {
499499
return nil, fmt.Errorf("git merge-base --is-ancestor: %v %v", stderr, err)
500500
}
501501

502-
// We can ignore this error since we only get here when there's a valid commit in headFile
503-
commitID, _ := ioutil.ReadFile(pr.BaseRepo.RepoPath() + "/" + headFile)
504-
cmd := string(commitID)[:40] + ".." + pr.BaseBranch
502+
commitIDBytes, err := ioutil.ReadFile(pr.BaseRepo.RepoPath() + "/" + headFile)
503+
if err != nil {
504+
return nil, fmt.Errorf("ReadFile(%s): %v", headFile, err)
505+
}
506+
commitID := string(commitIDBytes)
507+
if len(commitID) < 40 {
508+
return nil, fmt.Errorf(`ReadFile(%s): invalid commit-ID "%s"`, headFile, commitID)
509+
}
510+
cmd := commitID[:40] + ".." + pr.BaseBranch
505511

506512
// Get the commit from BaseBranch where the pull request got merged
507513
mergeCommit, stderr, err := process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("isMerged (git rev-list --ancestry-path --merges --reverse): %d", pr.BaseRepo.ID),
@@ -510,6 +516,9 @@ func (pr *PullRequest) getMergeCommit() (*git.Commit, error) {
510516

511517
if err != nil {
512518
return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %v %v", stderr, err)
519+
} else if len(mergeCommit) < 40 {
520+
// PR was fast-forwarded, so just use last commit of PR
521+
mergeCommit = commitID[:40]
513522
}
514523

515524
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())

0 commit comments

Comments
 (0)