diff options
author | Giteabot <teabot@gitea.io> | 2023-10-03 16:13:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-03 08:13:49 +0000 |
commit | 8c6464e39b5069ebfca254a9b7867f9b7fa57bd9 (patch) | |
tree | 0f9b9d70d026f5e6fe0562a78d211c8cab443b49 /modules | |
parent | 4f02b4a7b9912c11971b335cb8b14fcd229bc5af (diff) | |
download | gitea-8c6464e39b5069ebfca254a9b7867f9b7fa57bd9.tar.gz gitea-8c6464e39b5069ebfca254a9b7867f9b7fa57bd9.zip |
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>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/repo.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index 44ae624568..861df4af59 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -773,6 +773,8 @@ const ( RepoRefBlob ) +const headRefName = "HEAD" + // RepoRef handles repository reference names when the ref name is not // explicitly given func RepoRef() func(*Context) context.CancelFunc { @@ -833,6 +835,14 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string { case RepoRefBranch: ref := getRefNameFromPath(ctx, repo, path, repo.GitRepo.IsBranchExist) if len(ref) == 0 { + + // check if ref is HEAD + parts := strings.Split(path, "/") + if parts[0] == headRefName { + repo.TreePath = strings.Join(parts[1:], "/") + return repo.Repository.DefaultBranch + } + // maybe it's a renamed branch return getRefNameFromPath(ctx, repo, path, func(s string) bool { b, exist, err := git_model.FindRenamedBranch(ctx, repo.Repository.ID, s) @@ -861,6 +871,16 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string { repo.TreePath = strings.Join(parts[1:], "/") return parts[0] } + + if len(parts) > 0 && parts[0] == headRefName { + // HEAD ref points to last default branch commit + commit, err := repo.GitRepo.GetBranchCommit(repo.Repository.DefaultBranch) + if err != nil { + return "" + } + repo.TreePath = strings.Join(parts[1:], "/") + return commit.ID.String() + } case RepoRefBlob: _, err := repo.GitRepo.GetBlob(path) if err != nil { |