@@ -714,6 +714,8 @@ func indexCommit(commits []*git.Commit, commitID string) *git.Commit {
714714
715715// ViewPullFiles render pull request changed files list page
716716func viewPullFiles (ctx * context.Context , beforeCommitID , afterCommitID string ) {
717+ var err error
718+
717719 ctx .Data ["PageIsPullList" ] = true
718720 ctx .Data ["PageIsPullFiles" ] = true
719721
@@ -740,43 +742,53 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
740742 }
741743
742744 isSingleCommit := beforeCommitID == "" && afterCommitID != ""
743- ctx . Data [ "IsShowingOnlySingleCommit" ] = isSingleCommit
745+ // FIXME: when afterCommitID==headCommitID, isSingleCommit and isShowAllCommits can be both true, which doesn't seem right
744746 isShowAllCommits := (beforeCommitID == "" || beforeCommitID == prInfo .MergeBase ) && (afterCommitID == "" || afterCommitID == headCommitID )
747+
748+ ctx .Data ["IsShowingOnlySingleCommit" ] = isSingleCommit
745749 ctx .Data ["IsShowingAllCommits" ] = isShowAllCommits
746750
747- if afterCommitID == "" || afterCommitID == headCommitID {
748- afterCommitID = headCommitID
749- }
751+ // "commits list" is half-open, half-closed: (base, head]
752+ // * base commit is not in the list
753+ // * if the PR is empty, the list is also empty (head commit is not in the list)
754+
755+ afterCommitID = util .IfZero (afterCommitID , headCommitID )
750756 afterCommit := indexCommit (prInfo .Commits , afterCommitID )
757+ if afterCommit == nil && afterCommitID == headCommitID {
758+ afterCommit , err = gitRepo .GetCommit (afterCommitID )
759+ if err != nil {
760+ ctx .ServerError ("GetCommit(afterCommitID)" , err )
761+ return
762+ }
763+ }
751764 if afterCommit == nil {
752- ctx .HTTPError ( http . StatusBadRequest , "after commit not found in PR commits" )
765+ ctx .NotFound ( nil )
753766 return
754767 }
755768
756769 var beforeCommit * git.Commit
757- if ! isSingleCommit {
758- if beforeCommitID == "" || beforeCommitID == prInfo .MergeBase {
759- beforeCommitID = prInfo .MergeBase
770+ if isSingleCommit {
771+ beforeCommit , err = afterCommit .Parent (0 )
772+ if err != nil {
773+ ctx .ServerError ("afterCommit.Parent" , err )
774+ return
775+ }
776+ beforeCommitID = beforeCommit .ID .String ()
777+ } else {
778+ beforeCommitID = util .IfZero (beforeCommitID , prInfo .MergeBase )
779+ beforeCommit = indexCommit (prInfo .Commits , beforeCommitID )
780+ if beforeCommit == nil && beforeCommitID == prInfo .MergeBase {
760781 // mergebase commit is not in the list of the pull request commits
761782 beforeCommit , err = gitRepo .GetCommit (beforeCommitID )
762783 if err != nil {
763- ctx .ServerError ("GetCommit" , err )
784+ ctx .ServerError ("GetCommit(beforeCommitID) " , err )
764785 return
765786 }
766- } else {
767- beforeCommit = indexCommit (prInfo .Commits , beforeCommitID )
768- if beforeCommit == nil {
769- ctx .HTTPError (http .StatusBadRequest , "before commit not found in PR commits" )
770- return
771- }
772- }
773- } else {
774- beforeCommit , err = afterCommit .Parent (0 )
775- if err != nil {
776- ctx .ServerError ("Parent" , err )
777- return
778787 }
779- beforeCommitID = beforeCommit .ID .String ()
788+ }
789+ if beforeCommit == nil {
790+ ctx .NotFound (nil )
791+ return
780792 }
781793
782794 ctx .Data ["Username" ] = ctx .Repo .Owner .Name
0 commit comments