Skip to content

Commit d0ea927

Browse files
committed
Add canonical link headers for short commit ID URLs
1 parent 0358e92 commit d0ea927

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

modules/context/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"code.gitea.io/gitea/modules/markup/markdown"
2020
"code.gitea.io/gitea/modules/setting"
2121
api "code.gitea.io/gitea/modules/structs"
22+
"code.gitea.io/gitea/modules/util"
2223

2324
"gitea.com/macaron/macaron"
2425
"github.com/editorconfig/editorconfig-core-go/v2"
@@ -790,6 +791,11 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
790791
ctx.NotFound("GetCommit", err)
791792
return
792793
}
794+
// If short commit ID add canonical link header
795+
if len(refName) < 40 {
796+
ctx.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
797+
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), refName, ctx.Repo.Commit.ID.String(), 1))))
798+
}
793799
} else {
794800
ctx.NotFound("RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
795801
return

0 commit comments

Comments
 (0)