@@ -643,8 +643,17 @@ func ViewPullCommits(ctx *context.Context) {
643643 ctx .HTML (http .StatusOK , tplPullCommits )
644644}
645645
646+ func indexCommit (commits []* git.Commit , commitID string ) * git.Commit {
647+ for i := range commits {
648+ if commits [i ].ID .String () == commitID {
649+ return commits [i ]
650+ }
651+ }
652+ return nil
653+ }
654+
646655// ViewPullFiles render pull request changed files list page
647- func viewPullFiles (ctx * context.Context , specifiedStartCommit , specifiedEndCommit string , willShowSpecifiedCommitRange , willShowSpecifiedCommit bool ) {
656+ func viewPullFiles (ctx * context.Context , beforeCommitID , afterCommitID string ) {
648657 ctx .Data ["PageIsPullList" ] = true
649658 ctx .Data ["PageIsPullFiles" ] = true
650659
@@ -654,11 +663,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
654663 }
655664 pull := issue .PullRequest
656665
657- var (
658- startCommitID string
659- endCommitID string
660- gitRepo = ctx .Repo .GitRepo
661- )
666+ gitRepo := ctx .Repo .GitRepo
662667
663668 prInfo := preparePullViewPullInfo (ctx , issue )
664669 if ctx .Written () {
@@ -668,88 +673,75 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
668673 return
669674 }
670675
671- // Validate the given commit sha to show (if any passed)
672- if willShowSpecifiedCommit || willShowSpecifiedCommitRange {
673- foundStartCommit := len (specifiedStartCommit ) == 0
674- foundEndCommit := len (specifiedEndCommit ) == 0
675-
676- if ! (foundStartCommit && foundEndCommit ) {
677- for _ , commit := range prInfo .Commits {
678- if commit .ID .String () == specifiedStartCommit {
679- foundStartCommit = true
680- }
681- if commit .ID .String () == specifiedEndCommit {
682- foundEndCommit = true
683- }
684-
685- if foundStartCommit && foundEndCommit {
686- break
687- }
688- }
689- }
690-
691- if ! (foundStartCommit && foundEndCommit ) {
692- ctx .NotFound (nil )
693- return
694- }
695- }
696-
697- if ctx .Written () {
698- return
699- }
700-
701676 headCommitID , err := gitRepo .GetRefCommitID (pull .GetGitHeadRefName ())
702677 if err != nil {
703678 ctx .ServerError ("GetRefCommitID" , err )
704679 return
705680 }
706681
707- ctx .Data ["IsShowingOnlySingleCommit" ] = willShowSpecifiedCommit
682+ isSingleCommit := beforeCommitID == "" && afterCommitID != ""
683+ ctx .Data ["IsShowingOnlySingleCommit" ] = isSingleCommit
684+ isShowAllCommits := (beforeCommitID == "" || beforeCommitID == prInfo .MergeBase ) && (afterCommitID == "" || afterCommitID == headCommitID )
685+ ctx .Data ["IsShowingAllCommits" ] = isShowAllCommits
708686
709- if willShowSpecifiedCommit || willShowSpecifiedCommitRange {
710- if len (specifiedEndCommit ) > 0 {
711- endCommitID = specifiedEndCommit
712- } else {
713- endCommitID = headCommitID
714- }
715- if len (specifiedStartCommit ) > 0 {
716- startCommitID = specifiedStartCommit
687+ if afterCommitID == "" || afterCommitID == headCommitID {
688+ afterCommitID = headCommitID
689+ }
690+ afterCommit := indexCommit (prInfo .Commits , afterCommitID )
691+ if afterCommit == nil {
692+ ctx .HTTPError (http .StatusBadRequest , "after commit not found in PR commits" )
693+ return
694+ }
695+
696+ var beforeCommit * git.Commit
697+ if ! isSingleCommit {
698+ if beforeCommitID == "" || beforeCommitID == prInfo .MergeBase {
699+ beforeCommitID = prInfo .MergeBase
700+ // mergebase commit is not in the list of the pull request commits
701+ beforeCommit , err = gitRepo .GetCommit (beforeCommitID )
702+ if err != nil {
703+ ctx .ServerError ("GetCommit" , err )
704+ return
705+ }
717706 } else {
718- startCommitID = prInfo .MergeBase
707+ beforeCommit = indexCommit (prInfo .Commits , beforeCommitID )
708+ if beforeCommit == nil {
709+ ctx .HTTPError (http .StatusBadRequest , "before commit not found in PR commits" )
710+ return
711+ }
719712 }
720- ctx .Data ["IsShowingAllCommits" ] = false
721713 } else {
722- endCommitID = headCommitID
723- startCommitID = prInfo .MergeBase
724- ctx .Data ["IsShowingAllCommits" ] = true
714+ beforeCommit , err = afterCommit .Parent (0 )
715+ if err != nil {
716+ ctx .ServerError ("Parent" , err )
717+ return
718+ }
719+ beforeCommitID = beforeCommit .ID .String ()
725720 }
726721
727722 ctx .Data ["Username" ] = ctx .Repo .Owner .Name
728723 ctx .Data ["Reponame" ] = ctx .Repo .Repository .Name
729- ctx .Data ["AfterCommitID" ] = endCommitID
730- ctx .Data ["BeforeCommitID" ] = startCommitID
731-
732- fileOnly := ctx .FormBool ("file-only" )
724+ ctx .Data ["MergeBase" ] = prInfo .MergeBase
725+ ctx .Data ["AfterCommitID" ] = afterCommitID
726+ ctx .Data ["BeforeCommitID" ] = beforeCommitID
733727
734728 maxLines , maxFiles := setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffFiles
735729 files := ctx .FormStrings ("files" )
730+ fileOnly := ctx .FormBool ("file-only" )
736731 if fileOnly && (len (files ) == 2 || len (files ) == 1 ) {
737732 maxLines , maxFiles = - 1 , - 1
738733 }
739734
740735 diffOptions := & gitdiff.DiffOptions {
741- AfterCommitID : endCommitID ,
736+ BeforeCommitID : beforeCommitID ,
737+ AfterCommitID : afterCommitID ,
742738 SkipTo : ctx .FormString ("skip-to" ),
743739 MaxLines : maxLines ,
744740 MaxLineCharacters : setting .Git .MaxGitDiffLineCharacters ,
745741 MaxFiles : maxFiles ,
746742 WhitespaceBehavior : gitdiff .GetWhitespaceFlag (ctx .Data ["WhitespaceBehavior" ].(string )),
747743 }
748744
749- if ! willShowSpecifiedCommit {
750- diffOptions .BeforeCommitID = startCommitID
751- }
752-
753745 diff , err := gitdiff .GetDiffForRender (ctx , ctx .Repo .RepoLink , gitRepo , diffOptions , files ... )
754746 if err != nil {
755747 ctx .ServerError ("GetDiff" , err )
@@ -761,15 +753,15 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
761753 // as the viewed information is designed to be loaded only on latest PR
762754 // diff and if you're signed in.
763755 var reviewState * pull_model.ReviewState
764- if ctx .IsSigned && ! willShowSpecifiedCommit && ! willShowSpecifiedCommitRange {
756+ if ctx .IsSigned && isShowAllCommits {
765757 reviewState , err = gitdiff .SyncUserSpecificDiff (ctx , ctx .Doer .ID , pull , gitRepo , diff , diffOptions )
766758 if err != nil {
767759 ctx .ServerError ("SyncUserSpecificDiff" , err )
768760 return
769761 }
770762 }
771763
772- diffShortStat , err := gitdiff .GetDiffShortStat (ctx .Repo .GitRepo , startCommitID , endCommitID )
764+ diffShortStat , err := gitdiff .GetDiffShortStat (ctx .Repo .GitRepo , beforeCommitID , afterCommitID )
773765 if err != nil {
774766 ctx .ServerError ("GetDiffShortStat" , err )
775767 return
@@ -816,7 +808,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
816808
817809 if ! fileOnly {
818810 // note: use mergeBase is set to false because we already have the merge base from the pull request info
819- diffTree , err := gitdiff .GetDiffTree (ctx , gitRepo , false , startCommitID , endCommitID )
811+ diffTree , err := gitdiff .GetDiffTree (ctx , gitRepo , false , beforeCommitID , afterCommitID )
820812 if err != nil {
821813 ctx .ServerError ("GetDiffTree" , err )
822814 return
@@ -836,25 +828,14 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
836828 ctx .Data ["Diff" ] = diff
837829 ctx .Data ["DiffNotAvailable" ] = diffShortStat .NumFiles == 0
838830
839- baseCommit , err := ctx .Repo .GitRepo .GetCommit (startCommitID )
840- if err != nil {
841- ctx .ServerError ("GetCommit" , err )
842- return
843- }
844- commit , err := gitRepo .GetCommit (endCommitID )
845- if err != nil {
846- ctx .ServerError ("GetCommit" , err )
847- return
848- }
849-
850831 if ctx .IsSigned && ctx .Doer != nil {
851832 if ctx .Data ["CanMarkConversation" ], err = issues_model .CanMarkConversation (ctx , issue , ctx .Doer ); err != nil {
852833 ctx .ServerError ("CanMarkConversation" , err )
853834 return
854835 }
855836 }
856837
857- setCompareContext (ctx , baseCommit , commit , ctx .Repo .Owner .Name , ctx .Repo .Repository .Name )
838+ setCompareContext (ctx , beforeCommit , afterCommit , ctx .Repo .Owner .Name , ctx .Repo .Repository .Name )
858839
859840 assigneeUsers , err := repo_model .GetRepoAssignees (ctx , ctx .Repo .Repository )
860841 if err != nil {
@@ -901,7 +882,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
901882 ctx .Data ["CanBlockUser" ] = func (blocker , blockee * user_model.User ) bool {
902883 return user_service .CanBlockUser (ctx , ctx .Doer , blocker , blockee )
903884 }
904- if ! willShowSpecifiedCommit && ! willShowSpecifiedCommitRange && pull .Flow == issues_model .PullRequestFlowGithub {
885+ if isShowAllCommits && pull .Flow == issues_model .PullRequestFlowGithub {
905886 if err := pull .LoadHeadRepo (ctx ); err != nil {
906887 ctx .ServerError ("LoadHeadRepo" , err )
907888 return
@@ -930,19 +911,17 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
930911}
931912
932913func ViewPullFilesForSingleCommit (ctx * context.Context ) {
933- viewPullFiles (ctx , "" , ctx .PathParam ("sha" ), true , true )
914+ // it doesn't support showing files from mergebase to the special commit
915+ // otherwise it will be ambiguous
916+ viewPullFiles (ctx , "" , ctx .PathParam ("sha" ))
934917}
935918
936919func ViewPullFilesForRange (ctx * context.Context ) {
937- viewPullFiles (ctx , ctx .PathParam ("shaFrom" ), ctx .PathParam ("shaTo" ), true , false )
938- }
939-
940- func ViewPullFilesStartingFromCommit (ctx * context.Context ) {
941- viewPullFiles (ctx , "" , ctx .PathParam ("sha" ), true , false )
920+ viewPullFiles (ctx , ctx .PathParam ("shaFrom" ), ctx .PathParam ("shaTo" ))
942921}
943922
944923func ViewPullFilesForAllCommitsOfPr (ctx * context.Context ) {
945- viewPullFiles (ctx , "" , "" , false , false )
924+ viewPullFiles (ctx , "" , "" )
946925}
947926
948927// UpdatePullRequest merge PR's baseBranch into headBranch
0 commit comments