From 5ea8771ad178d4d17881c548f9ac1f6b8a06c596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mudrini=C4=87?= Date: Thu, 6 Nov 2025 13:30:49 +0100 Subject: [PATCH] Fix sync-tags handling of dependency tags MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On-behalf-of: @SAP marko.mudrinic@sap.com Signed-off-by: Marko Mudrinić --- cmd/sync-tags/gomod.go | 57 ++++++++++++++++++++++++++++++++++++++---- cmd/sync-tags/main.go | 37 ++++++++++++++------------- 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/cmd/sync-tags/gomod.go b/cmd/sync-tags/gomod.go index 0ece9627..8525c810 100644 --- a/cmd/sync-tags/gomod.go +++ b/cmd/sync-tags/gomod.go @@ -34,7 +34,7 @@ import ( // updateGomodWithTaggedDependencies gets the dependencies at the given tag and fills go.mod and go.sum. // If anything is changed, it commits the changes. Returns true if go.mod changed. -func updateGomodWithTaggedDependencies(tag string, depsRepo []string, semverTag bool) (bool, error) { +func updateGomodWithTaggedDependencies(searchTag string, depsRepo []string, semverTag bool) (bool, error) { found := map[string]bool{} changed := false @@ -55,12 +55,59 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string, semverTag return changed, fmt.Errorf("failed to get package at %s: %w", depPath, err) } - commit, commitTime, err := localOrPublishedTaggedCommitHashAndTime(dr, tag) + commit, commitTime, err := localOrPublishedTaggedCommitHashAndTime(dr, searchTag) if err != nil { - return changed, fmt.Errorf("failed to get tag %s for %q: %w", tag, depPkg, err) + return changed, fmt.Errorf("failed to get tag %s for %q: %w", searchTag, depPkg, err) } rev := commit.String() - pseudoVersionOrTag := fmt.Sprintf("v0.0.0-%s-%s", commitTime.UTC().Format("20060102150405"), rev[:12]) + + moduleMajor := uint64(0) + tag := "" + if semverTag { + iter, err := dr.Tags() + if err != nil { + return changed, fmt.Errorf("failed to get tags for repo %s: %w", dep, err) + } + + var tags []string + err = iter.ForEach(func(ref *plumbing.Reference) error { + if tagObj, err := dr.TagObject(ref.Hash()); err == nil { + if tagObj.Target == commit && strings.HasPrefix(strings.TrimPrefix(ref.Name().Short(), "origin/"), "v") { + tags = append(tags, ref.Name().Short()) + } + } + + return nil + }) + if err != nil { + return changed, fmt.Errorf("failed to find semver tag for given search tag: %w", err) + } + iter.Close() + + if len(tags) == 0 { + return changed, fmt.Errorf("no semver tag found for given search tag %s", searchTag) + } + + // pick the highest semver tag + semvers := make([]semver.Version, 0, len(tags)) + for _, t := range tags { + sv, err := semver.Parse(strings.TrimPrefix(t, "v")) + if err != nil { + return changed, fmt.Errorf("failed to parse semver tag %s: %w", t, err) + } + semvers = append(semvers, sv) + } + semver.Sort(semvers) + + latestSemver := semvers[len(semvers)-1] + moduleMajor = latestSemver.Major + if moduleMajor == 1 { + moduleMajor = 0 + } + tag = fmt.Sprintf("v%s", latestSemver.String()) + } + + pseudoVersionOrTag := fmt.Sprintf("v%d.0.0-%s-%s", moduleMajor, commitTime.UTC().Format("20060102150405"), rev[:12]) // TODO(xmudrii): We are configured to always hit this case because we have --publish-semver-tags set to true, // so pseudo-version is not important. At some point, it would be nice to fix this. @@ -126,7 +173,7 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string, semverTag if err := tidyCommand.Run(); err != nil { return changed, fmt.Errorf("unable to run go mod tidy: %w", err) } - fmt.Printf("Completed running go mod tidy for %s.\n", tag) + fmt.Printf("Completed running go mod tidy for %s.\n", searchTag) return changed, nil } diff --git a/cmd/sync-tags/main.go b/cmd/sync-tags/main.go index 1725c68b..8113c9a5 100644 --- a/cmd/sync-tags/main.go +++ b/cmd/sync-tags/main.go @@ -257,19 +257,24 @@ func main() { } rev := commit.String() - pseudoSemver, err := semver.Parse(strings.TrimPrefix(name, "v")) + // The Go mod cache is built upon the target tag name, + // not the source tag name, so we use generated tag name here. + // Only if it doesn't exist for some reason, we fall back to + // the original tag name. + pseudoSemverString := strings.TrimPrefix(semverTag, "v") + if pseudoSemverString == "" { + pseudoSemverString = strings.TrimPrefix(name, "v") + } + pseudoSemver, err := semver.Parse(pseudoSemverString) if err != nil { glog.Fatalf("error parsing pseudo-version: %v", err) } - - pseudoVersion := fmt.Sprintf("v0.0.0-%s-%s", commitTime.UTC().Format("20060102150405"), rev[:12]) - if pseudoSemver.Major >= 2 { - if len(pseudoSemver.Pre) == 0 { - pseudoVersion = fmt.Sprintf("v%d.%d.%d-0.%s-%s", pseudoSemver.Major, pseudoSemver.Minor, pseudoSemver.Patch+1, commitTime.UTC().Format("20060102150405"), rev[:12]) - } else { - pseudoVersion = fmt.Sprintf("v%s.0.%s-%s", strings.TrimPrefix(name, "v"), commitTime.UTC().Format("20060102150405"), rev[:12]) - } + // Both v0 and v1 use v0 in pseudo-version. + moduleMajor := pseudoSemver.Major + if moduleMajor == 1 { + moduleMajor = 0 } + pseudoVersion := fmt.Sprintf("v%d.0.0-%s-%s", moduleMajor, commitTime.UTC().Format("20060102150405"), rev[:12]) fmt.Printf("Clearing cache for local tag %s.\n", pseudoVersion) if err := cleanCacheForTag(pseudoVersion); err != nil { @@ -346,11 +351,7 @@ func main() { var changed bool _, err = os.Stat("go.mod") if err == nil { - if publishSemverTag { - changed = updateGoMod(semverTag, dependentRepos, true) - } else { - changed = updateGoMod(bName, dependentRepos, false) - } + changed = updateGoMod(bName, dependentRepos, publishSemverTag) } if changed { @@ -538,11 +539,11 @@ func checkoutBranchTagCommit(r *gogit.Repository, bh plumbing.Hash, dependentRep return wt } -func updateGoMod(tag string, dependentRepos []string, publishSemverTags bool) bool { - fmt.Printf("Updating go.mod and go.sum to point to %s tag.\n", tag) - changed, err := updateGomodWithTaggedDependencies(tag, dependentRepos, publishSemverTags) +func updateGoMod(searchTag string, dependentRepos []string, publishSemverTags bool) bool { + fmt.Printf("Updating go.mod and go.sum to point to %s tag.\n", searchTag) + changed, err := updateGomodWithTaggedDependencies(searchTag, dependentRepos, publishSemverTags) if err != nil { - glog.Fatalf("Failed to update go.mod and go.sum for tag %s: %v", tag, err) + glog.Fatalf("Failed to update go.mod and go.sum for tag %s: %v", searchTag, err) } return changed }