loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add support for HEAD ref in /src/branch and /src/commit routes (#27384)

Add support for HEAD in paths:
```
/src/branch/HEAD/README.md
/src/commit/HEAD/README.md
```

Closes #26920

authored by

Kirill Sorokin and committed by
GitHub
2b06c106 bc217237

+24
+20
modules/context/repo.go
··· 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 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) ··· 863 873 if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= git.SHAFullLength { 864 874 repo.TreePath = strings.Join(parts[1:], "/") 865 875 return parts[0] 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() 866 886 } 867 887 case RepoRefBlob: 868 888 _, err := repo.GitRepo.GetBlob(path)
+4
tests/integration/repo_test.go
··· 302 302 check("plain", "/user2/readme-test/src/branch/plain/", "README", "plain-text", "Birken my stocks gee howdy") 303 303 check("i18n", "/user2/readme-test/src/branch/i18n/", "README.zh.md", "markdown", "蛋糕是一个谎言") 304 304 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 + 305 309 // viewing different subdirectories 306 310 check("subdir", "/user2/readme-test/src/branch/subdir/libcake", "README.md", "markdown", "Four pints of sugar.") 307 311 check("docs-direct", "/user2/readme-test/src/branch/special-subdir-docs/docs/", "README.md", "markdown", "This is in docs/")