/
helpers.go
79 lines (70 loc) · 1.83 KB
/
helpers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
// helper to indent strings
func indent(text string, prefix string) (out string) {
for _, l := range strings.Split(text, "\n") {
out += prefix + l + "\n"
}
return out
}
// git diff
func diff() (dirs map[string]bool, err error) {
before := os.Getenv("DRONE_COMMIT_BEFORE")
ref := os.Getenv("DRONE_COMMIT_REF")
dirs = map[string]bool{}
if strings.HasPrefix(ref, "refs/pull/") {
// pull request
before = "origin/master"
} else if before != "" {
// normal commit, usually ref is a sha
before = strings.TrimPrefix(before, "refs/")
} else {
// empty history, skipping build
// TODO: remove this
return nil, nil
//return nil, fmt.Errorf("unable to fetch previos commit from DRONE_COMMIT_REF")
}
// changes since last commit
cmd := exec.Command("git", "diff", "--name-only", before)
_ = cmd.Wait()
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
// working directory changes
_, inDrone := os.LookupEnv("DRONE")
if !inDrone && len(out) == 0 {
log.Warn("No changes found, looking for uncommited changes.")
cmd = exec.Command("git", "status", "-u", "--porcelain")
_ = cmd.Wait()
out2, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
for _, line := range bytes.Split(out2, []byte("\n")) {
if len(line) > 3 {
line = line[3:]
out = append(out, line...)
out = append(out, []byte("\n")...)
}
}
}
for _, file := range strings.Split(string(out), "\n") {
split := strings.Split(file, string(os.PathSeparator))
if len(split) > 0 {
name := split[0]
if _, err := os.Stat(filepath.Join(name, "Dockerfile")); err == nil {
dirs[name] = true
}
}
}
log.Infof("Diff mode enabled (%s), building following images: %v", before, dirs)
return dirs, nil
}