repotoire 0.8.0

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
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
//! Shared helpers for Claude Code settings.json: path resolution, read/validate, atomic write.

use anyhow::{anyhow, Context, Result};
use std::path::PathBuf;

/// Resolve the path to Claude Code's `settings.json`.
///
/// Order:
///   1. `$CLAUDE_CONFIG_DIR/settings.json` — Claude Code's documented override (anthropics/claude-code#3833).
///   2. `home::home_dir()/.claude/settings.json` — default. Uses the `home` crate (NOT `dirs::home_dir()`)
///      so we honor `USERPROFILE` on Windows the way Claude Code does (via Node's `os.homedir`).
///
/// Returns `Err` only when both `CLAUDE_CONFIG_DIR` is unset AND `home::home_dir()` returns `None`
/// (rare: daemons, some CI, broken envs).
pub(super) fn settings_path() -> Result<PathBuf> {
    if let Ok(dir) = std::env::var("CLAUDE_CONFIG_DIR") {
        if !dir.is_empty() {
            return Ok(PathBuf::from(dir).join("settings.json"));
        }
    }
    let home = home::home_dir().ok_or_else(|| {
        anyhow!("could not determine home directory (set CLAUDE_CONFIG_DIR or HOME)")
    })?;
    Ok(home.join(".claude").join("settings.json"))
}

/// Maximum settings.json size we will read. Anything larger is suspicious; refuse.
const MAX_SETTINGS_BYTES: u64 = 10 * 1024 * 1024;

/// Read and parse settings.json.
///
/// Returns:
///   - `Ok(serde_json::Value::Object(_))` on success (or `{}` if the file does not exist).
///   - `Err` if the file is not a regular file, larger than 10 MiB, or not valid JSON.
pub(super) fn read_settings_or_empty(path: &std::path::Path) -> Result<serde_json::Value> {
    use std::fs;
    use std::io::Read;

    let meta = match fs::symlink_metadata(path) {
        Ok(m) => m,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            return Ok(serde_json::json!({}));
        }
        Err(e) => return Err(anyhow::Error::from(e).context(format!("stat {}", path.display()))),
    };

    // If this is a symlink, follow it and re-stat to validate the target.
    let target_meta = if meta.file_type().is_symlink() {
        fs::metadata(path)
            .with_context(|| format!("symlink target unreachable: {}", path.display()))?
    } else {
        meta
    };

    if target_meta.is_dir() {
        anyhow::bail!(
            "{} is not a regular file (it is a directory).",
            path.display()
        );
    }
    if !target_meta.is_file() {
        anyhow::bail!(
            "{} is not a regular file (FIFO, device, or socket).",
            path.display()
        );
    }
    if target_meta.len() > MAX_SETTINGS_BYTES {
        anyhow::bail!(
            "{} is suspiciously large (>{} MiB); refusing to parse.",
            path.display(),
            MAX_SETTINGS_BYTES / (1024 * 1024)
        );
    }

    let mut buf = String::new();
    fs::File::open(path)
        .with_context(|| format!("open {}", path.display()))?
        .take(MAX_SETTINGS_BYTES + 1)
        .read_to_string(&mut buf)
        .with_context(|| format!("read {}", path.display()))?;

    let parsed: serde_json::Value = serde_json::from_str(&buf).with_context(|| {
        format!(
            "{} is not valid JSON. Fix or remove it before re-running.",
            path.display()
        )
    })?;

    if !parsed.is_object() {
        anyhow::bail!("{} top-level value is not a JSON object.", path.display());
    }

    Ok(parsed)
}

/// Generate a backup file path: `<settings.json>.bak.<UTC-yyyymmdd-hhmmss>.<random-4>`.
/// The random suffix avoids collisions on rapid re-installs within the same second.
pub(super) fn backup_path(settings: &std::path::Path) -> std::path::PathBuf {
    use rand::Rng;
    let now = chrono::Utc::now().format("%Y%m%d-%H%M%S");
    let rand: String = (0..4)
        .map(|_| {
            let n: u8 = rand::rng().random_range(0..16);
            std::char::from_digit(n as u32, 16).expect("0..16 always valid hex digit")
        })
        .collect();
    let mut buf = settings.as_os_str().to_owned();
    buf.push(format!(".bak.{now}.{rand}"));
    buf.into()
}

/// Atomically write `contents` to `target`.
///
/// On Unix: atomic via tempfile-in-same-dir + rename. Preserves the existing file's
/// mode (so `chmod 600` survives). If `target` is a symlink, writes to the resolved
/// target so the link itself is not replaced.
///
/// On Windows: best-effort atomic. `std::fs::rename` first attempts a non-atomic
/// `FileRenameInfo` and falls back to atomic API on conflict. This is the closest
/// we get without a third-party Windows-specific crate.
pub(super) fn atomic_write(target: &std::path::Path, contents: &[u8]) -> Result<()> {
    use std::fs;

    // Resolve symlink so rename doesn't replace the link itself.
    let resolved: std::path::PathBuf = match fs::symlink_metadata(target) {
        Ok(m) if m.file_type().is_symlink() => fs::canonicalize(target)
            .with_context(|| format!("canonicalize symlink target {}", target.display()))?,
        _ => target.to_path_buf(),
    };

    let parent = resolved
        .parent()
        .ok_or_else(|| anyhow!("settings path has no parent: {}", resolved.display()))?;
    fs::create_dir_all(parent)
        .with_context(|| format!("create parent dir {}", parent.display()))?;

    // Snapshot the existing file's mode (Unix only) before writing.
    #[cfg(unix)]
    let prev_mode: Option<u32> = match fs::metadata(&resolved) {
        Ok(m) => {
            use std::os::unix::fs::PermissionsExt;
            Some(m.permissions().mode())
        }
        Err(_) => None,
    };

    let mut tmp = tempfile::NamedTempFile::new_in(parent)
        .with_context(|| format!("create temp file in {}", parent.display()))?;
    {
        use std::io::Write;
        tmp.write_all(contents)
            .with_context(|| format!("write temp for {}", resolved.display()))?;
        tmp.as_file()
            .sync_all()
            .with_context(|| format!("fsync temp for {}", resolved.display()))?;
    }

    // Apply previous mode to the tempfile before rename so chmod 600 survives.
    #[cfg(unix)]
    if let Some(mode) = prev_mode {
        use std::os::unix::fs::PermissionsExt;
        let perms = std::fs::Permissions::from_mode(mode);
        std::fs::set_permissions(tmp.path(), perms)
            .with_context(|| format!("preserve mode {:o} on temp file", mode))?;
    }

    tmp.persist(&resolved)
        .map_err(|e| anyhow!("persist {}: {}", resolved.display(), e.error))?;

    Ok(())
}

/// True if `cmd` is one of our `claude-hook run` invocations.
///
/// Splits on whitespace and checks: tokens[0] ends with `repotoire` (or `repotoire.exe`),
/// tokens[1] == `claude-hook`, tokens[2] == `run`. Allows future flags after `run`.
/// Suffix-match on the binary path filename means a stale install at a moved binary
/// is still detected.
pub(super) fn is_repotoire_hook_command(cmd: &str) -> bool {
    let mut parts = cmd.split_whitespace();
    let bin = match parts.next() {
        Some(s) => s,
        None => return false,
    };
    let leaf = std::path::Path::new(bin)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("");
    let is_our_bin = if cfg!(target_os = "windows") {
        leaf.eq_ignore_ascii_case("repotoire") || leaf.eq_ignore_ascii_case("repotoire.exe")
    } else {
        leaf == "repotoire"
    };
    if !is_our_bin {
        return false;
    }
    if parts.next() != Some("claude-hook") {
        return false;
    }
    if parts.next() != Some("run") {
        return false;
    }
    true
}

/// True if `cmd` matches an old-style hook (a script path under `~/.repotoire/hooks/`).
pub(super) fn is_old_style_hook_command(cmd: &str, home: &std::path::Path) -> bool {
    let needle = home.join(".repotoire").join("hooks");
    cmd.contains(needle.to_string_lossy().as_ref())
}

/// Insert our hook entry into `root`, preserving all other keys.
///
/// Ensures `hooks` (object) and `hooks.PreToolUse` (array) exist; appends a new
/// `{matcher: "Bash", hooks: [{type: "command", command: <our_command>}]}` entry.
pub(super) fn insert_hook_entry(root: &mut serde_json::Value, our_command: &str) {
    let obj = root.as_object_mut().expect("settings root must be object");
    let hooks = obj
        .entry("hooks".to_string())
        .or_insert_with(|| serde_json::json!({}));
    let hooks_obj = hooks
        .as_object_mut()
        .expect("hooks must be an object if present");
    let pre = hooks_obj
        .entry("PreToolUse".to_string())
        .or_insert_with(|| serde_json::json!([]));
    let pre_arr = pre
        .as_array_mut()
        .expect("PreToolUse must be an array if present");
    pre_arr.push(serde_json::json!({
        "matcher": "Bash",
        "hooks": [
            {"type": "command", "command": our_command}
        ]
    }));
}

/// Walk `hooks.PreToolUse[]` and look for a hook whose inner command field
/// matches `pred`. Returns the index of the first matching outer entry.
pub(super) fn find_pretool_entry_by_command<F>(
    root: &serde_json::Value,
    mut pred: F,
) -> Option<usize>
where
    F: FnMut(&str) -> bool,
{
    let arr = root.get("hooks")?.get("PreToolUse")?.as_array()?;
    for (i, entry) in arr.iter().enumerate() {
        if let Some(inner) = entry.get("hooks").and_then(|h| h.as_array()) {
            for h in inner {
                if let Some(cmd) = h.get("command").and_then(|c| c.as_str()) {
                    if pred(cmd) {
                        return Some(i);
                    }
                }
            }
        }
    }
    None
}

/// Remove every `hooks.PreToolUse[]` entry containing a command matching `pred`.
/// Returns the number of outer entries removed.
pub(super) fn filter_pretool_entries<F>(root: &mut serde_json::Value, mut pred: F) -> usize
where
    F: FnMut(&str) -> bool,
{
    let arr = match root
        .get_mut("hooks")
        .and_then(|h| h.get_mut("PreToolUse"))
        .and_then(|p| p.as_array_mut())
    {
        Some(a) => a,
        None => return 0,
    };
    let before = arr.len();
    arr.retain(|entry| {
        let inner = match entry.get("hooks").and_then(|h| h.as_array()) {
            Some(a) => a,
            None => return true,
        };
        let any_match = inner.iter().any(|h| {
            h.get("command")
                .and_then(|c| c.as_str())
                .map(&mut pred)
                .unwrap_or(false)
        });
        !any_match
    });
    before - arr.len()
}

/// After mutating, remove `hooks.PreToolUse` if empty, then `hooks` if empty.
/// Avoids leaving `{"hooks":{}}` or `{"hooks":{"PreToolUse":[]}}` clutter.
pub(super) fn cleanup_empty_containers(root: &mut serde_json::Value) {
    let obj = match root.as_object_mut() {
        Some(o) => o,
        None => return,
    };
    let hooks = match obj.get_mut("hooks").and_then(|v| v.as_object_mut()) {
        Some(h) => h,
        None => return,
    };
    if let Some(arr) = hooks.get("PreToolUse").and_then(|v| v.as_array()) {
        if arr.is_empty() {
            hooks.remove("PreToolUse");
        }
    }
    if hooks.is_empty() {
        obj.remove("hooks");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper: run `f` with a single env var set, restore previous state after.
    /// Note: env vars are process-global; these tests run serial via `--test-threads=1`
    /// in the e2e suite. For unit tests in this module we use unique var names where possible.
    fn with_env<R>(key: &str, val: Option<&str>, f: impl FnOnce() -> R) -> R {
        let prev = std::env::var(key).ok();
        match val {
            Some(v) => std::env::set_var(key, v),
            None => std::env::remove_var(key),
        }
        let result = f();
        match prev {
            Some(v) => std::env::set_var(key, v),
            None => std::env::remove_var(key),
        }
        result
    }

    #[test]
    fn settings_path_uses_claude_config_dir_when_set() {
        with_env("CLAUDE_CONFIG_DIR", Some("/tmp/fake-claude-config"), || {
            let p = settings_path().expect("path resolves");
            assert_eq!(p, PathBuf::from("/tmp/fake-claude-config/settings.json"));
        });
    }

    #[test]
    fn settings_path_falls_back_to_home_when_claude_config_dir_empty() {
        with_env("CLAUDE_CONFIG_DIR", Some(""), || {
            let p = settings_path().expect("path resolves");
            // Should end with .claude/settings.json — exact prefix depends on home::home_dir.
            assert!(p.ends_with(".claude/settings.json"), "got {}", p.display());
        });
    }

    #[test]
    fn settings_path_falls_back_to_home_when_claude_config_dir_unset() {
        with_env("CLAUDE_CONFIG_DIR", None, || {
            let p = settings_path().expect("path resolves");
            assert!(p.ends_with(".claude/settings.json"), "got {}", p.display());
        });
    }

    #[test]
    fn read_returns_empty_object_when_file_missing() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("does-not-exist.json");
        let val = read_settings_or_empty(&path).unwrap();
        assert_eq!(val, serde_json::json!({}));
    }

    #[test]
    fn read_returns_parsed_object_when_file_exists() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("settings.json");
        std::fs::write(&path, r#"{"theme":"dark"}"#).unwrap();
        let val = read_settings_or_empty(&path).unwrap();
        assert_eq!(val["theme"], "dark");
    }

    #[test]
    fn read_errors_on_malformed_json() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("settings.json");
        std::fs::write(&path, "{ broken json").unwrap();
        let err = read_settings_or_empty(&path).unwrap_err();
        assert!(format!("{err:#}").contains("is not valid JSON"));
    }

    #[test]
    fn read_errors_when_path_is_directory() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("settings.json");
        std::fs::create_dir(&path).unwrap();
        let err = read_settings_or_empty(&path).unwrap_err();
        assert!(format!("{err:#}").contains("not a regular file"));
    }

    #[test]
    fn read_errors_on_non_object_top_level() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("settings.json");
        std::fs::write(&path, "[]").unwrap();
        let err = read_settings_or_empty(&path).unwrap_err();
        assert!(format!("{err:#}").contains("top-level value is not a JSON object"));
    }

    #[test]
    fn atomic_write_creates_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("settings.json");
        atomic_write(&path, b"{\"theme\":\"dark\"}").unwrap();
        let read = std::fs::read_to_string(&path).unwrap();
        assert_eq!(read, "{\"theme\":\"dark\"}");
    }

    #[test]
    fn atomic_write_overwrites_existing() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("settings.json");
        std::fs::write(&path, "old").unwrap();
        atomic_write(&path, b"new").unwrap();
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "new");
    }

    #[cfg(unix)]
    #[test]
    fn atomic_write_preserves_mode() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("settings.json");
        std::fs::write(&path, "old").unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();

        atomic_write(&path, b"new").unwrap();

        let mode = std::fs::metadata(&path).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o600);
    }

    #[cfg(unix)]
    #[test]
    fn atomic_write_preserves_symlink() {
        use std::os::unix::fs::symlink;
        let dir = tempfile::tempdir().unwrap();
        let real = dir.path().join("real.json");
        let link = dir.path().join("link.json");
        std::fs::write(&real, "old").unwrap();
        symlink(&real, &link).unwrap();

        atomic_write(&link, b"new").unwrap();

        // link is still a symlink
        assert!(std::fs::symlink_metadata(&link)
            .unwrap()
            .file_type()
            .is_symlink());
        // and the target was written
        assert_eq!(std::fs::read_to_string(&real).unwrap(), "new");
    }

    #[test]
    fn backup_path_appends_suffix() {
        let p = std::path::Path::new("/tmp/settings.json");
        let b = backup_path(p);
        let s = b.to_string_lossy();
        assert!(s.starts_with("/tmp/settings.json.bak."), "got {s}");
        // bak.<14-digit-timestamp>.<4-hex>
        assert!(
            s.len() >= "/tmp/settings.json.bak.".len() + 14 + 1 + 4,
            "got {s}"
        );
    }

    #[test]
    fn is_repotoire_hook_command_matches_canonical_form() {
        assert!(is_repotoire_hook_command(
            "/usr/local/bin/repotoire claude-hook run"
        ));
        assert!(is_repotoire_hook_command(
            "/opt/homebrew/bin/repotoire claude-hook run"
        ));
        assert!(is_repotoire_hook_command(
            "/usr/local/bin/repotoire claude-hook run --debug"
        ));
    }

    #[test]
    fn is_repotoire_hook_command_rejects_unrelated() {
        assert!(!is_repotoire_hook_command("/usr/bin/git commit"));
        assert!(!is_repotoire_hook_command(
            "/usr/local/bin/repotoire-test claude-hook run"
        ));
        assert!(!is_repotoire_hook_command(
            "/usr/local/bin/repotoire analyze"
        ));
        assert!(!is_repotoire_hook_command(
            "/usr/local/bin/repotoire claude-hook install"
        ));
        assert!(!is_repotoire_hook_command(""));
    }

    // Old-style hooks only ever lived under ~/.repotoire/hooks/pre-commit.sh on
    // Unix (install.sh is bash-only). On Windows there's no path to detect.
    #[cfg(not(target_os = "windows"))]
    #[test]
    fn is_old_style_hook_command_matches_legacy_path() {
        let home = std::path::Path::new("/home/user");
        assert!(is_old_style_hook_command(
            "/home/user/.repotoire/hooks/pre-commit.sh",
            home
        ));
        assert!(!is_old_style_hook_command(
            "/usr/local/bin/repotoire claude-hook run",
            home
        ));
    }

    #[test]
    fn insert_hook_entry_into_empty_root() {
        let mut root = serde_json::json!({});
        insert_hook_entry(&mut root, "/bin/repotoire claude-hook run");
        assert_eq!(root["hooks"]["PreToolUse"][0]["matcher"], "Bash");
        assert_eq!(
            root["hooks"]["PreToolUse"][0]["hooks"][0]["command"],
            "/bin/repotoire claude-hook run"
        );
    }

    #[test]
    fn insert_hook_entry_preserves_other_keys() {
        let mut root = serde_json::json!({"theme": "dark", "enabledPlugins": {"foo": true}});
        insert_hook_entry(&mut root, "/bin/repotoire claude-hook run");
        assert_eq!(root["theme"], "dark");
        assert_eq!(root["enabledPlugins"]["foo"], true);
        assert_eq!(root["hooks"]["PreToolUse"].as_array().unwrap().len(), 1);
    }

    #[test]
    fn insert_hook_entry_appends_to_existing_array() {
        let mut root = serde_json::json!({
            "hooks": {
                "PreToolUse": [
                    {"matcher": "Bash", "hooks": [{"type": "command", "command": "/bin/foo"}]}
                ]
            }
        });
        insert_hook_entry(&mut root, "/bin/repotoire claude-hook run");
        assert_eq!(root["hooks"]["PreToolUse"].as_array().unwrap().len(), 2);
        assert_eq!(
            root["hooks"]["PreToolUse"][1]["hooks"][0]["command"],
            "/bin/repotoire claude-hook run"
        );
    }

    #[test]
    fn filter_pretool_entries_preserves_unrelated() {
        let mut root = serde_json::json!({
            "hooks": {
                "PreToolUse": [
                    {"matcher": "Bash", "hooks": [{"type": "command", "command": "/bin/foo"}]},
                    {"matcher": "Bash", "hooks": [{"type": "command", "command": "/bin/repotoire claude-hook run"}]}
                ]
            }
        });
        let removed = filter_pretool_entries(&mut root, is_repotoire_hook_command);
        assert_eq!(removed, 1);
        assert_eq!(root["hooks"]["PreToolUse"].as_array().unwrap().len(), 1);
        assert_eq!(
            root["hooks"]["PreToolUse"][0]["hooks"][0]["command"],
            "/bin/foo"
        );
    }

    #[test]
    fn cleanup_empty_containers_removes_empty_pretooluse_and_hooks() {
        let mut root = serde_json::json!({
            "theme": "dark",
            "hooks": {"PreToolUse": []}
        });
        cleanup_empty_containers(&mut root);
        assert!(root.get("hooks").is_none());
        assert_eq!(root["theme"], "dark");
    }

    #[test]
    fn cleanup_empty_containers_keeps_hooks_when_other_event_present() {
        let mut root = serde_json::json!({
            "hooks": {
                "PreToolUse": [],
                "PostToolUse": [{"matcher": "Bash", "hooks": [{"type":"command","command":"/bin/foo"}]}]
            }
        });
        cleanup_empty_containers(&mut root);
        assert!(root.get("hooks").is_some());
        assert!(root["hooks"].get("PreToolUse").is_none());
        assert!(root["hooks"].get("PostToolUse").is_some());
    }
}