-
Notifications
You must be signed in to change notification settings - Fork 288
/
squash.go
50 lines (43 loc) · 1.07 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(steps []Cmd) []Cmd {
newSteps := make([]Cmd, 0)
for i := 0; i < len(steps); i++ {
toSquash := []Cmd{}
for j := i; j < len(steps); j++ {
stepJ := steps[j]
if !stepJ.IsShellStandardForm() {
break
}
toSquash = append(toSquash, stepJ)
}
if len(toSquash) < 2 {
newSteps = append(newSteps, steps[i])
continue
}
newSteps = append(newSteps, squashHelper(toSquash))
i += len(toSquash) - 1
}
return newSteps
}
// Create a new shell script that combines the individual steps.
// We know all the scripts are in shell standard form.
func squashHelper(steps []Cmd) Cmd {
scripts := make([]string, len(steps))
for i, c := range steps {
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"),
},
}
}