git_trees.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }
  15. func (t Tree) String() string {
  16. return Stringify(t)
  17. }
  18. // TreeEntry represents the contents of a tree structure. TreeEntry can
  19. // represent either a blob, a commit (in the case of a submodule), or another
  20. // tree.
  21. type TreeEntry struct {
  22. SHA *string `json:"sha,omitempty"`
  23. Path *string `json:"path,omitempty"`
  24. Mode *string `json:"mode,omitempty"`
  25. Type *string `json:"type,omitempty"`
  26. Size *int `json:"size,omitempty"`
  27. Content *string `json:"content,omitempty"`
  28. URL *string `json:"url,omitempty"`
  29. }
  30. func (t TreeEntry) String() string {
  31. return Stringify(t)
  32. }
  33. // GetTree fetches the Tree object for a given sha hash from a repository.
  34. //
  35. // GitHub API docs: https://developer.github.com/v3/git/trees/#get-a-tree
  36. func (s *GitService) GetTree(ctx context.Context, owner string, repo string, sha string, recursive bool) (*Tree, *Response, error) {
  37. u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha)
  38. if recursive {
  39. u += "?recursive=1"
  40. }
  41. req, err := s.client.NewRequest("GET", u, nil)
  42. if err != nil {
  43. return nil, nil, err
  44. }
  45. t := new(Tree)
  46. resp, err := s.client.Do(ctx, req, t)
  47. if err != nil {
  48. return nil, resp, err
  49. }
  50. return t, resp, nil
  51. }
  52. // createTree represents the body of a CreateTree request.
  53. type createTree struct {
  54. BaseTree string `json:"base_tree,omitempty"`
  55. Entries []TreeEntry `json:"tree"`
  56. }
  57. // CreateTree creates a new tree in a repository. If both a tree and a nested
  58. // path modifying that tree are specified, it will overwrite the contents of
  59. // that tree with the new path contents and write a new tree out.
  60. //
  61. // GitHub API docs: https://developer.github.com/v3/git/trees/#create-a-tree
  62. func (s *GitService) CreateTree(ctx context.Context, owner string, repo string, baseTree string, entries []TreeEntry) (*Tree, *Response, error) {
  63. u := fmt.Sprintf("repos/%v/%v/git/trees", owner, repo)
  64. body := &createTree{
  65. BaseTree: baseTree,
  66. Entries: entries,
  67. }
  68. req, err := s.client.NewRequest("POST", u, body)
  69. if err != nil {
  70. return nil, nil, err
  71. }
  72. t := new(Tree)
  73. resp, err := s.client.Do(ctx, req, t)
  74. if err != nil {
  75. return nil, resp, err
  76. }
  77. return t, resp, nil
  78. }