-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (146 loc) · 3.48 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
package main
import (
"encoding/xml"
"flag"
"fmt"
"log"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"text/tabwriter"
"time"
)
var (
flags struct {
limit int
revision string
}
)
type typeXMLInfo struct {
RelativeURL string `xml:"entry>relative-url"`
}
type typePath struct {
Path string `xml:",chardata"`
Action string `xml:"action,attr"`
}
type typeXMLLog struct {
Entries []struct {
Revision string `xml:"revision,attr"`
Author string `xml:"author"`
Date string `xml:"date"`
Message string `xml:"msg"`
Paths []typePath `xml:"paths>path"`
} `xml:"logentry"`
}
type typeCommitPath struct {
Revision string
Author string
Date string
Message string
Path string
Action string
}
func getRelativeURL(content []byte) string {
res := typeXMLInfo{}
err := xml.Unmarshal([]byte(content), &res)
if err != nil {
return ""
}
return strings.TrimLeft(res.RelativeURL, "^")
}
func inSSlice(hay []typeCommitPath, needle string) bool {
for _, item := range hay {
if item.Path == needle {
return true
}
}
return false
}
func getXMLLog(relativeURL string, content []byte) []typeCommitPath {
logs := typeXMLLog{}
err := xml.Unmarshal([]byte(content), &logs)
if err != nil {
panic(err)
}
var (
res []typeCommitPath
)
entries := logs.Entries
sort.Slice(entries, func(i, j int) bool {
i, _ = strconv.Atoi(entries[i].Revision)
j, _ = strconv.Atoi(entries[j].Revision)
return i > j
})
for _, entry := range entries {
for _, path := range entry.Paths {
if !strings.HasPrefix(path.Path, relativeURL) {
continue
} else if path.Path[len(relativeURL):] == "" {
continue
}
path.Path = strings.TrimLeft(path.Path[len(relativeURL):], "/")
if !inSSlice(res, path.Path) {
res = append(res, typeCommitPath{
Revision: entry.Revision,
Author: entry.Author,
Date: textToLocalTimeText(entry.Date),
Message: entry.Message,
Path: path.Path,
Action: path.Action,
})
}
}
}
sort.Slice(res, func(i, j int) bool {
i, _ = strconv.Atoi(res[i].Revision)
j, _ = strconv.Atoi(res[j].Revision)
return i < j
})
return res
}
func textToLocalTimeText(text string) string {
result, err := time.Parse("2006-01-02T15:04:05.000000Z", text)
if err != nil {
log.Fatalf("error parsing date %s: %v", text, err)
}
return result.Local().Format("2006-01-02 15:04:05")
}
func main() {
flag.IntVar(&flags.limit, "l", 100, "How many last commits to check")
flag.StringVar(&flags.revision, "r", "", "Revision(s) (or range with NUMBER/DATE/HEAD/etc))")
flag.Parse()
var (
args []string
path string
)
args = flag.Args()
if len(args) == 0 {
path, _ = os.Getwd()
} else {
path = args[0]
}
infoContent, err := exec.Command("svn", "info", "--xml", path).CombinedOutput()
if err != nil {
fmt.Printf("%s\n", infoContent)
log.Fatal(err)
}
relativeURL := getRelativeURL(infoContent)
cmdargs := []string{"log", "-v", "--xml"}
if flags.revision != "" {
cmdargs = append(cmdargs, []string{"-r", flags.revision}...)
} else {
cmdargs = append(cmdargs, []string{"-l", fmt.Sprintf("%d", flags.limit)}...)
}
cmdargs = append(cmdargs, path)
logContent, err := exec.Command("svn", cmdargs...).Output()
if err != nil {
log.Fatal(err)
}
w := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
for _, entry := range getXMLLog(relativeURL, logContent) {
fmt.Fprintln(w, fmt.Sprintf("%s\t%s\t%s\t%s\t%s", entry.Date, entry.Revision, entry.Author, entry.Action, entry.Path))
}
w.Flush()
}