-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
206 lines (170 loc) · 4.39 KB
/
common.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
Copyright 2017 Continusec Pty Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitcache
import (
"archive/tar"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
)
var (
// "Plausible" default tar time, since 0 isn't good enough
defaultModTime = time.Unix(1237767840, 0)
)
func makeCommand(cmd string, args ...string) *exec.Cmd {
log.Println(cmd, strings.Join(args, " "))
return exec.Command(cmd, args...)
}
func fetchUpstream(gd, repo, branch string) error {
return makeCommand("git", "--git-dir", gd, "fetch", repo, "+"+branch+":"+branch).Run()
}
func sendDownstream(gd, commit, tree string, out io.Writer) error {
cmd := makeCommand("git", "--git-dir", gd, "archive", "--format", "tar", commit+":"+tree)
pipeTar, err := cmd.StdoutPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
tarIn := tar.NewReader(pipeTar)
tarOut := tar.NewWriter(out)
defer tarOut.Close()
for {
header, err := tarIn.Next()
if err != nil {
if err == io.EOF {
return cmd.Wait() // normal exit point
} else {
return err
}
}
// Reset modification time to constant value else we get non-deterministic
// output from git
header.ModTime = defaultModTime
err = tarOut.WriteHeader(header)
if err != nil {
return err
}
written, err := io.CopyN(tarOut, tarIn, header.Size)
if err != nil {
return err
}
if written != header.Size {
return err
}
}
}
func getHeadCommit(gd, repo, branch string) (string, error) {
err := fetchUpstream(gd, repo, branch)
if err != nil {
return "", err
}
commitHex, err := makeCommand("git", "--git-dir", gd, "rev-parse", branch).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(commitHex)), nil
}
// Return git workspace dir that is ready to go
func preflightAndInit(repo, branch, format, cacheDir string) (string, error) {
if len(repo) == 0 {
return "", errors.New("Must specify repo")
}
if len(branch) == 0 {
return "", errors.New("Must specify branch, even if you know the commit (we may need it to fetch)")
}
if len(format) == 0 {
return "", errors.New("Must specify format, e.g. tgz")
}
if format != "tar" && format != "tgz" {
return "", errors.New("Format must be tar or tgz for now")
}
// Make sure workspace exists
hash := sha256.Sum256([]byte(repo))
gd := path.Join(cacheDir, hex.EncodeToString(hash[:]))
_, err := os.Stat(gd)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(gd, 0755)
if err != nil {
return "", err
}
err = makeCommand("git", "--git-dir", gd, "init", "--bare").Run()
if err != nil {
return "", err
}
} else {
return "", err
}
}
return gd, nil
}
// If outputDir is "", write to w. Else write file to outpuDir, and name of file to w
func FetchLatest(repo, branch, commit, tree, format, cacheDir string, outputDir string, ourOutput io.Writer) error {
gd, err := preflightAndInit(repo, branch, format, cacheDir)
if err != nil {
return err
}
haveFetched := false
// If no commit is specified, fetch latest and set.
if len(commit) == 0 {
commit, err = getHeadCommit(gd, repo, branch)
if err != nil {
return err
}
haveFetched = true
}
var w io.Writer
if len(outputDir) == 0 {
w = ourOutput
} else {
fpath := filepath.Join(outputDir, commit+"."+format)
os.Stdout.Write([]byte(fpath))
f, err := os.Create(fpath)
if err != nil {
return err
}
defer f.Close()
w = f
}
if format == "tgz" {
gzipper := gzip.NewWriter(w)
defer gzipper.Close()
w = gzipper
}
// Optimistically try, will fail if we don't have the commit, but it's cheap to try
err = sendDownstream(gd, commit, tree, w)
if err == nil {
return nil
}
if haveFetched {
return err
}
// If we haven't fetched already, try one more time
err = fetchUpstream(gd, repo, branch)
if err != nil {
return err
}
return sendDownstream(gd, commit, tree, w)
}