@@ -643,8 +643,17 @@ func ViewPullCommits(ctx *context.Context) {
643
643
ctx .HTML (http .StatusOK , tplPullCommits )
644
644
}
645
645
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
+
646
655
// 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 ) {
648
657
ctx .Data ["PageIsPullList" ] = true
649
658
ctx .Data ["PageIsPullFiles" ] = true
650
659
@@ -654,11 +663,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
654
663
}
655
664
pull := issue .PullRequest
656
665
657
- var (
658
- startCommitID string
659
- endCommitID string
660
- gitRepo = ctx .Repo .GitRepo
661
- )
666
+ gitRepo := ctx .Repo .GitRepo
662
667
663
668
prInfo := preparePullViewPullInfo (ctx , issue )
664
669
if ctx .Written () {
@@ -668,88 +673,75 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
668
673
return
669
674
}
670
675
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
-
701
676
headCommitID , err := gitRepo .GetRefCommitID (pull .GetGitHeadRefName ())
702
677
if err != nil {
703
678
ctx .ServerError ("GetRefCommitID" , err )
704
679
return
705
680
}
706
681
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
708
686
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
+ }
717
706
} 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
+ }
719
712
}
720
- ctx .Data ["IsShowingAllCommits" ] = false
721
713
} 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 ()
725
720
}
726
721
727
722
ctx .Data ["Username" ] = ctx .Repo .Owner .Name
728
723
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
733
727
734
728
maxLines , maxFiles := setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffFiles
735
729
files := ctx .FormStrings ("files" )
730
+ fileOnly := ctx .FormBool ("file-only" )
736
731
if fileOnly && (len (files ) == 2 || len (files ) == 1 ) {
737
732
maxLines , maxFiles = - 1 , - 1
738
733
}
739
734
740
735
diffOptions := & gitdiff.DiffOptions {
741
- AfterCommitID : endCommitID ,
736
+ BeforeCommitID : beforeCommitID ,
737
+ AfterCommitID : afterCommitID ,
742
738
SkipTo : ctx .FormString ("skip-to" ),
743
739
MaxLines : maxLines ,
744
740
MaxLineCharacters : setting .Git .MaxGitDiffLineCharacters ,
745
741
MaxFiles : maxFiles ,
746
742
WhitespaceBehavior : gitdiff .GetWhitespaceFlag (ctx .Data ["WhitespaceBehavior" ].(string )),
747
743
}
748
744
749
- if ! willShowSpecifiedCommit {
750
- diffOptions .BeforeCommitID = startCommitID
751
- }
752
-
753
745
diff , err := gitdiff .GetDiffForRender (ctx , ctx .Repo .RepoLink , gitRepo , diffOptions , files ... )
754
746
if err != nil {
755
747
ctx .ServerError ("GetDiff" , err )
@@ -761,15 +753,15 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
761
753
// as the viewed information is designed to be loaded only on latest PR
762
754
// diff and if you're signed in.
763
755
var reviewState * pull_model.ReviewState
764
- if ctx .IsSigned && ! willShowSpecifiedCommit && ! willShowSpecifiedCommitRange {
756
+ if ctx .IsSigned && isShowAllCommits {
765
757
reviewState , err = gitdiff .SyncUserSpecificDiff (ctx , ctx .Doer .ID , pull , gitRepo , diff , diffOptions )
766
758
if err != nil {
767
759
ctx .ServerError ("SyncUserSpecificDiff" , err )
768
760
return
769
761
}
770
762
}
771
763
772
- diffShortStat , err := gitdiff .GetDiffShortStat (ctx .Repo .GitRepo , startCommitID , endCommitID )
764
+ diffShortStat , err := gitdiff .GetDiffShortStat (ctx .Repo .GitRepo , beforeCommitID , afterCommitID )
773
765
if err != nil {
774
766
ctx .ServerError ("GetDiffShortStat" , err )
775
767
return
@@ -816,7 +808,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
816
808
817
809
if ! fileOnly {
818
810
// 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 )
820
812
if err != nil {
821
813
ctx .ServerError ("GetDiffTree" , err )
822
814
return
@@ -836,25 +828,14 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
836
828
ctx .Data ["Diff" ] = diff
837
829
ctx .Data ["DiffNotAvailable" ] = diffShortStat .NumFiles == 0
838
830
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
-
850
831
if ctx .IsSigned && ctx .Doer != nil {
851
832
if ctx .Data ["CanMarkConversation" ], err = issues_model .CanMarkConversation (ctx , issue , ctx .Doer ); err != nil {
852
833
ctx .ServerError ("CanMarkConversation" , err )
853
834
return
854
835
}
855
836
}
856
837
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 )
858
839
859
840
assigneeUsers , err := repo_model .GetRepoAssignees (ctx , ctx .Repo .Repository )
860
841
if err != nil {
@@ -901,7 +882,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
901
882
ctx .Data ["CanBlockUser" ] = func (blocker , blockee * user_model.User ) bool {
902
883
return user_service .CanBlockUser (ctx , ctx .Doer , blocker , blockee )
903
884
}
904
- if ! willShowSpecifiedCommit && ! willShowSpecifiedCommitRange && pull .Flow == issues_model .PullRequestFlowGithub {
885
+ if isShowAllCommits && pull .Flow == issues_model .PullRequestFlowGithub {
905
886
if err := pull .LoadHeadRepo (ctx ); err != nil {
906
887
ctx .ServerError ("LoadHeadRepo" , err )
907
888
return
@@ -930,19 +911,17 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
930
911
}
931
912
932
913
func 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" ))
934
917
}
935
918
936
919
func 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" ))
942
921
}
943
922
944
923
func ViewPullFilesForAllCommitsOfPr (ctx * context.Context ) {
945
- viewPullFiles (ctx , "" , "" , false , false )
924
+ viewPullFiles (ctx , "" , "" )
946
925
}
947
926
948
927
// UpdatePullRequest merge PR's baseBranch into headBranch
0 commit comments