/
rcs_svn.go
142 lines (127 loc) · 2.93 KB
/
rcs_svn.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
package main
import (
"bytes"
"encoding/xml"
"errors"
"os/exec"
"regexp"
"strconv"
"strings"
)
type RcsSvn struct {
Root string
}
func (v RcsSvn) Name() string {
return "svn"
}
func (v RcsSvn) Branch() (string, error) {
info, err := v.SvnInfo()
if err != nil {
return "", nil
}
path, ok := info["Relative URL"]
if !ok {
return "", errors.New("could not find URL in svn output")
}
return ParseBranchFromSvnPath(path)
}
func (v RcsSvn) CommitCounter() (string, error) {
cmd := exec.Command("svn", "log", "-l", "1", "--xml")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return ParseRevisionFromXmlLog(out.String())
}
func (v RcsSvn) RepoCounter() (string, error) {
info, err := v.SvnInfo()
if err != nil {
return "", nil
}
rev, ok := info["Revision"]
if !ok {
return "", errors.New("could not find revision in svn output")
}
n, err := strconv.Atoi(rev)
if err != nil {
return "", errors.New("could not read revision as number")
}
return strconv.Itoa(n), nil
}
func (v RcsSvn) RepoRoot() (string, error) {
info, err := v.SvnInfo()
if err != nil {
return "", nil
}
url, ok := info["Repository Root"]
if !ok {
return "", errors.New("could not find repo root in svn output")
}
return url, nil
}
func (v RcsSvn) CommitHash() (string, error) {
return "", errors.New("SVN does not support commit hashes")
}
func (v RcsSvn) CommitHashShort() (string, error) {
return "", errors.New("SVN does not support commit hashes")
}
func (v RcsSvn) SvnInfo() (map[string]string, error) {
cmd := exec.Command("svn", "info")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return nil, err
}
return ParseSvnInfo(out.String())
}
func ParseSvnInfo(svnOut string) (map[string]string, error) {
lines := strings.Split(svnOut, "\n")
if len(lines) == 0 {
return nil, errors.New("expected at least one line of svn info output")
}
info := map[string]string{}
for _, line := range lines {
if line == "" {
continue
}
kv := strings.SplitN(line, ": ", 2)
if len(kv) != 2 {
return nil, errors.New("found unparsable svn info line")
}
info[kv[0]] = kv[1]
}
return info, nil
}
func ParseBranchFromSvnPath(url string) (string, error) {
ptrns := []*regexp.Regexp{
regexp.MustCompile("/branches/([^/]+)"),
regexp.MustCompile("/(trunk)/"),
regexp.MustCompile("/(trunk)$"),
regexp.MustCompile("/tags/([^/]+)"),
}
for _, ptrn := range ptrns {
m := ptrn.FindStringSubmatch(url)
if len(m) == 2 {
return string(m[1]), nil
}
}
return "", errors.New("could not extract branch from svn URL")
}
type LogRecord struct {
XMLName xml.Name `xml:"log"`
LogEntry LogEntry `xml:"logentry"`
}
type LogEntry struct {
Revision int `xml:"revision,attr"`
}
func ParseRevisionFromXmlLog(log string) (string, error) {
lr := LogRecord{}
err := xml.Unmarshal([]byte(log), &lr)
if err != nil {
return "", err
}
return strconv.Itoa(lr.LogEntry.Revision), nil
}