oy-cli 0.9.2

Local AI coding CLI for inspecting, editing, running commands, and auditing repositories
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
use anyhow::{Context, Result, anyhow, bail};
use diffy::patch_set::{FileOperation, FilePatch, ParseOptions, PatchSet};
use diffy::{apply, create_patch};
use glob::glob;
use globset::{Glob, GlobSet, GlobSetBuilder};
use grep_regex::RegexMatcher;
use grep_searcher::SearcherBuilder;
use grep_searcher::sinks::UTF8;
use ignore::WalkBuilder;
use regex::Regex;
use serde::Serialize;
use serde_json::Value;
use std::collections::BTreeSet;
use std::fs;
use std::path::{Component, Path, PathBuf};
use tokei::{Config as TokeiConfig, Languages as TokeiLanguages, Sort as TokeiSort};

use crate::config;

use super::args::{
    ExcludeArg, ListArgs, PatchArgs, ReadArgs, ReplaceArgs, ReplaceMode, SearchArgs, SearchMode,
    SlocArgs,
};
use super::{Approval, PREVIEW_ITEMS, ToolContext, require_mutation_approval};

pub(super) const MAX_WORKSPACE_FILE_BYTES: u64 = 2 * 1024 * 1024;
const MAX_SEARCH_MATCHES: usize = 10_000;

#[derive(Debug, Serialize)]
pub(super) struct ListOutput {
    pub path: String,
    pub items: Vec<String>,
    pub count: usize,
    pub truncated: bool,
    pub exclude: Option<Vec<String>>,
}

#[derive(Debug, Serialize)]
pub(super) struct ReadOutput {
    pub path: String,
    pub offset: usize,
    pub limit: usize,
    pub text: String,
    pub line_count: usize,
    pub truncated: bool,
}

#[derive(Debug, Serialize)]
pub(super) struct SearchHit {
    pub path: String,
    pub line_number: usize,
    pub column: usize,
    pub text: String,
}

#[derive(Debug, Serialize)]
pub(super) struct ToolErrorItem {
    pub path: String,
    pub message: String,
}

#[derive(Debug, Serialize)]
pub(super) struct SearchOutput {
    pub pattern: String,
    pub mode: &'static str,
    pub warning: Option<String>,
    pub path: String,
    pub match_count: usize,
    pub matches: Vec<SearchHit>,
    pub truncated: bool,
    pub exclude: Option<Vec<String>>,
    pub errors: Option<Vec<ToolErrorItem>>,
}

#[derive(Debug, Serialize)]
pub(super) struct ChangedFileOutput {
    pub path: String,
    pub replacements: usize,
    pub diff: String,
}

#[derive(Debug, Serialize)]
pub(super) struct SkippedFileOutput {
    pub path: String,
    pub reason: &'static str,
}

#[derive(Debug, Serialize)]
pub(super) struct ReplaceOutput {
    pub pattern: String,
    pub replacement: String,
    pub mode: &'static str,
    pub path: String,
    pub changed_file_count: usize,
    pub replacement_count: usize,
    pub changed_files: Vec<ChangedFileOutput>,
    pub diff: String,
    pub truncated: bool,
    pub exclude: Option<Vec<String>>,
    pub skipped: Vec<SkippedFileOutput>,
    pub errors: Vec<ToolErrorItem>,
}

#[derive(Debug, Serialize)]
pub(super) struct PatchChangedFileOutput {
    pub path: String,
    pub diff: String,
}

#[derive(Debug, Serialize)]
pub(super) struct PatchOutput {
    pub patch_count: usize,
    pub changed_file_count: usize,
    pub changed_files: Vec<PatchChangedFileOutput>,
    pub diff: String,
    pub truncated: bool,
}

struct PatchPlan {
    path: PathBuf,
    display_path: String,
    updated: String,
    diff: String,
}

#[derive(Debug, Serialize)]
pub(super) struct SlocOutput {
    pub path: String,
    pub format: &'static str,
    pub output: Value,
    pub exclude: Option<Vec<String>>,
}

// === Workspace tool implementations ===
pub(super) fn tool_list(ctx: &ToolContext, args: ListArgs) -> Result<Value> {
    reject_out_of_workspace_path(&ctx.root, &args.path, None)?;
    let exclude = build_exclude_set(args.exclude.as_ref())?;
    let shown_limit = args.limit.max(1);
    let items = if args.path == "." || args.path == "./" {
        let mut out = Vec::new();
        for entry in fs::read_dir(&ctx.root)? {
            let path = entry?.path();
            let rel = rel_path(&ctx.root, &path);
            if exclude.is_match(rel.as_str()) {
                continue;
            }
            out.push(display_path(&ctx.root, &path));
        }
        out.sort();
        out
    } else {
        let pattern = if Path::new(&args.path).is_absolute() {
            args.path.clone()
        } else {
            ctx.root.join(&args.path).to_string_lossy().to_string()
        };
        let mut out = glob(&pattern)?
            .filter_map(|entry| entry.ok())
            .filter_map(|path| safe_list_item(&ctx.root, &path))
            .filter(|item| !exclude.is_match(item.as_str()))
            .collect::<Vec<_>>();
        out.sort();
        out.dedup();
        out
    };
    Ok(serde_json::to_value(ListOutput {
        path: args.path,
        items: items.iter().take(shown_limit).cloned().collect(),
        count: items.len(),
        truncated: items.len() > shown_limit,
        exclude: args.exclude.as_ref().map(ExcludeArg::patterns),
    })?)
}

pub(super) fn tool_read(ctx: &ToolContext, args: ReadArgs) -> Result<Value> {
    let path = resolve_existing_path(ctx, &args.path)?;
    if path.is_dir() {
        bail!("read path is a directory: {}", args.path);
    }
    let Some(item) = read_text_file(&ctx.root, &path)? else {
        bail!("read path is not utf-8 text: {}", args.path);
    };
    let display_path = item.display_path;
    let text = item.text;
    let mut shown = Vec::new();
    let start = args.offset.saturating_sub(1);
    let stop = start + args.limit.max(1);
    let mut line_count = 0usize;
    for (idx, line) in text.lines().enumerate() {
        line_count = idx + 1;
        if idx < start {
            continue;
        }
        if idx < stop {
            shown.push(line.to_string());
        }
    }
    let truncated = line_count > stop;
    Ok(serde_json::to_value(ReadOutput {
        path: display_path,
        offset: args.offset,
        limit: args.limit,
        text: shown.join("\n"),
        line_count,
        truncated,
    })?)
}

fn search_matchers(
    pattern: &str,
    mode: SearchMode,
) -> Result<(RegexMatcher, Regex, &'static str, Option<String>)> {
    match mode {
        SearchMode::Regex => Ok((
            RegexMatcher::new_line_matcher(pattern)
                .with_context(|| format!("invalid regex: {pattern}"))?,
            Regex::new(pattern).with_context(|| format!("invalid regex: {pattern}"))?,
            "regex",
            None,
        )),
        SearchMode::Literal => {
            let escaped = regex::escape(pattern);
            Ok((
                RegexMatcher::new_line_matcher(&escaped)?,
                Regex::new(&escaped)?,
                "literal",
                None,
            ))
        }
        SearchMode::Auto => match Regex::new(pattern) {
            Ok(regex) => Ok((
                RegexMatcher::new_line_matcher(pattern)
                    .with_context(|| format!("invalid regex: {pattern}"))?,
                regex,
                "regex",
                None,
            )),
            Err(err) => {
                let escaped = regex::escape(pattern);
                Ok((
                    RegexMatcher::new_line_matcher(&escaped)?,
                    Regex::new(&escaped)?,
                    "literal",
                    Some(format!(
                        "pattern was not valid regex; searched literally: {err}"
                    )),
                ))
            }
        },
    }
}

fn replace_matcher_and_replacement(args: &ReplaceArgs) -> Result<(Regex, String, &'static str)> {
    match args.mode {
        ReplaceMode::Regex => Ok((
            Regex::new(&args.pattern)
                .with_context(|| format!("invalid regex: {}", args.pattern))?,
            args.replacement.clone(),
            "regex",
        )),
        ReplaceMode::Literal => Ok((
            Regex::new(&regex::escape(&args.pattern))?,
            args.replacement.replace('$', "$$"),
            "literal",
        )),
    }
}

pub(super) fn tool_search(ctx: &ToolContext, args: SearchArgs) -> Result<Value> {
    let (matcher, column_regex, mode, warning) = search_matchers(&args.pattern, args.mode)?;
    let exclude = build_exclude_set(args.exclude.as_ref())?;
    let targets = resolve_existing_paths(ctx, &args.path)?;
    let shown = args.limit.max(1);
    let cap = shown.min(MAX_SEARCH_MATCHES);
    let mut matches = Vec::new();
    let mut errors = Vec::new();
    let mut truncated = false;
    for target in &targets {
        for path in walk_files(&ctx.root, target, &exclude)? {
            match search_file_limited(
                &ctx.root,
                &path,
                &matcher,
                &column_regex,
                cap.saturating_sub(matches.len()),
            ) {
                Ok(SearchFileMatches {
                    matches: mut found,
                    truncated: file_truncated,
                }) => {
                    matches.append(&mut found);
                    if file_truncated || matches.len() >= cap {
                        truncated = true;
                        break;
                    }
                }
                Err(err) => errors.push(ToolErrorItem {
                    path: rel_path(&ctx.root, &path),
                    message: err.to_string(),
                }),
            }
        }
        if truncated {
            break;
        }
    }
    Ok(serde_json::to_value(SearchOutput {
        pattern: args.pattern,
        mode,
        warning,
        path: args.path,
        match_count: matches.len(),
        matches,
        truncated,
        exclude: args.exclude.as_ref().map(ExcludeArg::patterns),
        errors: (!errors.is_empty()).then_some(errors),
    })?)
}
pub(super) fn tool_replace(ctx: &ToolContext, args: ReplaceArgs) -> Result<Value> {
    let (regex, replacement, mode) = replace_matcher_and_replacement(&args)?;
    let exclude = build_exclude_set(args.exclude.as_ref())?;
    let target = resolve_existing_path(ctx, &args.path)?;
    let approval_preview = if ctx.policy.approval("replace") == Approval::Ask && ctx.interactive {
        preview_replace_plan(ctx, &args, &regex, &replacement, &target, &exclude).ok()
    } else {
        None
    };
    require_mutation_approval(ctx, "replace", approval_preview.as_deref())?;
    let mut changed_files = Vec::new();
    let mut skipped = Vec::new();
    let mut errors = Vec::new();
    let mut replacement_count = 0usize;
    for path in walk_files(&ctx.root, &target, &exclude)? {
        match replace_file(&path, &regex, &replacement) {
            Ok(ReplaceOutcome::Changed { count, diff }) => {
                changed_files.push(ChangedFileOutput {
                    path: rel_path(&ctx.root, &path),
                    replacements: count,
                    diff,
                });
                replacement_count += count;
            }
            Ok(ReplaceOutcome::Unchanged) => {}
            Ok(ReplaceOutcome::Skipped(reason)) => skipped.push(SkippedFileOutput {
                path: rel_path(&ctx.root, &path),
                reason,
            }),
            Err(err) => errors.push(ToolErrorItem {
                path: rel_path(&ctx.root, &path),
                message: err.to_string(),
            }),
        }
    }
    let shown = args.limit.max(1);
    let changed_file_count = changed_files.len();
    let diff = combined_diff(&changed_files);
    Ok(serde_json::to_value(ReplaceOutput {
        pattern: args.pattern,
        replacement: args.replacement,
        mode,
        path: args.path,
        changed_file_count,
        replacement_count,
        changed_files: changed_files.into_iter().take(shown).collect(),
        diff,
        truncated: changed_file_count > shown,
        exclude: args.exclude.as_ref().map(ExcludeArg::patterns),
        skipped,
        errors,
    })?)
}

pub(super) fn tool_patch(ctx: &ToolContext, mut args: PatchArgs) -> Result<Value> {
    // diffy parses incorrectly when the patch doesn't end with a newline:
    // Insert lines lack trailing \n (silent corruption) and context lines
    // fail to match (apply error).
    if !args.patch.ends_with('\n') {
        args.patch.push('\n');
    }
    let (patch_count, plans) = plan_patch(ctx, &args)?;
    let approval_preview = if ctx.policy.approval("patch") == Approval::Ask && ctx.interactive {
        Some(combined_patch_diff(&plans))
    } else {
        None
    };
    require_mutation_approval(ctx, "patch", approval_preview.as_deref())?;

    for plan in &plans {
        config::write_workspace_file(&plan.path, plan.updated.as_bytes())?;
    }

    let shown = args.limit.max(1);
    let changed_file_count = plans.len();
    let diff = combined_patch_diff(&plans);
    Ok(serde_json::to_value(PatchOutput {
        patch_count,
        changed_file_count,
        changed_files: plans
            .into_iter()
            .take(shown)
            .map(|plan| PatchChangedFileOutput {
                path: plan.display_path,
                diff: plan.diff,
            })
            .collect(),
        diff,
        truncated: changed_file_count > shown,
    })?)
}

pub(super) fn tool_sloc(ctx: &ToolContext, args: SlocArgs) -> Result<Value> {
    let targets = resolve_existing_paths(ctx, &args.path)?;
    let exclude = args
        .exclude
        .as_ref()
        .map(ExcludeArg::patterns)
        .unwrap_or_default();
    let targets = targets
        .iter()
        .map(|path| path.to_string_lossy().to_string())
        .collect::<Vec<_>>();
    let target_refs = targets.iter().map(String::as_str).collect::<Vec<_>>();
    let excluded = exclude.iter().map(String::as_str).collect::<Vec<_>>();

    let config = TokeiConfig {
        hidden: Some(false),
        no_ignore: Some(false),
        no_ignore_parent: Some(false),
        no_ignore_dot: Some(false),
        no_ignore_vcs: Some(false),
        ..TokeiConfig::default()
    };
    let mut languages = TokeiLanguages::new();
    languages.get_statistics(&target_refs, &excluded, &config);
    sort_tokei_reports(&mut languages);

    let mut output = serde_json::to_value(&languages)?;
    if let Value::Object(ref mut map) = output {
        map.insert(
            "Total".to_string(),
            serde_json::to_value(languages.total())?,
        );
    }

    Ok(serde_json::to_value(SlocOutput {
        path: args.path,
        format: "tokei-json",
        output,
        exclude: (!exclude.is_empty()).then_some(exclude),
    })?)
}

fn sort_tokei_reports(languages: &mut TokeiLanguages) {
    for language in languages.values_mut() {
        language.sort_by(TokeiSort::Code);
    }
}

// === Workspace filesystem boundary ===
fn reject_out_of_workspace_path(root: &Path, path: &str, resolved: Option<&Path>) -> Result<()> {
    let raw = Path::new(path);
    if raw.is_absolute() {
        bail!("path outside workspace is not allowed: {path} (absolute path)");
    }
    if raw.components().any(|c| matches!(c, Component::ParentDir)) {
        bail!("path outside workspace is not allowed: {path} (parent-directory path)");
    }
    if let Some(resolved) = resolved.filter(|resolved| !within_root(root, resolved)) {
        bail!(
            "path outside workspace is not allowed: {path} -> {}",
            resolved.display()
        );
    }
    Ok(())
}

fn resolve_existing_path(ctx: &ToolContext, path: &str) -> Result<PathBuf> {
    reject_out_of_workspace_path(&ctx.root, path, None)?;
    let joined = ctx.root.join(path);
    let resolved = joined
        .canonicalize()
        .with_context(|| format!("path does not exist: {path}"))?;
    reject_out_of_workspace_path(&ctx.root, path, Some(&resolved))?;
    Ok(resolved)
}

fn resolve_existing_paths(ctx: &ToolContext, path: &str) -> Result<Vec<PathBuf>> {
    match resolve_existing_path(ctx, path) {
        Ok(path) => Ok(vec![path]),
        Err(full_path_error) => {
            let parts = path.split_whitespace().collect::<Vec<_>>();
            if parts.len() <= 1 {
                return Err(full_path_error);
            }
            let mut out = Vec::new();
            for part in parts {
                out.push(resolve_existing_path(ctx, part)?);
            }
            out.sort();
            out.dedup();
            Ok(out)
        }
    }
}

fn build_exclude_set(exclude: Option<&ExcludeArg>) -> Result<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    if let Some(exclude) = exclude {
        for pattern in exclude.patterns() {
            builder.add(
                Glob::new(&pattern).with_context(|| format!("invalid exclude glob: {pattern}"))?,
            );
        }
    }
    Ok(builder.build()?)
}

fn rel_path(root: &Path, path: &Path) -> String {
    path.strip_prefix(root)
        .map(|p| p.to_string_lossy().replace('\\', "/"))
        .unwrap_or_else(|_| path.to_string_lossy().replace('\\', "/"))
}

fn display_path(root: &Path, path: &Path) -> String {
    let mut value = rel_path(root, path);
    if path.is_dir() && !value.ends_with('/') {
        value.push('/');
    }
    value
}

fn safe_list_item(root: &Path, path: &Path) -> Option<String> {
    let resolved = path.canonicalize().ok()?;
    if !within_root(root, &resolved) {
        return None;
    }
    Some(display_path(root, path))
}

fn within_root(root: &Path, path: &Path) -> bool {
    path == root || path.starts_with(root)
}

fn walk_files(root: &Path, target: &Path, exclude: &GlobSet) -> Result<Vec<PathBuf>> {
    if target.is_file() {
        return Ok(vec![target.to_path_buf()]);
    }
    let mut out = Vec::new();
    let mut builder = WalkBuilder::new(target);
    builder
        .hidden(false)
        .git_ignore(true)
        .git_global(false)
        .git_exclude(true);
    for entry in builder.build() {
        let entry = match entry {
            Ok(entry) => entry,
            Err(err) => return Err(anyhow!(err)),
        };
        let path = entry.path();
        if entry.file_type().is_some_and(|ft| ft.is_file()) {
            let rel = rel_path(root, path);
            if exclude.is_match(rel.as_str()) {
                continue;
            }
            out.push(path.to_path_buf());
        }
    }
    Ok(out)
}

struct SearchText {
    display_path: String,
    text: String,
}

fn read_text_file(root: &Path, path: &Path) -> Result<Option<SearchText>> {
    let rel = rel_path(root, path);
    if fs::metadata(path)?.len() > MAX_WORKSPACE_FILE_BYTES {
        bail!(
            "file exceeds workspace read cap of {} bytes: {rel}",
            MAX_WORKSPACE_FILE_BYTES
        );
    }
    let raw = fs::read(path)?;
    Ok(crate::decode_utf8(raw).ok().map(|text| SearchText {
        display_path: rel,
        text,
    }))
}

fn push_match(
    display_path: &str,
    line_number: usize,
    line: &str,
    column: usize,
    out: &mut Vec<SearchHit>,
) {
    out.push(SearchHit {
        path: display_path.to_string(),
        line_number,
        column,
        text: crate::ui::truncate_chars(line.trim_end_matches(['\r', '\n']), 1000),
    });
}

fn search_text_grep(
    display_path: &str,
    text: &str,
    matcher: &RegexMatcher,
    column_regex: &Regex,
    limit: usize,
    out: &mut Vec<SearchHit>,
) -> Result<bool> {
    if limit == 0 {
        return Ok(true);
    }
    let mut truncated = false;
    let mut searcher = SearcherBuilder::new().line_number(true).build();
    let mut sink = UTF8(|line_number, line: &str| {
        if out.len() >= limit {
            truncated = true;
            return Ok(false);
        }
        let column = column_regex.find(line).map(|m| m.start() + 1).unwrap_or(1);
        push_match(display_path, line_number as usize, line, column, out);
        if out.len() >= limit {
            truncated = true;
            return Ok(false);
        }
        Ok(true)
    });
    searcher.search_reader(matcher, text.as_bytes(), &mut sink)?;
    Ok(truncated)
}

pub(super) struct SearchFileMatches {
    pub(super) matches: Vec<SearchHit>,
    pub(super) truncated: bool,
}

pub(super) fn search_file_limited(
    root: &Path,
    path: &Path,
    matcher: &RegexMatcher,
    column_regex: &Regex,
    limit: usize,
) -> Result<SearchFileMatches> {
    let mut matches = Vec::new();
    let mut truncated = false;
    if let Some(item) = read_text_file(root, path)? {
        truncated = search_text_grep(
            &item.display_path,
            &item.text,
            matcher,
            column_regex,
            limit,
            &mut matches,
        )?;
    }
    Ok(SearchFileMatches { matches, truncated })
}

#[cfg(test)]
pub(super) fn search_file(
    root: &Path,
    path: &Path,
    matcher: &RegexMatcher,
    column_regex: &Regex,
) -> Result<Vec<SearchHit>> {
    Ok(search_file_limited(root, path, matcher, column_regex, usize::MAX)?.matches)
}

fn parse_patch_set(text: &str) -> Result<Vec<FilePatch<'_, str>>> {
    let git =
        PatchSet::parse(text, ParseOptions::gitdiff()).collect::<std::result::Result<Vec<_>, _>>();
    match git {
        Ok(patches) if !patches.is_empty() => Ok(patches),
        _ => PatchSet::parse(text, ParseOptions::unidiff())
            .collect::<std::result::Result<Vec<_>, _>>()
            .context("invalid patch"),
    }
}

fn plan_patch(ctx: &ToolContext, args: &PatchArgs) -> Result<(usize, Vec<PatchPlan>)> {
    let patches = parse_patch_set(args.patch.as_str())?;
    if patches.is_empty() {
        bail!("patch did not contain any file changes");
    }

    let patch_count = patches.len();
    let mut seen = BTreeSet::new();
    let mut plans = Vec::new();
    for file_patch in patches {
        let operation = file_patch.operation().strip_prefix(args.strip);
        let patch_path = patch_path_from_operation(&operation)?;
        let text_patch = file_patch
            .patch()
            .as_text()
            .ok_or_else(|| anyhow!("binary patches are not supported: {patch_path}"))?;
        let path = resolve_existing_path(ctx, &patch_path)?;
        if path.is_dir() {
            bail!("cannot patch directory: {patch_path}");
        }
        if ctx.root.join(&patch_path).is_symlink() || path.is_symlink() {
            bail!("cannot patch symlink: {patch_path}");
        }
        if fs::metadata(&path)?.len() > MAX_WORKSPACE_FILE_BYTES {
            bail!("cannot patch file over workspace read cap: {patch_path}");
        }

        let raw = fs::read(&path)?;
        let text = match crate::decode_utf8(raw) {
            Ok(text) => text,
            Err(crate::TextDecodeError::Binary) => bail!("cannot patch binary file: {patch_path}"),
            Err(crate::TextDecodeError::NonUtf8) => bail!("cannot decode utf-8: {patch_path}"),
        };
        let updated = apply(&text, text_patch)
            .with_context(|| format!("failed applying patch for {patch_path}"))?;
        if updated == text {
            continue;
        }

        let display_path = rel_path(&ctx.root, &path);
        if !seen.insert(display_path.clone()) {
            bail!("patch contains multiple changes for the same file: {display_path}");
        }
        let diff = unified_diff(&display_path, &text, &updated);
        plans.push(PatchPlan {
            path,
            display_path,
            updated,
            diff,
        });
    }
    Ok((patch_count, plans))
}

fn patch_path_from_operation(operation: &FileOperation<'_, str>) -> Result<String> {
    match operation {
        FileOperation::Modify { original, modified } if original == modified => {
            let path = original.as_ref();
            if path.trim().is_empty() {
                bail!("patch path is empty");
            }
            Ok(path.to_string())
        }
        FileOperation::Modify { original, modified } => bail!(
            "rename-style modify patches are not supported: {} -> {}",
            original.as_ref(),
            modified.as_ref()
        ),
        FileOperation::Create(path) => {
            bail!("file creation patches are not supported: {}", path.as_ref())
        }
        FileOperation::Delete(path) => {
            bail!("file deletion patches are not supported: {}", path.as_ref())
        }
        FileOperation::Rename { from, to } => bail!(
            "file rename patches are not supported: {} -> {}",
            from.as_ref(),
            to.as_ref()
        ),
        FileOperation::Copy { from, to } => bail!(
            "file copy patches are not supported: {} -> {}",
            from.as_ref(),
            to.as_ref()
        ),
    }
}

fn combined_patch_diff(files: &[PatchPlan]) -> String {
    let text = files
        .iter()
        .map(|item| item.diff.as_str())
        .filter(|diff| !diff.is_empty())
        .collect::<Vec<_>>()
        .join("\n");
    crate::ui::head_tail(&text, 12000).0
}

enum ReplaceOutcome {
    Changed { count: usize, diff: String },
    Unchanged,
    Skipped(&'static str),
}

fn replace_file(path: &Path, regex: &Regex, replacement: &str) -> Result<ReplaceOutcome> {
    if path.is_symlink() {
        return Ok(ReplaceOutcome::Skipped("symlink"));
    }
    if fs::metadata(path)?.len() > MAX_WORKSPACE_FILE_BYTES {
        return Ok(ReplaceOutcome::Skipped("file exceeds workspace read cap"));
    }
    let raw = fs::read(path)?;
    let text = match crate::decode_utf8(raw) {
        Ok(text) => text,
        Err(crate::TextDecodeError::Binary) => {
            return Ok(ReplaceOutcome::Skipped("binary file"));
        }
        Err(crate::TextDecodeError::NonUtf8) => bail!("cannot decode utf-8"),
    };
    let count = regex.find_iter(&text).count();
    if count == 0 {
        return Ok(ReplaceOutcome::Unchanged);
    }
    let updated = regex.replace_all(&text, replacement).into_owned();
    let diff = unified_diff(&path.to_string_lossy(), &text, &updated);
    config::write_workspace_file(path, updated.as_bytes())?;
    Ok(ReplaceOutcome::Changed { count, diff })
}

fn unified_diff(path: &str, old: &str, new: &str) -> String {
    let diff = create_patch(old, new).to_string();
    let diff = diff
        .strip_prefix("--- original\n+++ modified\n")
        .map(|body| format!("--- {path}\n+++ {path}\n{body}"))
        .unwrap_or(diff);
    crate::ui::head_tail(&diff, 12000).0
}

fn combined_diff(files: &[ChangedFileOutput]) -> String {
    let text = files
        .iter()
        .map(|item| item.diff.as_str())
        .filter(|diff| !diff.is_empty())
        .collect::<Vec<_>>()
        .join("\n");
    crate::ui::head_tail(&text, 12000).0
}

fn preview_replace_plan(
    ctx: &ToolContext,
    args: &ReplaceArgs,
    regex: &Regex,
    replacement: &str,
    target: &Path,
    exclude: &GlobSet,
) -> Result<String> {
    let mut changed = Vec::new();
    for path in walk_files(&ctx.root, target, exclude)? {
        if path.is_symlink() {
            continue;
        }
        if fs::metadata(&path)
            .ok()
            .is_some_and(|meta| meta.len() > MAX_WORKSPACE_FILE_BYTES)
        {
            continue;
        }
        let raw = match fs::read(&path) {
            Ok(raw) => raw,
            Err(_) => continue,
        };
        let Ok(text) = crate::decode_utf8(raw) else {
            continue;
        };
        if !regex.is_match(&text) {
            continue;
        }
        let updated = regex.replace_all(&text, replacement).into_owned();
        let display_path = rel_path(&ctx.root, &path);
        changed.push(ChangedFileOutput {
            replacements: regex.find_iter(&text).count(),
            diff: unified_diff(&display_path, &text, &updated),
            path: display_path,
        });
        if changed.len() >= args.limit.clamp(1, PREVIEW_ITEMS) {
            break;
        }
    }
    Ok(combined_diff(&changed))
}