@@ -683,6 +683,8 @@ func indexCommit(commits []*git.Commit, commitID string) *git.Commit {
683683
684684// ViewPullFiles render pull request changed files list page
685685func viewPullFiles (ctx * context.Context , beforeCommitID , afterCommitID string ) {
686+ var err error
687+
686688 ctx .Data ["PageIsPullList" ] = true
687689 ctx .Data ["PageIsPullFiles" ] = true
688690
@@ -707,44 +709,53 @@ func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
707709
708710 headCommitID := prCompareInfo .HeadCommitID
709711 isSingleCommit := beforeCommitID == "" && afterCommitID != ""
710- ctx . Data [ "IsShowingOnlySingleCommit" ] = isSingleCommit
712+ // FIXME: when afterCommitID==headCommitID, isSingleCommit and isShowAllCommits can be both true, which doesn't seem right
711713 isShowAllCommits := (beforeCommitID == "" || beforeCommitID == prCompareInfo .CompareBase ) && (afterCommitID == "" || afterCommitID == headCommitID )
714+
715+ ctx .Data ["IsShowingOnlySingleCommit" ] = isSingleCommit
712716 ctx .Data ["IsShowingAllCommits" ] = isShowAllCommits
713717
714- if afterCommitID == "" || afterCommitID == headCommitID {
715- afterCommitID = headCommitID
716- }
718+ // "commits list" is half-open, half-closed: (base, head]
719+ // * base commit is not in the list
720+ // * if the PR is empty, the list is also empty (head commit is not in the list)
721+
722+ afterCommitID = util .IfZero (afterCommitID , headCommitID )
717723 afterCommit := indexCommit (prCompareInfo .Commits , afterCommitID )
724+ if afterCommit == nil && afterCommitID == headCommitID {
725+ afterCommit , err = gitRepo .GetCommit (afterCommitID )
726+ if err != nil {
727+ ctx .ServerError ("GetCommit(afterCommitID)" , err )
728+ return
729+ }
730+ }
718731 if afterCommit == nil {
719- ctx .HTTPError ( http . StatusBadRequest , "after commit not found in PR commits" )
732+ ctx .NotFound ( nil )
720733 return
721734 }
722735
723736 var beforeCommit * git.Commit
724- var err error
725- if ! isSingleCommit {
726- if beforeCommitID == "" || beforeCommitID == prCompareInfo .CompareBase {
727- beforeCommitID = prCompareInfo .CompareBase
728- // merge base commit is not in the list of the pull request commits
729- beforeCommit , err = gitRepo .GetCommit (beforeCommitID )
730- if err != nil {
731- ctx .ServerError ("GetCommit" , err )
732- return
733- }
734- } else {
735- beforeCommit = indexCommit (prCompareInfo .Commits , beforeCommitID )
736- if beforeCommit == nil {
737- ctx .HTTPError (http .StatusBadRequest , "before commit not found in PR commits" )
738- return
739- }
740- }
741- } else {
737+ if isSingleCommit {
742738 beforeCommit , err = afterCommit .Parent (0 )
743739 if err != nil {
744- ctx .ServerError ("Parent" , err )
740+ ctx .ServerError ("afterCommit. Parent" , err )
745741 return
746742 }
747743 beforeCommitID = beforeCommit .ID .String ()
744+ } else {
745+ beforeCommitID = util .IfZero (beforeCommitID , prCompareInfo .CompareBase )
746+ beforeCommit = indexCommit (prCompareInfo .Commits , beforeCommitID )
747+ if beforeCommit == nil && beforeCommitID == prCompareInfo .CompareBase {
748+ // base commit is not in the list of the pull request commits
749+ beforeCommit , err = gitRepo .GetCommit (beforeCommitID )
750+ if err != nil {
751+ ctx .ServerError ("GetCommit(beforeCommitID)" , err )
752+ return
753+ }
754+ }
755+ }
756+ if beforeCommit == nil {
757+ ctx .NotFound (nil )
758+ return
748759 }
749760
750761 ctx .Data ["CompareInfo" ] = prCompareInfo
0 commit comments