Skip to content

Commit 35c0dc7

Browse files
committed
Download only the repo, not its dependencies.
Thanks to tincho for the hint! This refactors standard library filtering to use “go list std” instead of using “go list” to tell us about which packages are in the standard library (which would require the packages to be present). This also means we don’t need to run “go get” after removing vendor/ anymore. Further, we now use vcs.CreateAtRev instead of running “git reset” ourselves, which is slightly cleaner and VCS-agnostic (all but 5 upstreams packaged in Debian do use git, though…). This commit cuts down cloning time of large packages to 1/3rd (e.g. grafana goes from 3 minutes to 1 minute).
1 parent 3c94535 commit 35c0dc7

File tree

1 file changed

+28
-50
lines changed

1 file changed

+28
-50
lines changed

make.go

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,20 @@ type upstream struct {
7474
repoDeps []string // the repository paths of all dependencies (e.g. github.com/zyedidia/glob)
7575
}
7676

77-
func (u *upstream) get(gopath, repo string) error {
77+
func (u *upstream) get(gopath, repo, rev string) error {
7878
done := make(chan struct{})
7979
defer close(done)
8080
go progressSize("go get", filepath.Join(gopath, "src"), done)
8181

82-
// As per https://groups.google.com/forum/#!topic/golang-nuts/N5apfenE4m4,
83-
// the arguments to “go get” are packages, not repositories. Hence, we
84-
// specify “gopkg/...” in order to cover all packages.
85-
// As a concrete example, github.com/jacobsa/util is a repository we want
86-
// to package into a single Debian package, and using “go get -d
87-
// github.com/jacobsa/util” fails because there are no buildable go files
88-
// in the top level of that repository.
89-
cmd := exec.Command("go", "get", "-d", "-t", repo+"/...")
90-
cmd.Stderr = os.Stderr
91-
cmd.Env = append([]string{
92-
fmt.Sprintf("GOPATH=%s", gopath),
93-
}, passthroughEnv()...)
94-
return cmd.Run()
82+
rr, err := vcs.RepoRootForImportPath(repo, false)
83+
if err != nil {
84+
return err
85+
}
86+
dir := filepath.Join(gopath, "src", rr.Root)
87+
if rev != "" {
88+
return rr.VCS.CreateAtRev(dir, rr.Repo, rev)
89+
}
90+
return rr.VCS.Create(dir, rr.Repo)
9591
}
9692

9793
func (u *upstream) tar(gopath, repo string) error {
@@ -158,7 +154,7 @@ func (u *upstream) findDependencies(gopath, repo string) error {
158154
return fmt.Errorf("%v: %v", cmd.Args, err)
159155
}
160156

161-
var godependencies []string
157+
godependencies := make(map[string]bool)
162158
for _, p := range strings.Split(strings.TrimSpace(string(out)), "\n") {
163159
if p == "" {
164160
continue // skip separators between import types
@@ -170,7 +166,7 @@ func (u *upstream) findDependencies(gopath, repo string) error {
170166
if p == "C" {
171167
// TODO: maybe parse the comments to figure out C deps from pkg-config files?
172168
} else {
173-
godependencies = append(godependencies, p)
169+
godependencies[p] = true
174170
}
175171
}
176172

@@ -179,10 +175,7 @@ func (u *upstream) findDependencies(gopath, repo string) error {
179175
}
180176

181177
// Remove all packages which are in the standard lib.
182-
args := []string{"list", "-f", "{{.ImportPath}}: {{.Standard}}"}
183-
args = append(args, godependencies...)
184-
185-
cmd = exec.Command("go", args...)
178+
cmd = exec.Command("go", "list", "std")
186179
cmd.Dir = filepath.Join(gopath, "src", repo)
187180
cmd.Stderr = os.Stderr
188181
cmd.Env = append([]string{
@@ -194,24 +187,24 @@ func (u *upstream) findDependencies(gopath, repo string) error {
194187
return fmt.Errorf("%v: %v", cmd.Args, err)
195188
}
196189

197-
// dependencies is a map in order to de-duplicate multiple imports
198-
// pointing into the same repository.
199-
dependencies := make(map[string]bool)
200190
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
201-
if !strings.HasSuffix(line, ": false") {
202-
continue
203-
}
204-
importpath := strings.TrimSuffix(line, ": false")
205-
rr, err := vcs.RepoRootForImportPath(importpath, false)
191+
delete(godependencies, line)
192+
}
193+
194+
// Resolve all packages to the root of their repository.
195+
roots := make(map[string]bool)
196+
for dep := range godependencies {
197+
rr, err := vcs.RepoRootForImportPath(dep, false)
206198
if err != nil {
207-
log.Printf("Could not determine repo path for import path %q: %v\n", importpath, err)
199+
log.Printf("Could not determine repo path for import path %q: %v\n", dep, err)
208200
}
209-
dependencies[rr.Root] = true
201+
202+
roots[rr.Root] = true
210203
}
211204

212-
u.repoDeps = make([]string, 0, len(dependencies))
213-
for dep := range dependencies {
214-
u.repoDeps = append(u.repoDeps, dep)
205+
u.repoDeps = make([]string, 0, len(godependencies))
206+
for root := range roots {
207+
u.repoDeps = append(u.repoDeps, root)
215208
}
216209

217210
return nil
@@ -228,7 +221,7 @@ func makeUpstreamSourceTarball(repo, revision string) (*upstream, error) {
228221
var u upstream
229222

230223
log.Printf("Downloading %q\n", repo+"/...")
231-
if err := u.get(gopath, repo); err != nil {
224+
if err := u.get(gopath, repo, revision); err != nil {
232225
return nil, err
233226
}
234227

@@ -237,18 +230,6 @@ func makeUpstreamSourceTarball(repo, revision string) (*upstream, error) {
237230
return nil, fmt.Errorf("Not a git repository, dh-make-golang currently only supports git")
238231
}
239232

240-
if revision != "" {
241-
log.Printf("Checking out git revision %q\n", revision)
242-
if err := runGitCommandIn(repoDir, "reset", "--hard", revision); err != nil {
243-
return nil, fmt.Errorf("could not check out git revision %q: %v\n", revision, err)
244-
}
245-
246-
log.Printf("Refreshing %q\n", repo+"/...")
247-
if err := u.get(gopath, repo); err != nil {
248-
return nil, err
249-
}
250-
}
251-
252233
if _, err := os.Stat(filepath.Join(repoDir, "debian")); err == nil {
253234
log.Printf("WARNING: ignoring debian/ directory that came with the upstream sources\n")
254235
}
@@ -258,15 +239,12 @@ func makeUpstreamSourceTarball(repo, revision string) (*upstream, error) {
258239
return nil, err
259240
}
260241
if len(u.vendorDirs) > 0 {
261-
log.Printf("Deleting upstream vendor/ directories, installing remaining dependencies")
242+
log.Printf("Deleting upstream vendor/ directories")
262243
for _, dir := range u.vendorDirs {
263244
if err := os.RemoveAll(filepath.Join(repoDir, dir)); err != nil {
264245
return nil, err
265246
}
266247
}
267-
if err := u.get(gopath, repo); err != nil {
268-
return nil, err
269-
}
270248
}
271249

272250
log.Printf("Determining upstream version number\n")

0 commit comments

Comments
 (0)