rewrite: skip snapshot parts not matchable by include patterns

This commit is contained in:
Michael Eischer 2026-01-31 22:32:54 +01:00
parent 74d60ad223
commit 901235efc9
2 changed files with 39 additions and 26 deletions

View file

@ -364,12 +364,13 @@ func runRewrite(ctx context.Context, opts RewriteOptions, gopts global.Options,
func gatherIncludeFilters(includeByNameFuncs []filter.IncludeByNameFunc, printer progress.Printer) (rewriteNode walker.NodeRewriteFunc, keepEmptyDirectory walker.NodeKeepEmptyDirectoryFunc) {
inSelectByName := func(nodepath string, node *data.Node) bool {
for _, include := range includeByNameFuncs {
if node.Type == data.NodeTypeDir {
// always include directories
return true
}
matched, childMayMatch := include(nodepath)
if matched && childMayMatch {
if node.Type == data.NodeTypeDir {
// include directories if they or some of their children may be included
if matched || childMayMatch {
return true
}
} else if matched {
return true
}
}

View file

@ -233,30 +233,42 @@ func TestRewriteSnaphotSummary(t *testing.T) {
rtest.Equals(t, oldSummary.TotalFilesProcessed, newSn.Summary.TotalFilesProcessed, "unexpected TotalFilesProcessed value")
}
func TestRewriteIncludeFiles(t *testing.T) {
env, cleanup := withTestEnvironment(t)
defer cleanup()
// opens repo, creates one backup of the whole lot of 'testdata'
createBasicRewriteRepo(t, env)
snapshots := testListSnapshots(t, env.gopts, 1)
// include txt files
err := testRunRewriteWithOpts(t,
RewriteOptions{
func TestRewriteInclude(t *testing.T) {
for _, tc := range []struct {
name string
opts RewriteOptions
lsSubstring string
lsExpectedCount int
summaryFilesExpected uint
}{
{"relative", RewriteOptions{
Forget: true,
IncludePatternOptions: filter.IncludePatternOptions{Includes: []string{"*.txt"}},
},
env.gopts,
[]string{"latest"})
rtest.OK(t, err)
newSnapshots := testListSnapshots(t, env.gopts, 1)
rtest.Assert(t, snapshots[0] != newSnapshots[0], "snapshot id should have changed")
}, ".txt", 2, 2},
{"absolute", RewriteOptions{
Forget: true,
// test that childMatches are working by only matching a subdirectory
IncludePatternOptions: filter.IncludePatternOptions{Includes: []string{"/testdata/0/for_cmd_ls"}},
}, "/testdata/0", 5, 3},
} {
t.Run(tc.name, func(t *testing.T) {
env, cleanup := withTestEnvironment(t)
defer cleanup()
createBasicRewriteRepo(t, env)
snapshots := testListSnapshots(t, env.gopts, 1)
testLsOutputContainsCount(t, env.gopts, LsOptions{}, []string{"latest"}, ".txt", 2)
sn := testLoadSnapshot(t, env.gopts, newSnapshots[0])
rtest.Assert(t, sn.Summary != nil, "snapshot should have a summary attached")
rtest.Assert(t, sn.Summary.TotalFilesProcessed == 2,
"there should be 2 files in the snapshot, but there are %d files", sn.Summary.TotalFilesProcessed)
rtest.OK(t, testRunRewriteWithOpts(t, tc.opts, env.gopts, []string{"latest"}))
newSnapshots := testListSnapshots(t, env.gopts, 1)
rtest.Assert(t, snapshots[0] != newSnapshots[0], "snapshot id should have changed")
testLsOutputContainsCount(t, env.gopts, LsOptions{}, []string{"latest"}, tc.lsSubstring, tc.lsExpectedCount)
sn := testLoadSnapshot(t, env.gopts, newSnapshots[0])
rtest.Assert(t, sn.Summary != nil, "snapshot should have a summary attached")
rtest.Assert(t, sn.Summary.TotalFilesProcessed == tc.summaryFilesExpected,
"there should be %d files in the snapshot, but there are %d files", tc.summaryFilesExpected, sn.Summary.TotalFilesProcessed)
})
}
}
func TestRewriteExcludeFiles(t *testing.T) {