@@ -773,6 +773,8 @@ const (
773
773
RepoRefBlob
774
774
)
775
775
776
+ const headRefName = "HEAD"
777
+
776
778
// RepoRef handles repository reference names when the ref name is not
777
779
// explicitly given
778
780
func RepoRef () func (* Context ) context.CancelFunc {
@@ -833,6 +835,14 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
833
835
case RepoRefBranch :
834
836
ref := getRefNameFromPath (ctx , repo , path , repo .GitRepo .IsBranchExist )
835
837
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
+
836
846
// maybe it's a renamed branch
837
847
return getRefNameFromPath (ctx , repo , path , func (s string ) bool {
838
848
b , exist , err := git_model .FindRenamedBranch (ctx , repo .Repository .ID , s )
@@ -861,6 +871,16 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
861
871
repo .TreePath = strings .Join (parts [1 :], "/" )
862
872
return parts [0 ]
863
873
}
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
+ }
864
884
case RepoRefBlob :
865
885
_ , err := repo .GitRepo .GetBlob (path )
866
886
if err != nil {
0 commit comments