Skip to content

Commit 556e2d5

Browse files
lunnyzeripath
andauthored
Fix generating compare link (#21519) (#21530)
Fix #6318, backport #21519 Co-authored-by: zeripath <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 675c14a commit 556e2d5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

modules/templates/helper.go

+13
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,19 @@ func NewFuncMap() []template.FuncMap {
458458
return items
459459
},
460460
"HasPrefix": strings.HasPrefix,
461+
"CompareLink": func(baseRepo, repo *repo_model.Repository, branchName string) string {
462+
var curBranch string
463+
if repo.ID != baseRepo.ID {
464+
curBranch += fmt.Sprintf("%s/%s:", url.PathEscape(repo.OwnerName), url.PathEscape(repo.Name))
465+
}
466+
curBranch += util.PathEscapeSegments(branchName)
467+
468+
return fmt.Sprintf("%s/compare/%s...%s",
469+
baseRepo.Link(),
470+
util.PathEscapeSegments(baseRepo.DefaultBranch),
471+
curBranch,
472+
)
473+
},
461474
}}
462475
}
463476

templates/repo/home.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<!-- If home page, show new PR. If not, show breadcrumb -->
6969
{{if eq $n 0}}
7070
{{if and .CanCompareOrPull .IsViewBranch (not .Repository.IsArchived)}}
71-
<a href="{{.BaseRepo.Link}}/compare/{{PathEscapeSegments .BaseRepo.DefaultBranch}}...{{if ne .Repository.Owner.Name .BaseRepo.Owner.Name}}{{PathEscape .Repository.Owner.Name}}{{if .BaseRepo.IsFork}}/{{PathEscape .Repository.Name}}{{end}}:{{end}}{{PathEscapeSegments .BranchName}}">
71+
<a href="{{CompareLink .BaseRepo .Repository .BranchName}}">
7272
<button id="new-pull-request" class="ui compact basic button tooltip" data-content="{{if .PullRequestCtx.Allowed}}{{.i18n.Tr "repo.pulls.compare_changes"}}{{else}}{{.i18n.Tr "action.compare_branch"}}{{end}}"><span class="text">{{svg "octicon-git-pull-request"}}</span></button>
7373
</a>
7474
{{end}}

0 commit comments

Comments
 (0)