forked from stevejefferson/trac2gitea
-
Notifications
You must be signed in to change notification settings - Fork 1
/
revisionMap.go
58 lines (46 loc) · 1.3 KB
/
revisionMap.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
var revisionRegexp = regexp.MustCompile(`^r\d+`)
// readRevisionMap reads the revision map (SVN revisions -> Git commits map)
// from the provided file. Returns nil if no file was provided
func readRevisionMap(mapFile string) (map[string]string, error) {
if mapFile == "" {
return nil, nil
}
fd, err := os.Open(mapFile)
if err != nil {
return nil, err
}
defer fd.Close()
revisionMap := make(map[string]string)
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
revisionMapLine := scanner.Text()
if revisionMapLine == "" {
continue
}
equalsPos := strings.LastIndex(revisionMapLine, "=")
if equalsPos == -1 {
return nil, fmt.Errorf("badly formatted revision map file %s: found line %s", mapFile, revisionMapLine)
}
gitRef := strings.Trim(revisionMapLine[0:equalsPos], " ")
svnRevision := strings.Trim(revisionMapLine[equalsPos+1:], " ")
svnRevision = revisionRegexp.FindString(svnRevision)
if svnRevision != "" {
if _, found := revisionMap[svnRevision]; found == true {
return nil, fmt.Errorf("Revision map file %s: contains multiple entries for %s", mapFile, svnRevision)
}
revisionMap[svnRevision] = gitRef
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return revisionMap, nil
}