diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index c5f01d91d8c50..2c3eea1b00331 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -433,17 +433,16 @@ func RegisterRoutes(m *macaron.Macaron) { // Repositories m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo) - m.Group("/repos", func() { - m.Get("/search", repo.Search) - }) - m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID) m.Group("/repos", func() { + m.Get("/search", repo.Search) m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate) - 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..253e201b47c10 --- /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 +}