Skip to content

Commit 2fa799b

Browse files
authored
Merge pull request #243 from n-peugnet/estimate-git-rev
Add -git_revision flag to estimate command
2 parents faa54a6 + 0b5578c commit 2fa799b

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

estimate.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// majorVersionRegexp checks if an import path contains a major version suffix.
1818
var majorVersionRegexp = regexp.MustCompile(`([/.])v([0-9]+)$`)
1919

20-
func get(gopath, repodir, repo string) error {
20+
func get(gopath, repodir, repo, rev string) error {
2121
done := make(chan struct{})
2222
defer close(done)
2323
go progressSize("go get", repodir, done)
@@ -29,7 +29,11 @@ func get(gopath, repodir, repo string) error {
2929
// to package into a single Debian package, and using “go get -t
3030
// github.com/jacobsa/util” fails because there are no buildable go files
3131
// in the top level of that repository.
32-
cmd := exec.Command("go", "get", "-t", repo+"/...")
32+
packages := repo + "/..."
33+
if rev != "" {
34+
packages += "@" + rev
35+
}
36+
cmd := exec.Command("go", "get", "-t", packages)
3337
cmd.Dir = repodir
3438
cmd.Stderr = os.Stderr
3539
cmd.Env = append([]string{
@@ -76,7 +80,7 @@ func otherVersions(mod string) (mods []string) {
7680
return
7781
}
7882

79-
func estimate(importpath string) error {
83+
func estimate(importpath, revision string) error {
8084
removeTemp := func(path string) {
8185
if err := forceRemoveAll(path); err != nil {
8286
log.Printf("could not remove all %s: %v", path, err)
@@ -102,7 +106,7 @@ func estimate(importpath string) error {
102106
return fmt.Errorf("create dummymod: %w", err)
103107
}
104108

105-
if err := get(gopath, repodir, importpath); err != nil {
109+
if err := get(gopath, repodir, importpath, revision); err != nil {
106110
return fmt.Errorf("go get: %w", err)
107111
}
108112

@@ -113,7 +117,7 @@ func estimate(importpath string) error {
113117

114118
if found {
115119
// Fetch un-vendored dependencies
116-
if err := get(gopath, repodir, importpath); err != nil {
120+
if err := get(gopath, repodir, importpath, revision); err != nil {
117121
return fmt.Errorf("fetch un-vendored: go get: %w", err)
118122
}
119123
}
@@ -246,8 +250,19 @@ func execEstimate(args []string) {
246250
fmt.Fprintf(os.Stderr, "Estimates the work necessary to bring <go-module-importpath> into Debian\n"+
247251
"by printing all currently unpacked repositories.\n")
248252
fmt.Fprintf(os.Stderr, "Example: %s estimate github.com/Debian/dh-make-golang\n", os.Args[0])
253+
fmt.Fprintf(os.Stderr, "\n")
254+
fmt.Fprintf(os.Stderr, "Flags:\n")
255+
fs.PrintDefaults()
249256
}
250257

258+
var gitRevision string
259+
fs.StringVar(&gitRevision,
260+
"git_revision",
261+
"",
262+
"git revision (see gitrevisions(7)) of the specified Go package\n"+
263+
"to estimate, defaulting to the default behavior of go get.\n"+
264+
"Useful in case you do not want to estimate the latest version.")
265+
251266
err := fs.Parse(args)
252267
if err != nil {
253268
log.Fatalf("parse args: %s", err)
@@ -258,9 +273,9 @@ func execEstimate(args []string) {
258273
os.Exit(1)
259274
}
260275

261-
// TODO: support the -git_revision flag
276+
gitRevision = strings.TrimSpace(gitRevision)
262277

263-
if err := estimate(fs.Arg(0)); err != nil {
278+
if err := estimate(fs.Arg(0), gitRevision); err != nil {
264279
log.Fatalf("estimate: %s", err)
265280
}
266281
}

0 commit comments

Comments
 (0)