/
rebaser.go
286 lines (232 loc) · 8.73 KB
/
rebaser.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
280
281
282
283
284
285
286
package command
import (
"errors"
"fmt"
"io"
"os/exec"
"strings"
"syscall"
"github.com/guywithnose/runner"
"github.com/urfave/cli"
)
type rebaser struct {
errorWriter io.Writer
verboseWriter io.Writer
cmdWrapper runner.Builder
}
func newRebaser(errorWriter, verboseWriter io.Writer, cmdWrapper runner.Builder) *rebaser {
return &rebaser{
errorWriter: errorWriter,
verboseWriter: verboseWriter,
cmdWrapper: cmdWrapper,
}
}
func (r rebaser) rebasePullRequests(pullRequests <-chan *pullRequest, pullRequestNumber int) error {
var completeError error
for pullRequest := range pullRequests {
if pullRequestNumber != 0 && pullRequest.PullRequestID != pullRequestNumber {
continue
}
err := r.rebasePullRequest(pullRequest)
if err != nil {
fmt.Fprintf(r.errorWriter, "Could not rebase PR #%d in %s/%s because: %v\n", pullRequest.PullRequestID, pullRequest.Repo.Owner, pullRequest.Repo.Name, err)
completeError = cli.NewExitError("Unable to rebase all pull requests", 1)
}
}
return completeError
}
func (r rebaser) rebasePullRequest(pr *pullRequest) error {
path, ownedRemote, upstreamRemote, localChanges, err := r.getRepoData(pr)
if err != nil {
return err
}
if localChanges {
fmt.Fprintln(r.verboseWriter, "Local changes found... stashing")
err = r.runCommand(path, "git", "stash")
if err != nil {
return wrapExitError(err, fmt.Sprintf("Unable to stash changes in %s", path))
}
defer r.popStash(path)
}
currentBranchName, tempBranch, err := r.checkoutTempBranch(path, pr.Branch)
if err != nil {
return err
}
defer r.cleanUp(currentBranchName, tempBranch, path)
return r.doRebase(path, ownedRemote, upstreamRemote, tempBranch, pr)
}
func (r rebaser) doRebase(path, ownedRemote, upstreamRemote, tempBranch string, pr *pullRequest) error {
myRemoteBranch := fmt.Sprintf("%s/%s", ownedRemote, pr.Branch)
fmt.Fprintf(r.verboseWriter, "Resetting code to %s\n", myRemoteBranch)
err := r.runCommand(path, "git", "reset", "--hard", myRemoteBranch)
if err != nil {
return wrapExitError(err, fmt.Sprintf("Unable to reset the code to %s", myRemoteBranch))
}
upstreamBranch := fmt.Sprintf("%s/%s", upstreamRemote, pr.TargetBranch)
fmt.Fprintf(r.verboseWriter, "Rebasing against %s\n", upstreamBranch)
err = r.runCommand(path, "git", "rebase", upstreamBranch)
if err != nil {
abortErr := r.runCommand(path, "git", "rebase", "--abort")
if abortErr != nil {
fmt.Fprintf(r.errorWriter, "Could not abort rebase PR #%d in %s/%s because: %v\n", pr.PullRequestID, pr.Repo.Owner, pr.Repo.Name, abortErr)
}
return wrapExitError(err, fmt.Sprintf("Unable to rebase against %s, there may be a conflict", upstreamBranch))
}
fmt.Fprintf(r.verboseWriter, "Pushing to %s\n", myRemoteBranch)
err = r.runCommand(path, "git", "push", ownedRemote, fmt.Sprintf("%s:%s", tempBranch, pr.Branch), "--force")
if err != nil {
return wrapExitError(err, fmt.Sprintf("Unable to push to %s", myRemoteBranch))
}
return nil
}
func (r rebaser) detectLocalChanges(path string) (bool, error) {
localChangesCommand := r.cmdWrapper.New(path, "git", "diff-index", "--quiet", "HEAD")
_, err := localChangesCommand.Output()
if err != nil {
code := getErrorCode(err)
if code == 1 {
return true, nil
}
return true, err
}
return false, nil
}
func (r rebaser) getRepoData(pr *pullRequest) (string, string, string, bool, error) {
fmt.Fprintln(r.verboseWriter, "Requesting repo data from config")
err := pr.checkLocalPath()
if err != nil {
return "", "", "", false, err
}
fmt.Fprintln(r.verboseWriter, "Analyzing remotes")
ownedRemote, upstreamRemote, err := r.getRemotes(pr.Repo.LocalPath, pr)
if err != nil {
return "", "", "", false, err
}
fmt.Fprintln(r.verboseWriter, "Checking for local changes")
localChanges, err := r.detectLocalChanges(pr.Repo.LocalPath)
if err != nil {
return "", "", "", false, wrapExitError(err, fmt.Sprintf("Unable to detect local changes in %s", pr.Repo.LocalPath))
}
err = r.fetchRemotes(pr.Repo.LocalPath, ownedRemote, upstreamRemote)
if err != nil {
return "", "", "", false, err
}
return pr.Repo.LocalPath, ownedRemote, upstreamRemote, localChanges, nil
}
func (r rebaser) cleanUp(currentBranchName, tempBranch, path string) {
fmt.Fprintf(r.verboseWriter, "Going back to branch %s\n", currentBranchName)
err := r.runCommand(path, "git", "checkout", currentBranchName)
if err != nil {
fmt.Fprintf(r.errorWriter, "%v\nWarning: Could not go back to branch %s in %s\n", wrapExitError(err, ""), currentBranchName, path)
return
}
fmt.Fprintf(r.verboseWriter, "Deleting temporary branch %s\n", tempBranch)
err = r.runCommand(path, "git", "branch", "-D", tempBranch)
if err != nil {
fmt.Fprintf(r.errorWriter, "%v\nWarning: Could not delete temporary branch %s in %s\n", wrapExitError(err, ""), tempBranch, path)
}
}
func (r rebaser) popStash(path string) {
fmt.Fprintln(r.verboseWriter, "Popping the stash")
err := r.runCommand(path, "git", "stash", "pop")
if err != nil {
fmt.Fprintf(r.errorWriter, "%v\nWarning: Could not pop stash in %s\n", wrapExitError(err, ""), path)
}
}
func (r rebaser) runCommand(path string, command ...string) error {
cmd := r.cmdWrapper.New(path, command...)
_, err := cmd.Output()
return err
}
func wrapExitError(err error, extra string) error {
if exitErr, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("%s\n%s", extra, string(exitErr.Stderr))
}
return errors.New(extra)
}
func (r rebaser) checkoutTempBranch(path, branch string) (string, string, error) {
fmt.Fprintln(r.verboseWriter, "Saving current branch name")
currentBranchName, err := r.getCurrentBranch(path)
if err != nil {
return "", "", fmt.Errorf("Unable to get current branch name in %s\n%v", path, err)
}
fmt.Fprintf(r.verboseWriter, "Current branch name is %s\n", currentBranchName)
tempBranch := fmt.Sprintf("prp-%s", branch)
fmt.Fprintf(r.verboseWriter, "Checking out temporary branch: %s\n", tempBranch)
err = r.runCommand(path, "git", "checkout", "-b", tempBranch)
if err != nil {
code := getErrorCode(err)
if code == 128 {
return "", "", wrapExitError(err, fmt.Sprintf("Branch %s already exists", tempBranch))
}
return "", "", wrapExitError(err, fmt.Sprintf("Unable to checkout temporary branch %s in %s", tempBranch, path))
}
return currentBranchName, tempBranch, nil
}
func (r rebaser) getCurrentBranch(path string) (string, error) {
getCurrentBranch := r.cmdWrapper.New(path, "git", "symbolic-ref", "HEAD")
currentBranchOutput, err := getCurrentBranch.CombinedOutput()
if err != nil {
code := getErrorCode(err)
if code == 128 {
getCurrentBranch = r.cmdWrapper.New(path, "git", "rev-parse", "HEAD")
currentBranchOutput, err = getCurrentBranch.CombinedOutput()
if err != nil {
return "", fmt.Errorf("No branch checked out in %s\n%s", path, string(currentBranchOutput))
}
return strings.Replace(strings.Replace(string(currentBranchOutput), "refs/heads/", "", -1), "\n", "", -1), nil
}
return "", fmt.Errorf("%s\n%s", err, string(currentBranchOutput))
}
return strings.Replace(strings.Replace(string(currentBranchOutput), "refs/heads/", "", -1), "\n", "", -1), nil
}
func (r rebaser) fetchRemotes(path, ownedRemote, upstreamRemote string) error {
err := r.fetchRemote(path, ownedRemote)
if err != nil {
return err
}
return r.fetchRemote(path, upstreamRemote)
}
func (r rebaser) fetchRemote(path, remoteName string) error {
fmt.Fprintf(r.verboseWriter, "Fetching from remote: %s\n", remoteName)
err := r.runCommand(path, "git", "fetch", remoteName)
if err != nil {
return wrapExitError(err, fmt.Sprintf("Unable to fetch code from %s", remoteName))
}
return nil
}
func (r rebaser) getRemotes(path string, pr *pullRequest) (string, string, error) {
getRemotes := r.cmdWrapper.New(path, "git", "remote", "-v")
remotesOutput, err := getRemotes.CombinedOutput()
if err != nil {
return "", "", fmt.Errorf("Unable to analyze remotes in %s\n%s", path, string(remotesOutput))
}
remotes := parseRemotes(strings.Split(string(remotesOutput), "\n"))
ownedRemote, ok := remotes[fmt.Sprintf("%s (push)", pr.HeadSSHURL)]
if !ok {
return "", "", fmt.Errorf("No remote exists in %s that points to %s", path, pr.HeadSSHURL)
}
upstreamRemote, ok := remotes[fmt.Sprintf("%s (fetch)", pr.BaseSSHURL)]
if !ok {
return "", "", fmt.Errorf("No remote exists in %s that points to %s", path, pr.BaseSSHURL)
}
return ownedRemote, upstreamRemote, nil
}
func parseRemotes(lines []string) map[string]string {
remotes := make(map[string]string)
for _, line := range lines {
parts := strings.Split(line, "\t")
if len(parts) == 2 {
remotes[parts[1]] = parts[0]
}
}
return remotes
}
func getErrorCode(err error) int {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus()
}
}
return -1
}