-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfingerprint.go
158 lines (125 loc) · 2.99 KB
/
fingerprint.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"crypto/sha1"
"fmt"
"hash"
"io"
"os"
"path/filepath"
)
func hex(h hash.Hash) string {
return fmt.Sprintf("%x", h.Sum(nil))
}
func hashFiles(pathname string) (string, error) {
h := sha1.New()
err := filepath.Walk(pathname, func(p string,
info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
// Ignore hidden directories
if p != "." && filepath.Base(p)[0] == '.' {
return filepath.SkipDir
}
return nil
}
f, err := os.Open(p)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(h, f); err != nil {
return err
}
return nil
})
if err != nil {
return "", err
}
return hex(h), nil
}
func parseAndHashDockerfile(dockerfile string) ([]string, string, error) {
f, err := os.Open(dockerfile)
if err != nil {
return nil, "", err
}
defer f.Close()
sources, err := collectSourcesFromDockerfile(f)
if err != nil {
return nil, "", err
}
if _, err = f.Seek(0, io.SeekStart); err != nil {
return nil, "", err
}
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
return nil, "", err
}
return sources, hex(h), nil
}
func computeFingerprint(workingDir, dockerfile string, buildArgs []string,
quiet bool) (string, error) {
workingDir = filepath.Clean(workingDir)
if dockerfile == "" {
dockerfile = filepath.Join(workingDir, "Dockerfile")
}
sources, hash, err := parseAndHashDockerfile(dockerfile)
h := sha1.New()
addSourceHash := func(source, hashType, hash string) {
if !quiet {
fmt.Println("Source:", source, hashType, hash)
}
h.Write([]byte(source + "@" + hashType + ":" + hash + "\n"))
}
addSourceHash("Dockerfile", "sha1", hash)
hashSource := func(source, pathname string) error {
hash, err = getLastCommitHash(pathname)
if err == nil {
addSourceHash(source, "commit", hash)
} else {
fmt.Fprintf(os.Stderr, "Warning: unable to use git "+
"commit hash for '%s': %v; falling back to "+
"file content hashing\n", pathname, err)
hash, err = hashFiles(pathname)
if err != nil {
return err
}
addSourceHash(source, "sha1", hash)
}
return nil
}
for _, source := range sources {
source = filepath.Clean(source)
pathname := filepath.Join(workingDir, source)
if _, err := os.Stat(pathname); err != nil {
if !os.IsNotExist(err) {
return "", err
}
// Try interpreting the path as a glob pattern.
matches, _ := filepath.Glob(pathname)
// If nothing matched, return the original Stat() error.
if len(matches) == 0 {
return "", err
}
for _, pathname = range matches {
// Ignore the impossible Rel() error.
source, _ = filepath.Rel(workingDir, pathname)
if err = hashSource(
source, pathname); err != nil {
return "", err
}
}
} else if err = hashSource(source, pathname); err != nil {
return "", err
}
}
for _, buildArg := range buildArgs {
if !quiet {
fmt.Println("Arg:", buildArg)
}
h.Write([]byte(buildArg))
h.Write([]byte("\n"))
}
return hex(h), nil
}