Skip to content

Commit c1cf2e9

Browse files
committed
New upstream version 0.3.0
2 parents b845199 + e1bfd01 commit c1cf2e9

File tree

3 files changed

+105
-38
lines changed

3 files changed

+105
-38
lines changed

make.go

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,18 @@ func downloadFile(filename, url string) error {
102102

103103
// upstream describes the upstream repo we are about to package.
104104
type upstream struct {
105+
rr *vcs.RepoRoot
105106
tarPath string // path to the downloaded or generated orig tarball tempfile
106107
compression string // compression method, either "gz" or "xz"
107108
version string // Debian package upstream version number, e.g. 0.0~git20180204.1d24609
109+
commitIsh string // commit-ish corresponding to upstream version to be packaged
110+
remote string // git remote, set to short hostname if upstream git history is included
108111
firstMain string // import path of the first main package within repo, if any
109112
vendorDirs []string // all vendor sub directories, relative to the repo directory
110113
repoDeps []string // the repository paths of all dependencies (e.g. github.com/zyedidia/glob)
111114
hasGodeps bool // whether the Godeps/_workspace directory exists
112115
hasRelease bool // whether any release tags exist, for debian/watch
113-
isRelease bool // whether we are packaging a tagged version or not
116+
isRelease bool // whether what we end up packaging is a tagged release
114117
}
115118

116119
func (u *upstream) get(gopath, repo, rev string) error {
@@ -122,6 +125,7 @@ func (u *upstream) get(gopath, repo, rev string) error {
122125
if err != nil {
123126
return err
124127
}
128+
u.rr = rr
125129
dir := filepath.Join(gopath, "src", rr.Root)
126130
if rev != "" {
127131
return rr.VCS.CreateAtRev(dir, rr.Repo, rev)
@@ -341,7 +345,7 @@ func makeUpstreamSourceTarball(repo, revision string, forcePrerelease bool) (*up
341345

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

344-
u.version, u.hasRelease, u.isRelease, err = pkgVersionFromGit(repoDir, forcePrerelease)
348+
u.version, err = pkgVersionFromGit(repoDir, &u, forcePrerelease)
345349
if err != nil {
346350
return nil, err
347351
}
@@ -370,7 +374,8 @@ func runGitCommandIn(dir string, arg ...string) error {
370374
return cmd.Run()
371375
}
372376

373-
func createGitRepository(debsrc, gopkg, orig string, dep14, pristineTar bool) (string, error) {
377+
func createGitRepository(debsrc, gopkg, orig string, u *upstream,
378+
includeUpstreamHistory, allowUnknownHoster, dep14, pristineTar bool) (string, error) {
374379
wd, err := os.Getwd()
375380
if err != nil {
376381
return "", err
@@ -412,6 +417,21 @@ func createGitRepository(debsrc, gopkg, orig string, dep14, pristineTar bool) (s
412417
return dir, err
413418
}
414419

420+
if includeUpstreamHistory {
421+
u.remote, err = shortHostName(gopkg, allowUnknownHoster)
422+
if err != nil {
423+
return dir, fmt.Errorf("Unable to fetch upstream history: %q", err)
424+
}
425+
log.Printf("Adding %q as remote %q\n", u.rr.Repo, u.remote)
426+
if err := runGitCommandIn(dir, "remote", "add", u.remote, u.rr.Repo); err != nil {
427+
return dir, err
428+
}
429+
log.Printf("Running \"git fetch %s\"\n", u.remote)
430+
if err := runGitCommandIn(dir, "fetch", u.remote); err != nil {
431+
return dir, err
432+
}
433+
}
434+
415435
// Import upstream orig tarball
416436

417437
arg := []string{"import-orig", "--no-interactive"}
@@ -421,6 +441,9 @@ func createGitRepository(debsrc, gopkg, orig string, dep14, pristineTar bool) (s
421441
if pristineTar {
422442
arg = append(arg, "--pristine-tar")
423443
}
444+
if includeUpstreamHistory {
445+
arg = append(arg, "--upstream-vcs-tag="+u.commitIsh)
446+
}
424447
arg = append(arg, filepath.Join(wd, orig))
425448
cmd := exec.Command("gbp", arg...)
426449
cmd.Dir = dir
@@ -484,16 +507,11 @@ func normalizeDebianProgramName(str string) string {
484507
return safe
485508
}
486509

487-
// This follows https://fedoraproject.org/wiki/PackagingDrafts/Go#Package_Names
488-
func debianNameFromGopkg(gopkg string, t packageType, allowUnknownHoster bool) string {
510+
func shortHostName(gopkg string, allowUnknownHoster bool) (host string, err error) {
489511
parts := strings.Split(gopkg, "/")
512+
fqdn := parts[0]
490513

491-
if t == typeProgram || t == typeProgramLibrary {
492-
return normalizeDebianProgramName(parts[len(parts)-1])
493-
}
494-
495-
host := parts[0]
496-
switch host {
514+
switch fqdn {
497515
case "github.com":
498516
host = "github"
499517
case "code.google.com":
@@ -526,12 +544,30 @@ func debianNameFromGopkg(gopkg string, t packageType, allowUnknownHoster bool) s
526544
if allowUnknownHoster {
527545
suffix, _ := publicsuffix.PublicSuffix(host)
528546
host = host[:len(host)-len(suffix)-len(".")]
529-
log.Printf("WARNING: Using %q as canonical hostname for %q. If that is not okay, please file a bug against %s.\n", host, parts[0], os.Args[0])
547+
log.Printf("WARNING: Using %q as canonical hostname for %q. If that is not okay, please file a bug against %s.\n", host, fqdn, os.Args[0])
530548
} else {
531-
log.Fatalf("Cannot derive Debian package name: unknown hoster %q. See -help output for -allow_unknown_hoster\n", host)
549+
err = fmt.Errorf("unknown hoster %q", fqdn)
532550
}
533551
}
552+
return host, err
553+
}
554+
555+
// debianNameFromGopkg maps a Go package repo path to a Debian package name,
556+
// e.g. "golang.org/x/text" → "golang-golang-x-text".
557+
// This follows https://fedoraproject.org/wiki/PackagingDrafts/Go#Package_Names
558+
func debianNameFromGopkg(gopkg string, t packageType, allowUnknownHoster bool) string {
559+
parts := strings.Split(gopkg, "/")
560+
561+
if t == typeProgram || t == typeProgramLibrary {
562+
return normalizeDebianProgramName(parts[len(parts)-1])
563+
}
564+
565+
host, err := shortHostName(gopkg, allowUnknownHoster)
566+
if err != nil {
567+
log.Fatalf("Cannot derive Debian package name: %v. See -help output for -allow_unknown_hoster\n", err)
568+
}
534569
parts[0] = host
570+
535571
return strings.Trim("golang-"+strings.ToLower(strings.Replace(strings.Join(parts, "-"), "_", "-", -1)), "-")
536572
}
537573

@@ -706,6 +742,13 @@ func execMake(args []string, usage func()) {
706742
` * "library+program" (aliases: "lib+prog", "l+p", "both")`+"\n"+
707743
` * "program+library" (aliases: "prog+lib", "p+l", "combined")`)
708744

745+
var includeUpstreamHistory bool
746+
fs.BoolVar(&includeUpstreamHistory,
747+
"upstream-git-history",
748+
true,
749+
"Include upstream git history (Debian pkg-go team new workflow).\n"+
750+
"New in dh-make-golang 0.3.0, currently experimental.")
751+
709752
fs.StringVar(&wrapAndSort,
710753
"wrap-and-sort",
711754
"a",
@@ -848,7 +891,7 @@ func execMake(args []string, usage func()) {
848891

849892
debversion := u.version + "-1"
850893

851-
dir, err := createGitRepository(debsrc, gopkg, orig, dep14, pristineTar)
894+
dir, err := createGitRepository(debsrc, gopkg, orig, u, includeUpstreamHistory, allowUnknownHoster, dep14, pristineTar)
852895
if err != nil {
853896
log.Fatalf("Could not create git repository: %v\n", err)
854897
}
@@ -913,4 +956,18 @@ func execMake(args []string, usage func()) {
913956
fmt.Printf(" git remote set-url origin [email protected]:go-team/packages/%s.git\n", debsrc)
914957
fmt.Printf(" gbp push\n")
915958
fmt.Printf("\n")
959+
960+
if includeUpstreamHistory {
961+
fmt.Printf("NOTE: Full upstream git history has been included as per pkg-go team's\n")
962+
fmt.Printf(" new workflow. This feature is new and somewhat experimental,\n")
963+
fmt.Printf(" and all feedback are welcome!\n")
964+
fmt.Printf(" (For old behavior, use --include-upstream-history=false)\n")
965+
fmt.Printf("\n")
966+
fmt.Printf("The upstream git history is being tracked with the remote named %q.\n", u.remote)
967+
fmt.Printf("To upgrade to the latest upstream version, you may use something like:\n")
968+
fmt.Printf(" git fetch %-15v # note the latest tag or commit-ish\n", u.remote)
969+
fmt.Printf(" uscan --report-status # check we get the same tag or commit-ish\n")
970+
fmt.Printf(" gbp import-orig --sign-tags --uscan --upstream-vcs-tag=<commit-ish>\n")
971+
fmt.Printf("\n")
972+
}
916973
}

version.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ var (
2121
semverRegexp = regexp.MustCompile(`^v(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`)
2222
)
2323

24+
// pkgVersionFromGit determines the actual version to be packaged
25+
// from the git repository status and user preference.
26+
// Besides returning the Debian upstream version, the "upstream" struct
27+
// struct fields u.version, u.commitIsh, u.hasRelease and u.isRelease
28+
// are also set.
2429
// TODO: also support other VCS
25-
func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, hasRelease, isRelease bool, err error) {
30+
func pkgVersionFromGit(gitdir string, u *upstream, forcePrerelease bool) (string, error) {
2631
var latestTag string
2732
var commitsAhead int
2833

@@ -31,7 +36,7 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
3136
cmd.Dir = gitdir
3237
if out, err := cmd.Output(); err == nil {
3338
latestTag = strings.TrimSpace(string(out))
34-
hasRelease = true
39+
u.hasRelease = true
3540
log.Printf("Found latest tag %q", latestTag)
3641

3742
if !semverRegexp.MatchString(latestTag) {
@@ -44,11 +49,11 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
4449
cmd.Dir = gitdir
4550
out, err := cmd.Output()
4651
if err != nil {
47-
return "", true, false, err
52+
return "", err
4853
}
4954
commitsAhead, err = strconv.Atoi(strings.TrimSpace(string(out)))
5055
if err != nil {
51-
return "", true, false, err
56+
return "", err
5257
}
5358

5459
if commitsAhead == 0 {
@@ -58,13 +63,15 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
5863
log.Printf("INFO: master is ahead of %q by %v commits", latestTag, commitsAhead)
5964
}
6065

61-
version = strings.TrimPrefix(latestTag, "v")
62-
isRelease = true
66+
u.commitIsh = latestTag
67+
u.version = strings.TrimPrefix(latestTag, "v")
6368

6469
if forcePrerelease {
6570
log.Printf("INFO: Force packaging master (prerelease) as requested by user")
71+
// Fallthrough to package @master (prerelease)
6672
} else {
67-
return version, hasRelease, isRelease, nil
73+
u.isRelease = true
74+
return u.version, nil
6875
}
6976
}
7077

@@ -73,26 +80,26 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
7380
// 1.0~rc1 < 1.0 < 1.0+b1, as per
7481
// https://www.debian.org/doc/manuals/maint-guide/first.en.html#namever
7582
mainVer := "0.0~"
76-
if hasRelease {
77-
mainVer = version + "+"
83+
if u.hasRelease {
84+
mainVer = u.version + "+"
7885
}
7986

8087
// Find committer date, UNIX timestamp
8188
cmd = exec.Command("git", "log", "--pretty=format:%ct", "-n1")
8289
cmd.Dir = gitdir
8390
lastCommitUnixBytes, err := cmd.Output()
8491
if err != nil {
85-
return "", hasRelease, isRelease, err
92+
return "", err
8693
}
8794
lastCommitUnix, err := strconv.ParseInt(strings.TrimSpace(string(lastCommitUnixBytes)), 0, 64)
8895
if err != nil {
89-
return "", hasRelease, isRelease, err
96+
return "", err
9097
}
9198

92-
// This results in an output like 4.10.2-232-g9f107c8
99+
// This results in an output like "v4.10.2-232-g9f107c8"
93100
cmd = exec.Command("git", "describe", "--long", "--tags")
94101
cmd.Dir = gitdir
95-
lastCommitSha := ""
102+
lastCommitHash := ""
96103
describeBytes, err := cmd.Output()
97104
if err != nil {
98105
// In case there are no tags at all, we just use the sha of the current commit
@@ -101,19 +108,21 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
101108
cmd.Stderr = os.Stderr
102109
revparseBytes, err := cmd.Output()
103110
if err != nil {
104-
return "", hasRelease, isRelease, err
111+
return "", err
105112
}
106-
lastCommitSha = strings.TrimSpace(string(revparseBytes))
113+
lastCommitHash = strings.TrimSpace(string(revparseBytes))
114+
u.commitIsh = lastCommitHash
107115
} else {
108116
submatches := describeRegexp.FindSubmatch(describeBytes)
109117
if submatches == nil {
110-
return "", hasRelease, isRelease, fmt.Errorf("git describe output %q does not match expected format", string(describeBytes))
118+
return "", fmt.Errorf("git describe output %q does not match expected format", string(describeBytes))
111119
}
112-
lastCommitSha = string(submatches[1])
120+
lastCommitHash = string(submatches[1])
121+
u.commitIsh = strings.TrimSpace(string(describeBytes))
113122
}
114-
version = fmt.Sprintf("%sgit%s.%s",
123+
u.version = fmt.Sprintf("%sgit%s.%s",
115124
mainVer,
116125
time.Unix(lastCommitUnix, 0).UTC().Format("20060102"),
117-
lastCommitSha)
118-
return version, hasRelease, isRelease, nil
126+
lastCommitHash)
127+
return u.version, nil
119128
}

version_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func TestSnapshotVersion(t *testing.T) {
4242
t.Fatalf("Could not run %v: %v", cmd.Args, err)
4343
}
4444

45-
got, _, _, err := pkgVersionFromGit(tempdir, false)
45+
var u upstream
46+
got, err := pkgVersionFromGit(tempdir, &u, false)
4647
if err != nil {
4748
t.Fatalf("Determining package version from git failed: %v", err)
4849
}
@@ -52,7 +53,7 @@ func TestSnapshotVersion(t *testing.T) {
5253

5354
gitCmdOrFatal(t, tempdir, "tag", "-a", "v1", "-m", "release v1")
5455

55-
got, _, _, err = pkgVersionFromGit(tempdir, false)
56+
got, err = pkgVersionFromGit(tempdir, &u, false)
5657
if err != nil {
5758
t.Fatalf("Determining package version from git failed: %v", err)
5859
}
@@ -72,7 +73,7 @@ func TestSnapshotVersion(t *testing.T) {
7273
t.Fatalf("Could not run %v: %v", cmd.Args, err)
7374
}
7475

75-
got, _, _, err = pkgVersionFromGit(tempdir, false)
76+
got, err = pkgVersionFromGit(tempdir, &u, false)
7677
if err != nil {
7778
t.Fatalf("Determining package version from git failed: %v", err)
7879
}
@@ -92,7 +93,7 @@ func TestSnapshotVersion(t *testing.T) {
9293
t.Fatalf("Could not run %v: %v", cmd.Args, err)
9394
}
9495

95-
got, _, _, err = pkgVersionFromGit(tempdir, false)
96+
got, err = pkgVersionFromGit(tempdir, &u, false)
9697
if err != nil {
9798
t.Fatalf("Determining package version from git failed: %v", err)
9899
}

0 commit comments

Comments
 (0)