diff --git a/check_version.go b/check_version.go index c99cb65..7614680 100644 --- a/check_version.go +++ b/check_version.go @@ -38,9 +38,8 @@ func shortCommitHash(hash string) string { // pkgVersionFromGit determines the actual version to be packaged // from the git repository status and user preference. -// Besides returning the upstream version, the "upstream" struct -// struct fields u.version, u.commitIsh, u.hasRelease and u.isRelease -// are also set. +// Besides returning the upstream version, the "upstream" struct fields +// u.version, u.commitID, 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, preferredRev string, forcePrerelease bool) (string, error) { @@ -129,9 +128,10 @@ func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePre } fullCommitHash := strings.TrimSpace(string(fullCommitHashBytes)) lastCommitHash := shortCommitHash(fullCommitHash) + u.commitID = fullCommitHash u.commitIsh = lastCommitHash // Snapshot versions are based on the packaging date, not the commit date. - u.version = fmt.Sprintf("0+git%s.%s\n%%define commit_id %s", packagingDateString(), lastCommitHash, fullCommitHash) + u.version = fmt.Sprintf("0+git%s.%s", packagingDateString(), lastCommitHash) return u.version, nil } diff --git a/pack.go b/pack.go index d1f9494..b183691 100644 --- a/pack.go +++ b/pack.go @@ -38,6 +38,7 @@ type upstream struct { tarPath string // path to the downloaded or generated orig tarball tempfile compression string // compression method, either "gz" or "xz" version string // upstream version number, e.g. 0.0~git20180204.1d24609 + commitID string // full commit hash for commit-pinned snapshot builds tag string // Latest upstream tag, if any commitIsh string // commit-ish corresponding to upstream version to be packaged remote string // git remote, set to short hostname if upstream git history is included @@ -327,7 +328,7 @@ func moduleUsesRepoSubdir(modulePath, repoURL string) bool { // the upstream version maps cleanly to %{commit_id} or %{version}. func (u *upstream) sourceRefForSpec() (string, error) { ref := u.tag - if strings.Contains(u.version, "commit_id") { + if u.commitID != "" { ref = "%{commit_id}" } else if u.isRelease && u.tag == "v"+u.version { ref = "v%{version}" diff --git a/pkgsite_test.go b/pkgsite_test.go index 0a75e57..a493cf3 100644 --- a/pkgsite_test.go +++ b/pkgsite_test.go @@ -428,8 +428,9 @@ func TestGetPkgsitePackageRetriesAmbiguousPathWithLongestModule(t *testing.T) { func TestSourceURLForSpecUsesCommitIDMacro(t *testing.T) { u := upstream{ - repoURL: "https://github.com/example/project", - version: "0+git20260522.abcdef\n%define commit_id 0123456789abcdef", + repoURL: "https://github.com/example/project", + version: "0+git20260522.abcdef", + commitID: "0123456789abcdef", } got, err := u.sourceURLForSpec("github.com/example/project") if err != nil { @@ -494,8 +495,9 @@ func TestSourceURLForSpecUsesModuleProxyForRepoSubmodule(t *testing.T) { func TestSourceURLForSpecRejectsCommitIDForRepoSubmodule(t *testing.T) { u := upstream{ - repoURL: "https://github.com/charmbracelet/x", - version: "0+git20260522.abcdef\n%define commit_id abcdef1234567890", + repoURL: "https://github.com/charmbracelet/x", + version: "0+git20260522.abcdef", + commitID: "abcdef1234567890", } _, err := u.sourceURLForSpec("github.com/charmbracelet/x/ansi") if err == nil { @@ -529,7 +531,7 @@ func TestPkgVersionFromGitUsesPackagingDateAndSevenCharHash(t *testing.T) { }, "commit", "-m", "initial") fullHash := strings.TrimSpace(runGit(t, dir, nil, "rev-parse", "HEAD")) - want := "0+git20250808." + fullHash[:7] + "\n%define commit_id " + fullHash + want := "0+git20250808." + fullHash[:7] u := upstream{} got, err := pkgVersionFromGit(dir, &u, "", false) @@ -539,6 +541,12 @@ func TestPkgVersionFromGitUsesPackagingDateAndSevenCharHash(t *testing.T) { if got != want { t.Fatalf("pkgVersionFromGit() = %q, want %q", got, want) } + if strings.Contains(got, "\n") || strings.Contains(got, "commit_id") { + t.Fatalf("pkgVersionFromGit() returned metadata in version: %q", got) + } + if u.commitID != fullHash { + t.Fatalf("commitID = %q, want %q", u.commitID, fullHash) + } if strings.Contains(got, "19990102") { t.Fatalf("pkgVersionFromGit() used commit date: %q", got) } diff --git a/spec.go b/spec.go index 0be5c60..5fb632c 100644 --- a/spec.go +++ b/spec.go @@ -122,13 +122,8 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st fmt.Fprintf(f, "%%define _name %s\n", upstreamName) fmt.Fprintf(f, "%%define go_import_path %s\n", gopkg) - // If pkgVersionFromGit embedded a commit_id define in u.version, extract and write it here - commitRe := regexp.MustCompile(`%define\s+commit_id\s+([0-9a-fA-F]+)`) - if m := commitRe.FindStringSubmatch(u.version); m != nil { - fmt.Fprintf(f, "%%define commit_id %s\n", m[1]) - // Remove the embedded %define line from version so it doesn't get written to Version: field - version = commitRe.ReplaceAllString(version, "") - version = strings.TrimSpace(version) + if u.commitID != "" { + fmt.Fprintf(f, "%%define commit_id %s\n", u.commitID) } fmt.Fprintf(f, "\n")