From 556e2d5506a4831f14e2b1c4508d9ec778fba628 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 21 Oct 2022 20:59:27 +0800 Subject: [PATCH] Fix generating compare link (#21519) (#21530) Fix #6318, backport #21519 Co-authored-by: zeripath Co-authored-by: zeripath --- modules/templates/helper.go | 13 +++++++++++++ templates/repo/home.tmpl | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 2ea7bdacc695..61f1f6adad0c 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -458,6 +458,19 @@ func NewFuncMap() []template.FuncMap { return items }, "HasPrefix": strings.HasPrefix, + "CompareLink": func(baseRepo, repo *repo_model.Repository, branchName string) string { + var curBranch string + if repo.ID != baseRepo.ID { + curBranch += fmt.Sprintf("%s/%s:", url.PathEscape(repo.OwnerName), url.PathEscape(repo.Name)) + } + curBranch += util.PathEscapeSegments(branchName) + + return fmt.Sprintf("%s/compare/%s...%s", + baseRepo.Link(), + util.PathEscapeSegments(baseRepo.DefaultBranch), + curBranch, + ) + }, }} } diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 18c02aa9ecac..a24aa52247e5 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -68,7 +68,7 @@ {{if eq $n 0}} {{if and .CanCompareOrPull .IsViewBranch (not .Repository.IsArchived)}} - + {{end}}