Skip to content

Commit d768d25

Browse files
committed
Git refs API implementation
1 parent ea3fb69 commit d768d25

File tree

267 files changed

+48368
-54
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

267 files changed

+48368
-54
lines changed

Gopkg.lock

Lines changed: 153 additions & 5 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
@@ -574,6 +574,7 @@ func RegisterRoutes(m *macaron.Macaron) {
574574
m.Get("/statuses", repo.GetCommitStatusesByRef)
575575
})
576576
m.Group("/git", func() {
577+
m.Get("/refs", repo.GetGitRefs)
577578
m.Get("/refs/*", repo.GetGitRefs)
578579
})
579580
}, repoAssignment())

routers/api/v1/repo/git_ref.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package repo
66

77
import (
88
"code.gitea.io/gitea/modules/context"
9+
10+
"code.gitea.io/git"
11+
api "code.gitea.io/sdk/gitea"
912
)
1013

1114
// GetGitRefs get ref or an list all the refs of a repository
@@ -33,6 +36,49 @@ func GetGitRefs(ctx *context.APIContext) {
3336
// required: false
3437
// responses:
3538
// "200":
36-
// "$ref": "#/responses/Ref"
37-
// "$ref": "#/responses/RefList"
39+
// "$ref": "#/responses/Reference"
40+
// "$ref": "#/responses/ReferenceList"
41+
// "404":
42+
// "$ref": "#/responses/notFound"
43+
44+
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
45+
if err != nil {
46+
ctx.Error(500, "OpenRepository", err)
47+
return
48+
}
49+
filter := ctx.Params("*")
50+
if len(filter) > 0 {
51+
filter = "refs/" + filter
52+
}
53+
54+
refs, err := gitRepo.GetRefsFiltered(filter)
55+
if err != nil {
56+
ctx.Error(500, "GetRefsFiltered", err)
57+
return
58+
}
59+
60+
if len(refs) == 0 {
61+
ctx.Status(404)
62+
return
63+
}
64+
65+
apiRefs := make([]*api.Reference, len(refs))
66+
for i := range refs {
67+
apiRefs[i] = &api.Reference{
68+
Ref: refs[i].Name,
69+
URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Name,
70+
Object: &api.GitObject{
71+
SHA: refs[i].Object.String(),
72+
Type: refs[i].Type,
73+
// TODO: Add commit/tag info URL
74+
//URL: ctx.Repo.Repository.APIURL() + "/git/" + refs[i].Type + "s/" + refs[i].Object.String(),
75+
},
76+
}
77+
}
78+
// If single reference is found and it matches filter exactly return it as object
79+
if len(apiRefs) == 1 && apiRefs[0].Ref == filter {
80+
ctx.JSON(200, &apiRefs[0])
81+
return
82+
}
83+
ctx.JSON(200, &apiRefs)
3884
}

routers/api/v1/swagger/repo.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ type swaggerResponseBranchList struct {
3636
Body []api.Branch `json:"body"`
3737
}
3838

39+
// Reference
40+
// swagger:response Reference
41+
type swaggerResponseReference struct {
42+
// in:body
43+
Body api.Reference `json:"body"`
44+
}
45+
46+
// ReferenceList
47+
// swagger:response ReferenceList
48+
type swaggerResponseReferenceList struct {
49+
// in:body
50+
Body []api.Reference `json:"body"`
51+
}
52+
3953
// Hook
4054
// swagger:response Hook
4155
type swaggerResponseHook struct {

vendor/code.gitea.io/git/ref.go

Lines changed: 18 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)