Skip to content

Commit 33d6c65

Browse files
committed
feat: improve git tag handling and changelog generation with version-aware commits
1 parent 555fbb6 commit 33d6c65

4 files changed

Lines changed: 63 additions & 13 deletions

File tree

extension/zip.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,14 @@ func PrepareExtensionForRelease(ctx context.Context, sourceRoot, extensionRoot s
521521
if ext.GetExtensionConfig().Changelog.Enabled {
522522
v, _ := ext.GetVersion()
523523

524-
logging.FromContext(ctx).Infof("Generated changelog for version %s", v.String())
524+
logging.FromContext(ctx).Infof("Generated changelog for version %s", v.Original())
525525

526-
content, err := changelog.GenerateChangelog(ctx, sourceRoot, ext.GetExtensionConfig().Changelog)
526+
content, err := changelog.GenerateChangelog(ctx, v.Original(), sourceRoot, ext.GetExtensionConfig().Changelog)
527527
if err != nil {
528528
return err
529529
}
530530

531-
changelogFile := fmt.Sprintf("# %s\n%s", v.String(), content)
531+
changelogFile := fmt.Sprintf("# %s\n%s", v.Original(), content)
532532

533533
if err := os.WriteFile(path.Join(extensionRoot, "CHANGELOG_en-GB.md"), []byte(changelogFile), os.ModePerm); err != nil {
534534
return err

internal/changelog/changelog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Commit struct {
3535
}
3636

3737
// GenerateChangelog generates a changelog from the git repository.
38-
func GenerateChangelog(ctx context.Context, repository string, cfg Config) (string, error) {
38+
func GenerateChangelog(ctx context.Context, currentVersion string, repository string, cfg Config) (string, error) {
3939
var err error
4040

4141
if cfg.Template == "" {
@@ -50,7 +50,7 @@ func GenerateChangelog(ctx context.Context, repository string, cfg Config) (stri
5050
return "", err
5151
}
5252

53-
commits, err := git.GetCommits(ctx, repository)
53+
commits, err := git.GetCommits(ctx, currentVersion, repository)
5454
if err != nil {
5555
return "", err
5656
}

internal/git/git.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"os/exec"
88
"path"
99
"strings"
10+
11+
"github.com/shopware/shopware-cli/logging"
1012
)
1113

1214
type 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+
106148
func GetPublicVCSURL(ctx context.Context, repo string) (string, error) {
107149
origin, err := runGit(ctx, repo, "config", "--get", "remote.origin.url")
108150
if err != nil {

internal/git/git_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestInvalidGitRepository(t *testing.T) {
1313
repo := "invalid"
1414
ctx := t.Context()
1515

16-
tag, err := getPreviousTag(ctx, repo)
16+
tag, err := getPreviousTag(ctx, "", repo)
1717
assert.Error(t, err)
1818
assert.Empty(t, tag)
1919
}
@@ -25,11 +25,15 @@ func TestNoTags(t *testing.T) {
2525
runCommand(t, tmpDir, "add", "a")
2626
runCommand(t, tmpDir, "commit", "-m", "initial commit", "--no-verify", "--no-gpg-sign")
2727

28-
tag, err := getPreviousTag(t.Context(), tmpDir)
28+
tag, err := getPreviousTag(t.Context(), "", tmpDir)
2929
assert.NoError(t, err)
3030
assert.NotEmpty(t, tag)
3131

32-
commits, err := GetCommits(t.Context(), tmpDir)
32+
currentTag, err := getTagForVersion(t.Context(), "1.0.0", tmpDir)
33+
assert.NoError(t, err)
34+
assert.Equal(t, "", currentTag)
35+
36+
commits, err := GetCommits(t.Context(), "", tmpDir)
3337
assert.NoError(t, err)
3438
assert.Len(t, commits, 0)
3539
}
@@ -45,11 +49,15 @@ func TestWithOneTagAndCommit(t *testing.T) {
4549
runCommand(t, tmpDir, "add", "b")
4650
runCommand(t, tmpDir, "commit", "-m", "second commit", "--no-verify", "--no-gpg-sign")
4751

48-
tag, err := getPreviousTag(t.Context(), tmpDir)
52+
tag, err := getPreviousTag(t.Context(), "", tmpDir)
4953
assert.NoError(t, err)
5054
assert.Equal(t, tag, "v1.0.0")
5155

52-
commits, err := GetCommits(t.Context(), tmpDir)
56+
currentTag, err := getTagForVersion(t.Context(), "1.0.0", tmpDir)
57+
assert.NoError(t, err)
58+
assert.Equal(t, "1.0.0", currentTag)
59+
60+
commits, err := GetCommits(t.Context(), "", tmpDir)
5361
assert.NoError(t, err)
5462
assert.Len(t, commits, 1)
5563
assert.Equal(t, commits[0].Message, "second commit")

0 commit comments

Comments
 (0)