Author SHA1 Message Date
Jvle 540faa9f51 fix: separate pseudo-version commit id
Signed-off-by: Jvle <keke.oerv@isrc.iscas.cn>
2026-07-16 12:02:24 +08:00
4 changed files with 21 additions and 17 deletions
+4 -4
View File
@@ -38,9 +38,8 @@ func shortCommitHash(hash string) string {
// pkgVersionFromGit determines the actual version to be packaged // pkgVersionFromGit determines the actual version to be packaged
// from the git repository status and user preference. // from the git repository status and user preference.
// Besides returning the upstream version, the "upstream" struct // Besides returning the upstream version, the "upstream" struct fields
// struct fields u.version, u.commitIsh, u.hasRelease and u.isRelease // u.version, u.commitID, u.commitIsh, u.hasRelease and u.isRelease are also set.
// are also set.
// `preferredRev` should be empty if there are no user preferences. // `preferredRev` should be empty if there are no user preferences.
// TODO: also support other VCS // TODO: also support other VCS
func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePrerelease bool) (string, error) { 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)) fullCommitHash := strings.TrimSpace(string(fullCommitHashBytes))
lastCommitHash := shortCommitHash(fullCommitHash) lastCommitHash := shortCommitHash(fullCommitHash)
u.commitID = fullCommitHash
u.commitIsh = lastCommitHash u.commitIsh = lastCommitHash
// Snapshot versions are based on the packaging date, not the commit date. // 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 return u.version, nil
} }
+2 -1
View File
@@ -38,6 +38,7 @@ type upstream struct {
tarPath string // path to the downloaded or generated orig tarball tempfile tarPath string // path to the downloaded or generated orig tarball tempfile
compression string // compression method, either "gz" or "xz" compression string // compression method, either "gz" or "xz"
version string // upstream version number, e.g. 0.0~git20180204.1d24609 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 tag string // Latest upstream tag, if any
commitIsh string // commit-ish corresponding to upstream version to be packaged 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 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}. // the upstream version maps cleanly to %{commit_id} or %{version}.
func (u *upstream) sourceRefForSpec() (string, error) { func (u *upstream) sourceRefForSpec() (string, error) {
ref := u.tag ref := u.tag
if strings.Contains(u.version, "commit_id") { if u.commitID != "" {
ref = "%{commit_id}" ref = "%{commit_id}"
} else if u.isRelease && u.tag == "v"+u.version { } else if u.isRelease && u.tag == "v"+u.version {
ref = "v%{version}" ref = "v%{version}"
+13 -5
View File
@@ -428,8 +428,9 @@ func TestGetPkgsitePackageRetriesAmbiguousPathWithLongestModule(t *testing.T) {
func TestSourceURLForSpecUsesCommitIDMacro(t *testing.T) { func TestSourceURLForSpecUsesCommitIDMacro(t *testing.T) {
u := upstream{ u := upstream{
repoURL: "https://github.com/example/project", repoURL: "https://github.com/example/project",
version: "0+git20260522.abcdef\n%define commit_id 0123456789abcdef", version: "0+git20260522.abcdef",
commitID: "0123456789abcdef",
} }
got, err := u.sourceURLForSpec("github.com/example/project") got, err := u.sourceURLForSpec("github.com/example/project")
if err != nil { if err != nil {
@@ -494,8 +495,9 @@ func TestSourceURLForSpecUsesModuleProxyForRepoSubmodule(t *testing.T) {
func TestSourceURLForSpecRejectsCommitIDForRepoSubmodule(t *testing.T) { func TestSourceURLForSpecRejectsCommitIDForRepoSubmodule(t *testing.T) {
u := upstream{ u := upstream{
repoURL: "https://github.com/charmbracelet/x", repoURL: "https://github.com/charmbracelet/x",
version: "0+git20260522.abcdef\n%define commit_id abcdef1234567890", version: "0+git20260522.abcdef",
commitID: "abcdef1234567890",
} }
_, err := u.sourceURLForSpec("github.com/charmbracelet/x/ansi") _, err := u.sourceURLForSpec("github.com/charmbracelet/x/ansi")
if err == nil { if err == nil {
@@ -529,7 +531,7 @@ func TestPkgVersionFromGitUsesPackagingDateAndSevenCharHash(t *testing.T) {
}, "commit", "-m", "initial") }, "commit", "-m", "initial")
fullHash := strings.TrimSpace(runGit(t, dir, nil, "rev-parse", "HEAD")) 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{} u := upstream{}
got, err := pkgVersionFromGit(dir, &u, "", false) got, err := pkgVersionFromGit(dir, &u, "", false)
@@ -539,6 +541,12 @@ func TestPkgVersionFromGitUsesPackagingDateAndSevenCharHash(t *testing.T) {
if got != want { if got != want {
t.Fatalf("pkgVersionFromGit() = %q, want %q", 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") { if strings.Contains(got, "19990102") {
t.Fatalf("pkgVersionFromGit() used commit date: %q", got) t.Fatalf("pkgVersionFromGit() used commit date: %q", got)
} }
+2 -7
View File
@@ -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 _name %s\n", upstreamName)
fmt.Fprintf(f, "%%define go_import_path %s\n", gopkg) 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 if u.commitID != "" {
commitRe := regexp.MustCompile(`%define\s+commit_id\s+([0-9a-fA-F]+)`) fmt.Fprintf(f, "%%define commit_id %s\n", u.commitID)
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)
} }
fmt.Fprintf(f, "\n") fmt.Fprintf(f, "\n")