Skip to content

Commit 138aace

Browse files
authored
Merge pull request #1117 from shopware/fix/extension-zip-head-not-newest-tag
fix: package HEAD instead of newest repo tag in extension zip
2 parents e517fae + 359b2f8 commit 138aace

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎internal/extension/git.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import (
1212
)
1313

1414
func gitTagOrBranchOfFolder(ctx context.Context, source string) (string, error) {
15-
tagCmd := exec.CommandContext(ctx, "git", "-C", source, "tag", "--sort=-creatordate")
15+
// Prefer a tag that points exactly at HEAD. This keeps the resulting filename
16+
// in sync with the actually checked-out commit (e.g. when a CI pipeline builds
17+
// a specific tag in detached-HEAD state). Picking the newest tag in the repo
18+
// regardless of HEAD would package the wrong tree (see issue #1116 / #753).
19+
tagCmd := exec.CommandContext(ctx, "git", "-C", source, "tag", "--points-at", "HEAD", "--sort=-creatordate")
1620

1721
stdout, err := tagCmd.Output()
1822
if err != nil {

‎internal/extension/git_test.go‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package extension
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func gitRun(t *testing.T, dir string, args ...string) {
14+
t.Helper()
15+
cmd := exec.CommandContext(t.Context(), "git", append([]string{"-C", dir}, args...)...)
16+
cmd.Env = append(os.Environ(),
17+
"GIT_AUTHOR_NAME=test",
18+
"GIT_AUTHOR_EMAIL=test@example.com",
19+
"GIT_COMMITTER_NAME=test",
20+
"GIT_COMMITTER_EMAIL=test@example.com",
21+
)
22+
out, err := cmd.CombinedOutput()
23+
require.NoError(t, err, "git %v: %s", args, string(out))
24+
}
25+
26+
func writeComposerJSON(t *testing.T, dir, content string) {
27+
t.Helper()
28+
require.NoError(t, os.WriteFile(filepath.Join(dir, "composer.json"), []byte(content), 0o644))
29+
}
30+
31+
func TestGitCopyFolderArchivesHeadNotNewestTag(t *testing.T) {
32+
source := t.TempDir()
33+
ctx := t.Context()
34+
35+
gitRun(t, source, "init")
36+
gitRun(t, source, "config", "commit.gpgsign", "false")
37+
gitRun(t, source, "config", "tag.gpgSign", "false")
38+
gitRun(t, source, "config", "tag.forceSignAnnotated", "false")
39+
40+
// Older release 1.0.0
41+
writeComposerJSON(t, source, `{"version":"1.0.0"}`)
42+
gitRun(t, source, "add", ".")
43+
gitRun(t, source, "commit", "-m", "1.0.0")
44+
45+
// Current release 2.0.0 on HEAD
46+
writeComposerJSON(t, source, `{"version":"2.0.0"}`)
47+
gitRun(t, source, "add", ".")
48+
gitRun(t, source, "commit", "-m", "2.0.0")
49+
gitRun(t, source, "tag", "-a", "-m", "2.0.0", "2.0.0")
50+
51+
// Tag the older commit *after* the newer one, so a -creatordate sort over all
52+
// tags would surface 1.0.0 first - the regression from issue #1116.
53+
gitRun(t, source, "tag", "-a", "-m", "1.0.0", "1.0.0", "HEAD~1")
54+
55+
target := t.TempDir()
56+
57+
tag, err := GitCopyFolder(ctx, source, target, "")
58+
require.NoError(t, err)
59+
60+
assert.Equal(t, "2.0.0", tag)
61+
62+
content, err := os.ReadFile(filepath.Join(target, "composer.json"))
63+
require.NoError(t, err)
64+
assert.Equal(t, `{"version":"2.0.0"}`, string(content))
65+
}
66+
67+
func TestGitCopyFolderFallsBackToBranchWithoutTagAtHead(t *testing.T) {
68+
source := t.TempDir()
69+
ctx := t.Context()
70+
71+
gitRun(t, source, "init")
72+
gitRun(t, source, "config", "commit.gpgsign", "false")
73+
gitRun(t, source, "config", "tag.gpgSign", "false")
74+
gitRun(t, source, "config", "tag.forceSignAnnotated", "false")
75+
gitRun(t, source, "checkout", "-b", "main")
76+
77+
writeComposerJSON(t, source, `{"version":"1.0.0"}`)
78+
gitRun(t, source, "add", ".")
79+
gitRun(t, source, "commit", "-m", "1.0.0")
80+
gitRun(t, source, "tag", "-a", "-m", "1.0.0", "1.0.0")
81+
82+
// New unreleased commit on top of the tag - no tag points at HEAD.
83+
writeComposerJSON(t, source, `{"version":"1.0.1"}`)
84+
gitRun(t, source, "add", ".")
85+
gitRun(t, source, "commit", "-m", "wip")
86+
87+
target := t.TempDir()
88+
89+
tag, err := GitCopyFolder(ctx, source, target, "")
90+
require.NoError(t, err)
91+
92+
assert.Equal(t, "main", tag)
93+
94+
content, err := os.ReadFile(filepath.Join(target, "composer.json"))
95+
require.NoError(t, err)
96+
assert.Equal(t, `{"version":"1.0.1"}`, string(content))
97+
}

0 commit comments

Comments
 (0)