-
Notifications
You must be signed in to change notification settings - Fork 303
/
squash.go
50 lines (43 loc) · 1.06 KB
/
squash.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
package model
import (
"strings"
)
func TrySquash(runs []Cmd) []Cmd {
newRuns := make([]Cmd, 0)
for i := 0; i < len(runs); i++ {
toSquash := []Cmd{}
for j := i; j < len(runs); j++ {
runJ := runs[j]
if !runJ.IsShellStandardForm() {
break
}
toSquash = append(toSquash, runJ)
}
if len(toSquash) < 2 {
newRuns = append(newRuns, runs[i])
continue
}
newRuns = append(newRuns, squashHelper(toSquash))
i += len(toSquash) - 1
}
return newRuns
}
// Create a new shell script that combines the individual runs.
// We know all the scripts are in shell standard form.
func squashHelper(runs []Cmd) Cmd {
scripts := make([]string, len(runs))
for i, c := range runs {
scripts[i] = c.ShellStandardScript()
}
return Cmd{
// This could potentially break things (because it converts normal shell
// scripts to scripts run with -ex). We're not too worried about it right
// now. In the future, we might need to do manual exit code checks for
// correctness.
Argv: []string{
"sh",
"-exc",
strings.Join(scripts, ";\n"),
},
}
}