···776776 RepoRefBlob
777777)
778778779779+const headRefName = "HEAD"
780780+779781// RepoRef handles repository reference names when the ref name is not
780782// explicitly given
781783func RepoRef() func(*Context) context.CancelFunc {
···836838 case RepoRefBranch:
837839 ref := getRefNameFromPath(ctx, repo, path, repo.GitRepo.IsBranchExist)
838840 if len(ref) == 0 {
841841+842842+ // check if ref is HEAD
843843+ parts := strings.Split(path, "/")
844844+ if parts[0] == headRefName {
845845+ repo.TreePath = strings.Join(parts[1:], "/")
846846+ return repo.Repository.DefaultBranch
847847+ }
848848+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)
···863873 if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= git.SHAFullLength {
864874 repo.TreePath = strings.Join(parts[1:], "/")
865875 return parts[0]
876876+ }
877877+878878+ if len(parts) > 0 && parts[0] == headRefName {
879879+ // HEAD ref points to last default branch commit
880880+ commit, err := repo.GitRepo.GetBranchCommit(repo.Repository.DefaultBranch)
881881+ if err != nil {
882882+ return ""
883883+ }
884884+ repo.TreePath = strings.Join(parts[1:], "/")
885885+ return commit.ID.String()
866886 }
867887 case RepoRefBlob:
868888 _, err := repo.GitRepo.GetBlob(path)
+4
tests/integration/repo_test.go
···302302 check("plain", "/user2/readme-test/src/branch/plain/", "README", "plain-text", "Birken my stocks gee howdy")
303303 check("i18n", "/user2/readme-test/src/branch/i18n/", "README.zh.md", "markdown", "蛋糕是一个谎言")
304304305305+ // using HEAD ref
306306+ check("branch-HEAD", "/user2/readme-test/src/branch/HEAD/", "README.md", "markdown", "The cake is a lie.")
307307+ check("commit-HEAD", "/user2/readme-test/src/commit/HEAD/", "README.md", "markdown", "The cake is a lie.")
308308+305309 // viewing different subdirectories
306310 check("subdir", "/user2/readme-test/src/branch/subdir/libcake", "README.md", "markdown", "Four pints of sugar.")
307311 check("docs-direct", "/user2/readme-test/src/branch/special-subdir-docs/docs/", "README.md", "markdown", "This is in docs/")