patchloom 0.7.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
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
use crate::cli::global::GlobalFlags;
use crate::cmd::output::WritePhase;
use crate::cmd::output::execute_via_engine;
use crate::cmd::write_dispatch::{WriteMessages, execute_write};
use crate::exit;
use crate::plan::Operation;
use anyhow::Context;
use clap::Args;
use serde::Serialize;
use std::fs;

#[derive(Debug, Args)]
#[command(after_help = "\
EXAMPLES:
  patchloom rename old_config.json config.json --apply
  patchloom rename src/utils.rs src/helpers.rs --apply --force")]
pub struct RenameArgs {
    /// Source file path.
    pub from: String,
    /// Destination file path.
    pub to: String,
    /// Overwrite if destination already exists.
    #[arg(long)]
    pub force: bool,
    #[command(flatten)]
    pub write: crate::cli::global::WriteFlags,
}

#[derive(Debug, Serialize)]
struct RenameOutput {
    ok: bool,
    from: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    to: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    diff: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    applied: Option<bool>,
}

pub fn run(args: RenameArgs, global: &GlobalFlags) -> anyhow::Result<u8> {
    crate::verbose!(
        "rename: from={}, to={}, force={}",
        args.from,
        args.to,
        args.force
    );
    let cwd = global.resolve_cwd()?;
    let src = cwd.join(&args.from);
    let dst = cwd.join(&args.to);

    // Pre-validation for CLI-specific checks.
    if !src.exists() {
        anyhow::bail!("source file not found: {}", args.from);
    }
    if !src.is_file() {
        anyhow::bail!("source is not a file: {}", args.from);
    }
    if dst.exists() && !dst.is_file() {
        anyhow::bail!("destination is not a file: {}", args.to);
    }

    // Same-file no-op check. On case-insensitive filesystems (macOS APFS),
    // canonicalize() treats case differences as identical. Allow the rename
    // when only the case differs so users can change filename casing (#1167).
    let is_case_only_change = src != dst
        && src.parent() == dst.parent()
        && src.file_name().map(|n| n.to_ascii_lowercase())
            == dst.file_name().map(|n| n.to_ascii_lowercase());
    if !is_case_only_change
        && (src == dst
            || matches!(
                (src.canonicalize(), dst.canonicalize()),
                (Ok(ref s), Ok(ref d)) if s == d
            ))
    {
        let output = RenameOutput {
            ok: true,
            from: args.from.clone(),
            to: Some(args.to.clone()),

            diff: None,
            applied: None,
        };
        if !global.emit_json(&output)? && !global.quiet {
            println!("source and destination are the same: {}", args.from);
        }
        return Ok(exit::SUCCESS);
    }

    if !args.force && !is_case_only_change && dst.exists() {
        anyhow::bail!("destination already exists: {}", args.to);
    }

    // Binary files can't go through the tx engine (it reads as UTF-8 text).
    // Use a direct fs::rename (or copy+delete) for binary renames.
    // Only read the first 8 KiB instead of the entire file to avoid
    // unnecessary memory allocation on large binaries.
    let is_binary = {
        use std::io::Read;
        let mut file = fs::File::open(&src)?;
        let mut buf = [0u8; 8192];
        let n = file.read(&mut buf)?;
        crate::files::is_binary(&buf[..n])
    };
    if is_binary {
        return run_binary_rename(&args, global, &cwd, &src, &dst);
    }

    // Case-only renames on case-insensitive filesystems (e.g. macOS APFS)
    // cannot go through the tx engine because its write-then-delete approach
    // would delete the destination (same inode as source). Use fs::rename
    // directly, which handles case changes atomically (#1167).
    if is_case_only_change {
        return run_binary_rename(&args, global, &cwd, &src, &dst);
    }

    let op = Operation::FileRename {
        from: args.from.clone(),
        to: args.to.clone(),
        force: args.force,
    };

    let check_msg = format!("would rename {} -> {}", args.from, args.to);
    let apply_msg = format!("renamed {} -> {}", args.from, args.to);

    execute_via_engine(
        op,
        global,
        |phase, diff| RenameOutput {
            ok: true,
            from: args.from.clone(),
            to: Some(args.to.clone()),

            diff,
            applied: match phase {
                WritePhase::Confirmed(a) => Some(a),
                _ => None,
            },
        },
        &check_msg,
        &apply_msg,
    )
}

/// Handle binary file renames directly (bypasses the tx engine which only
/// handles UTF-8 text files).
fn run_binary_rename(
    args: &RenameArgs,
    global: &GlobalFlags,
    cwd: &std::path::Path,
    src: &std::path::Path,
    dst: &std::path::Path,
) -> anyhow::Result<u8> {
    // Write policies require reading the file as UTF-8 text, which binary
    // files don't support. Fail early with a clear error.
    if global.trim_trailing_whitespace
        || global.ensure_final_newline
        || global.normalize_eol.is_some()
    {
        anyhow::bail!(
            "cannot apply write policy to binary file: {}",
            src.display()
        );
    }

    let check_msg = format!("would rename {} -> {} (binary)", args.from, args.to);
    let apply_msg = format!("renamed {} -> {} (binary)", args.from, args.to);
    let post_msg = format!("renamed {} -> {}", args.from, args.to);

    execute_write(
        global,
        cwd,
        |phase, _diff| RenameOutput {
            ok: true,
            from: args.from.clone(),
            to: Some(args.to.clone()),

            diff: None,
            applied: match phase {
                WritePhase::Confirmed(a) => Some(a),
                _ => None,
            },
        },
        None::<&dyn Fn(bool) -> String>,
        || {
            let mut backup = crate::backup::BackupSession::new(cwd)?;
            backup.save_before_delete(src)?;
            backup.save_before_write(dst)?;
            if let Some(parent) = dst.parent()
                && !parent.as_os_str().is_empty()
                && !parent.exists()
            {
                fs::create_dir_all(parent)?;
            }
            backup.finalize()?;
            rename_or_copy(src, dst)?;
            Ok(())
        },
        WriteMessages {
            check: &check_msg,
            apply: &apply_msg,
            post_confirm: Some(&post_msg),
        },
    )
}

/// Rename a file, falling back to copy+delete when the source and destination
/// are on different filesystems (where `fs::rename` would fail).
fn rename_or_copy(src: &std::path::Path, dst: &std::path::Path) -> anyhow::Result<()> {
    match fs::rename(src, dst) {
        Ok(()) => Ok(()),
        Err(e) if is_cross_device(&e) => {
            fs::copy(src, dst).with_context(|| {
                format!("cross-device copy {} -> {}", src.display(), dst.display())
            })?;
            fs::remove_file(src).with_context(|| {
                format!("removing source after cross-device copy: {}", src.display())
            })?;
            Ok(())
        }
        Err(e) => Err(e.into()),
    }
}

/// Check if an I/O error is a cross-device link error.
fn is_cross_device(e: &std::io::Error) -> bool {
    #[cfg(unix)]
    {
        e.raw_os_error() == Some(libc::EXDEV)
    }
    #[cfg(windows)]
    {
        e.raw_os_error() == Some(17)
    }
    #[cfg(not(any(unix, windows)))]
    {
        let _ = e;
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn rename_moves_file() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("old.txt");
        let dst = dir.path().join("new.txt");
        fs::write(&src, "hello\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(!src.exists());
        assert!(dst.exists());
        assert_eq!(fs::read_to_string(&dst).unwrap(), "hello\n");
    }

    #[test]
    fn rename_fails_if_dst_exists() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("old.txt");
        let dst = dir.path().join("new.txt");
        fs::write(&src, "hello\n").unwrap();
        fs::write(&dst, "existing\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let err = run(args, &global).unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }

    #[test]
    fn rename_force_rejects_directory_destination() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("old.txt");
        let dst = dir.path().join("folder");
        fs::write(&src, "hello\n").unwrap();
        fs::create_dir(&dst).unwrap();

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: true,
            write: Default::default(),
        };

        let err = run(args, &GlobalFlags::test_with_cwd(dir.path())).unwrap_err();
        assert!(err.to_string().contains("destination is not a file"));
    }

    #[test]
    fn rename_force_overwrites_dst() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("old.txt");
        let dst = dir.path().join("new.txt");
        fs::write(&src, "new content\n").unwrap();
        fs::write(&dst, "old content\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: true,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(!src.exists());
        assert_eq!(fs::read_to_string(&dst).unwrap(), "new content\n");
    }

    #[test]
    fn rename_check_reports_changes() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("old.txt");
        let dst = dir.path().join("new.txt");
        fs::write(&src, "hello\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.check = true;

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::CHANGES_DETECTED);
        // Source should still exist in --check mode.
        assert!(src.exists());
        assert_eq!(
            fs::read_to_string(&src).unwrap(),
            "hello\n",
            "--check must not modify source content"
        );
        // Destination must NOT be created in --check mode.
        assert!(!dst.exists(), "--check must not create destination file");
    }

    #[test]
    fn rename_fails_if_src_missing() {
        let dir = TempDir::new().unwrap();
        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: dir.path().join("nope.txt").to_string_lossy().into_owned(),
            to: dir.path().join("dst.txt").to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let err = run(args, &global).unwrap_err();
        assert!(err.to_string().contains("not found"));
    }

    #[test]
    fn rename_same_path_is_noop() {
        let dir = TempDir::new().unwrap();
        let file = dir.path().join("same.txt");
        fs::write(&file, "hello\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: file.to_string_lossy().into_owned(),
            to: file.to_string_lossy().into_owned(),
            force: true,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(file.exists(), "file should still exist");
        assert_eq!(fs::read_to_string(&file).unwrap(), "hello\n");
    }

    #[test]
    fn rename_same_file_via_different_path_is_noop() {
        let dir = TempDir::new().unwrap();
        let file = dir.path().join("same.txt");
        fs::write(&file, "hello\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        // Use "./same.txt" as destination - different string, same file.
        let args = RenameArgs {
            from: file.to_string_lossy().into_owned(),
            to: dir.path().join("./same.txt").to_string_lossy().into_owned(),
            force: true,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(file.exists(), "file must not be deleted");
        assert_eq!(fs::read_to_string(&file).unwrap(), "hello\n");
    }

    #[test]
    fn rename_binary_file() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("image.bin");
        let dst = dir.path().join("moved.bin");
        // Write non-UTF-8 content.
        fs::write(&src, b"\x00\x01\x02\xff\xfe").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(!src.exists());
        assert_eq!(fs::read(&dst).unwrap(), b"\x00\x01\x02\xff\xfe");
    }

    #[test]
    fn rename_creates_parent_dirs() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("old.txt");
        let dst = dir.path().join("sub").join("dir").join("new.txt");
        fs::write(&src, "hello\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(!src.exists());
        assert_eq!(fs::read_to_string(&dst).unwrap(), "hello\n");
    }

    #[test]
    fn rename_fails_if_src_is_directory() {
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("folder");
        let dst = dir.path().join("new.txt");
        fs::create_dir(&src).unwrap();

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let err = run(args, &GlobalFlags::test_with_cwd(dir.path())).unwrap_err();
        assert!(err.to_string().contains("source is not a file"));
    }

    #[test]
    fn rename_creates_backup_before_mutation() {
        // R4 fix: backup.finalize() must be called before rename_or_copy()
        // so the backup is committed even if the rename itself fails.
        // We verify this by checking that a backup session is created
        // after a successful rename.
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("original.txt");
        let dst = dir.path().join("moved.txt");
        fs::write(&src, "backup me\n").unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;

        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };

        let code = run(args, &global).unwrap();
        assert_eq!(code, exit::SUCCESS);
        assert!(!src.exists(), "source should be gone after rename");
        assert_eq!(fs::read_to_string(&dst).unwrap(), "backup me\n");

        // Verify backup session was created (finalize() was called).
        let backup_dir = dir.path().join(".patchloom/backups");
        assert!(
            backup_dir.exists(),
            "backup directory should exist after rename --apply"
        );
        let sessions: Vec<_> = fs::read_dir(&backup_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().is_dir())
            .collect();
        assert!(
            !sessions.is_empty(),
            "at least one backup session should be created"
        );
    }

    #[cfg(unix)]
    #[test]
    fn rename_unreadable_file_propagates_io_error() {
        use std::os::unix::fs::PermissionsExt;
        let dir = TempDir::new().unwrap();
        let src = dir.path().join("unreadable.txt");
        let dst = dir.path().join("dest.txt");
        fs::write(&src, "hello\n").unwrap();
        fs::set_permissions(&src, fs::Permissions::from_mode(0o000)).unwrap();

        let mut global = GlobalFlags::test_with_cwd(dir.path());
        global.apply = true;
        let args = RenameArgs {
            from: src.to_string_lossy().into_owned(),
            to: dst.to_string_lossy().into_owned(),
            force: false,
            write: Default::default(),
        };
        // Should propagate the I/O error, not misclassify as binary
        let result = run(args, &global);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            !err_msg.contains("binary"),
            "should not misclassify permission error as binary: {err_msg}"
        );
        // Cleanup: restore permissions so TempDir can clean up
        fs::set_permissions(&src, fs::Permissions::from_mode(0o644)).unwrap();
    }
}