Skip to content

Add raw blob endpoint #4110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ const (
RepoRefTag
// RepoRefCommit commit
RepoRefCommit
// RepoRefObject object
RepoRefBlob
)

// RepoRef handles repository reference names when the ref name is not
Expand Down Expand Up @@ -514,6 +516,9 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
return refName
}
if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 {
return refName
}
ctx.Repo.TreePath = path
return ctx.Repo.Repository.DefaultBranch
case RepoRefBranch:
Expand All @@ -526,6 +531,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
case RepoRefBlob:
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBlobExist)
default:
log.Error(4, "Unrecognized path type: %v", path)
}
Expand Down
16 changes: 16 additions & 0 deletions routers/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ func SingleDownload(ctx *context.Context) {
ctx.ServerError("ServeBlob", err)
}
}

// DownloadById download a file by sha1 ID
func DownloadByID(ctx *context.Context) {
blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params["sha"])
if err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetBlob", nil)
} else {
ctx.ServerError("GetBlob", err)
}
return
}
if err = ServeBlob(ctx, blob); err != nil {
ctx.ServerError("ServeBlob", err)
}
}
1 change: 1 addition & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.SingleDownload)
m.Get("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.SingleDownload)
m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), repo.SingleDownload)
m.Get("/blob/:sha", context.RepoRefByType(context.RepoRefBlob), repo.DownloadByID)
// "/*" route is deprecated, and kept for backward compatibility
m.Get("/*", context.RepoRefByType(context.RepoRefLegacy), repo.SingleDownload)
}, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))
Expand Down