@@ -34,7 +34,7 @@ import (
3434
3535// updateGomodWithTaggedDependencies gets the dependencies at the given tag and fills go.mod and go.sum.
3636// If anything is changed, it commits the changes. Returns true if go.mod changed.
37- func updateGomodWithTaggedDependencies (tag string , depsRepo []string , semverTag bool ) (bool , error ) {
37+ func updateGomodWithTaggedDependencies (searchTag string , depsRepo []string , semverTag bool ) (bool , error ) {
3838 found := map [string ]bool {}
3939 changed := false
4040
@@ -55,12 +55,56 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string, semverTag
5555 return changed , fmt .Errorf ("failed to get package at %s: %w" , depPath , err )
5656 }
5757
58- commit , commitTime , err := localOrPublishedTaggedCommitHashAndTime (dr , tag )
58+ commit , commitTime , err := localOrPublishedTaggedCommitHashAndTime (dr , searchTag )
5959 if err != nil {
60- return changed , fmt .Errorf ("failed to get tag %s for %q: %w" , tag , depPkg , err )
60+ return changed , fmt .Errorf ("failed to get tag %s for %q: %w" , searchTag , depPkg , err )
6161 }
6262 rev := commit .String ()
63- pseudoVersionOrTag := fmt .Sprintf ("v0.0.0-%s-%s" , commitTime .UTC ().Format ("20060102150405" ), rev [:12 ])
63+
64+ moduleMajor := uint64 (0 )
65+ tag := ""
66+ if semverTag {
67+ iter , err := dr .Tags ()
68+ if err != nil {
69+ return changed , fmt .Errorf ("failed to get tags for repo %s: %w" , dep , err )
70+ }
71+
72+ var tags []string
73+ err = iter .ForEach (func (ref * plumbing.Reference ) error {
74+ if tagObj , err := dr .TagObject (ref .Hash ()); err == nil {
75+ if tagObj .Target == commit && strings .HasPrefix (strings .TrimPrefix (ref .Name ().Short (), "origin/" ), "v" ) {
76+ tags = append (tags , ref .Name ().Short ())
77+ }
78+ }
79+
80+ return nil
81+ })
82+ if err != nil {
83+ return changed , fmt .Errorf ("failed to find semver tag for given search tag: %w" , err )
84+ }
85+ iter .Close ()
86+
87+ if len (tags ) == 0 {
88+ return changed , fmt .Errorf ("no semver tag found for given search tag %s" , searchTag )
89+ }
90+
91+ // pick the highest semver tag
92+ semvers := make ([]semver.Version , 0 , len (tags ))
93+ for _ , t := range tags {
94+ sv , err := semver .Parse (strings .TrimPrefix (t , "v" ))
95+ if err != nil {
96+ return changed , fmt .Errorf ("failed to parse semver tag %s: %w" , t , err )
97+ }
98+ semvers = append (semvers , sv )
99+ }
100+ semver .Sort (semvers )
101+
102+ latestSemver := semvers [len (semvers )- 1 ]
103+ moduleMajor = latestSemver .Major
104+ tag = fmt .Sprintf ("v%s" , latestSemver .String ())
105+ }
106+
107+ pseudoVersionOrTag := fmt .Sprintf ("v%d.0.0-%s-%s" , moduleMajor , commitTime .UTC ().Format ("20060102150405" ), rev [:12 ])
64108
65109 // TODO(xmudrii): We are configured to always hit this case because we have --publish-semver-tags set to true,
66110 // so pseudo-version is not important. At some point, it would be nice to fix this.
@@ -126,7 +170,7 @@ func updateGomodWithTaggedDependencies(tag string, depsRepo []string, semverTag
126170 if err := tidyCommand .Run (); err != nil {
127171 return changed , fmt .Errorf ("unable to run go mod tidy: %w" , err )
128172 }
129- fmt .Printf ("Completed running go mod tidy for %s.\n " , tag )
173+ fmt .Printf ("Completed running go mod tidy for %s.\n " , searchTag )
130174
131175 return changed , nil
132176}
0 commit comments