-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
git_town_output_parser.go
58 lines (47 loc) · 1.7 KB
/
git_town_output_parser.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 test
import (
"strings"
)
// ExecutedGitCommand describes a Git command that was executed by Git Town during testing.
type ExecutedGitCommand struct {
// Branch contains the branch in which this command ran.
Branch string
// Command contains the command executed.
Command string
}
// GitCommandsInGitTownOutput provides the Git commands mentioned in the given Git Town output.
func GitCommandsInGitTownOutput(output string) (result []ExecutedGitCommand) {
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if lineContainsGitTownCommand(line) {
result = append(result, parseLine(line))
}
}
return result
}
// gitCommandLineBeginning contains the first few characters of lines containing Git commands in Git Town output.
const gitCommandLineBeginning = "\x1b[1m" // "\e[1m"
// lineContainsGitTownCommand indicates whether the given line contains a Git Town command.
func lineContainsGitTownCommand(line string) bool {
return strings.HasPrefix(line, gitCommandLineBeginning)
}
// parseLine provides the Git Town command and branchname in the given line.
func parseLine(line string) ExecutedGitCommand {
// NOTE: implementing this without regex
// because the regex has gotten very complex and hard to maintain
// remove the color codes at the beginning
line = strings.Replace(line, gitCommandLineBeginning, "", 1)
// extract branch name if it exists
branchName := ""
if line[0] == '[' {
// line contains a branch name
line = line[1:] // remove the leading "["
parts := strings.SplitN(line, "]", 2)
branchName = parts[0]
line = parts[1]
}
return ExecutedGitCommand{Command: strings.TrimSpace(line), Branch: branchName}
}