Skip to content

Commit 8c6464e

Browse files
GiteaBotrbhz
andauthored
Add support for HEAD ref in /src/branch and /src/commit routes (#27384) (#27407)
Backport #27384 by @rbhz Add support for HEAD in paths: ``` /src/branch/HEAD/README.md /src/commit/HEAD/README.md ``` Closes #26920 Co-authored-by: Kirill Sorokin <48334247+rbhz@users.noreply.github.com>
1 parent 4f02b4a commit 8c6464e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

modules/context/repo.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
778780
func 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 {

tests/integration/repo_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ func TestViewRepoDirectoryReadme(t *testing.T) {
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", "蛋糕是一个谎言")
304304

305+
// using HEAD ref
306+
check("branch-HEAD", "/user2/readme-test/src/branch/HEAD/", "README.md", "markdown", "The cake is a lie.")
307+
check("commit-HEAD", "/user2/readme-test/src/commit/HEAD/", "README.md", "markdown", "The cake is a lie.")
308+
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/")

0 commit comments

Comments
 (0)