-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
render_executed_git_commands.go
63 lines (59 loc) · 1.69 KB
/
render_executed_git_commands.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
package output
import (
"github.com/cucumber/messages-go/v10"
"github.com/git-town/git-town/v13/test/datatable"
)
// RenderExecutedGitCommands provides the textual Gherkin table representation of the given executed Git commands.
// The DataTable table matches the structure of the given Gherkin table.
func RenderExecutedGitCommands(commands []ExecutedGitCommand, table *messages.PickleStepArgument_PickleTable) datatable.DataTable {
tableHasBranches := table.Rows[0].Cells[0].Value == "BRANCH"
tableHasType := len(table.Rows[0].Cells) > 1 && table.Rows[0].Cells[1].Value == "TYPE"
result := datatable.DataTable{}
if tableHasBranches {
if tableHasType {
result.AddRow("BRANCH", "TYPE", "COMMAND")
} else {
result.AddRow("BRANCH", "COMMAND")
}
} else {
result.AddRow("COMMAND")
}
lastBranch := ""
for _, cmd := range commands {
if tableHasBranches {
if tableHasType {
branch := branchForTableWithTypes(cmd, lastBranch)
result.AddRow(branch, cmd.CommandType.String(), cmd.Command)
} else {
branch := branchForTableWithoutTypes(cmd, lastBranch)
result.AddRow(branch, cmd.Command)
}
} else {
result.AddRow(cmd.Command)
}
lastBranch = cmd.Branch
}
return result
}
func branchForTableWithTypes(cmd ExecutedGitCommand, lastBranch string) string {
switch {
case cmd.Branch == "" && cmd.CommandType == CommandTypeFrontend:
return "<none>"
case cmd.Branch == lastBranch:
return ""
case cmd.Branch == "":
return ""
default:
return cmd.Branch
}
}
func branchForTableWithoutTypes(cmd ExecutedGitCommand, lastBranch string) string {
switch {
case cmd.Branch == lastBranch:
return ""
case cmd.Branch == "":
return "<none>"
default:
return cmd.Branch
}
}