Skip to content

Commit ad36d4d

Browse files
authored
fix: git generator sorting by timestamp is inverted (#189)
1 parent f1e09ce commit ad36d4d

File tree

4 files changed

+260
-225
lines changed

4 files changed

+260
-225
lines changed

internal/generator/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (generator *GitGenerator) Versions() ([]string, error) {
176176
return true
177177
}
178178
if a != b {
179-
return a < b
179+
return a > b
180180
}
181181
aa := normalizeVersion(generator.filter.FindAllStringSubmatch(filtered[i], -1))
182182
bb := normalizeVersion(generator.filter.FindAllStringSubmatch(filtered[j], -1))

internal/generator/git_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,31 @@ func TestGitGeneratorVersionsCustomSort(t *testing.T) {
312312
assert.Nil(t, err)
313313
assert.Equal(t, []string{"2.10.0", "2.2.0", "2.1.0", "1.01.01", "1.0.0"}, versions)
314314
}
315+
316+
func TestGitGeneratorVersionsSortByTimestamp(t *testing.T) {
317+
baseTime := int64(1704000000)
318+
newerTime := baseTime + 86400
319+
320+
bundles := []gitBundle{
321+
{tag: "v1.0.0", timestamp: &baseTime, paths: []gitPath{{path: "crds/v1.yaml", file: "testdata/test-crd.yaml"}}},
322+
{tag: "v2.0.0", timestamp: &baseTime, paths: []gitPath{{path: "crds/v2.yaml", file: "testdata/test-crd.yaml"}}},
323+
{tag: "v3.0.0", timestamp: &newerTime, paths: []gitPath{{path: "crds/v3.yaml", file: "testdata/test-crd.yaml"}}},
324+
}
325+
326+
p, err := setupGit(t, bundles)
327+
assert.Nil(t, err)
328+
assert.NotNil(t, p)
329+
330+
config := configuration.Configuration{
331+
Kind: configuration.Git,
332+
Repository: fmt.Sprintf("file://%s", *p),
333+
}
334+
335+
filter := regexp.MustCompile(`^v([0-9]+\.[0-9]+\.[0-9]+)$`)
336+
generator := NewGitGenerator(config, nil, filter)
337+
defer generator.Close()
338+
339+
versions, err := generator.Versions()
340+
assert.Nil(t, err)
341+
assert.Equal(t, []string{"v3.0.0", "v2.0.0", "v1.0.0"}, versions)
342+
}

internal/generator/setup_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path"
1616
"strings"
1717
"testing"
18+
"time"
1819

1920
"github.com/stretchr/testify/assert"
2021
)
@@ -193,9 +194,10 @@ func setupOciServer(t *testing.T, chart ociChart) (mockServer, func()) {
193194
}
194195

195196
type gitBundle struct {
196-
tag string
197-
branch string
198-
paths []gitPath
197+
tag string
198+
branch string
199+
paths []gitPath
200+
timestamp *int64
199201
}
200202

201203
type gitPath struct {
@@ -272,7 +274,12 @@ func setupGit(t *testing.T, bundles []gitBundle) (*string, error) {
272274
}
273275

274276
if len(bundle.paths) > 0 {
275-
err = exec.Command("git", "--git-dir", gitDir, "--work-tree", tmpDir, "commit", "--message", "Add bundle").Run()
277+
cmd := exec.Command("git", "--git-dir", gitDir, "--work-tree", tmpDir, "commit", "--message", "Add bundle")
278+
if bundle.timestamp != nil {
279+
date := time.Unix(*bundle.timestamp, 0).Format("2006-01-02T15:04:05Z")
280+
cmd.Env = append(os.Environ(), "GIT_AUTHOR_DATE="+date, "GIT_COMMITTER_DATE="+date)
281+
}
282+
err = cmd.Run()
276283
if err != nil {
277284
return nil, fmt.Errorf("unable to commit: %w", err)
278285
}

0 commit comments

Comments
 (0)