repos_commits.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. "bytes"
  8. "context"
  9. "fmt"
  10. "time"
  11. )
  12. // RepositoryCommit represents a commit in a repo.
  13. // Note that it's wrapping a Commit, so author/committer information is in two places,
  14. // but contain different details about them: in RepositoryCommit "github details", in Commit - "git details".
  15. type RepositoryCommit struct {
  16. SHA *string `json:"sha,omitempty"`
  17. Commit *Commit `json:"commit,omitempty"`
  18. Author *User `json:"author,omitempty"`
  19. Committer *User `json:"committer,omitempty"`
  20. Parents []Commit `json:"parents,omitempty"`
  21. HTMLURL *string `json:"html_url,omitempty"`
  22. URL *string `json:"url,omitempty"`
  23. CommentsURL *string `json:"comments_url,omitempty"`
  24. // Details about how many changes were made in this commit. Only filled in during GetCommit!
  25. Stats *CommitStats `json:"stats,omitempty"`
  26. // Details about which files, and how this commit touched. Only filled in during GetCommit!
  27. Files []CommitFile `json:"files,omitempty"`
  28. }
  29. func (r RepositoryCommit) String() string {
  30. return Stringify(r)
  31. }
  32. // CommitStats represents the number of additions / deletions from a file in a given RepositoryCommit or GistCommit.
  33. type CommitStats struct {
  34. Additions *int `json:"additions,omitempty"`
  35. Deletions *int `json:"deletions,omitempty"`
  36. Total *int `json:"total,omitempty"`
  37. }
  38. func (c CommitStats) String() string {
  39. return Stringify(c)
  40. }
  41. // CommitFile represents a file modified in a commit.
  42. type CommitFile struct {
  43. SHA *string `json:"sha,omitempty"`
  44. Filename *string `json:"filename,omitempty"`
  45. Additions *int `json:"additions,omitempty"`
  46. Deletions *int `json:"deletions,omitempty"`
  47. Changes *int `json:"changes,omitempty"`
  48. Status *string `json:"status,omitempty"`
  49. Patch *string `json:"patch,omitempty"`
  50. BlobURL *string `json:"blob_url,omitempty"`
  51. RawURL *string `json:"raw_url,omitempty"`
  52. ContentsURL *string `json:"contents_url,omitempty"`
  53. }
  54. func (c CommitFile) String() string {
  55. return Stringify(c)
  56. }
  57. // CommitsComparison is the result of comparing two commits.
  58. // See CompareCommits() for details.
  59. type CommitsComparison struct {
  60. BaseCommit *RepositoryCommit `json:"base_commit,omitempty"`
  61. MergeBaseCommit *RepositoryCommit `json:"merge_base_commit,omitempty"`
  62. // Head can be 'behind' or 'ahead'
  63. Status *string `json:"status,omitempty"`
  64. AheadBy *int `json:"ahead_by,omitempty"`
  65. BehindBy *int `json:"behind_by,omitempty"`
  66. TotalCommits *int `json:"total_commits,omitempty"`
  67. Commits []RepositoryCommit `json:"commits,omitempty"`
  68. Files []CommitFile `json:"files,omitempty"`
  69. HTMLURL *string `json:"html_url,omitempty"`
  70. PermalinkURL *string `json:"permalink_url,omitempty"`
  71. DiffURL *string `json:"diff_url,omitempty"`
  72. PatchURL *string `json:"patch_url,omitempty"`
  73. URL *string `json:"url,omitempty"` // API URL.
  74. }
  75. func (c CommitsComparison) String() string {
  76. return Stringify(c)
  77. }
  78. // CommitsListOptions specifies the optional parameters to the
  79. // RepositoriesService.ListCommits method.
  80. type CommitsListOptions struct {
  81. // SHA or branch to start listing Commits from.
  82. SHA string `url:"sha,omitempty"`
  83. // Path that should be touched by the returned Commits.
  84. Path string `url:"path,omitempty"`
  85. // Author of by which to filter Commits.
  86. Author string `url:"author,omitempty"`
  87. // Since when should Commits be included in the response.
  88. Since time.Time `url:"since,omitempty"`
  89. // Until when should Commits be included in the response.
  90. Until time.Time `url:"until,omitempty"`
  91. ListOptions
  92. }
  93. // ListCommits lists the commits of a repository.
  94. //
  95. // GitHub API docs: https://developer.github.com/v3/repos/commits/#list
  96. func (s *RepositoriesService) ListCommits(ctx context.Context, owner, repo string, opt *CommitsListOptions) ([]*RepositoryCommit, *Response, error) {
  97. u := fmt.Sprintf("repos/%v/%v/commits", owner, repo)
  98. u, err := addOptions(u, opt)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. req, err := s.client.NewRequest("GET", u, nil)
  103. if err != nil {
  104. return nil, nil, err
  105. }
  106. // TODO: remove custom Accept header when this API fully launches.
  107. req.Header.Set("Accept", mediaTypeGitSigningPreview)
  108. var commits []*RepositoryCommit
  109. resp, err := s.client.Do(ctx, req, &commits)
  110. if err != nil {
  111. return nil, resp, err
  112. }
  113. return commits, resp, nil
  114. }
  115. // GetCommit fetches the specified commit, including all details about it.
  116. //
  117. // GitHub API docs: https://developer.github.com/v3/repos/commits/#get-a-single-commit
  118. // See also: https://developer.github.com/v3/git/commits/#get-a-single-commit provides the same functionality
  119. func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha string) (*RepositoryCommit, *Response, error) {
  120. u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
  121. req, err := s.client.NewRequest("GET", u, nil)
  122. if err != nil {
  123. return nil, nil, err
  124. }
  125. // TODO: remove custom Accept header when this API fully launches.
  126. req.Header.Set("Accept", mediaTypeGitSigningPreview)
  127. commit := new(RepositoryCommit)
  128. resp, err := s.client.Do(ctx, req, commit)
  129. if err != nil {
  130. return nil, resp, err
  131. }
  132. return commit, resp, nil
  133. }
  134. // GetCommitRaw fetches the specified commit in raw (diff or patch) format.
  135. func (s *RepositoriesService) GetCommitRaw(ctx context.Context, owner string, repo string, sha string, opt RawOptions) (string, *Response, error) {
  136. u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
  137. req, err := s.client.NewRequest("GET", u, nil)
  138. if err != nil {
  139. return "", nil, err
  140. }
  141. switch opt.Type {
  142. case Diff:
  143. req.Header.Set("Accept", mediaTypeV3Diff)
  144. case Patch:
  145. req.Header.Set("Accept", mediaTypeV3Patch)
  146. default:
  147. return "", nil, fmt.Errorf("unsupported raw type %d", opt.Type)
  148. }
  149. var buf bytes.Buffer
  150. resp, err := s.client.Do(ctx, req, &buf)
  151. if err != nil {
  152. return "", resp, err
  153. }
  154. return buf.String(), resp, nil
  155. }
  156. // GetCommitSHA1 gets the SHA-1 of a commit reference. If a last-known SHA1 is
  157. // supplied and no new commits have occurred, a 304 Unmodified response is returned.
  158. //
  159. // GitHub API docs: https://developer.github.com/v3/repos/commits/#get-the-sha-1-of-a-commit-reference
  160. func (s *RepositoriesService) GetCommitSHA1(ctx context.Context, owner, repo, ref, lastSHA string) (string, *Response, error) {
  161. u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, ref)
  162. req, err := s.client.NewRequest("GET", u, nil)
  163. if err != nil {
  164. return "", nil, err
  165. }
  166. if lastSHA != "" {
  167. req.Header.Set("If-None-Match", `"`+lastSHA+`"`)
  168. }
  169. req.Header.Set("Accept", mediaTypeV3SHA)
  170. var buf bytes.Buffer
  171. resp, err := s.client.Do(ctx, req, &buf)
  172. if err != nil {
  173. return "", resp, err
  174. }
  175. return buf.String(), resp, nil
  176. }
  177. // CompareCommits compares a range of commits with each other.
  178. // todo: support media formats - https://github.com/google/go-github/issues/6
  179. //
  180. // GitHub API docs: https://developer.github.com/v3/repos/commits/index.html#compare-two-commits
  181. func (s *RepositoriesService) CompareCommits(ctx context.Context, owner, repo string, base, head string) (*CommitsComparison, *Response, error) {
  182. u := fmt.Sprintf("repos/%v/%v/compare/%v...%v", owner, repo, base, head)
  183. req, err := s.client.NewRequest("GET", u, nil)
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. comp := new(CommitsComparison)
  188. resp, err := s.client.Do(ctx, req, comp)
  189. if err != nil {
  190. return nil, resp, err
  191. }
  192. return comp, resp, nil
  193. }