-
Notifications
You must be signed in to change notification settings - Fork 4
/
jirafun.go
406 lines (382 loc) · 9.46 KB
/
jirafun.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"bufio"
"errors"
"fmt"
"flag"
"io"
"os"
"os/exec"
"regexp"
"sort"
"strings"
)
type Commit struct {
hash string
text string
lineno int
associatedSvnText string
status string
}
func (c *Commit) String() string {
return fmt.Sprintf("%s%s %s%s %s%s %s%s %s",
c.status, *fieldSeparator, *branchName, *fieldSeparator, c.hash,
*fieldSeparator, c.text, *fieldSeparator, c.associatedSvnText);
}
func CommitFromLine(line string, lineno int) (*Commit, error) {
if ((len(line) > 0) && (line[0] == '#')) {
// this is a comment line; ignore it
return nil, nil
}
parts := strings.SplitAfterN(line, " ", 2)
if (len(parts) < 2) {
return nil, errors.New(fmt.Sprintf("failed to find a space " +
"on line %d", lineno))
}
commit := Commit { parts[0], strings.TrimSpace(parts[1]), lineno, "", "???" }
return &commit, nil
}
func (c *Commit) Match(regex *regexp.Regexp) *string {
if (regex.NumSubexp() == 1) {
m := regex.FindStringSubmatch(c.text)
if m == nil {
return nil
}
return &m[1]
} else {
if (!regex.MatchString(c.text)) {
return nil;
}
return &c.text
}
return nil
}
func isNumeric(text string) bool {
for _, cp := range(text) {
if (cp != '0') && (cp != '1') && (cp != '2') &&
(cp != '3') && (cp != '4') && (cp != '5') &&
(cp != '6') && (cp != '7') && (cp != '8') &&
(cp != '9') {
return false;
}
}
return true
}
func ResetBranch(fileName string, status string) {
resetTo := "HEAD"
if status == "CLEAN" {
resetTo = "HEAD~"
}
err := gitCommand(fileName, false, "git", "reset", "--hard", resetTo)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
}
func (c *Commit) PopulateStatus() {
fileName := fmt.Sprintf("/tmp/jirafun.status.%d", os.Getpid());
err := gitCommand(fileName, false, "git", "checkout", *mergeBranchName)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
// fmt.Println("processing %v", c)
err = gitCommand(fileName, false, "git", "reset", "--hard")
if err != nil {
fmt.Print(err)
os.Exit(1)
}
// fmt.Println(" cherry picking %v", c.hash)
err = gitCommand(fileName, false, "git", "cherry-pick", strings.TrimSpace(c.hash))
if err == nil {
c.status = "CLEAN"
ResetBranch(fileName, c.status)
return
}
cmd := exec.Command("git", "checkout", "HEAD")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err2 := cmd.Run(); err2 != nil {
fmt.Print(err2)
os.Exit(1)
}
// use git commit to determine whether the conflict has been resolved.
err = gitCommand(fileName, false, "git", "commit", "-m", "test")
if err != nil {
c.status = "MERGE ERROR"
} else {
c.status = "CLEAN"
}
ResetBranch(fileName, c.status)
}
func (c *Commit) PopulateSvnText(jiraId string) {
words := strings.Fields(c.text)
ret := ""
prefix := ""
for idx := range(words) {
word := words[idx]
if (len(word) > 1) && (word[0] == 'r') && isNumeric(word[1:]) {
ret = ret + prefix + revisionToSvnText(word, jiraId)
prefix = ", "
} else if (len(word) > 6) && isNumeric(word) {
ret = ret + prefix + revisionToSvnText("r" + word, jiraId)
prefix = ", "
}
}
//fmt.Fprintf(os.Stderr, "%s\n", "c.associatedSvnText = \"" + ret + "\"")
if ret == "" {
ret = "(none)"
}
c.associatedSvnText = ret
}
func revisionToSvnText(rev string, jiraId string) string {
if (*associatedSvnRepo == "") {
return "(no svn repo associated)"
}
cmd := exec.Command("svn")
cmd.Args = []string { "svn", "log", "-r", rev }
cmd.Stderr = os.Stderr
cmd.Dir = *associatedSvnRepo
pipe, err := cmd.StdoutPipe(); if err != nil {
panic(err.Error())
}
scanner := bufio.NewScanner(pipe)
cmd.Start()
defer cmd.Wait()
for scanner.Scan() {
text := scanner.Text()
if (strings.Contains(text, jiraId)) {
return strings.TrimSpace(text)
}
}
fmt.Fprintf(os.Stderr, "failed to find information about %s " +
"in svn rev %s\n", jiraId, rev)
return "(no info found for " + rev + ")"
}
type RefLog struct {
commits map[string] *Commit
regex *regexp.Regexp
}
func NewRefLog(regex *regexp.Regexp) *RefLog {
rl := new(RefLog)
rl.commits = make(map[string] *Commit)
rl.regex = regex
return rl
}
func (rl *RefLog) LoadFile(name string, il *IgnoreList) error {
lineno := 1
f, err := os.Open(name)
if err != nil {
return err;
}
defer f.Close()
for fileBuf := bufio.NewReader(f); ; lineno++ {
line, err := fileBuf.ReadString('\n')
if (err != nil) {
if (err == io.EOF) {
break
} else {
return err
}
}
var commit *Commit
commit, err = CommitFromLine(line, lineno)
if (err != nil) {
return err
}
if (commit == nil) {
continue
}
key := commit.Match(rl.regex)
if (key != nil) {
_, ignored := il.ignores[strings.TrimSpace(*key)]
if (!ignored) {
rl.commits[*key] = commit
}
}
}
return nil
}
func (rl *RefLog) GetMissing(alt *RefLog) *RefLog {
missing := NewRefLog(rl.regex)
for k, c := range alt.commits {
_, present := rl.commits[k]
if (!present) {
missing.commits[k] = c
}
}
return missing
}
func (rl *RefLog) PopulateSvnText() {
for jiraId, c := range rl.commits {
c.PopulateSvnText(jiraId)
}
}
func (rl *RefLog) PopulateStatus() {
for _, c := range rl.commits {
c.PopulateStatus()
}
}
type CommitSlice []*Commit
func (arr CommitSlice) Len() int { return len(arr) }
func (arr CommitSlice) Less(i, j int) bool { return arr[i].lineno < arr[j].lineno }
func (arr CommitSlice) Swap(i, j int) { arr[i], arr[j] = arr[j], arr[i] }
func (rl *RefLog) String() string {
byLine := make(CommitSlice, len(rl.commits))
i := 0
for _, c := range rl.commits {
byLine[i] = c
i++
}
sort.Sort(byLine)
ret := ""
for i = 0; i < len(byLine); i++ {
if (rl.regex.NumSubexp() > 0) {
ret += *byLine[i].Match(rl.regex) + *fieldSeparator + " "
} else {
ret += "HDFS-????" + *fieldSeparator + " "
}
ret += byLine[i].String() + "\n"
}
return ret;
}
type IgnoreList struct {
ignores map[string] bool
}
func newIgnoreList() *IgnoreList {
il := new(IgnoreList)
il.ignores = make(map[string] bool)
return il
}
func (il *IgnoreList) ReadIgnoreFile(fileName string,
regex *regexp.Regexp) error {
f, err := os.Open(fileName)
if (err != nil) {
return err
}
defer f.Close()
lineno := 1
for fileBuf := bufio.NewReader(f); ; lineno++ {
line, err := fileBuf.ReadString('\n')
if (err != nil) {
if (err == io.EOF) {
break
} else {
return err
}
}
var commit *Commit
commit, err = CommitFromLine(line, lineno)
if (err != nil) {
if (regex.MatchString(line)) {
il.ignores[strings.TrimSpace(line)] = true
continue
}
return err
} else if (commit == nil) {
continue
}
key := commit.Match(regex)
if (key != nil) {
il.ignores[strings.TrimSpace(*key)] = true
}
}
fmt.Fprintf(os.Stderr, "Ignoring %d jiras\n", len(il.ignores));
return nil
}
func gitCommand(outFile string, printErr bool, args ...string) error {
f, err := os.Create(outFile)
if err != nil {
return err;
}
defer f.Close()
cmd := exec.Command(args[0])
cmd.Args = args
if (printErr) {
cmd.Stderr = os.Stderr
} else {
cmd.Stderr = nil
}
cmd.Stdout = f
if err2 := cmd.Run(); err2 != nil {
return err2
}
return nil
}
var branchName *string = flag.String("b", "", "the branch to look for " +
"commits in")
var regexStr *string = flag.String("r", "(HDFS-[0123456789]*)[^0123456789]",
"the regular expression to use to determine which commits to examine")
var ignoreFile *string = flag.String("i", "",
"a file containing a newline-separated list of JIRAs to ignore. If you " +
"are using a regex with a backrefernece, each line should contain the " +
"contents")
var associatedSvnRepo *string = flag.String("s", "",
"optional local directory with an associated subversion repository. " +
"This will be used to make 'merging change rXYZ messages' more helpful.")
var fieldSeparator *string = flag.String("f", "❆", "field separator to use.")
var mergeBranchName *string = flag.String("m", "", "the branch to merge")
func main() {
flag.Parse()
regex, err := regexp.Compile(*regexStr)
if (err != nil) {
fmt.Printf("Error compiling regular expression \"%s\":\n" +
" %s\n", *regexStr, err)
os.Exit(1)
}
if (regex.NumSubexp() > 1) {
fmt.Printf("Can't handle more than one subexpression in the regular " +
" expression \"%s\"\n", *regexStr, err)
os.Exit(1)
}
if (*branchName == "") {
fmt.Printf("You must specify a branch to compare against using -b.\n")
os.Exit(1)
}
il := newIgnoreList()
if (*ignoreFile != "") {
err = il.ReadIgnoreFile(*ignoreFile, regex)
if (err != nil) {
fmt.Printf("error reading ignore file: %s\n", err)
os.Exit(1)
}
}
fileNames := []string {
fmt.Sprintf("/tmp/jirafun.1.%d", os.Getpid()),
fmt.Sprintf("/tmp/jirafun.2.%d", os.Getpid()) }
defer os.Remove(fileNames[0])
defer os.Remove(fileNames[1])
err = gitCommand(fileNames[1], true, "git", "rev-list",
"--pretty=oneline", *branchName)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
err = gitCommand(fileNames[0], true, "git", "rev-list",
"--pretty=oneline", "HEAD")
if err != nil {
fmt.Print(err)
os.Exit(1)
}
refLogs := []*RefLog { NewRefLog(regex), NewRefLog(regex) }
err = refLogs[0].LoadFile(fileNames[0], il)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
err = refLogs[1].LoadFile(fileNames[1], il)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
missing := refLogs[0].GetMissing(refLogs[1])
missing.PopulateSvnText()
if (*mergeBranchName != "") {
missing.PopulateStatus()
}
fmt.Printf("JIRA%s status%s branch%s git hash%s commit text%s" +
"svn auxillary text\n",
*fieldSeparator, *fieldSeparator, *fieldSeparator,
*fieldSeparator, *fieldSeparator)
fmt.Print(missing.String())
}