@@ -17,30 +17,19 @@ import (
17
17
// majorVersionRegexp checks if an import path contains a major version suffix.
18
18
var majorVersionRegexp = regexp .MustCompile (`([/.])v([0-9]+)$` )
19
19
20
- func clone (srcdir , repo string ) error {
21
- done := make (chan struct {})
22
- defer close (done )
23
- go progressSize ("vcs clone" , srcdir , done )
24
-
25
- // Get the sources of the module in a temporary dir to be able to run
26
- // go get in module mode, as the gopath mode has been removed in latest
27
- // version of Go.
28
- rr , err := vcs .RepoRootForImportPath (repo , false )
29
- if err != nil {
30
- return fmt .Errorf ("get repo root: %w" , err )
31
- }
32
- // Run "git clone {repo} {dir}" (or the equivalent command for hg, svn, bzr)
33
- return rr .VCS .Create (srcdir , rr .Repo )
34
- }
35
-
36
20
func get (gopath , repodir , repo string ) error {
37
21
done := make (chan struct {})
38
22
defer close (done )
39
23
go progressSize ("go get" , repodir , done )
40
24
41
- // Run go mod tidy directly in the module directory to sync go.(mod|sum) and
42
- // download all its dependencies.
43
- cmd := exec .Command ("go" , "mod" , "tidy" )
25
+ // As per https://groups.google.com/forum/#!topic/golang-nuts/N5apfenE4m4,
26
+ // the arguments to “go get” are packages, not repositories. Hence, we
27
+ // specify “gopkg/...” in order to cover all packages.
28
+ // As a concrete example, github.com/jacobsa/util is a repository we want
29
+ // to package into a single Debian package, and using “go get -t
30
+ // github.com/jacobsa/util” fails because there are no buildable go files
31
+ // in the top level of that repository.
32
+ cmd := exec .Command ("go" , "get" , "-t" , repo + "/..." )
44
33
cmd .Dir = repodir
45
34
cmd .Stderr = os .Stderr
46
35
cmd .Env = append ([]string {
@@ -107,17 +96,10 @@ func estimate(importpath string) error {
107
96
}
108
97
defer removeTemp (repodir )
109
98
110
- // clone the repo inside the src directory of the GOPATH
111
- // and init a Go module if it is not yet one.
112
- if err := clone (repodir , importpath ); err != nil {
113
- return fmt .Errorf ("vcs clone: %w" , err )
114
- }
115
- if ! isFile (filepath .Join (repodir , "go.mod" )) {
116
- cmd := exec .Command ("go" , "mod" , "init" , importpath )
117
- cmd .Dir = repodir
118
- if err := cmd .Run (); err != nil {
119
- return fmt .Errorf ("go mod init: %w" , err )
120
- }
99
+ // Create a dummy go module in repodir to be able to use go get.
100
+ err = os .WriteFile (filepath .Join (repodir , "go.mod" ), []byte ("module dummymod\n " ), 0644 )
101
+ if err != nil {
102
+ return fmt .Errorf ("create dummymod: %w" , err )
121
103
}
122
104
123
105
if err := get (gopath , repodir , importpath ); err != nil {
@@ -168,23 +150,25 @@ func estimate(importpath string) error {
168
150
// imported it, separated by a single space. The module names
169
151
// can have a version information delimited by the @ character
170
152
src , dep , _ := strings .Cut (line , " " )
171
- depNode := & Node {name : dep }
172
- // Sometimes, the given import path is not the one outputed by
173
- // go mod graph, for instance when there are multiple major
174
- // versions.
175
153
// The root module is the only one that does not have a version
176
- // indication with @ in the output of go mod graph, so if there
177
- // is no @ we always use the given importpath instead.
178
- if ! strings .Contains (src , "@" ) {
154
+ // indication with @ in the output of go mod graph. We use this
155
+ // to filter out the depencencies of the "dummymod" module.
156
+ if mod , _ , found := strings .Cut (src , "@" ); ! found {
157
+ continue
158
+ } else if mod == importpath || strings .HasPrefix (mod , importpath + "/" ) {
179
159
src = importpath
180
160
}
161
+ depNode , ok := nodes [dep ]
162
+ if ! ok {
163
+ depNode = & Node {name : dep }
164
+ nodes [dep ] = depNode
165
+ }
181
166
srcNode , ok := nodes [src ]
182
167
if ! ok {
183
- log . Printf ( "source not found in graph: %s" , src )
184
- continue
168
+ srcNode = & Node { name : src }
169
+ nodes [ src ] = srcNode
185
170
}
186
171
srcNode .children = append (srcNode .children , depNode )
187
- nodes [dep ] = depNode
188
172
}
189
173
190
174
// Analyse the dependency graph
0 commit comments