Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dh-make-golang make: directly use the user preferred tag specified in "-git_revision" option #227

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion make.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func makeUpstreamSourceTarball(repo, revision string, forcePrerelease bool) (*up

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

u.version, err = pkgVersionFromGit(repoDir, &u, forcePrerelease)
u.version, err = pkgVersionFromGit(repoDir, &u, revision, forcePrerelease)
if err != nil {
return nil, fmt.Errorf("get package version from Git: %w", err)
}
Expand Down
21 changes: 19 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"regexp"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -32,16 +33,32 @@ var (
// Besides returning the Debian upstream version, the "upstream" struct
// struct fields u.version, u.commitIsh, u.hasRelease and u.isRelease
// are also set.
// `preferredRev` should be empty if there are no user preferences.
// TODO: also support other VCS
func pkgVersionFromGit(gitdir string, u *upstream, forcePrerelease bool) (string, error) {
func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePrerelease bool) (string, error) {
var latestTag string
var commitsAhead int

var cmd *exec.Cmd

// If the user specifies a valid tag as the preferred revision, that tag should be used without additional heuristics.
if u.rr != nil {
if out, err := u.rr.VCS.Tags(gitdir); err == nil && slices.Contains(out, preferredRev) {
latestTag = preferredRev
goto FoundLatestTag
}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are some comments regarding why I use a goto statement. Please let me know if there are better alternatives.

I want to use the git describe heuristics as a fallback in case I cannot verify if the user specified git revision is valid or not.

The logic I want to implement is:

if (u.rr is valid (which makes u.rr.VCS.Tags meaningful) and the user specified tag exists as a valid tag) {
    set the latest tag to this user specified tag
} else {
    the fallback heuristics
}

However, to implement the "if" part of the above pseudocode, I cannot find a way to implement it as a single if clause. This will result in the "else" part being written out twice, and I want to avoid such duplication.

In other words, I am trying to deal with the problem in this post: https://stackoverflow.com/questions/13197425/multiple-if-with-one-else

William

// Find @latest version tag (whether annotated or not)
cmd := exec.Command("git", "describe", "--abbrev=0", "--tags", "--exclude", "*/v*")
cmd = exec.Command("git", "describe", "--abbrev=0", "--tags", "--exclude", "*/v*")
cmd.Dir = gitdir
if out, err := cmd.Output(); err == nil {
latestTag = strings.TrimSpace(string(out))
}

FoundLatestTag:

if len(latestTag) > 0 {
u.hasRelease = true
u.tag = latestTag
log.Printf("Found latest tag %q", latestTag)
Expand Down
8 changes: 4 additions & 4 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestSnapshotVersion(t *testing.T) {
}

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

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

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

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

got, err = pkgVersionFromGit(tempdir, &u, false)
got, err = pkgVersionFromGit(tempdir, &u, "", false)
if err != nil {
t.Fatalf("Determining package version from git failed: %v", err)
}
Expand Down