From 347120d900cd2b62aa7f1be6d44de00afabc192f Mon Sep 17 00:00:00 2001 From: Kasi Date: Fri, 8 Jun 2018 14:26:23 +0100 Subject: [PATCH] Started on Git-Trees api --- routers/api/v1/api.go | 8 ++++ routers/api/v1/repo/tree.go | 89 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 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 466d6b06f..d36b1c6d6 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -377,6 +377,11 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:username/:reponame", func() { m.Combo("").Get(repo.Get).Delete(reqToken(), repo.Delete) + + m.Group("/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) @@ -387,6 +392,9 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/tests", context.RepoRef(), repo.TestHook) }) }, reqToken(), reqRepoWriter()) + + + m.Group("/collaborators", func() { m.Get("", repo.ListCollaborators) m.Combo("/:collaborator").Get(repo.IsCollaborator). diff --git a/routers/api/v1/repo/tree.go b/routers/api/v1/repo/tree.go new file mode 100644 index 000000000..8ba0e01a5 --- /dev/null +++ b/routers/api/v1/repo/tree.go @@ -0,0 +1,89 @@ +package repo + +import ( + "code.gitea.io/gitea/modules/context" + "fmt" + _"code.gitea.io/git" + "code.gitea.io/gitea/modules/setting" + "strings" +) + +type TreeEntry struct { + Path string `json:"path"` + Mode string `json:"mode"` + Type string `json:"type"` + Size int64 `json:"size,omitempty"` + SHA string `json:"sha"` + URL string `json:"url"` +} + +type Tree struct { + SHA string `json:"sha"` + URL string `json:"url"` + Entries []TreeEntry `json:"tree,omitempty"` + Truncated bool `json:"truncated"` +} + +func GetTree(ctx *context.APIContext) { + sha := ctx.Params("sha") + if len(sha) == 0 { + ctx.Error(400, "sha not provided", nil) + return + } + Temp := GetTreeBySHA(ctx, nil, "", sha, ctx.QueryBool("recursive")) + if Temp != nil { + ctx.JSON(200, Temp) + } else { + ctx.Error(400, "sha invalid", nil) + } + +} + +func GetTreeBySHA(ctx *context.APIContext, tree *Tree, CurrentPath string, sha string, recursive bool) *Tree { + GitTree, err := ctx.Repo.GitRepo.GetTree(sha) + if err != nil { + return tree + } + RepoID := strings.TrimRight(setting.AppURL, "/") + "/api/v1/repos/" + ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name + if tree == nil { + tree = new(Tree) + if GitTree != nil { + tree.SHA = GitTree.ID.String() + tree.URL = RepoID + "/trees/" + tree.SHA; + } + } + if GitTree == nil { + return tree + } + Trees, err := GitTree.ListEntries() + if err != nil { + return tree + } + if len(CurrentPath) != 0 { + CurrentPath += "/" + } + for e := range Trees { + if len(tree.Entries) > 1000 { + tree.Truncated = true + break + } + E_URL := RepoID + if Trees[e].IsDir() { + E_URL += "/trees/" + } else { + E_URL += "/blobs/" + } + tree.Entries = append(tree.Entries, TreeEntry{ + CurrentPath + Trees[e].Name(), + fmt.Sprintf("%06x", Trees[e].Mode()), + string(Trees[e].Type), + Trees[e].Size(), + Trees[e].ID.String(), + E_URL + Trees[e].ID.String()}) + + if recursive && Trees[e].IsDir() { + tree = GetTreeBySHA(ctx, tree, CurrentPath + Trees[e].Name(), Trees[e].ID.String(), recursive) + } + } + return tree +}