|
| 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 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "net/url" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// RepositoryCommit represents a commit in a repo. |
| 15 | +// Note that it's wrapping a Commit, so author/committer information is in two places, |
| 16 | +// but contain different details about them: in RepositoryCommit "github details", in Commit - "git details". |
| 17 | +type RepositoryCommit struct { |
| 18 | + SHA *string `json:"sha,omitempty"` |
| 19 | + Commit *Commit `json:"commit,omitempty"` |
| 20 | + Author *User `json:"author,omitempty"` |
| 21 | + Committer *User `json:"committer,omitempty"` |
| 22 | + Parents []Commit `json:"parents,omitempty"` |
| 23 | + Message *string `json:"message,omitempty"` |
| 24 | + |
| 25 | + // Details about how many changes were made in this commit. Only filled in during GetCommit! |
| 26 | + Stats *CommitStats `json:"stats,omitempty"` |
| 27 | + // Details about which files, and how this commit touched. Only filled in during GetCommit! |
| 28 | + Files []CommitFile `json:"files,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +func (r RepositoryCommit) String() string { |
| 32 | + return Stringify(r) |
| 33 | +} |
| 34 | + |
| 35 | +// CommitStats represents the number of additions / deletions from a file in a given RepositoryCommit. |
| 36 | +type CommitStats struct { |
| 37 | + Additions *int `json:"additions,omitempty"` |
| 38 | + Deletions *int `json:"deletions,omitempty"` |
| 39 | + Total *int `json:"total,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +func (c CommitStats) String() string { |
| 43 | + return Stringify(c) |
| 44 | +} |
| 45 | + |
| 46 | +// CommitFile represents a file modified in a commit. |
| 47 | +type CommitFile struct { |
| 48 | + SHA *string `json:"sha,omitempty"` |
| 49 | + Filename *string `json:"filename,omitempty"` |
| 50 | + Additions *int `json:"additions,omitempty"` |
| 51 | + Deletions *int `json:"deletions,omitempty"` |
| 52 | + Changes *int `json:"changes,omitempty"` |
| 53 | + Status *string `json:"status,omitempty"` |
| 54 | + Patch *string `json:"patch,omitempty"` |
| 55 | +} |
| 56 | + |
| 57 | +func (c CommitFile) String() string { |
| 58 | + return Stringify(c) |
| 59 | +} |
| 60 | + |
| 61 | +// CommitsComparison is the result of comparing two commits. |
| 62 | +// See CompareCommits() for details. |
| 63 | +type CommitsComparison struct { |
| 64 | + BaseCommit *RepositoryCommit `json:"base_commit,omitempty"` |
| 65 | + |
| 66 | + // Head can be 'behind' or 'ahead' |
| 67 | + Status *string `json:"status,omitempty"` |
| 68 | + AheadBy *int `json:"ahead_by,omitempty"` |
| 69 | + BehindBy *int `json:"behind_by,omitempty"` |
| 70 | + TotalCommits *int `json:"total_commits,omitempty"` |
| 71 | + |
| 72 | + Commits []RepositoryCommit `json:"commits,omitempty"` |
| 73 | + |
| 74 | + Files []CommitFile `json:"files,omitempty"` |
| 75 | +} |
| 76 | + |
| 77 | +func (c CommitsComparison) String() string { |
| 78 | + return Stringify(c) |
| 79 | +} |
| 80 | + |
| 81 | +// CommitsListOptions specifies the optional parameters to the |
| 82 | +// RepositoriesService.ListCommits method. |
| 83 | +type CommitsListOptions struct { |
| 84 | + // SHA or branch to start listing Commits from. |
| 85 | + SHA string |
| 86 | + // Path that should be touched by the returned Commits. |
| 87 | + Path string |
| 88 | + // Author of by which to filter Commits. |
| 89 | + Author string |
| 90 | + // Since when should Commits be included in the response. |
| 91 | + Since time.Time |
| 92 | + // Until when should Commits be included in the response. |
| 93 | + Until time.Time |
| 94 | +} |
| 95 | + |
| 96 | +// ListCommits lists the commits of a repository. |
| 97 | +// |
| 98 | +// GitHub API docs: http://developer.github.com/v3/repos/commits/#list |
| 99 | +func (s *RepositoriesService) ListCommits(owner, repo string, opts *CommitsListOptions) ([]RepositoryCommit, *Response, error) { |
| 100 | + u := fmt.Sprintf("repos/%v/%v/commits", owner, repo) |
| 101 | + |
| 102 | + if opts != nil { |
| 103 | + params := url.Values{ |
| 104 | + "sha": {opts.SHA}, |
| 105 | + "path": {opts.Path}, |
| 106 | + "author": {opts.Author}, |
| 107 | + } |
| 108 | + if !opts.Since.IsZero() { |
| 109 | + params.Add("since", opts.Since.Format(time.RFC3339)) |
| 110 | + } |
| 111 | + if !opts.Until.IsZero() { |
| 112 | + params.Add("until", opts.Until.Format(time.RFC3339)) |
| 113 | + } |
| 114 | + |
| 115 | + u += "?" + params.Encode() |
| 116 | + } |
| 117 | + |
| 118 | + req, err := s.client.NewRequest("GET", u, nil) |
| 119 | + if err != nil { |
| 120 | + return nil, nil, err |
| 121 | + } |
| 122 | + |
| 123 | + commits := new([]RepositoryCommit) |
| 124 | + resp, err := s.client.Do(req, commits) |
| 125 | + return *commits, resp, err |
| 126 | +} |
| 127 | + |
| 128 | +// GetCommit fetches the specified commit, including all details about it. |
| 129 | +// todo: support media formats - https://github.com/google/go-github/issues/6 |
| 130 | +// |
| 131 | +// GitHub API docs: http://developer.github.com/v3/repos/commits/#get-a-single-commit |
| 132 | +// See also: http://developer.github.com//v3/git/commits/#get-a-single-commit provides the same functionality |
| 133 | +func (s *RepositoriesService) GetCommit(owner, repo, sha string) (*RepositoryCommit, *Response, error) { |
| 134 | + u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha) |
| 135 | + |
| 136 | + req, err := s.client.NewRequest("GET", u, nil) |
| 137 | + if err != nil { |
| 138 | + return nil, nil, err |
| 139 | + } |
| 140 | + |
| 141 | + commit := new(RepositoryCommit) |
| 142 | + resp, err := s.client.Do(req, commit) |
| 143 | + return commit, resp, err |
| 144 | +} |
| 145 | + |
| 146 | +// CompareCommits compares a range of commits with each other. |
| 147 | +// todo: support media formats - https://github.com/google/go-github/issues/6 |
| 148 | +// |
| 149 | +// GitHub API docs: http://developer.github.com/v3/repos/commits/index.html#compare-two-commits |
| 150 | +func (s *RepositoriesService) CompareCommits(owner, repo string, base, head string) (*CommitsComparison, *Response, error) { |
| 151 | + u := fmt.Sprintf("repos/%v/%v/compare/%v...%v", owner, repo, base, head) |
| 152 | + |
| 153 | + req, err := s.client.NewRequest("GET", u, nil) |
| 154 | + if err != nil { |
| 155 | + return nil, nil, err |
| 156 | + } |
| 157 | + |
| 158 | + comp := new(CommitsComparison) |
| 159 | + resp, err := s.client.Do(req, comp) |
| 160 | + return comp, resp, err |
| 161 | +} |
0 commit comments