77 "os/exec"
88 "path"
99 "strings"
10+
11+ "github.com/shopware/shopware-cli/logging"
1012)
1113
1214type GitCommit struct {
@@ -31,7 +33,7 @@ func runGit(ctx context.Context, repo string, args ...string) (string, error) {
3133 return strings .Trim (gitOuput , " " ), nil
3234}
3335
34- func getPreviousTag (ctx context.Context , repo string ) (string , error ) {
36+ func getPreviousTag (ctx context.Context , currentTag , repo string ) (string , error ) {
3537 if err := unshallowRepository (ctx , repo ); err != nil {
3638 return "" , err
3739 }
@@ -63,23 +65,36 @@ func getPreviousTag(ctx context.Context, repo string) (string, error) {
6365 continue
6466 }
6567
68+ if matchingTags [0 ] == currentTag {
69+ continue
70+ }
71+
6672 return matchingTags [0 ], nil
6773 }
6874
6975 // if no tag was found, return the first commit
7076 return commitsArray [len (commitsArray )- 1 ], nil
7177}
7278
73- func GetCommits (ctx context.Context , repo string ) ([]GitCommit , error ) {
79+ func GetCommits (ctx context.Context , currentVersion , repo string ) ([]GitCommit , error ) {
7480 if err := unshallowRepository (ctx , repo ); err != nil {
7581 return nil , err
7682 }
7783
78- previousTag , err := getPreviousTag (ctx , repo )
84+ currentTag , err := getTagForVersion (ctx , currentVersion , repo )
85+ if err != nil {
86+ return nil , err
87+ }
88+
89+ logging .FromContext (ctx ).Debugf ("Current tag: %s" , currentTag )
90+
91+ previousTag , err := getPreviousTag (ctx , currentTag , repo )
7992 if err != nil {
8093 return nil , err
8194 }
8295
96+ logging .FromContext (ctx ).Debugf ("Previous tag: %s" , previousTag )
97+
8398 commits , err := runGit (ctx , repo , "log" , "--pretty=format:%h|%s" , previousTag + "..HEAD" , "--no-merges" )
8499 if err != nil {
85100 return nil , fmt .Errorf ("cannot get commits: %w" , err )
@@ -103,6 +118,33 @@ func GetCommits(ctx context.Context, repo string) ([]GitCommit, error) {
103118 return gitCommits , nil
104119}
105120
121+ func getTagForVersion (ctx context.Context , version , repo string ) (string , error ) {
122+ version = strings .TrimPrefix (version , "v" )
123+
124+ tags , err := runGit (ctx , repo , "tag" , "--list" )
125+ if err != nil {
126+ return "" , fmt .Errorf ("failed to run git command: %w" , err )
127+ }
128+
129+ // direct tag match
130+ tagsArray := strings .Split (tags , "\n " )
131+ for _ , tag := range tagsArray {
132+ if tag == version {
133+ return tag , nil
134+ }
135+ }
136+
137+ // tag prefix match
138+ for _ , tag := range tagsArray {
139+ tag = strings .TrimPrefix (tag , "v" )
140+ if strings .HasPrefix (tag , version ) {
141+ return tag , nil
142+ }
143+ }
144+
145+ return "" , nil
146+ }
147+
106148func GetPublicVCSURL (ctx context.Context , repo string ) (string , error ) {
107149 origin , err := runGit (ctx , repo , "config" , "--get" , "remote.origin.url" )
108150 if err != nil {
0 commit comments