Skip to content

Commit b62ca33

Browse files
author
fredbi
authored
squash: added option to retain n latest versions (#747)
Signed-off-by: Frederic BIDON <frederic@oneconcern.com> Signed-off-by: Frederic BIDON <frederic@oneconcern.com>
1 parent 9090a85 commit b62ca33

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

cmd/datamon/cmd/context_squash.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func applyRepoSquash(remoteStores context2.Stores, datamonFlags *flagsT, logger
6969
return core.RepoSquash(remoteStores, repo.Name,
7070
core.WithRetainTags(datamonFlags.squash.RetainTags),
7171
core.WithRetainSemverTags(datamonFlags.squash.RetainSemverTags),
72+
core.WithRetainNLatest(datamonFlags.squash.RetainNLatest),
7273
core.ConcurrentList(datamonFlags.core.ConcurrencyFactor),
7374
core.BatchSize(datamonFlags.core.BatchSize),
7475
)
@@ -81,6 +82,7 @@ func init() {
8182
)
8283
addRetainTagsFlag(contextSquash)
8384
addRetainSemverTagsFlag(contextSquash)
85+
addRetainNLatestFlag(contextSquash)
8486
addCoreConcurrencyFactorFlag(contextSquash, 500)
8587
addBatchSizeFlag(contextSquash)
8688

cmd/datamon/cmd/flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type flagsT struct {
105105
squash struct {
106106
RetainTags bool
107107
RetainSemverTags bool
108+
RetainNLatest int
108109
}
109110
}
110111

@@ -641,6 +642,14 @@ func addRepoSizeFlag(cmd *cobra.Command) string {
641642
return c
642643
}
643644

645+
func addRetainNLatestFlag(cmd *cobra.Command) string {
646+
const c = "retain-n-latest"
647+
if cmd != nil {
648+
cmd.Flags().IntVar(&datamonFlags.squash.RetainNLatest, c, 1, "Squash past bundles and retain n latest versions. May be combined with retain-tags and retain-semver-flags")
649+
}
650+
return c
651+
}
652+
644653
/** parameters struct from other formats */
645654

646655
// apply config file + env vars to structure used to parse cli flags

cmd/datamon/cmd/repo_squash.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Optionally, the squashing may also retain past tagged bundles, or only past tagg
3535
err = core.RepoSquash(remoteStores, datamonFlags.repo.RepoName,
3636
core.WithRetainTags(datamonFlags.squash.RetainTags),
3737
core.WithRetainSemverTags(datamonFlags.squash.RetainSemverTags),
38+
core.WithRetainNLatest(datamonFlags.squash.RetainNLatest),
3839
core.ConcurrentList(datamonFlags.core.ConcurrencyFactor),
3940
core.BatchSize(datamonFlags.core.BatchSize),
4041
)
@@ -67,6 +68,7 @@ func init() {
6768
)
6869
addRetainTagsFlag(repoSquash)
6970
addRetainSemverTagsFlag(repoSquash)
71+
addRetainNLatestFlag(repoSquash)
7072
addCoreConcurrencyFactorFlag(repoSquash, 500)
7173
addBatchSizeFlag(repoSquash)
7274

cmd/datamon/cmd/repo_squash_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,25 @@ func TestSquashRepo(t *testing.T) {
222222
require.Contains(t, lines[2], "sixth and last commit for the repo")
223223
})
224224

225-
t.Run("should squash repo13 with no tags retained and get only the last commit", func(t *testing.T) {
225+
runCmd(t, []string{"bundle",
226+
"upload",
227+
"--path", input3,
228+
"--message", "keep this commit",
229+
"--repo", repo13,
230+
"--context", dcontext,
231+
}, fmt.Sprintf("added bundle at %q", input3), false)
232+
233+
t.Run("should squash repo13 with no tags retained and get only the 2 last commits", func(t *testing.T) {
226234
r, w := startCapture(t)
227235
runCmd(t, []string{"repo",
228236
"squash",
237+
"--retain-n-latest", "2",
229238
"--repo", repo13,
230239
}, fmt.Sprintf("squashed repo %q", repo13), false)
231240
lines := endCapture(t, r, w, []string{})
232-
require.Len(t, lines, 1)
241+
require.Len(t, lines, 2)
233242
require.Contains(t, lines[0], "sixth and last commit for the repo")
243+
require.Contains(t, lines[1], "keep this commit")
234244
})
235245

236246
t.Run("with squash context", func(t *testing.T) {

pkg/core/options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Settings struct {
2424
ignoreCorruptedMetadata bool
2525
retainTags bool
2626
retainSemverTags bool
27+
retainNLatest int
2728
// m *M // TODO(fred): enable metrics for list operations
2829
}
2930

@@ -113,10 +114,19 @@ func WithRetainSemverTags(enabled bool) Option {
113114
}
114115
}
115116

117+
func WithRetainNLatest(n int) Option {
118+
return func(s *Settings) {
119+
if n > 0 {
120+
s.retainNLatest = n
121+
}
122+
}
123+
}
124+
116125
func defaultSettings() Settings {
117126
return Settings{
118127
concurrentList: defaultListConcurrency,
119128
batchSize: defaultBatchSize,
120129
memProfDir: ".",
130+
retainNLatest: 1,
121131
}
122132
}

pkg/core/repo_squash.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ func RepoSquash(stores context2.Stores, repoName string, opts ...Option) error {
1313
return err
1414
}
1515

16-
if len(bundles) < 2 {
17-
// nothing to be squashed
18-
return nil
19-
}
20-
2116
settings := defaultSettings()
2217
for _, bApply := range opts {
2318
bApply(&settings)
2419
}
20+
if settings.retainNLatest == 0 {
21+
settings.retainNLatest = 1
22+
}
23+
24+
if len(bundles) < settings.retainNLatest+1 {
25+
// nothing to be squashed
26+
return nil
27+
}
2528

2629
labelsIndex := make(map[string]struct{}, 10)
2730
if settings.retainTags || settings.retainSemverTags {
@@ -52,7 +55,7 @@ func RepoSquash(stores context2.Stores, repoName string, opts ...Option) error {
5255
sort.SliceStable(bundles, func(i, j int) bool {
5356
return bundles[i].Timestamp.Before(bundles[j].Timestamp)
5457
})
55-
for _, bundle := range bundles[:len(bundles)-1] {
58+
for _, bundle := range bundles[:len(bundles)-settings.retainNLatest] {
5659
if settings.retainTags || settings.retainSemverTags {
5760
if _, retain := labelsIndex[bundle.ID]; retain {
5861
continue

0 commit comments

Comments
 (0)