@@ -19,6 +19,7 @@ import (
19
19
"code.gitea.io/gitea/modules/markup/markdown"
20
20
"code.gitea.io/gitea/modules/setting"
21
21
api "code.gitea.io/gitea/modules/structs"
22
+ "code.gitea.io/gitea/modules/util"
22
23
23
24
"gitea.com/macaron/macaron"
24
25
"github.com/editorconfig/editorconfig-core-go/v2"
@@ -672,8 +673,11 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
672
673
if refName := getRefName (ctx , RepoRefTag ); len (refName ) > 0 {
673
674
return refName
674
675
}
675
- if refName := getRefName (ctx , RepoRefCommit ); len (refName ) > 0 {
676
- return refName
676
+ // For legacy and API support only full commit sha
677
+ parts := strings .Split (path , "/" )
678
+ if len (parts ) > 0 && len (parts [0 ]) == 40 {
679
+ ctx .Repo .TreePath = strings .Join (parts [1 :], "/" )
680
+ return parts [0 ]
677
681
}
678
682
if refName := getRefName (ctx , RepoRefBlob ); len (refName ) > 0 {
679
683
return refName
@@ -686,7 +690,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
686
690
return getRefNameFromPath (ctx , path , ctx .Repo .GitRepo .IsTagExist )
687
691
case RepoRefCommit :
688
692
parts := strings .Split (path , "/" )
689
- if len (parts ) > 0 && len (parts [0 ]) = = 40 {
693
+ if len (parts ) > 0 && len (parts [0 ]) >= 7 && len ( parts [ 0 ]) < = 40 {
690
694
ctx .Repo .TreePath = strings .Join (parts [1 :], "/" )
691
695
return parts [0 ]
692
696
}
@@ -778,7 +782,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
778
782
return
779
783
}
780
784
ctx .Repo .CommitID = ctx .Repo .Commit .ID .String ()
781
- } else if len (refName ) = = 40 {
785
+ } else if len (refName ) >= 7 && len ( refName ) < = 40 {
782
786
ctx .Repo .IsViewCommit = true
783
787
ctx .Repo .CommitID = refName
784
788
@@ -787,6 +791,11 @@ func RepoRefByType(refType RepoRefType) macaron.Handler {
787
791
ctx .NotFound ("GetCommit" , err )
788
792
return
789
793
}
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
+ }
790
799
} else {
791
800
ctx .NotFound ("RepoRef invalid repo" , fmt .Errorf ("branch or tag not exist: %s" , refName ))
792
801
return
0 commit comments