Skip to content

Commit d5d847e

Browse files
Kasi-Rtechknowlogick
authored andcommitted
Git-Trees API (#5403)
* Git-Trees API * update vendor'd libs * added comments to exported function and formatted. * make fmt * update per @lafirks feedback
1 parent f17524b commit d5d847e

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

Gopkg.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ func RegisterRoutes(m *macaron.Macaron) {
610610
m.Group("/git", func() {
611611
m.Get("/refs", repo.GetGitAllRefs)
612612
m.Get("/refs/*", repo.GetGitRefs)
613+
m.Combo("/trees/:sha", context.RepoRef()).Get(repo.GetTree)
613614
}, reqRepoReader(models.UnitTypeCode))
614615
}, repoAssignment())
615616
})

routers/api/v1/repo/tree.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repo
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
"code.gitea.io/git"
12+
"code.gitea.io/gitea/modules/context"
13+
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/sdk/gitea"
15+
)
16+
17+
// GetTree get the tree of a repository.
18+
func GetTree(ctx *context.APIContext) {
19+
sha := ctx.Params("sha")
20+
if len(sha) == 0 {
21+
ctx.Error(400, "sha not provided", nil)
22+
return
23+
}
24+
tree := GetTreeBySHA(ctx, sha)
25+
if tree != nil {
26+
ctx.JSON(200, tree)
27+
} else {
28+
ctx.Error(400, "sha invalid", nil)
29+
}
30+
}
31+
32+
// GetTreeBySHA get the GitTreeResponse of a repository using a sha hash.
33+
func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse {
34+
gitTree, err := ctx.Repo.GitRepo.GetTree(sha)
35+
if err != nil || gitTree == nil {
36+
return nil
37+
}
38+
tree := new(gitea.GitTreeResponse)
39+
repoID := strings.TrimRight(setting.AppURL, "/") + "/api/v1/repos/" + ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
40+
tree.SHA = gitTree.ID.String()
41+
tree.URL = repoID + "/git/trees/" + tree.SHA
42+
var entries git.Entries
43+
if ctx.QueryBool("recursive") {
44+
entries, err = gitTree.ListEntriesRecursive()
45+
} else {
46+
entries, err = gitTree.ListEntries()
47+
}
48+
if err != nil {
49+
return tree
50+
}
51+
repoIDLen := len(repoID)
52+
53+
// 51 is len(sha1) + len("/git/blobs/"). 40 + 11.
54+
blobURL := make([]byte, repoIDLen+51)
55+
copy(blobURL[:], repoID)
56+
copy(blobURL[repoIDLen:], "/git/blobs/")
57+
58+
// 51 is len(sha1) + len("/git/trees/"). 40 + 11.
59+
treeURL := make([]byte, repoIDLen+51)
60+
copy(treeURL[:], repoID)
61+
copy(treeURL[repoIDLen:], "/git/trees/")
62+
63+
// 40 is the size of the sha1 hash in hexadecimal format.
64+
copyPos := len(treeURL) - 40
65+
66+
if len(entries) > 1000 {
67+
tree.Entries = make([]gitea.GitEntry, 1000)
68+
} else {
69+
tree.Entries = make([]gitea.GitEntry, len(entries))
70+
}
71+
for e := range entries {
72+
if e > 1000 {
73+
tree.Truncated = true
74+
break
75+
}
76+
77+
tree.Entries[e].Path = entries[e].Name()
78+
tree.Entries[e].Mode = fmt.Sprintf("%06x", entries[e].Mode())
79+
tree.Entries[e].Type = string(entries[e].Type)
80+
tree.Entries[e].Size = entries[e].Size()
81+
tree.Entries[e].SHA = entries[e].ID.String()
82+
83+
if entries[e].IsDir() {
84+
copy(treeURL[copyPos:], entries[e].ID.String())
85+
tree.Entries[e].URL = string(treeURL[:])
86+
} else {
87+
copy(blobURL[copyPos:], entries[e].ID.String())
88+
tree.Entries[e].URL = string(blobURL[:])
89+
}
90+
}
91+
return tree
92+
}

vendor/code.gitea.io/sdk/gitea/repo_tree.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)