Skip to content

Commit 348b707

Browse files
authored
Fix incorrect ref "blob" (#33240)
1. "blob" is not a "ref", it shouldn't (and not unable to) be handled by `RepoRefByType` 2. the `/blob/{sha}` handle should use the path param "sha" directly
1 parent 2ea929a commit 348b707

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

routers/web/repo/render.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import (
2121

2222
// RenderFile renders a file by repos path
2323
func RenderFile(ctx *context.Context) {
24-
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
24+
var blob *git.Blob
25+
var err error
26+
if ctx.Repo.TreePath != "" {
27+
blob, err = ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
28+
} else {
29+
blob, err = ctx.Repo.GitRepo.GetBlob(ctx.PathParam("sha"))
30+
}
2531
if err != nil {
2632
if git.IsErrNotExist(err) {
2733
ctx.NotFound("GetBlobByPath", err)

routers/web/web.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ func registerRoutes(m *web.Router) {
15241524
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownloadOrLFS)
15251525
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownloadOrLFS)
15261526
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownloadOrLFS)
1527-
m.Get("/blob/{sha}", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByIDOrLFS)
1527+
m.Get("/blob/{sha}", repo.DownloadByIDOrLFS)
15281528
// "/*" route is deprecated, and kept for backward compatibility
15291529
m.Get("/*", context.RepoRefByType(context.RepoRefUnknown), repo.SingleDownloadOrLFS)
15301530
}, repo.MustBeNotEmpty)
@@ -1533,7 +1533,7 @@ func registerRoutes(m *web.Router) {
15331533
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownload)
15341534
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload)
15351535
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload)
1536-
m.Get("/blob/{sha}", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByID)
1536+
m.Get("/blob/{sha}", repo.DownloadByID)
15371537
// "/*" route is deprecated, and kept for backward compatibility
15381538
m.Get("/*", context.RepoRefByType(context.RepoRefUnknown), repo.SingleDownload)
15391539
}, repo.MustBeNotEmpty)
@@ -1542,7 +1542,7 @@ func registerRoutes(m *web.Router) {
15421542
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.RenderFile)
15431543
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.RenderFile)
15441544
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.RenderFile)
1545-
m.Get("/blob/{sha}", context.RepoRefByType(context.RepoRefBlob), repo.RenderFile)
1545+
m.Get("/blob/{sha}", repo.RenderFile)
15461546
}, repo.MustBeNotEmpty)
15471547

15481548
m.Group("/commits", func() {

services/context/repo.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@ const (
686686
RepoRefBranch
687687
RepoRefTag
688688
RepoRefCommit
689-
RepoRefBlob
690689
)
691690

692691
const headRefName = "HEAD"
@@ -725,9 +724,6 @@ func getRefNameLegacy(ctx *Base, repo *Repository, reqPath, extraRef string) (st
725724
repo.TreePath = strings.Join(reqRefPathParts[1:], "/")
726725
return reqRefPathParts[0], RepoRefCommit
727726
}
728-
if refName := getRefName(ctx, repo, reqPath, RepoRefBlob); refName != "" {
729-
return refName, RepoRefBlob
730-
}
731727
// FIXME: the old code falls back to default branch if "ref" doesn't exist, there could be an edge case:
732728
// "README?ref=no-such" would read the README file from the default branch, but the user might expect a 404
733729
repo.TreePath = reqPath
@@ -785,12 +781,6 @@ func getRefName(ctx *Base, repo *Repository, path string, pathType RepoRefType)
785781
repo.TreePath = strings.Join(parts[1:], "/")
786782
return commit.ID.String()
787783
}
788-
case RepoRefBlob:
789-
_, err := repo.GitRepo.GetBlob(path)
790-
if err != nil {
791-
return ""
792-
}
793-
return path
794784
default:
795785
panic(fmt.Sprintf("Unrecognized path type: %v", pathType))
796786
}

0 commit comments

Comments
 (0)