@@ -776,6 +776,8 @@ const (
776776 RepoRefBlob
777777)
778778
779+ const headRefName = "HEAD"
780+
779781// RepoRef handles repository reference names when the ref name is not
780782// explicitly given
781783func RepoRef () func (* Context ) context.CancelFunc {
@@ -836,6 +838,14 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
836838 case RepoRefBranch :
837839 ref := getRefNameFromPath (ctx , repo , path , repo .GitRepo .IsBranchExist )
838840 if len (ref ) == 0 {
841+
842+ // check if ref is HEAD
843+ parts := strings .Split (path , "/" )
844+ if parts [0 ] == headRefName {
845+ repo .TreePath = strings .Join (parts [1 :], "/" )
846+ return repo .Repository .DefaultBranch
847+ }
848+
839849 // maybe it's a renamed branch
840850 return getRefNameFromPath (ctx , repo , path , func (s string ) bool {
841851 b , exist , err := git_model .FindRenamedBranch (ctx , repo .Repository .ID , s )
@@ -864,6 +874,16 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
864874 repo .TreePath = strings .Join (parts [1 :], "/" )
865875 return parts [0 ]
866876 }
877+
878+ if len (parts ) > 0 && parts [0 ] == headRefName {
879+ // HEAD ref points to last default branch commit
880+ commit , err := repo .GitRepo .GetBranchCommit (repo .Repository .DefaultBranch )
881+ if err != nil {
882+ return ""
883+ }
884+ repo .TreePath = strings .Join (parts [1 :], "/" )
885+ return commit .ID .String ()
886+ }
867887 case RepoRefBlob :
868888 _ , err := repo .GitRepo .GetBlob (path )
869889 if err != nil {
0 commit comments