mirror of
https://github.com/restic/restic.git
synced 2026-02-17 06:23:56 +00:00
rewrite: skip snapshot parts not matchable by include patterns
This commit is contained in:
@@ -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) {
|
func gatherIncludeFilters(includeByNameFuncs []filter.IncludeByNameFunc, printer progress.Printer) (rewriteNode walker.NodeRewriteFunc, keepEmptyDirectory walker.NodeKeepEmptyDirectoryFunc) {
|
||||||
inSelectByName := func(nodepath string, node *data.Node) bool {
|
inSelectByName := func(nodepath string, node *data.Node) bool {
|
||||||
for _, include := range includeByNameFuncs {
|
for _, include := range includeByNameFuncs {
|
||||||
if node.Type == data.NodeTypeDir {
|
|
||||||
// always include directories
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
matched, childMayMatch := include(nodepath)
|
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
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,30 +233,42 @@ func TestRewriteSnaphotSummary(t *testing.T) {
|
|||||||
rtest.Equals(t, oldSummary.TotalFilesProcessed, newSn.Summary.TotalFilesProcessed, "unexpected TotalFilesProcessed value")
|
rtest.Equals(t, oldSummary.TotalFilesProcessed, newSn.Summary.TotalFilesProcessed, "unexpected TotalFilesProcessed value")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRewriteIncludeFiles(t *testing.T) {
|
func TestRewriteInclude(t *testing.T) {
|
||||||
env, cleanup := withTestEnvironment(t)
|
for _, tc := range []struct {
|
||||||
defer cleanup()
|
name string
|
||||||
// opens repo, creates one backup of the whole lot of 'testdata'
|
opts RewriteOptions
|
||||||
createBasicRewriteRepo(t, env)
|
lsSubstring string
|
||||||
snapshots := testListSnapshots(t, env.gopts, 1)
|
lsExpectedCount int
|
||||||
|
summaryFilesExpected uint
|
||||||
// include txt files
|
}{
|
||||||
err := testRunRewriteWithOpts(t,
|
{"relative", RewriteOptions{
|
||||||
RewriteOptions{
|
|
||||||
Forget: true,
|
Forget: true,
|
||||||
IncludePatternOptions: filter.IncludePatternOptions{Includes: []string{"*.txt"}},
|
IncludePatternOptions: filter.IncludePatternOptions{Includes: []string{"*.txt"}},
|
||||||
},
|
}, ".txt", 2, 2},
|
||||||
env.gopts,
|
{"absolute", RewriteOptions{
|
||||||
[]string{"latest"})
|
Forget: true,
|
||||||
rtest.OK(t, err)
|
// test that childMatches are working by only matching a subdirectory
|
||||||
newSnapshots := testListSnapshots(t, env.gopts, 1)
|
IncludePatternOptions: filter.IncludePatternOptions{Includes: []string{"/testdata/0/for_cmd_ls"}},
|
||||||
rtest.Assert(t, snapshots[0] != newSnapshots[0], "snapshot id should have changed")
|
}, "/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)
|
rtest.OK(t, testRunRewriteWithOpts(t, tc.opts, env.gopts, []string{"latest"}))
|
||||||
sn := testLoadSnapshot(t, env.gopts, newSnapshots[0])
|
|
||||||
rtest.Assert(t, sn.Summary != nil, "snapshot should have a summary attached")
|
newSnapshots := testListSnapshots(t, env.gopts, 1)
|
||||||
rtest.Assert(t, sn.Summary.TotalFilesProcessed == 2,
|
rtest.Assert(t, snapshots[0] != newSnapshots[0], "snapshot id should have changed")
|
||||||
"there should be 2 files in the snapshot, but there are %d files", sn.Summary.TotalFilesProcessed)
|
|
||||||
|
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) {
|
func TestRewriteExcludeFiles(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user