Skip to content

Commit 70e9c9c

Browse files
committed
dh-make-golang make: directly use the user preferred tag specified in "-git_revision" option
When a user specifies a valid tag when using dh-make-golang make -git_revision {some valid tag} if there is another tag pointing to the same commit as the specified tag, then dh-make-golang might use the other tag to determine the package version. This patch modifies the behavior of "dh-make-golang make" such that, if the user specifies a valid tag in "-git_revision" option, "dh-make-golang make" will always use the specified tag to determine the upstream package version. This patch also trivially changes the tests written in version_test.go so that the tests adjusts to the new function signature of version.go:pkgVersionFromGit(). Signed-off-by: William Lyu <William.Lyu@windriver.com>
1 parent d70d43c commit 70e9c9c

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

make.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func makeUpstreamSourceTarball(repo, revision string, forcePrerelease bool) (*up
383383

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

386-
u.version, err = pkgVersionFromGit(repoDir, &u, forcePrerelease)
386+
u.version, err = pkgVersionFromGit(repoDir, &u, revision, forcePrerelease)
387387
if err != nil {
388388
return nil, fmt.Errorf("get package version from Git: %w", err)
389389
}

version.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/exec"
88
"regexp"
9+
"slices"
910
"strconv"
1011
"strings"
1112
"time"
@@ -32,16 +33,32 @@ var (
3233
// Besides returning the Debian upstream version, the "upstream" struct
3334
// struct fields u.version, u.commitIsh, u.hasRelease and u.isRelease
3435
// are also set.
36+
// `preferredRev` should be empty if there are no user preferences.
3537
// TODO: also support other VCS
36-
func pkgVersionFromGit(gitdir string, u *upstream, forcePrerelease bool) (string, error) {
38+
func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePrerelease bool) (string, error) {
3739
var latestTag string
3840
var commitsAhead int
3941

42+
var cmd *exec.Cmd
43+
44+
// If the user specifies a valid tag as the preferred revision, that tag should be used without additional heuristics.
45+
if u.rr != nil {
46+
if out, err := u.rr.VCS.Tags(gitdir); err == nil && slices.Contains(out, preferredRev) {
47+
latestTag = preferredRev
48+
goto FoundLatestTag
49+
}
50+
}
51+
4052
// Find @latest version tag (whether annotated or not)
41-
cmd := exec.Command("git", "describe", "--abbrev=0", "--tags", "--exclude", "*/v*")
53+
cmd = exec.Command("git", "describe", "--abbrev=0", "--tags", "--exclude", "*/v*")
4254
cmd.Dir = gitdir
4355
if out, err := cmd.Output(); err == nil {
4456
latestTag = strings.TrimSpace(string(out))
57+
}
58+
59+
FoundLatestTag:
60+
61+
if len(latestTag) > 0 {
4562
u.hasRelease = true
4663
u.tag = latestTag
4764
log.Printf("Found latest tag %q", latestTag)

version_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestSnapshotVersion(t *testing.T) {
4545
}
4646

4747
var u upstream
48-
got, err := pkgVersionFromGit(tempdir, &u, false)
48+
got, err := pkgVersionFromGit(tempdir, &u, "", false)
4949
if err != nil {
5050
t.Fatalf("Determining package version from git failed: %v", err)
5151
}
@@ -55,7 +55,7 @@ func TestSnapshotVersion(t *testing.T) {
5555

5656
gitCmdOrFatal(t, tempdir, "tag", "-a", "v1", "-m", "release v1")
5757

58-
got, err = pkgVersionFromGit(tempdir, &u, false)
58+
got, err = pkgVersionFromGit(tempdir, &u, "", false)
5959
if err != nil {
6060
t.Fatalf("Determining package version from git failed: %v", err)
6161
}
@@ -75,7 +75,7 @@ func TestSnapshotVersion(t *testing.T) {
7575
t.Fatalf("Could not run %v: %v", cmd.Args, err)
7676
}
7777

78-
got, err = pkgVersionFromGit(tempdir, &u, false)
78+
got, err = pkgVersionFromGit(tempdir, &u, "", false)
7979
if err != nil {
8080
t.Fatalf("Determining package version from git failed: %v", err)
8181
}
@@ -95,7 +95,7 @@ func TestSnapshotVersion(t *testing.T) {
9595
t.Fatalf("Could not run %v: %v", cmd.Args, err)
9696
}
9797

98-
got, err = pkgVersionFromGit(tempdir, &u, false)
98+
got, err = pkgVersionFromGit(tempdir, &u, "", false)
9999
if err != nil {
100100
t.Fatalf("Determining package version from git failed: %v", err)
101101
}

0 commit comments

Comments
 (0)