@@ -773,6 +773,8 @@ const (
773773 RepoRefBlob
774774)
775775
776+ const headRefName = "HEAD"
777+
776778// RepoRef handles repository reference names when the ref name is not
777779// explicitly given
778780func RepoRef () func (* Context ) context.CancelFunc {
@@ -833,6 +835,14 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
833835 case RepoRefBranch :
834836 ref := getRefNameFromPath (ctx , repo , path , repo .GitRepo .IsBranchExist )
835837 if len (ref ) == 0 {
838+
839+ // check if ref is HEAD
840+ parts := strings .Split (path , "/" )
841+ if parts [0 ] == headRefName {
842+ repo .TreePath = strings .Join (parts [1 :], "/" )
843+ return repo .Repository .DefaultBranch
844+ }
845+
836846 // maybe it's a renamed branch
837847 return getRefNameFromPath (ctx , repo , path , func (s string ) bool {
838848 b , exist , err := git_model .FindRenamedBranch (ctx , repo .Repository .ID , s )
@@ -861,6 +871,16 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
861871 repo .TreePath = strings .Join (parts [1 :], "/" )
862872 return parts [0 ]
863873 }
874+
875+ if len (parts ) > 0 && parts [0 ] == headRefName {
876+ // HEAD ref points to last default branch commit
877+ commit , err := repo .GitRepo .GetBranchCommit (repo .Repository .DefaultBranch )
878+ if err != nil {
879+ return ""
880+ }
881+ repo .TreePath = strings .Join (parts [1 :], "/" )
882+ return commit .ID .String ()
883+ }
864884 case RepoRefBlob :
865885 _ , err := repo .GitRepo .GetBlob (path )
866886 if err != nil {
0 commit comments