Skip to main content

git_worktree_manager/operations/setup_claude/
writer.rs

1//! File-write helpers used by the marketplace install. Writes are
2//! content-addressed (skip when bytes match) so re-running `gw
3//! setup-claude` is a true no-op when nothing changed.
4
5use std::io;
6use std::path::Path;
7
8/// Write `content` to `path`. If the file already exists with byte-identical
9/// content, do nothing. Returns `true` if a write actually occurred.
10/// Creates any missing parent directories.
11pub fn write_if_changed(path: &Path, content: &str) -> io::Result<bool> {
12    if path.exists() {
13        let existing = std::fs::read_to_string(path).unwrap_or_default();
14        if existing == content {
15            return Ok(false);
16        }
17    }
18    if let Some(parent) = path.parent() {
19        std::fs::create_dir_all(parent)?;
20    }
21    std::fs::write(path, content)?;
22    Ok(true)
23}
24
25/// Drop a sentinel marker that lets future `gw setup-claude` runs (and
26/// the legacy-cleanup step) recognize a directory we own.
27pub fn write_sentinel(path: &Path) -> io::Result<()> {
28    if let Some(parent) = path.parent() {
29        std::fs::create_dir_all(parent)?;
30    }
31    std::fs::write(
32        path,
33        "managed by git-worktree-manager (`gw setup-claude`); safe to delete on uninstall\n",
34    )
35}
36
37pub fn sentinel_present(path: &Path) -> bool {
38    path.exists()
39}