]> source.dussan.org Git - gitea.git/commitdiff
Add raw blob endpoint to get objects by SHA ID (#5334)
authorPeter Hoffmann <Hoffmann.P@gmx.net>
Sun, 18 Nov 2018 18:45:40 +0000 (19:45 +0100)
committertechknowlogick <hello@techknowlogick.com>
Sun, 18 Nov 2018 18:45:40 +0000 (13:45 -0500)
* Add raw blob endpoint

This should make it possible to download raw blobs directly from
/:repo/:username/raw/blob/:sha1 URLs.

* fix: Make it work

* As an SHA-ID is no path getRefNameFromPath can't be used to verify
file specifying parameter
* added relevant change in go-gitea/git #132

Signed-off-by: Berengar W. Lehr <Berengar.Lehr@kompetenztest.de>
* Update Gopkg.lock

Can't update all vendors due to errors

Signed-off-by: Berengar W. Lehr <Berengar.Lehr@kompetenztest.de>
* style: Add Gitea copyright header

* feat: Added integration test for /repo/u/r/raw/blob

* fix: correct year in copyright header

Gopkg.lock
integrations/download_test.go [new file with mode: 0644]
modules/context/repo.go
routers/repo/download.go
routers/routes/routes.go
vendor/code.gitea.io/git/repo_blob.go [new file with mode: 0644]

index cbc089fead46e9cb49bba92f33690d3139eb5db8..a4efca060f78e67ecd5b34b1ca15e31fd4c4a814 100644 (file)
@@ -3,11 +3,11 @@
 
 [[projects]]
   branch = "master"
-  digest = "1:835585f8450b4ec12252d032b0f13e6571ecf846e49076f69067f2503a7c1e07"
+  digest = "1:296fd9dfbae66f6feeb09c7163ec39c262de425289154430a55d0a248c520486"
   name = "code.gitea.io/git"
   packages = ["."]
   pruneopts = "NUT"
-  revision = "6ef79e80b3b06ca13a1f3a7b940903ebc73b44cb"
+  revision = "d945eda535aa7d6b3c1f486279df2a3f7d05f78b"
 
 [[projects]]
   branch = "master"
diff --git a/integrations/download_test.go b/integrations/download_test.go
new file mode 100644 (file)
index 0000000..0d5fef6
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2018 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package integrations
+
+import (
+       "net/http"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestDownloadByID(t *testing.T) {
+       prepareTestEnv(t)
+
+       session := loginUser(t, "user2")
+
+       // Request raw blob
+       req := NewRequest(t, "GET", "/user2/repo1/raw/blob/4b4851ad51df6a7d9f25c979345979eaeb5b349f")
+       resp := session.MakeRequest(t, req, http.StatusOK)
+
+       assert.Equal(t, "# repo1\n\nDescription for repo1", resp.Body.String())
+}
index 7221ad7c8a1d9db593ac120d263f3a3b1a07abae..be08bc4c77f9f2d38b5b88123c46727430d9d536 100644 (file)
@@ -484,6 +484,8 @@ const (
        RepoRefTag
        // RepoRefCommit commit
        RepoRefCommit
+       // RepoRefBlob blob
+       RepoRefBlob
 )
 
 // RepoRef handles repository reference names when the ref name is not
@@ -519,6 +521,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:
@@ -531,6 +536,12 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
                        ctx.Repo.TreePath = strings.Join(parts[1:], "/")
                        return parts[0]
                }
+       case RepoRefBlob:
+               _, err := ctx.Repo.GitRepo.GetBlob(path)
+               if err != nil {
+                       return ""
+               }
+               return path
        default:
                log.Error(4, "Unrecognized path type: %v", path)
        }
index 820a98c0dc7fd36d4d7bb779ef7b05f6cfa92acd..a863236d6ea54a07583c6da3a7403f6434ff1ec7 100644 (file)
@@ -1,4 +1,5 @@
 // Copyright 2014 The Gogs Authors. All rights reserved.
+// Copyright 2018 The Gitea Authors. All rights reserved.
 // Use of this source code is governed by a MIT-style
 // license that can be found in the LICENSE file.
 
@@ -69,3 +70,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)
+       }
+}
index af216866bbfc48cb1bee55fb28776162aabb2163..06292557b3f317fe56b15dabb9dd9489a5a75d2c 100644 (file)
@@ -693,6 +693,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))
diff --git a/vendor/code.gitea.io/git/repo_blob.go b/vendor/code.gitea.io/git/repo_blob.go
new file mode 100644 (file)
index 0000000..a9445a1
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright 2018 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package git
+
+func (repo *Repository) getBlob(id SHA1) (*Blob, error) {
+       if _, err := NewCommand("cat-file", "-p", id.String()).RunInDir(repo.Path); err != nil {
+               return nil, ErrNotExist{id.String(), ""}
+       }
+
+       return &Blob{
+               repo: repo,
+               TreeEntry: &TreeEntry{
+                       ID: id,
+                       ptree: &Tree{
+                               repo: repo,
+                       },
+               },
+       }, nil
+}
+
+// GetBlob finds the blob object in the repository.
+func (repo *Repository) GetBlob(idStr string) (*Blob, error) {
+       id, err := NewIDFromString(idStr)
+       if err != nil {
+               return nil, err
+       }
+       return repo.getBlob(id)
+}