mirror of https://github.com/gogs/gogs.git
api: minor changes to `/repo/owner/repo/git/trees` (#5982)
parent
047bf94908
commit
927ffef864
|
@ -274,7 +274,7 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||||
m.Get("/contents/*", repo.GetContents)
|
m.Get("/contents/*", repo.GetContents)
|
||||||
m.Get("/archive/*", repo.GetArchive)
|
m.Get("/archive/*", repo.GetArchive)
|
||||||
m.Group("/git/trees", func() {
|
m.Group("/git/trees", func() {
|
||||||
m.Get("/:sha", context.RepoRef(), repo.GetRepoGitTree)
|
m.Get("/:sha", repo.GetRepoGitTree)
|
||||||
})
|
})
|
||||||
m.Get("/forks", repo.ListForks)
|
m.Get("/forks", repo.ListForks)
|
||||||
m.Group("/branches", func() {
|
m.Group("/branches", func() {
|
||||||
|
|
|
@ -13,10 +13,24 @@ import (
|
||||||
"gogs.io/gogs/internal/gitutil"
|
"gogs.io/gogs/internal/gitutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type repoGitTree struct {
|
func GetRepoGitTree(c *context.APIContext) {
|
||||||
Sha string `json:"sha"`
|
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
|
||||||
URL string `json:"url"`
|
if err != nil {
|
||||||
Tree []*repoGitTreeEntry `json:"tree"`
|
c.ServerError("open repository", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sha := c.Params(":sha")
|
||||||
|
tree, err := gitRepo.LsTree(sha)
|
||||||
|
if err != nil {
|
||||||
|
c.NotFoundOrServerError("get tree", gitutil.IsErrRevisionNotExist, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := tree.Entries()
|
||||||
|
if err != nil {
|
||||||
|
c.ServerError("list entries", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type repoGitTreeEntry struct {
|
type repoGitTreeEntry struct {
|
||||||
|
@ -27,25 +41,18 @@ type repoGitTreeEntry struct {
|
||||||
Sha string `json:"sha"`
|
Sha string `json:"sha"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
type repoGitTree struct {
|
||||||
func GetRepoGitTree(c *context.APIContext) {
|
Sha string `json:"sha"`
|
||||||
gitTree, err := c.Repo.GitRepo.LsTree(c.Params(":sha"))
|
URL string `json:"url"`
|
||||||
if err != nil {
|
Tree []*repoGitTreeEntry `json:"tree"`
|
||||||
c.NotFoundOrServerError("get tree", gitutil.IsErrRevisionNotExist, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
entries, err := gitTree.Entries()
|
|
||||||
if err != nil {
|
|
||||||
c.ServerError("list entries", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
templateURL := fmt.Sprintf("%s/repos/%s/%s/git/trees", c.BaseURL, c.Params(":username"), c.Params(":reponame"))
|
treesURL := fmt.Sprintf("%s/repos/%s/%s/git/trees", c.BaseURL, c.Params(":username"), c.Params(":reponame"))
|
||||||
|
|
||||||
if len(entries) == 0 {
|
if len(entries) == 0 {
|
||||||
c.JSONSuccess(&repoGitTree{
|
c.JSONSuccess(&repoGitTree{
|
||||||
Sha: c.Params(":sha"),
|
Sha: sha,
|
||||||
URL: fmt.Sprintf(templateURL+"/%s", c.Params(":sha")),
|
URL: fmt.Sprintf(treesURL+"/%s", sha),
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -63,7 +70,7 @@ func GetRepoGitTree(c *context.APIContext) {
|
||||||
case git.ObjectTag:
|
case git.ObjectTag:
|
||||||
mode = "100644"
|
mode = "100644"
|
||||||
default:
|
default:
|
||||||
mode = ""
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
children = append(children, &repoGitTreeEntry{
|
children = append(children, &repoGitTreeEntry{
|
||||||
Path: entry.Name(),
|
Path: entry.Name(),
|
||||||
|
@ -71,12 +78,12 @@ func GetRepoGitTree(c *context.APIContext) {
|
||||||
Type: string(entry.Type()),
|
Type: string(entry.Type()),
|
||||||
Size: entry.Size(),
|
Size: entry.Size(),
|
||||||
Sha: entry.ID().String(),
|
Sha: entry.ID().String(),
|
||||||
URL: fmt.Sprintf(templateURL+"/%s", entry.ID().String()),
|
URL: fmt.Sprintf(treesURL+"/%s", entry.ID().String()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
c.JSONSuccess(&repoGitTree{
|
c.JSONSuccess(&repoGitTree{
|
||||||
Sha: c.Params(":sha"),
|
Sha: c.Params(":sha"),
|
||||||
URL: fmt.Sprintf(templateURL+"/%s", c.Params(":sha")),
|
URL: fmt.Sprintf(treesURL+"/%s", sha),
|
||||||
Tree: children,
|
Tree: children,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue