aboutsummaryrefslogtreecommitdiffstats
path: root/modules/repofiles
diff options
context:
space:
mode:
authorRichard Mahn <richmahn@users.noreply.github.com>2019-06-29 16:51:10 -0400
committertechknowlogick <techknowlogick@gitea.io>2019-06-29 16:51:10 -0400
commitcd96dee9822c8b744526ba862fd8b5ec0e2c30ff (patch)
treed7bbf2f2b7adf80b17f3ab3971ae49bae7b010c4 /modules/repofiles
parent738285a4aac5df2e60f4038aa79be3e9fe921bdb (diff)
downloadgitea-cd96dee9822c8b744526ba862fd8b5ec0e2c30ff.tar.gz
gitea-cd96dee9822c8b744526ba862fd8b5ec0e2c30ff.zip
Fixes #7292 - API File Contents bug (#7301)
Diffstat (limited to 'modules/repofiles')
-rw-r--r--modules/repofiles/content.go189
-rw-r--r--modules/repofiles/content_test.go154
-rw-r--r--modules/repofiles/file.go4
-rw-r--r--modules/repofiles/file_test.go35
4 files changed, 317 insertions, 65 deletions
diff --git a/modules/repofiles/content.go b/modules/repofiles/content.go
index 3098087dc6..9637658e78 100644
--- a/modules/repofiles/content.go
+++ b/modules/repofiles/content.go
@@ -5,26 +5,52 @@
package repofiles
import (
+ "fmt"
"net/url"
+ "path"
+ "strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
)
-// GetFileContents gets the meta data on a file's contents
-func GetFileContents(repo *models.Repository, treePath, ref string) (*api.FileContentResponse, error) {
+// ContentType repo content type
+type ContentType string
+
+// The string representations of different content types
+const (
+ // ContentTypeRegular regular content type (file)
+ ContentTypeRegular ContentType = "file"
+ // ContentTypeDir dir content type (dir)
+ ContentTypeDir ContentType = "dir"
+ // ContentLink link content type (symlink)
+ ContentTypeLink ContentType = "symlink"
+ // ContentTag submodule content type (submodule)
+ ContentTypeSubmodule ContentType = "submodule"
+)
+
+// String gets the string of ContentType
+func (ct *ContentType) String() string {
+ return string(*ct)
+}
+
+// GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree
+// directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag
+func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface{}, error) {
if ref == "" {
ref = repo.DefaultBranch
}
+ origRef := ref
// Check that the path given in opts.treePath is valid (not a git path)
- treePath = CleanUploadFileName(treePath)
- if treePath == "" {
+ cleanTreePath := CleanUploadFileName(treePath)
+ if cleanTreePath == "" && treePath != "" {
return nil, models.ErrFilenameInvalid{
Path: treePath,
}
}
+ treePath = cleanTreePath
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
@@ -42,32 +68,145 @@ func GetFileContents(repo *models.Repository, treePath, ref string) (*api.FileCo
return nil, err
}
- urlRef := ref
- if _, err := gitRepo.GetBranchCommit(ref); err == nil {
- urlRef = "branch/" + ref
+ if entry.Type() != "tree" {
+ return GetContents(repo, treePath, origRef, false)
+ }
+
+ // We are in a directory, so we return a list of FileContentResponse objects
+ var fileList []*api.ContentsResponse
+
+ gitTree, err := commit.SubTree(treePath)
+ if err != nil {
+ return nil, err
+ }
+ entries, err := gitTree.ListEntries()
+ if err != nil {
+ return nil, err
+ }
+ for _, e := range entries {
+ subTreePath := path.Join(treePath, e.Name())
+ fileContentResponse, err := GetContents(repo, subTreePath, origRef, true)
+ if err != nil {
+ return nil, err
+ }
+ fileList = append(fileList, fileContentResponse)
+ }
+ return fileList, nil
+}
+
+// GetContents gets the meta data on a file's contents. Ref can be a branch, commit or tag
+func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*api.ContentsResponse, error) {
+ if ref == "" {
+ ref = repo.DefaultBranch
}
+ origRef := ref
- selfURL, _ := url.Parse(repo.APIURL() + "/contents/" + treePath)
- gitURL, _ := url.Parse(repo.APIURL() + "/git/blobs/" + entry.ID.String())
- downloadURL, _ := url.Parse(repo.HTMLURL() + "/raw/" + urlRef + "/" + treePath)
- htmlURL, _ := url.Parse(repo.HTMLURL() + "/blob/" + ref + "/" + treePath)
+ // Check that the path given in opts.treePath is valid (not a git path)
+ cleanTreePath := CleanUploadFileName(treePath)
+ if cleanTreePath == "" && treePath != "" {
+ return nil, models.ErrFilenameInvalid{
+ Path: treePath,
+ }
+ }
+ treePath = cleanTreePath
+
+ gitRepo, err := git.OpenRepository(repo.RepoPath())
+ if err != nil {
+ return nil, err
+ }
- fileContent := &api.FileContentResponse{
- Name: entry.Name(),
- Path: treePath,
- SHA: entry.ID.String(),
- Size: entry.Size(),
- URL: selfURL.String(),
- HTMLURL: htmlURL.String(),
- GitURL: gitURL.String(),
- DownloadURL: downloadURL.String(),
- Type: entry.Type(),
+ // Get the commit object for the ref
+ commit, err := gitRepo.GetCommit(ref)
+ if err != nil {
+ return nil, err
+ }
+ commitID := commit.ID.String()
+ if len(ref) >= 4 && strings.HasPrefix(commitID, ref) {
+ ref = commit.ID.String()
+ }
+
+ entry, err := commit.GetTreeEntryByPath(treePath)
+ if err != nil {
+ return nil, err
+ }
+
+ refType := gitRepo.GetRefType(ref)
+ if refType == "invalid" {
+ return nil, fmt.Errorf("no commit found for the ref [ref: %s]", ref)
+ }
+
+ selfURL, err := url.Parse(fmt.Sprintf("%s/contents/%s?ref=%s", repo.APIURL(), treePath, origRef))
+ if err != nil {
+ return nil, err
+ }
+ selfURLString := selfURL.String()
+
+ // All content types have these fields in populated
+ contentsResponse := &api.ContentsResponse{
+ Name: entry.Name(),
+ Path: treePath,
+ SHA: entry.ID.String(),
+ Size: entry.Size(),
+ URL: &selfURLString,
Links: &api.FileLinksResponse{
- Self: selfURL.String(),
- GitURL: gitURL.String(),
- HTMLURL: htmlURL.String(),
+ Self: &selfURLString,
},
}
- return fileContent, nil
+ // Now populate the rest of the ContentsResponse based on entry type
+ if entry.IsRegular() {
+ contentsResponse.Type = string(ContentTypeRegular)
+ if blobResponse, err := GetBlobBySHA(repo, entry.ID.String()); err != nil {
+ return nil, err
+ } else if !forList {
+ // We don't show the content if we are getting a list of FileContentResponses
+ contentsResponse.Encoding = &blobResponse.Encoding
+ contentsResponse.Content = &blobResponse.Content
+ }
+ } else if entry.IsDir() {
+ contentsResponse.Type = string(ContentTypeDir)
+ } else if entry.IsLink() {
+ contentsResponse.Type = string(ContentTypeLink)
+ // The target of a symlink file is the content of the file
+ targetFromContent, err := entry.Blob().GetBlobContent()
+ if err != nil {
+ return nil, err
+ }
+ contentsResponse.Target = &targetFromContent
+ } else if entry.IsSubModule() {
+ contentsResponse.Type = string(ContentTypeSubmodule)
+ submodule, err := commit.GetSubModule(treePath)
+ if err != nil {
+ return nil, err
+ }
+ contentsResponse.SubmoduleGitURL = &submodule.URL
+ }
+ // Handle links
+ if entry.IsRegular() || entry.IsLink() {
+ downloadURL, err := url.Parse(fmt.Sprintf("%s/raw/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
+ if err != nil {
+ return nil, err
+ }
+ downloadURLString := downloadURL.String()
+ contentsResponse.DownloadURL = &downloadURLString
+ }
+ if !entry.IsSubModule() {
+ htmlURL, err := url.Parse(fmt.Sprintf("%s/src/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
+ if err != nil {
+ return nil, err
+ }
+ htmlURLString := htmlURL.String()
+ contentsResponse.HTMLURL = &htmlURLString
+ contentsResponse.Links.HTMLURL = &htmlURLString
+
+ gitURL, err := url.Parse(fmt.Sprintf("%s/git/blobs/%s", repo.APIURL(), entry.ID.String()))
+ if err != nil {
+ return nil, err
+ }
+ gitURLString := gitURL.String()
+ contentsResponse.GitURL = &gitURLString
+ contentsResponse.Links.GitURL = &gitURLString
+ }
+
+ return contentsResponse, nil
}
diff --git a/modules/repofiles/content_test.go b/modules/repofiles/content_test.go
index ce3f5f3678..ef6c5eafc2 100644
--- a/modules/repofiles/content_test.go
+++ b/modules/repofiles/content_test.go
@@ -9,7 +9,7 @@ import (
"testing"
"code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/structs"
+ api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert"
@@ -19,7 +19,36 @@ func TestMain(m *testing.M) {
models.MainTest(m, filepath.Join("..", ".."))
}
-func TestGetFileContents(t *testing.T) {
+func getExpectedReadmeContentsResponse() *api.ContentsResponse {
+ treePath := "README.md"
+ sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
+ encoding := "base64"
+ content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
+ selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
+ htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath
+ gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
+ downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
+ return &api.ContentsResponse{
+ Name: treePath,
+ Path: treePath,
+ SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
+ Type: "file",
+ Size: 30,
+ Encoding: &encoding,
+ Content: &content,
+ URL: &selfURL,
+ HTMLURL: &htmlURL,
+ GitURL: &gitURL,
+ DownloadURL: &downloadURL,
+ Links: &api.FileLinksResponse{
+ Self: &selfURL,
+ GitURL: &gitURL,
+ HTMLURL: &htmlURL,
+ },
+ }
+}
+
+func TestGetContents(t *testing.T) {
models.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
@@ -30,37 +59,110 @@ func TestGetFileContents(t *testing.T) {
treePath := "README.md"
ref := ctx.Repo.Repository.DefaultBranch
- expectedFileContentResponse := &structs.FileContentResponse{
- Name: treePath,
- Path: treePath,
- SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
- Size: 30,
- URL: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
- HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
- GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
- DownloadURL: "https://try.gitea.io/user2/repo1/raw/branch/master/README.md",
- Type: "blob",
- Links: &structs.FileLinksResponse{
- Self: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
- GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
- HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
- },
+ expectedContentsResponse := getExpectedReadmeContentsResponse()
+
+ t.Run("Get README.md contents with GetContents()", func(t *testing.T) {
+ fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, ref, false)
+ assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
+ assert.Nil(t, err)
+ })
+
+ t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch) with GetContents()", func(t *testing.T) {
+ fileContentResponse, err := GetContents(ctx.Repo.Repository, treePath, "", false)
+ assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
+ assert.Nil(t, err)
+ })
+}
+
+func TestGetContentsOrListForDir(t *testing.T) {
+ models.PrepareTestEnv(t)
+ ctx := test.MockContext(t, "user2/repo1")
+ ctx.SetParams(":id", "1")
+ test.LoadRepo(t, ctx, 1)
+ test.LoadRepoCommit(t, ctx)
+ test.LoadUser(t, ctx, 2)
+ test.LoadGitRepo(t, ctx)
+ treePath := "" // root dir
+ ref := ctx.Repo.Repository.DefaultBranch
+
+ readmeContentsResponse := getExpectedReadmeContentsResponse()
+ // because will be in a list, doesn't have encoding and content
+ readmeContentsResponse.Encoding = nil
+ readmeContentsResponse.Content = nil
+
+ expectedContentsListResponse := []*api.ContentsResponse{
+ readmeContentsResponse,
}
- t.Run("Get README.md contents", func(t *testing.T) {
- fileContentResponse, err := GetFileContents(ctx.Repo.Repository, treePath, ref)
- assert.EqualValues(t, expectedFileContentResponse, fileContentResponse)
+ t.Run("Get root dir contents with GetContentsOrList()", func(t *testing.T) {
+ fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
+ assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
+ assert.Nil(t, err)
+ })
+
+ t.Run("Get root dir contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) {
+ fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
+ assert.EqualValues(t, expectedContentsListResponse, fileContentResponse)
+ assert.Nil(t, err)
+ })
+}
+
+func TestGetContentsOrListForFile(t *testing.T) {
+ models.PrepareTestEnv(t)
+ ctx := test.MockContext(t, "user2/repo1")
+ ctx.SetParams(":id", "1")
+ test.LoadRepo(t, ctx, 1)
+ test.LoadRepoCommit(t, ctx)
+ test.LoadUser(t, ctx, 2)
+ test.LoadGitRepo(t, ctx)
+ treePath := "README.md"
+ ref := ctx.Repo.Repository.DefaultBranch
+
+ expectedContentsResponse := getExpectedReadmeContentsResponse()
+
+ t.Run("Get README.md contents with GetContentsOrList()", func(t *testing.T) {
+ fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, ref)
+ assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
assert.Nil(t, err)
})
- t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch)", func(t *testing.T) {
- fileContentResponse, err := GetFileContents(ctx.Repo.Repository, treePath, "")
- assert.EqualValues(t, expectedFileContentResponse, fileContentResponse)
+ t.Run("Get REAMDE.md contents with ref as empty string (should then use the repo's default branch) with GetContentsOrList()", func(t *testing.T) {
+ fileContentResponse, err := GetContentsOrList(ctx.Repo.Repository, treePath, "")
+ assert.EqualValues(t, expectedContentsResponse, fileContentResponse)
assert.Nil(t, err)
})
}
-func TestGetFileContentsErrors(t *testing.T) {
+func TestGetContentsErrors(t *testing.T) {
+ models.PrepareTestEnv(t)
+ ctx := test.MockContext(t, "user2/repo1")
+ ctx.SetParams(":id", "1")
+ test.LoadRepo(t, ctx, 1)
+ test.LoadRepoCommit(t, ctx)
+ test.LoadUser(t, ctx, 2)
+ test.LoadGitRepo(t, ctx)
+ repo := ctx.Repo.Repository
+ treePath := "README.md"
+ ref := repo.DefaultBranch
+
+ t.Run("bad treePath", func(t *testing.T) {
+ badTreePath := "bad/tree.md"
+ fileContentResponse, err := GetContents(repo, badTreePath, ref, false)
+ assert.Error(t, err)
+ assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
+ assert.Nil(t, fileContentResponse)
+ })
+
+ t.Run("bad ref", func(t *testing.T) {
+ badRef := "bad_ref"
+ fileContentResponse, err := GetContents(repo, treePath, badRef, false)
+ assert.Error(t, err)
+ assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
+ assert.Nil(t, fileContentResponse)
+ })
+}
+
+func TestGetContentsOrListErrors(t *testing.T) {
models.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
@@ -74,7 +176,7 @@ func TestGetFileContentsErrors(t *testing.T) {
t.Run("bad treePath", func(t *testing.T) {
badTreePath := "bad/tree.md"
- fileContentResponse, err := GetFileContents(repo, badTreePath, ref)
+ fileContentResponse, err := GetContentsOrList(repo, badTreePath, ref)
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: , rel_path: bad]")
assert.Nil(t, fileContentResponse)
@@ -82,7 +184,7 @@ func TestGetFileContentsErrors(t *testing.T) {
t.Run("bad ref", func(t *testing.T) {
badRef := "bad_ref"
- fileContentResponse, err := GetFileContents(repo, treePath, badRef)
+ fileContentResponse, err := GetContentsOrList(repo, treePath, badRef)
assert.Error(t, err)
assert.EqualError(t, err, "object does not exist [id: "+badRef+", rel_path: ]")
assert.Nil(t, fileContentResponse)
diff --git a/modules/repofiles/file.go b/modules/repofiles/file.go
index 70fd57bba0..801f770e02 100644
--- a/modules/repofiles/file.go
+++ b/modules/repofiles/file.go
@@ -17,8 +17,8 @@ import (
// GetFileResponseFromCommit Constructs a FileResponse from a Commit object
func GetFileResponseFromCommit(repo *models.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) {
- fileContents, _ := GetFileContents(repo, treeName, branch) // ok if fails, then will be nil
- fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
+ fileContents, _ := GetContents(repo, treeName, branch, false) // ok if fails, then will be nil
+ fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
verification := GetPayloadCommitVerification(commit)
fileResponse := &api.FileResponse{
Content: fileContents,
diff --git a/modules/repofiles/file_test.go b/modules/repofiles/file_test.go
index 5f6320a938..00feb93fff 100644
--- a/modules/repofiles/file_test.go
+++ b/modules/repofiles/file_test.go
@@ -5,6 +5,7 @@
package repofiles
import (
+ "code.gitea.io/gitea/modules/setting"
"testing"
"code.gitea.io/gitea/models"
@@ -16,21 +17,31 @@ import (
)
func getExpectedFileResponse() *api.FileResponse {
+ treePath := "README.md"
+ sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
+ encoding := "base64"
+ content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
+ selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
+ htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + treePath
+ gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
+ downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
return &api.FileResponse{
- Content: &api.FileContentResponse{
- Name: "README.md",
- Path: "README.md",
- SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
+ Content: &api.ContentsResponse{
+ Name: treePath,
+ Path: treePath,
+ SHA: sha,
+ Type: "file",
Size: 30,
- URL: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
- HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
- GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
- DownloadURL: "https://try.gitea.io/user2/repo1/raw/branch/master/README.md",
- Type: "blob",
+ Encoding: &encoding,
+ Content: &content,
+ URL: &selfURL,
+ HTMLURL: &htmlURL,
+ GitURL: &gitURL,
+ DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
- Self: "https://try.gitea.io/api/v1/repos/user2/repo1/contents/README.md",
- GitURL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
- HTMLURL: "https://try.gitea.io/user2/repo1/blob/master/README.md",
+ Self: &selfURL,
+ GitURL: &gitURL,
+ HTMLURL: &htmlURL,
},
},
Commit: &api.FileCommitResponse{