Skip to content
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

Git-Trees API #5403

Merged
merged 10 commits into from
Nov 28, 2018
Next Next commit
Git-Trees API
  • Loading branch information
Kasi-R committed Nov 26, 2018
commit 28063631a9f0b5dc25f57686f2404b17ed0265ab
3 changes: 3 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
m.Combo("/:sha", context.RepoRef()).Get(repo.GetTree)
})
m.Group("/hooks", func() {
m.Combo("").Get(repo.ListHooks).
Post(bind(api.CreateHookOption{}), repo.CreateHook)
Expand Down
90 changes: 90 additions & 0 deletions routers/api/v1/repo/tree.go
Original file line number Diff line number Diff line change
@@ -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) {
Kasi-R marked this conversation as resolved.
Show resolved Hide resolved
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 {
Kasi-R marked this conversation as resolved.
Show resolved Hide resolved
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.
Kasi-R marked this conversation as resolved.
Show resolved Hide resolved
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
}