@@ -776,6 +776,8 @@ const (
776
776
RepoRefBlob
777
777
)
778
778
779
+ const headRefName = "HEAD"
780
+
779
781
// RepoRef handles repository reference names when the ref name is not
780
782
// explicitly given
781
783
func RepoRef () func (* Context ) context.CancelFunc {
@@ -836,6 +838,14 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
836
838
case RepoRefBranch :
837
839
ref := getRefNameFromPath (ctx , repo , path , repo .GitRepo .IsBranchExist )
838
840
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
+
839
849
// maybe it's a renamed branch
840
850
return getRefNameFromPath (ctx , repo , path , func (s string ) bool {
841
851
b , exist , err := git_model .FindRenamedBranch (ctx , repo .Repository .ID , s )
@@ -864,6 +874,16 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
864
874
repo .TreePath = strings .Join (parts [1 :], "/" )
865
875
return parts [0 ]
866
876
}
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
+ }
867
887
case RepoRefBlob :
868
888
_ , err := repo .GitRepo .GetBlob (path )
869
889
if err != nil {
0 commit comments