This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
242 lines (221 loc) · 5.39 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/tabwriter"
"time"
)
var (
// ErrNoSVN is given if svn is not in $PATH
ErrNoSVN = errors.New("svn not present in $PATH")
flagVersion bool
flagUser string
flagSearch string
)
const constVersion string = "0.0.8"
const constSVNMessageLimit = 120
const constSVNSepartatorLine = "------------------------------------------------------------------------"
const constSVNCommitLineRegex = `^r(\d*)\s\|\s([^\|]*)\s\|\s([^\|]*)\|\s(.*)$`
const constTicketRegex = `([A-Z]+-[0-9]+)`
type svnCommit struct {
revision string
author string
date string
msg string
}
func stringInSlice(hey []string, needle string) bool {
for _, item := range hey {
if item == needle {
return true
}
}
return false
}
func (commit svnCommit) matchRevision(hay []string) bool {
return stringInSlice(hay, commit.revision)
}
func (commit svnCommit) matchTicket(hay []string) bool {
if len(hay) == 0 {
return false
}
rex, _ := regexp.Compile(constTicketRegex)
for _, item := range rex.FindStringSubmatch(commit.msg) {
if stringInSlice(hay, item) {
return true
}
}
return false
}
func (commit svnCommit) CherryPick(source string) error {
fmt.Printf("Cherrypicking r%s from %s...\n", commit.revision, source)
cmd := exec.Command(
"svn", "merge", "-c", commit.revision, source,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func getEligibleCommits(source string) ([]svnCommit, error) {
_, err := exec.LookPath("svn")
if err != nil {
return nil, ErrNoSVN
}
content, err := exec.Command(
"svn", "mergeinfo", "--show-revs", "eligible", "--log", source, ".",
).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("%s", content)
}
return parseMergeInfoLog(content)
}
func parseMergeInfoLog(content []byte) ([]svnCommit, error) {
var (
res = []svnCommit{}
commit svnCommit
line string
)
rex, _ := regexp.Compile(constSVNCommitLineRegex)
lines := strings.Split(string(content), "\n")
for len(lines) != 0 {
lines, line = lines[1:], lines[0]
if line == constSVNSepartatorLine {
lines, line = lines[1:], lines[0]
resultSlice := rex.FindStringSubmatch(line)
if len(resultSlice) == 5 {
commit = svnCommit{}
commit.revision = resultSlice[1]
commit.author = resultSlice[2]
commit.date = textToLocalTimeText(resultSlice[3][:25])
// skip blank line, take first comment line
lines, line = lines[2:], lines[1]
commit.msg = line
if len(commit.msg) > constSVNMessageLimit {
commit.msg = commit.msg[:constSVNMessageLimit-3] + "..."
}
res = append(res, commit)
}
}
}
return res, nil
}
func textToLocalTimeText(text string) string {
result, err := time.Parse("2006-01-02 15:04:05 -0700", text)
if err != nil {
log.Fatalf("error parsing date %s: %v", text, err)
}
return result.Local().Format("2006-01-02 15:04:05")
}
func parseArgs(args []string) (string, []string, []string) {
var (
filterCommit []string
filterTicket []string
arg string
)
if len(args) > 1 {
for i := 1; i < len(args); i++ {
arg = args[i]
_, err := strconv.Atoi(arg)
if err == nil {
filterCommit = append(filterCommit, arg)
} else {
filterTicket = append(filterTicket, arg)
}
}
}
sort.Slice(filterCommit, func(i, j int) bool {
i, _ = strconv.Atoi(filterCommit[i])
j, _ = strconv.Atoi(filterCommit[j])
return i < j
})
source := flag.Arg(0)
if !strings.HasPrefix(source, "^/") {
if source != "trunk" && !strings.HasPrefix(source, "branches/") {
source = "branches/" + source
}
source = "^/" + source
}
return source, filterCommit, filterTicket
}
func printUsage() {
fmt.Fprint(os.Stderr, fmt.Sprintf(
"Usage: %s [options] <source-path/branch-name/trunk> [revision-numbers and/or ticket-numbers]\n\n",
filepath.Base(os.Args[0]),
))
fmt.Println("Options:")
flag.PrintDefaults()
os.Exit(1)
}
func main() {
flag.BoolVar(&flagVersion, "V", false, "Print version")
flag.StringVar(&flagUser, "u", "", "filter by username")
flag.StringVar(&flagSearch, "s", "", "filter by comment")
flag.Usage = printUsage
flag.Parse()
if flagVersion {
fmt.Printf("%s %v\n", filepath.Base(os.Args[0]), constVersion)
os.Exit(0)
}
if flagSearch != "" {
flagSearch = strings.ToLower(flagSearch)
}
var args []string
args = flag.Args()
if len(args) == 0 {
flag.Usage()
}
source, filterCommit, filterTicket := parseArgs(args)
commits, err := getEligibleCommits(source)
if err != nil {
log.Fatalf("Error getting eligible commits: %s\n", err)
}
w := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
line := "%s\t%s\t%s\t%s"
firstRow := false
for _, commit := range commits {
if len(filterTicket) > 0 && !commit.matchTicket(filterTicket) {
continue
}
if flagUser != "" && commit.author != flagUser {
continue
}
if flagSearch != "" && strings.Index(strings.ToLower(commit.msg), flagSearch) == -1 {
continue
}
if len(filterCommit) > 0 {
if commit.matchRevision(filterCommit) {
err := commit.CherryPick(source)
if err != nil {
log.Fatal(err)
}
}
} else {
if firstRow == false {
firstRow = true
fmt.Fprintln(w, fmt.Sprintf(
line,
"Revision",
"Author",
"Date",
"Message",
))
}
fmt.Fprintln(w, fmt.Sprintf(
line,
commit.revision,
commit.author,
commit.date,
commit.msg,
))
}
}
w.Flush()
}