This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.go
279 lines (247 loc) · 6.98 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package util
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)
var UpstreamSummaryPattern = regexp.MustCompile(`UPSTREAM: (revert: [a-f0-9]{7,}: )?(([\w\.-]+\/[\w-\.-]+)?: )?(\d+:|<carry>:|<drop>:)`)
// supportedHosts maps source hosts to the number of path segments that
// represent the account/repo for that host. This is necessary because we
// can't tell just by looking at an import path whether the repo is identified
// by the first 2 or 3 path segments.
//
// If dependencies are introduced from new hosts, they'll need to be added
// here.
var SupportedHosts = map[string]int{
"bitbucket.org": 3,
"code.google.com": 3,
"github.com": 3,
"golang.org": 3,
"google.golang.org": 2,
"gopkg.in": 2,
"k8s.io": 2,
"speter.net": 2,
}
type Commit struct {
Sha string
Summary string
Files []File
}
func (c Commit) DeclaresUpstreamChange() bool {
return strings.HasPrefix(strings.ToLower(c.Summary), "upstream")
}
func (c Commit) MatchesUpstreamSummaryPattern() bool {
return UpstreamSummaryPattern.MatchString(c.Summary)
}
func (c Commit) DeclaredUpstreamRepo() (string, error) {
if !c.DeclaresUpstreamChange() {
return "", fmt.Errorf("commit declares no upstream changes")
}
if !c.MatchesUpstreamSummaryPattern() {
return "", fmt.Errorf("commit doesn't match the upstream commit summary pattern")
}
groups := UpstreamSummaryPattern.FindStringSubmatch(c.Summary)
repo := groups[3]
if len(repo) == 0 {
repo = "k8s.io/kubernetes"
}
return repo, nil
}
func (c Commit) HasGodepsChanges() bool {
for _, file := range c.Files {
if file.HasGodepsChanges() {
return true
}
}
return false
}
func (c Commit) HasNonGodepsChanges() bool {
for _, file := range c.Files {
if !file.HasGodepsChanges() {
return true
}
}
return false
}
func (c Commit) GodepsReposChanged() ([]string, error) {
repos := map[string]struct{}{}
for _, file := range c.Files {
if !file.HasGodepsChanges() {
continue
}
repo, err := file.GodepsRepoChanged()
if err != nil {
return nil, fmt.Errorf("problem with file %q in commit %s: %s", file, c.Sha, err)
}
repos[repo] = struct{}{}
}
changed := []string{}
for repo := range repos {
changed = append(changed, repo)
}
return changed, nil
}
type File string
func (f File) HasGodepsChanges() bool {
return strings.HasPrefix(string(f), "Godeps")
}
func (f File) GodepsRepoChanged() (string, error) {
if !f.HasGodepsChanges() {
return "", fmt.Errorf("file doesn't appear to be a Godeps change")
}
// Find the _workspace path segment index.
workspaceIdx := -1
parts := strings.Split(string(f), string(os.PathSeparator))
for i, part := range parts {
if part == "_workspace" {
workspaceIdx = i
break
}
}
// Godeps path struture assumption: Godeps/_workspace/src/...
if workspaceIdx == -1 || len(parts) < (workspaceIdx+3) {
return "", fmt.Errorf("file doesn't appear to be a Godeps workspace path")
}
// Deal with repos which could be identified by either 2 or 3 path segments.
host := parts[workspaceIdx+2]
segments := -1
for supportedHost, count := range SupportedHosts {
if host == supportedHost {
segments = count
break
}
}
if segments == -1 {
return "", fmt.Errorf("file modifies an unsupported repo host %q", host)
}
switch segments {
case 2:
return fmt.Sprintf("%s/%s", host, parts[workspaceIdx+3]), nil
case 3:
return fmt.Sprintf("%s/%s/%s", host, parts[workspaceIdx+3], parts[workspaceIdx+4]), nil
}
return "", fmt.Errorf("file modifies an unsupported repo host %q", host)
}
func CommitsBetween(a, b string) ([]Commit, error) {
commits := []Commit{}
stdout, _, err := run("git", "log", "--oneline", fmt.Sprintf("%s..%s", a, b))
if err != nil {
return nil, fmt.Errorf("error executing git log: %s", err)
}
for _, log := range strings.Split(stdout, "\n") {
if len(log) == 0 {
continue
}
commit, err := NewCommitFromOnelineLog(log)
if err != nil {
return nil, err
}
commits = append(commits, commit)
}
return commits, nil
}
func NewCommitFromOnelineLog(log string) (Commit, error) {
var commit Commit
parts := strings.Split(log, " ")
if len(parts) < 2 {
return commit, fmt.Errorf("invalid log entry: %s", log)
}
commit.Sha = parts[0]
commit.Summary = strings.Join(parts[1:], " ")
files, err := filesInCommit(commit.Sha)
if err != nil {
return commit, err
}
commit.Files = files
return commit, nil
}
func IsAncestor(commit1, commit2, repoDir string) (bool, error) {
cwd, err := os.Getwd()
if err != nil {
return false, err
}
defer os.Chdir(cwd)
if err := os.Chdir(repoDir); err != nil {
return false, err
}
if stdout, stderr, err := run("git", "fetch", "origin"); err != nil {
return false, fmt.Errorf("out=%s, err=%s, %s", strings.TrimSpace(stdout), strings.TrimSpace(stderr), err)
}
if stdout, stderr, err := run("git", "merge-base", "--is-ancestor", commit1, commit2); err != nil {
return false, fmt.Errorf("out=%s, err=%s, %s", strings.TrimSpace(stdout), strings.TrimSpace(stderr), err)
}
return true, nil
}
func CommitDate(commit, repoDir string) (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
defer os.Chdir(cwd)
if err := os.Chdir(repoDir); err != nil {
return "", err
}
if stdout, stderr, err := run("git", "fetch", "origin"); err != nil {
return "", fmt.Errorf("out=%s, err=%s, %s", strings.TrimSpace(stdout), strings.TrimSpace(stderr), err)
}
if stdout, stderr, err := run("git", "show", "-s", "--format=%ci", commit); err != nil {
return "", fmt.Errorf("out=%s, err=%s, %s", strings.TrimSpace(stdout), strings.TrimSpace(stderr), err)
} else {
return strings.TrimSpace(stdout), nil
}
}
func Checkout(commit, repoDir string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
defer os.Chdir(cwd)
if err := os.Chdir(repoDir); err != nil {
return err
}
if stdout, stderr, err := run("git", "checkout", commit); err != nil {
return fmt.Errorf("out=%s, err=%s, %s", strings.TrimSpace(stdout), strings.TrimSpace(stderr), err)
}
return nil
}
func CurrentRev(repoDir string) (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
defer os.Chdir(cwd)
if err := os.Chdir(repoDir); err != nil {
return "", err
}
if stdout, stderr, err := run("git", "rev-parse", "HEAD"); err != nil {
return "", fmt.Errorf("out=%s, err=%s, %s", strings.TrimSpace(stdout), strings.TrimSpace(stderr), err)
} else {
return strings.TrimSpace(stdout), nil
}
}
func filesInCommit(sha string) ([]File, error) {
files := []File{}
stdout, _, err := run("git", "diff-tree", "--no-commit-id", "--name-only", "-r", sha)
if err != nil {
return nil, err
}
for _, filename := range strings.Split(stdout, "\n") {
if len(filename) == 0 {
continue
}
files = append(files, File(filename))
}
return files, nil
}
func run(args ...string) (string, string, error) {
cmd := exec.Command(args[0], args[1:]...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}