Skip to content

Commit 71fcef4

Browse files
committed
misc cleanup of trees API
- complete the rename from TreesService to GitService. For example, renaming Get to GetTree, and Create to CreateTree. - rename GitTree to TreeEntry. I really have no idea why GitHub chose to use the field name "tree" for this, since these aren't necessarily trees in the git sense. Looking at it more closely, these are really just the entries within a tree, hence the rename. - simplify several tests and shuffle some things for consistency with the rest of the library
1 parent cf1e5f6 commit 71fcef4

File tree

2 files changed

+71
-97
lines changed

2 files changed

+71
-97
lines changed

github/git.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,80 @@
1-
// Copyright 2013 Google. All rights reserved.
1+
// Copyright 2013 The go-github AUTHORS. All rights reserved.
22
//
33
// Use of this source code is governed by a BSD-style
4-
// license that can be found in the LICENSE file or at
5-
// https://developers.google.com/open-source/licenses/bsd
4+
// license that can be found in the LICENSE file.
65

76
package github
87

98
import "fmt"
109

11-
// GitService handles communication with the tree related
10+
// GitService handles communication with the git data related
1211
// methods of the GitHub API.
1312
//
1413
// GitHub API docs: http://developer.github.com/v3/git/
1514
type GitService struct {
1615
client *Client
1716
}
1817

18+
// Tree represents a GitHub tree.
1919
type Tree struct {
20-
SHA string `json:"sha,omitempty"`
21-
Trees []GitTree `json:"tree,omitempty"`
20+
SHA string `json:"sha,omitempty"`
21+
Entries []TreeEntry `json:"tree,omitempty"`
2222
}
2323

24-
// Tree represents a Git tree.
25-
type GitTree struct {
24+
// TreeEntry represents the contents of a tree structure. TreeEntry can
25+
// represent either a blob, a commit (in the case of a submodule), or another
26+
// tree.
27+
type TreeEntry struct {
2628
SHA string `json:"sha,omitempty"`
2729
Path string `json:"path,omitempty"`
2830
Mode string `json:"mode,omitempty"`
2931
Type string `json:"type,omitempty"`
3032
Size int `json:"size,omitempty"`
3133
}
3234

35+
// createTree represents the body of a CreateTree request.
3336
type createTree struct {
34-
baseTree string `json:base_tree`
35-
trees []GitTree `json:tree`
37+
BaseTree string `json:base_tree`
38+
Entries []TreeEntry `json:tree`
3639
}
3740

38-
// Get the Tree object for a given sha hash from a users repository.
41+
// GetTree fetches the Tree object for a given sha hash from a users repository.
3942
//
4043
// GitHub API docs: http://developer.github.com/v3/git/trees/#get-a-tree
41-
func (s *GitService) Get(user string, repo string, sha string, recursive bool) (*Tree, error) {
42-
url_ := fmt.Sprintf("repos/%v/%v/git/trees/%v", user, repo, sha)
43-
44+
func (s *GitService) GetTree(user string, repo string, sha string, recursive bool) (*Tree, error) {
45+
u := fmt.Sprintf("repos/%v/%v/git/trees/%v", user, repo, sha)
4446
if recursive {
45-
url_ += "?recursive=1"
47+
u += "?recursive=1"
4648
}
4749

48-
req, err := s.client.NewRequest("GET", url_, nil)
50+
req, err := s.client.NewRequest("GET", u, nil)
4951
if err != nil {
5052
return nil, err
5153
}
5254

53-
var response Tree
54-
_, err = s.client.Do(req, &response)
55-
return &response, err
55+
t := new(Tree)
56+
_, err = s.client.Do(req, t)
57+
return t, err
5658
}
5759

58-
// The tree creation API will take nested entries as well.
59-
// If both a tree and a nested path modifying that tree are specified,
60-
// it will overwrite the contents of that tree with the new path contents and write a new tree out.
60+
// CreateTree creates a new tree in a repository. If both a tree and a nested
61+
// path modifying that tree are specified, it will overwrite the contents of
62+
// that tree with the new path contents and write a new tree out.
6163
//
6264
// GitHub API docs: http://developer.github.com/v3/git/trees/#create-a-tree
63-
func (s *GitService) Create(owner string, repo string, sha string, baseTreeSha string, trees []GitTree) (*Tree, error) {
64-
url_ := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha)
65+
func (s *GitService) CreateTree(owner string, repo string, sha string, baseTree string, entries []TreeEntry) (*Tree, error) {
66+
u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha)
6567

66-
req, err := s.client.NewRequest("POST", url_, createTree{
67-
baseTree: baseTreeSha,
68-
trees: trees,
69-
})
68+
body := &createTree{
69+
BaseTree: baseTree,
70+
Entries: entries,
71+
}
72+
req, err := s.client.NewRequest("POST", u, body)
7073
if err != nil {
7174
return nil, err
7275
}
7376

74-
r := new(Tree)
75-
_, err = s.client.Do(req, r)
76-
return r, err
77+
t := new(Tree)
78+
_, err = s.client.Do(req, t)
79+
return t, err
7780
}

github/git_test.go

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Copyright 2013 Google. All rights reserved.
1+
// Copyright 2013 The go-github AUTHORS. All rights reserved.
22
//
33
// Use of this source code is governed by a BSD-style
4-
// license that can be found in the LICENSE file or at
5-
// https://developers.google.com/open-source/licenses/bsd
4+
// license that can be found in the LICENSE file.
65

76
package github
87

@@ -14,95 +13,67 @@ import (
1413
"testing"
1514
)
1615

17-
func TestGitService_Get_authenticatedUser(t *testing.T) {
16+
func TestGitService_GetTree(t *testing.T) {
1817
setup()
1918
defer teardown()
2019

21-
url_ := fmt.Sprintf("/repos/%v/%v/git/trees/%v", "user", "repo", "coffebabecoffebabecoffebabe")
22-
23-
mux.HandleFunc(url_, func(w http.ResponseWriter, r *http.Request) {
20+
mux.HandleFunc("/repos/u/r/git/trees/s", func(w http.ResponseWriter, r *http.Request) {
2421
if m := "GET"; m != r.Method {
2522
t.Errorf("Request method = %v, want %v", r.Method, m)
2623
}
2724
fmt.Fprint(w, `{
28-
"sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
29-
"url": "https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
30-
"tree": [
31-
{
32-
"path": "file.rb",
33-
"mode": "100644",
34-
"type": "blob",
35-
"size": 30,
36-
"sha": "44b4fc6d56897b048c772eb4087f854f46256132",
37-
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132"
38-
},
39-
{
40-
"path": "subdir",
41-
"mode": "040000",
42-
"type": "tree",
43-
"sha": "f484d249c660418515fb01c2b9662073663c242e",
44-
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e"
45-
}
46-
]
25+
"sha": "s",
26+
"tree": [ { "type": "blob" } ]
4727
}`)
4828
})
4929

50-
trees, err := client.Trees.Get("user", "repo", "coffebabecoffebabecoffebabe", true)
30+
tree, err := client.Git.GetTree("u", "r", "s", true)
5131
if err != nil {
52-
t.Errorf("Trees.List returned error: %v", err)
32+
t.Errorf("Git.GetTree returned error: %v", err)
5333
}
5434

5535
want := Tree{
56-
SHA: `9fb037999f264ba9a7fc6274d15fa3ae2ab98312`,
57-
Trees: []GitTree{
58-
GitTree{
59-
Path: "file.rb",
60-
Mode: "100644",
36+
SHA: "s",
37+
Entries: []TreeEntry{
38+
TreeEntry{
6139
Type: "blob",
62-
Size: 30,
63-
SHA: "44b4fc6d56897b048c772eb4087f854f46256132",
64-
},
65-
GitTree{
66-
Path: "subdir",
67-
Mode: "040000",
68-
Type: "tree",
69-
SHA: "f484d249c660418515fb01c2b9662073663c242e",
7040
},
7141
},
7242
}
73-
if !reflect.DeepEqual(*trees, want) {
74-
t.Errorf("Tree.List returned %+v, want %+v", *trees, want)
43+
if !reflect.DeepEqual(*tree, want) {
44+
t.Errorf("Tree.Get returned %+v, want %+v", *tree, want)
7545
}
7646
}
7747

78-
func TestGitService_Create_authenticatedUser(t *testing.T) {
48+
func TestGitService_CreateTree(t *testing.T) {
7949
setup()
8050
defer teardown()
8151

82-
url_ := fmt.Sprintf("/repos/%v/%v/git/trees/%v", "user", "repo", "coffebabecoffebabecoffebabe")
83-
84-
treesToCreate :=
85-
[]GitTree{
86-
GitTree{
87-
Path: "file.rb",
88-
Mode: "100644",
89-
Type: "blob",
90-
SHA: "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b",
91-
},
92-
}
93-
94-
mux.HandleFunc(url_, func(w http.ResponseWriter, r *http.Request) {
95-
v := new(struct {
96-
BaseTree string `json:base_tree`
97-
Tree []GitTree `json:tree`
98-
})
52+
input := []TreeEntry{
53+
TreeEntry{
54+
Path: "file.rb",
55+
Mode: "100644",
56+
Type: "blob",
57+
SHA: "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b",
58+
},
59+
}
9960

61+
mux.HandleFunc("/repos/u/r/git/trees/s", func(w http.ResponseWriter, r *http.Request) {
62+
v := new(createTree)
10063
json.NewDecoder(r.Body).Decode(v)
10164

10265
if m := "POST"; m != r.Method {
10366
t.Errorf("Request method = %v, want %v", r.Method, m)
10467
}
10568

69+
want := &createTree{
70+
BaseTree: "b",
71+
Entries: input,
72+
}
73+
if !reflect.DeepEqual(v, want) {
74+
t.Errorf("Git.CreateTree request body: %+v, want %+v", v, want)
75+
}
76+
10677
fmt.Fprint(w, `{
10778
"sha": "cd8274d15fa3ae2ab983129fb037999f264ba9a7",
10879
"tree": [
@@ -117,15 +88,15 @@ func TestGitService_Create_authenticatedUser(t *testing.T) {
11788
}`)
11889
})
11990

120-
tree, err := client.Trees.Create("user", "repo", "coffebabecoffebabecoffebabe", "basebasebase", treesToCreate)
91+
tree, err := client.Git.CreateTree("u", "r", "s", "b", input)
12192
if err != nil {
122-
t.Errorf("Trees.Create returned error: %v", err)
93+
t.Errorf("Git.CreateTree returned error: %v", err)
12394
}
12495

12596
want := Tree{
12697
"cd8274d15fa3ae2ab983129fb037999f264ba9a7",
127-
[]GitTree{
128-
GitTree{
98+
[]TreeEntry{
99+
TreeEntry{
129100
Path: "file.rb",
130101
Mode: "100644",
131102
Type: "blob",
@@ -136,6 +107,6 @@ func TestGitService_Create_authenticatedUser(t *testing.T) {
136107
}
137108

138109
if !reflect.DeepEqual(*tree, want) {
139-
t.Errorf("Tree.Create returned %+v, want %+v", *tree, want)
110+
t.Errorf("Git.CreateTree returned %+v, want %+v", *tree, want)
140111
}
141112
}

0 commit comments

Comments
 (0)