本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.7KB

  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // Tree represents a GitHub tree.
  11. type Tree struct {
  12. SHA *string `json:"sha,omitempty"`
  13. Entries []TreeEntry `json:"tree,omitempty"`
  14. // Truncated is true if the number of items in the tree
  15. // exceeded GitHub's maximum limit and the Entries were truncated
  16. // in the response. Only populated for requests that fetch
  17. // trees like Git.GetTree.
  18. Truncated *bool `json:"truncated,omitempty"`
  19. }
  20. func (t Tree) String() string {
  21. return Stringify(t)
  22. }
  23. // TreeEntry represents the contents of a tree structure. TreeEntry can
  24. // represent either a blob, a commit (in the case of a submodule), or another
  25. // tree.
  26. type TreeEntry struct {
  27. SHA *string `json:"sha,omitempty"`
  28. Path *string `json:"path,omitempty"`
  29. Mode *string `json:"mode,omitempty"`
  30. Type *string `json:"type,omitempty"`
  31. Size *int `json:"size,omitempty"`
  32. Content *string `json:"content,omitempty"`
  33. URL *string `json:"url,omitempty"`
  34. }
  35. func (t TreeEntry) String() string {
  36. return Stringify(t)
  37. }
  38. // GetTree fetches the Tree object for a given sha hash from a repository.
  39. //
  40. // GitHub API docs: https://developer.github.com/v3/git/trees/#get-a-tree
  41. func (s *GitService) GetTree(ctx context.Context, owner string, repo string, sha string, recursive bool) (*Tree, *Response, error) {
  42. u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha)
  43. if recursive {
  44. u += "?recursive=1"
  45. }
  46. req, err := s.client.NewRequest("GET", u, nil)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. t := new(Tree)
  51. resp, err := s.client.Do(ctx, req, t)
  52. if err != nil {
  53. return nil, resp, err
  54. }
  55. return t, resp, nil
  56. }
  57. // createTree represents the body of a CreateTree request.
  58. type createTree struct {
  59. BaseTree string `json:"base_tree,omitempty"`
  60. Entries []TreeEntry `json:"tree"`
  61. }
  62. // CreateTree creates a new tree in a repository. If both a tree and a nested
  63. // path modifying that tree are specified, it will overwrite the contents of
  64. // that tree with the new path contents and write a new tree out.
  65. //
  66. // GitHub API docs: https://developer.github.com/v3/git/trees/#create-a-tree
  67. func (s *GitService) CreateTree(ctx context.Context, owner string, repo string, baseTree string, entries []TreeEntry) (*Tree, *Response, error) {
  68. u := fmt.Sprintf("repos/%v/%v/git/trees", owner, repo)
  69. body := &createTree{
  70. BaseTree: baseTree,
  71. Entries: entries,
  72. }
  73. req, err := s.client.NewRequest("POST", u, body)
  74. if err != nil {
  75. return nil, nil, err
  76. }
  77. t := new(Tree)
  78. resp, err := s.client.Do(ctx, req, t)
  79. if err != nil {
  80. return nil, resp, err
  81. }
  82. return t, resp, nil
  83. }
上海开阖软件有限公司 沪ICP备12045867号-1