diff --git a/CHANGELOG.md b/CHANGELOG.md index f771bf4..a8f6757 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Keep scoped mirror commits limited to literal file and directory selections, so bracketed filenames do not include matching neighboring files. + - Add `crawlctl preservation verify` to check all remote backup content without retaining a second local copy. - Add `crawlctl preservation inspect` to verify a pinned remote backup manifest without downloading chunks or writing files. diff --git a/docs/packages.md b/docs/packages.md index d2e78a7..4d16dcc 100644 --- a/docs/packages.md +++ b/docs/packages.md @@ -40,6 +40,10 @@ sidecars. Nonpositive limits leave file size unbounded. - `backup` writes age-encrypted JSONL/Gzip shards and manifests, manages recipients and identities, lists Git-backed history, and verifies historical restores. - `mirror` clones, initializes, pulls, commits, and pushes Git-backed archives. It also provides non-mutating fetches, immutable snapshot tags, Git-object reads, and history inspection. +`mirror.CommitPaths` selects files and directories literally after its existing +whitespace trimming and repository-relative validation. Git glob patterns and +pathspec magic are not expanded. Use `.` to commit the whole archive. + Managed sidecar trees reject overlapping source and destination roots, including mixed relative and absolute paths. Configured root symlinks remain supported; symlink directories inside a destination copy path are rejected so copying and diff --git a/mirror/mirror.go b/mirror/mirror.go index 6b390ed..9b65931 100644 --- a/mirror/mirror.go +++ b/mirror/mirror.go @@ -294,6 +294,8 @@ func Commit(ctx context.Context, opts Options, message string) (bool, error) { return CommitPaths(ctx, opts, message, []string{"."}) } +// CommitPaths commits selected files or directories without Git pathspec expansion. +// Paths retain the existing whitespace trimming and repository-relative checks. func CommitPaths(ctx context.Context, opts Options, message string, paths []string) (bool, error) { opts = normalize(opts) if message == "" { @@ -306,7 +308,7 @@ func CommitPaths(ctx context.Context, opts Options, message string, paths []stri if len(pathspecs) == 0 { return false, nil } - args := append([]string{"add", "--"}, pathspecs...) + args := append([]string{"--literal-pathspecs", "add", "--"}, pathspecs...) if err := run(ctx, opts.RepoPath, opts.Git, args...); err != nil { return false, err } @@ -318,6 +320,7 @@ func CommitPaths(ctx context.Context, opts Options, message string, paths []stri return false, nil } commitArgs := []string{ + "--literal-pathspecs", "-c", "commit.gpgsign=false", "-c", "user.name=crawlkit", "-c", "user.email=crawlkit@example.invalid", @@ -443,7 +446,7 @@ func cleanPathspecs(paths []string) ([]string, error) { func staged(ctx context.Context, opts Options, pathspecs []string) (bool, error) { opts = normalize(opts) - args := []string{"diff", "--cached", "--quiet", "--"} + args := []string{"--literal-pathspecs", "diff", "--cached", "--quiet", "--"} args = append(args, pathspecs...) out, err := output(ctx, opts.RepoPath, opts.Git, args...) if err == nil { diff --git a/mirror/mirror_test.go b/mirror/mirror_test.go index b8fb840..adb5378 100644 --- a/mirror/mirror_test.go +++ b/mirror/mirror_test.go @@ -250,6 +250,73 @@ func TestCommitPathsDoesNotCommitPrestagedUnrelatedFiles(t *testing.T) { } } +func TestCommitPathsTreatsPathsLiterally(t *testing.T) { + for _, tc := range []struct { + name string + selected string + neighbor string + }{ + {name: "file", selected: "notes[1].md", neighbor: "notes1.md"}, + {name: "directory", selected: "archives[1]", neighbor: "archives1/note.md"}, + } { + for _, prestaged := range []bool{false, true} { + name := tc.name + "/untracked" + if prestaged { + name = tc.name + "/prestaged" + } + t.Run(name, func(t *testing.T) { + ctx := context.Background() + opts := Options{RepoPath: t.TempDir()} + if err := EnsureRepo(ctx, opts); err != nil { + t.Fatal(err) + } + selectedFile := tc.selected + if tc.name == "directory" { + selectedFile += "/note.md" + } + for _, file := range []string{selectedFile, tc.neighbor} { + path := filepath.Join(opts.RepoPath, filepath.FromSlash(file)) + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(file), 0o600); err != nil { + t.Fatal(err) + } + } + if prestaged { + if err := run(ctx, opts.RepoPath, "git", "add", "--", tc.neighbor); err != nil { + t.Fatal(err) + } + } + if committed, err := CommitPaths(ctx, opts, "archive: selected path", []string{tc.selected}); err != nil || !committed { + t.Fatalf("commit = %v, %v", committed, err) + } + tree, err := output(ctx, opts.RepoPath, "git", "ls-tree", "-r", "--name-only", "HEAD") + if err != nil { + t.Fatal(err) + } + if tree != selectedFile+"\n" { + t.Errorf("committed tree = %q, want only %q", tree, selectedFile) + } + staged, err := output(ctx, opts.RepoPath, "git", "diff", "--cached", "--name-only") + if err != nil { + t.Fatal(err) + } + wantStaged := "" + if prestaged { + wantStaged = tc.neighbor + "\n" + } + if staged != wantStaged { + t.Errorf("remaining staged files = %q, want %q", staged, wantStaged) + } + if committed, err := CommitPaths(ctx, opts, "archive: unchanged selection", []string{tc.selected}); err != nil || committed { + t.Errorf("unchanged selected path commit = %v, %v; want false, nil", committed, err) + } + }) + } + } +} + func TestPullCurrentUsesExistingOrigin(t *testing.T) { ctx := context.Background() dir := t.TempDir()