@@ -31,6 +31,7 @@ import (
3131const (
3232 tplCompare base.TplName = "repo/diff/compare"
3333 tplBlobExcerpt base.TplName = "repo/diff/blob_excerpt"
34+ tplDiffBox base.TplName = "repo/diff/box"
3435)
3536
3637// setCompareContext sets context data.
@@ -161,6 +162,8 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
161162 baseRepo := ctx .Repo .Repository
162163 ci := & CompareInfo {}
163164
165+ fileOnly := ctx .FormBool ("file-only" )
166+
164167 // Get compared branches information
165168 // A full compare url is of the form:
166169 //
@@ -411,15 +414,19 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
411414 if rootRepo != nil &&
412415 rootRepo .ID != ci .HeadRepo .ID &&
413416 rootRepo .ID != baseRepo .ID {
414- perm , branches , tags , err := getBranchesAndTagsForRepo (ctx .User , rootRepo )
415- if err != nil {
416- ctx .ServerError ("GetBranchesForRepo" , err )
417- return nil
418- }
419- if perm {
417+ canRead := rootRepo .CheckUnitUser (ctx .User , models .UnitTypeCode )
418+ if canRead {
420419 ctx .Data ["RootRepo" ] = rootRepo
421- ctx .Data ["RootRepoBranches" ] = branches
422- ctx .Data ["RootRepoTags" ] = tags
420+ if ! fileOnly {
421+ branches , tags , err := getBranchesAndTagsForRepo (ctx .User , rootRepo )
422+ if err != nil {
423+ ctx .ServerError ("GetBranchesForRepo" , err )
424+ return nil
425+ }
426+
427+ ctx .Data ["RootRepoBranches" ] = branches
428+ ctx .Data ["RootRepoTags" ] = tags
429+ }
423430 }
424431 }
425432
@@ -432,15 +439,18 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
432439 ownForkRepo .ID != ci .HeadRepo .ID &&
433440 ownForkRepo .ID != baseRepo .ID &&
434441 (rootRepo == nil || ownForkRepo .ID != rootRepo .ID ) {
435- perm , branches , tags , err := getBranchesAndTagsForRepo (ctx .User , ownForkRepo )
436- if err != nil {
437- ctx .ServerError ("GetBranchesForRepo" , err )
438- return nil
439- }
440- if perm {
442+ canRead := ownForkRepo .CheckUnitUser (ctx .User , models .UnitTypeCode )
443+ if canRead {
441444 ctx .Data ["OwnForkRepo" ] = ownForkRepo
442- ctx .Data ["OwnForkRepoBranches" ] = branches
443- ctx .Data ["OwnForkRepoTags" ] = tags
445+ if ! fileOnly {
446+ branches , tags , err := getBranchesAndTagsForRepo (ctx .User , ownForkRepo )
447+ if err != nil {
448+ ctx .ServerError ("GetBranchesForRepo" , err )
449+ return nil
450+ }
451+ ctx .Data ["OwnForkRepoBranches" ] = branches
452+ ctx .Data ["OwnForkRepoTags" ] = tags
453+ }
444454 }
445455 }
446456
@@ -492,7 +502,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
492502 headBranchRef = git .TagPrefix + ci .HeadBranch
493503 }
494504
495- ci .CompareInfo , err = ci .HeadGitRepo .GetCompareInfo (baseRepo .RepoPath (), baseBranchRef , headBranchRef , ci .DirectComparison )
505+ ci .CompareInfo , err = ci .HeadGitRepo .GetCompareInfo (baseRepo .RepoPath (), baseBranchRef , headBranchRef , ci .DirectComparison , fileOnly )
496506 if err != nil {
497507 ctx .ServerError ("GetCompareInfo" , err )
498508 return nil
@@ -545,7 +555,7 @@ func PrepareCompareDiff(
545555 }
546556
547557 diff , err := gitdiff .GetDiffRangeWithWhitespaceBehavior (ci .HeadGitRepo ,
548- beforeCommitID , headCommitID , setting .Git .MaxGitDiffLines ,
558+ beforeCommitID , headCommitID , ctx . FormString ( "skip-to" ), setting .Git .MaxGitDiffLines ,
549559 setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , whitespaceBehavior , ci .DirectComparison )
550560 if err != nil {
551561 ctx .ServerError ("GetDiffRangeWithWhitespaceBehavior" , err )
@@ -606,29 +616,22 @@ func PrepareCompareDiff(
606616 return false
607617}
608618
609- func getBranchesAndTagsForRepo (user * models.User , repo * models.Repository ) (bool , []string , []string , error ) {
610- perm , err := models .GetUserRepoPermission (repo , user )
611- if err != nil {
612- return false , nil , nil , err
613- }
614- if ! perm .CanRead (models .UnitTypeCode ) {
615- return false , nil , nil , nil
616- }
619+ func getBranchesAndTagsForRepo (user * models.User , repo * models.Repository ) (branches , tags []string , err error ) {
617620 gitRepo , err := git .OpenRepository (repo .RepoPath ())
618621 if err != nil {
619- return false , nil , nil , err
622+ return nil , nil , err
620623 }
621624 defer gitRepo .Close ()
622625
623- branches , _ , err : = gitRepo .GetBranches (0 , 0 )
626+ branches , _ , err = gitRepo .GetBranches (0 , 0 )
624627 if err != nil {
625- return false , nil , nil , err
628+ return nil , nil , err
626629 }
627- tags , err : = gitRepo .GetTags (0 , 0 )
630+ tags , err = gitRepo .GetTags (0 , 0 )
628631 if err != nil {
629- return false , nil , nil , err
632+ return nil , nil , err
630633 }
631- return true , branches , tags , nil
634+ return branches , tags , nil
632635}
633636
634637// CompareDiff show different from one commit to another commit
@@ -665,6 +668,12 @@ func CompareDiff(ctx *context.Context) {
665668 }
666669 ctx .Data ["Tags" ] = baseTags
667670
671+ fileOnly := ctx .FormBool ("file-only" )
672+ if fileOnly {
673+ ctx .HTML (http .StatusOK , tplDiffBox )
674+ return
675+ }
676+
668677 headBranches , _ , err := ci .HeadGitRepo .GetBranches (0 , 0 )
669678 if err != nil {
670679 ctx .ServerError ("GetBranches" , err )
0 commit comments