From 28063631a9f0b5dc25f57686f2404b17ed0265ab Mon Sep 17 00:00:00 2001 From: Kasi Date: Mon, 26 Nov 2018 07:06:56 +0000 Subject: [PATCH 1/5] Git-Trees API --- routers/api/v1/api.go | 3 ++ routers/api/v1/repo/tree.go | 90 +++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 routers/api/v1/repo/tree.go diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index c5f01d91d8c50..7610c3b85dc23 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -444,6 +444,9 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:username/:reponame", func() { m.Combo("").Get(repo.Get).Delete(reqToken(), repo.Delete) + m.Group("/git/trees", func() { + m.Combo("/:sha", context.RepoRef()).Get(repo.GetTree) + }) m.Group("/hooks", func() { m.Combo("").Get(repo.ListHooks). Post(bind(api.CreateHookOption{}), repo.CreateHook) diff --git a/routers/api/v1/repo/tree.go b/routers/api/v1/repo/tree.go new file mode 100644 index 0000000000000..d689472977b41 --- /dev/null +++ b/routers/api/v1/repo/tree.go @@ -0,0 +1,90 @@ +// 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 repo + +import ( + "fmt" + "strings" + + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/git" + "code.gitea.io/sdk/gitea" +) + +func GetTree(ctx *context.APIContext) { + sha := ctx.Params("sha") + if len(sha) == 0 { + ctx.Error(400, "sha not provided", nil) + return + } + tree := GetTreeBySHA(ctx, sha) + if tree != nil { + ctx.JSON(200, tree) + } else { + ctx.Error(400, "sha invalid", nil) + } +} + +func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse { + gitTree, err := ctx.Repo.GitRepo.GetTree(sha) + if err != nil || gitTree == nil{ + return nil + } + tree := new(gitea.GitTreeResponse) + repoID := strings.TrimRight(setting.AppURL, "/") + "/api/v1/repos/" + ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name + tree.SHA = gitTree.ID.String() + tree.URL = repoID + "/git/trees/" + tree.SHA + var entries git.Entries + if ctx.QueryBool("recursive") { + entries, err = gitTree.ListEntriesRecursive() + } else { + entries, err = gitTree.ListEntries() + } + if err != nil { + return tree + } + repoIDLen := len(repoID) + + // 51 is len(sha1) + len("/git/blobs/"). 40 + 11. + blobURL := make([]byte, repoIDLen+ 51) + copy(blobURL[:], repoID) + copy(blobURL[repoIDLen:], "/git/blobs/") + + // 51 is len(sha1) + len("/git/trees/"). 40 + 11. + treeURL := make([]byte, repoIDLen+ 51) + copy(treeURL[:], repoID) + copy(treeURL[repoIDLen:], "/git/trees/") + + // 40 is the size of the sha1 hash in hexadecimal format. + copyPos := len(treeURL) - 40 + + if len(entries) > 1000 { + tree.Entries = make([]gitea.GitEntry, 1000) + } else { + tree.Entries = make([]gitea.GitEntry, len(entries)) + } + for e := range entries { + if e > 1000 { + tree.Truncated = true + break + } + + tree.Entries[e].Path = entries[e].Name() + tree.Entries[e].Mode = fmt.Sprintf("%06x", entries[e].Mode()) + tree.Entries[e].Type = string(entries[e].Type) + tree.Entries[e].Size = entries[e].Size() + tree.Entries[e].SHA = entries[e].ID.String() + + if entries[e].IsDir() { + copy(treeURL[copyPos:], entries[e].ID.String()) + tree.Entries[e].URL = string(treeURL[:]) + } else { + copy(blobURL[copyPos:], entries[e].ID.String()) + tree.Entries[e].URL = string(blobURL[:]) + } + } + return tree +} From f5a8364b420651717437515eb2aed1e67d472a48 Mon Sep 17 00:00:00 2001 From: Matti Ranta Date: Tue, 27 Nov 2018 16:55:33 -0500 Subject: [PATCH 2/5] update vendor'd libs --- Gopkg.lock | 8 ++--- vendor/code.gitea.io/git/repo_tree.go | 9 +++++ vendor/code.gitea.io/git/tree.go | 14 ++++++++ vendor/code.gitea.io/git/tree_entry.go | 15 +++++--- vendor/code.gitea.io/sdk/gitea/repo_tree.go | 39 +++++++++++++++++++++ 5 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 vendor/code.gitea.io/sdk/gitea/repo_tree.go diff --git a/Gopkg.lock b/Gopkg.lock index 5142aa40dc246..151100d8dabbd 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -3,19 +3,19 @@ [[projects]] branch = "master" - digest = "1:ebd587087cf937b6d3db7dde843a557d157fd68820a9d3d0157a8d8f4011ad29" + digest = "1:881b74414ee4f7de02c375f7726dc363b415186567434c19c089b44fbd6b6b58" name = "code.gitea.io/git" packages = ["."] pruneopts = "NUT" - revision = "578ad8f1259b0d660d19b05a011596f8fd3fea37" + revision = "6b819173eda7e5da4f8292c99498bee73b6c8874" [[projects]] branch = "master" - digest = "1:4d2822cfcdf270183cee220e79e7bba55d5214a9c2bfa9b1fd6c6daaf5016eda" + digest = "1:aed2bc1c4026233af8ad43cab9d9464a0e3b906d3d058d2d6e814f3e1ddfa528" name = "code.gitea.io/sdk" packages = ["gitea"] pruneopts = "NUT" - revision = "59ddbdc4be1423ab3d5f30b859193ac0308df147" + revision = "d95a6e0392218961d1bdd18020290a20bd61b063" [[projects]] digest = "1:3fcef06a1a6561955c94af6c7757a6fa37605eb653f0d06ab960e5bb80092195" diff --git a/vendor/code.gitea.io/git/repo_tree.go b/vendor/code.gitea.io/git/repo_tree.go index 6e3843f7e5389..146b919d7a197 100644 --- a/vendor/code.gitea.io/git/repo_tree.go +++ b/vendor/code.gitea.io/git/repo_tree.go @@ -18,6 +18,15 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) { // GetTree find the tree object in the repository. func (repo *Repository) GetTree(idStr string) (*Tree, error) { + if len(idStr) != 40 { + res, err := NewCommand("rev-parse", idStr).RunInDir(repo.Path) + if err != nil { + return nil, err; + } + if len(res) > 0 { + idStr = res[:len(res)-1] + } + } id, err := NewIDFromString(idStr) if err != nil { return nil, err diff --git a/vendor/code.gitea.io/git/tree.go b/vendor/code.gitea.io/git/tree.go index 4654dac30ea7c..b67bf5584084b 100644 --- a/vendor/code.gitea.io/git/tree.go +++ b/vendor/code.gitea.io/git/tree.go @@ -70,3 +70,17 @@ func (t *Tree) ListEntries() (Entries, error) { t.entries, err = parseTreeEntries(stdout, t) return t.entries, err } + +// ListEntriesRecursive returns all entries of current tree recursively including all subtrees +func (t *Tree) ListEntriesRecursive() (Entries, error) { + if t.entriesParsed { + return t.entries, nil + } + stdout, err := NewCommand("ls-tree", "-t", "-r", t.ID.String()).RunInDirBytes(t.repo.Path) + + if err != nil { + return nil, err + } + t.entries, err = parseTreeEntries(stdout, t) + return t.entries, err +} diff --git a/vendor/code.gitea.io/git/tree_entry.go b/vendor/code.gitea.io/git/tree_entry.go index 6201eef8fd5bd..ead0d4f5df768 100644 --- a/vendor/code.gitea.io/git/tree_entry.go +++ b/vendor/code.gitea.io/git/tree_entry.go @@ -18,15 +18,15 @@ type EntryMode int // one of these. const ( // EntryModeBlob - EntryModeBlob EntryMode = 0100644 + EntryModeBlob EntryMode = 0x0100644 // EntryModeExec - EntryModeExec EntryMode = 0100755 + EntryModeExec EntryMode = 0x0100755 // EntryModeSymlink - EntryModeSymlink EntryMode = 0120000 + EntryModeSymlink EntryMode = 0x0120000 // EntryModeCommit - EntryModeCommit EntryMode = 0160000 + EntryModeCommit EntryMode = 0x0160000 // EntryModeTree - EntryModeTree EntryMode = 0040000 + EntryModeTree EntryMode = 0x0040000 ) // TreeEntry the leaf in the git tree @@ -50,6 +50,11 @@ func (te *TreeEntry) Name() string { return te.name } +// Mode returns the mode of the entry +func (te *TreeEntry) Mode() EntryMode { + return te.mode +} + // Size returns the size of the entry func (te *TreeEntry) Size() int64 { if te.IsDir() { diff --git a/vendor/code.gitea.io/sdk/gitea/repo_tree.go b/vendor/code.gitea.io/sdk/gitea/repo_tree.go new file mode 100644 index 0000000000000..cef3c64673859 --- /dev/null +++ b/vendor/code.gitea.io/sdk/gitea/repo_tree.go @@ -0,0 +1,39 @@ +// 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 gitea + +import ( + "fmt" +) + +// GitEntry represents a git tree +type GitEntry struct { + Path string `json:"path"` + Mode string `json:"mode"` + Type string `json:"type"` + Size int64 `json:"size"` + SHA string `json:"sha"` + URL string `json:"url"` +} + +// GitTreeResponse returns a git tree +type GitTreeResponse struct { + SHA string `json:"sha"` + URL string `json:"url"` + Entries []GitEntry `json:"tree"` + Truncated bool `json:"truncated"` +} + +// GetTrees downloads a file of repository, ref can be branch/tag/commit. +// e.g.: ref -> master, tree -> macaron.go(no leading slash) +func (c *Client) GetTrees(user, repo, ref string, recursive bool) (*GitTreeResponse, error) { + var trees GitTreeResponse + var path = fmt.Sprintf("/repos/%s/%s/git/trees/%s", user, repo, ref) + if recursive { + path += "?recursive=1" + } + err := c.getParsedResponse("GET", path, nil, nil, &trees) + return &trees, err +} From 1985243aec690df8a77a5d992ae0d670da839f19 Mon Sep 17 00:00:00 2001 From: Kasi Reddy Date: Wed, 28 Nov 2018 03:27:30 +0000 Subject: [PATCH 3/5] added comments to exported function and formatted. --- routers/api/v1/repo/tree.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/routers/api/v1/repo/tree.go b/routers/api/v1/repo/tree.go index d689472977b41..fd0bf8413240a 100644 --- a/routers/api/v1/repo/tree.go +++ b/routers/api/v1/repo/tree.go @@ -8,12 +8,13 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/modules/context" "code.gitea.io/git" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/sdk/gitea" ) +// GetTree get the tree of a repository. func GetTree(ctx *context.APIContext) { sha := ctx.Params("sha") if len(sha) == 0 { @@ -28,9 +29,10 @@ func GetTree(ctx *context.APIContext) { } } +// GetTreeBySHA get the GitTreeResponse of a repository using a sha hash. func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse { gitTree, err := ctx.Repo.GitRepo.GetTree(sha) - if err != nil || gitTree == nil{ + if err != nil || gitTree == nil { return nil } tree := new(gitea.GitTreeResponse) @@ -49,12 +51,12 @@ func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse { repoIDLen := len(repoID) // 51 is len(sha1) + len("/git/blobs/"). 40 + 11. - blobURL := make([]byte, repoIDLen+ 51) + blobURL := make([]byte, repoIDLen+51) copy(blobURL[:], repoID) copy(blobURL[repoIDLen:], "/git/blobs/") // 51 is len(sha1) + len("/git/trees/"). 40 + 11. - treeURL := make([]byte, repoIDLen+ 51) + treeURL := make([]byte, repoIDLen+51) copy(treeURL[:], repoID) copy(treeURL[repoIDLen:], "/git/trees/") From b3810aabc56940735796b13439c75c38d9c56898 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Wed, 28 Nov 2018 10:37:15 -0500 Subject: [PATCH 4/5] make fmt --- routers/api/v1/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 8eb203a076d0a..3a22132bd6612 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -480,7 +480,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:username/:reponame", func() { m.Combo("").Get(reqAnyRepoReader(), repo.Get). Delete(reqToken(), reqOwner(), repo.Delete) - m.Group("/git/trees", func() { + m.Group("/git/trees", func() { m.Combo("/:sha", context.RepoRef()).Get(repo.GetTree) }) m.Group("/hooks", func() { From ba600c9ec4b8422d8cdf84f390d6f3cfe0eaaedc Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Wed, 28 Nov 2018 15:56:59 -0500 Subject: [PATCH 5/5] update per @lafirks feedback --- routers/api/v1/api.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 3a22132bd6612..0284e845ff846 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -480,9 +480,6 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:username/:reponame", func() { m.Combo("").Get(reqAnyRepoReader(), repo.Get). Delete(reqToken(), reqOwner(), repo.Delete) - m.Group("/git/trees", func() { - m.Combo("/:sha", context.RepoRef()).Get(repo.GetTree) - }) m.Group("/hooks", func() { m.Combo("").Get(repo.ListHooks). Post(bind(api.CreateHookOption{}), repo.CreateHook) @@ -613,6 +610,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/git", func() { m.Get("/refs", repo.GetGitAllRefs) m.Get("/refs/*", repo.GetGitRefs) + m.Combo("/trees/:sha", context.RepoRef()).Get(repo.GetTree) }, reqRepoReader(models.UnitTypeCode)) }, repoAssignment()) })