@@ -6,9 +6,11 @@ import (
66 "os"
77 "os/exec"
88 "path"
9+ "sort"
910 "strings"
1011
1112 "github.com/shopware/shopware-cli/logging"
13+ "github.com/shyim/go-version"
1214)
1315
1416type GitCommit struct {
@@ -34,44 +36,56 @@ func runGit(ctx context.Context, repo string, args ...string) (string, error) {
3436}
3537
3638func getPreviousTag (ctx context.Context , currentTag , repo string ) (string , error ) {
37- if err := unshallowRepository (ctx , repo ); err != nil {
38- return "" , err
39- }
40-
4139 previousVersion := os .Getenv ("SHOPWARE_CLI_PREVIOUS_TAG" )
4240 if previousVersion != "" {
4341 return previousVersion , nil
4442 }
4543
46- commits , err := runGit (ctx , repo , "log " , "--pretty=format:%h" , "--no-merges " )
44+ tags , err := runGit (ctx , repo , "tag " , "--list " )
4745 if err != nil {
48- return "" , fmt .Errorf ("cannot get previous tag : %w" , err )
46+ return "" , fmt .Errorf ("failed to run git command : %w" , err )
4947 }
5048
51- commitsArray := strings .Split (commits , "\n " )
52- for commit := range commitsArray {
53- contains , err := runGit (ctx , repo , "tag" , "--contains" , commitsArray [commit ])
54- if err != nil {
55- return "" , fmt .Errorf ("cannot get previous tag: %w" , err )
56- }
49+ // direct tag match
50+ tagsArray := strings .Split (tags , "\n " )
51+
52+ var tagList []* version.Version
5753
58- if contains == "" {
54+ for _ , tag := range tagsArray {
55+ v , err := version .NewVersion (tag )
56+ if err != nil {
5957 continue
6058 }
6159
62- matchingTags := strings .Split (contains , "\n " )
60+ tagList = append (tagList , v )
61+ }
6362
64- if len (matchingTags ) == 0 {
65- continue
63+ currentVersion := version .Must (version .NewVersion (currentTag ))
64+
65+ sort .Sort (sort .Reverse (version .Collection (tagList )))
66+
67+ // same major version
68+ currentMajor := currentVersion .Segments ()[0 ]
69+ for _ , tag := range tagList {
70+ if tag .Segments ()[0 ] == currentMajor && tag .LessThan (currentVersion ) {
71+ return tag .String (), nil
6672 }
73+ }
6774
68- if matchingTags [0 ] == currentTag {
69- continue
75+ // Look at previous major version
76+ for _ , tag := range tagList {
77+ if tag .Segments ()[0 ] == currentMajor - 1 {
78+ return tag .String (), nil
7079 }
80+ }
7181
72- return matchingTags [0 ], nil
82+ commits , err := runGit (ctx , repo , "log" , "--pretty=format:%h" , "--no-merges" )
83+ if err != nil {
84+ return "" , fmt .Errorf ("cannot get previous tag: %w" , err )
7385 }
7486
87+ commitsArray := strings .Split (commits , "\n " )
88+
7589 // if no tag was found, return the first commit
7690 return commitsArray [len (commitsArray )- 1 ], nil
7791}
@@ -94,6 +108,7 @@ func GetCommits(ctx context.Context, currentVersion, repo string) ([]GitCommit,
94108 }
95109
96110 logging .FromContext (ctx ).Debugf ("Previous tag: %s" , previousTag )
111+ logging .FromContext (ctx ).Debugf ("Diffing %s..HEAD" , previousTag )
97112
98113 commits , err := runGit (ctx , repo , "log" , "--pretty=format:%h|%s" , previousTag + "..HEAD" , "--no-merges" )
99114 if err != nil {
@@ -142,7 +157,7 @@ func getTagForVersion(ctx context.Context, version, repo string) (string, error)
142157 }
143158 }
144159
145- return "" , nil
160+ return version , nil
146161}
147162
148163func GetPublicVCSURL (ctx context.Context , repo string ) (string , error ) {
0 commit comments