hallouminate 0.2.2

A markdown corpus indexer for LLMs to build and query their own per-repo wikis.
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
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, anyhow};

const BLOCK_START: &str = "# hallouminate-managed-block";
const BLOCK_END: &str = "# /hallouminate-managed-block";
const HOOK_BODY: &str = r#"hallouminate index >/dev/null 2>&1 &"#;
const SHEBANG: &str = "#!/bin/sh";
const HOOK_FILES: &[&str] = &["post-commit", "post-merge"];

#[derive(Debug, Default, Clone)]
pub struct HookArgs {
    pub repo: Option<PathBuf>,
}

pub fn cmd_hook_install(args: HookArgs) -> anyhow::Result<()> {
    let hooks_dir = resolve_hooks_dir(args.repo.as_deref())?;
    fs::create_dir_all(&hooks_dir).with_context(|| format!("create {}", hooks_dir.display()))?;
    for name in HOOK_FILES {
        install_managed_block(&hooks_dir.join(name))?;
    }
    println!("installed hallouminate hooks in {}", hooks_dir.display());
    Ok(())
}

pub fn cmd_hook_uninstall(args: HookArgs) -> anyhow::Result<()> {
    let hooks_dir = resolve_hooks_dir(args.repo.as_deref())?;
    for name in HOOK_FILES {
        uninstall_managed_block(&hooks_dir.join(name))?;
    }
    println!("removed hallouminate hooks from {}", hooks_dir.display());
    Ok(())
}

fn resolve_hooks_dir(repo: Option<&Path>) -> anyhow::Result<PathBuf> {
    let root = match repo {
        Some(p) => p.to_path_buf(),
        None => std::env::current_dir().context("resolve current dir")?,
    };
    // Defer to git itself so worktrees (where `.git` is a *file* pointing at
    // `<main>/.git/worktrees/<name>`), submodules, and `core.hooksPath` all
    // resolve to whatever git would actually invoke at hook time. The naive
    // `<root>/.git/hooks` assumption breaks in worktrees — git puts hooks at
    // the common gitdir, not the per-worktree gitdir.
    let output = std::process::Command::new("git")
        .arg("-C")
        .arg(&root)
        .args(["rev-parse", "--git-path", "hooks"])
        .output()
        .with_context(|| format!("invoke `git -C {} rev-parse`", root.display()))?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(anyhow!(
            "not a git repository: {} ({})",
            root.display(),
            stderr.trim()
        ));
    }
    let stdout = std::str::from_utf8(&output.stdout)
        .context("git rev-parse output not UTF-8")?
        .trim();
    let resolved = PathBuf::from(stdout);
    Ok(if resolved.is_absolute() {
        resolved
    } else {
        root.join(resolved)
    })
}

fn install_managed_block(hook_path: &Path) -> anyhow::Result<()> {
    let existing = read_hook_or_empty(hook_path)?;
    let stripped =
        strip_managed_block(&existing).with_context(|| format!("hook {}", hook_path.display()))?;
    let mut next = ensure_shebang(&stripped);
    if !next.ends_with('\n') {
        next.push('\n');
    }
    next.push_str(BLOCK_START);
    next.push('\n');
    next.push_str(HOOK_BODY);
    next.push('\n');
    next.push_str(BLOCK_END);
    next.push('\n');
    write_executable(hook_path, &next)
}

fn uninstall_managed_block(hook_path: &Path) -> anyhow::Result<()> {
    if !hook_path.exists() {
        return Ok(());
    }
    let existing =
        fs::read_to_string(hook_path).with_context(|| format!("read {}", hook_path.display()))?;
    let stripped =
        strip_managed_block(&existing).with_context(|| format!("hook {}", hook_path.display()))?;
    if is_only_shebang_or_empty(&stripped) {
        fs::remove_file(hook_path).with_context(|| format!("remove {}", hook_path.display()))?;
    } else {
        write_executable(hook_path, &stripped)?;
    }
    Ok(())
}

fn read_hook_or_empty(path: &Path) -> anyhow::Result<String> {
    if path.exists() {
        fs::read_to_string(path).with_context(|| format!("read {}", path.display()))
    } else {
        Ok(String::new())
    }
}

fn strip_managed_block(text: &str) -> anyhow::Result<String> {
    let mut out = String::with_capacity(text.len());
    let mut in_block = false;
    for line in text.split_inclusive('\n') {
        let trimmed = line.trim_end_matches(['\n', '\r']);
        if trimmed == BLOCK_START {
            in_block = true;
            continue;
        }
        if in_block {
            if trimmed == BLOCK_END {
                in_block = false;
            }
            continue;
        }
        out.push_str(line);
    }
    if in_block {
        // BLOCK_START without a matching BLOCK_END means the file was
        // truncated, hand-edited, or otherwise corrupted. Rewriting would
        // silently drop every line after the orphan marker (the user's
        // content), so refuse loudly and let them resolve it.
        return Err(anyhow!(
            "malformed hook: `{BLOCK_START}` is not closed by `{BLOCK_END}`; \
             refusing to rewrite to avoid losing content after the orphan marker"
        ));
    }
    Ok(out)
}

fn ensure_shebang(text: &str) -> String {
    if text.is_empty() {
        return format!("{SHEBANG}\n");
    }
    if text.starts_with("#!") {
        return text.to_string();
    }
    format!("{SHEBANG}\n{text}")
}

fn is_only_shebang_or_empty(text: &str) -> bool {
    let trimmed = text.trim();
    trimmed.is_empty() || trimmed == SHEBANG
}

#[cfg(unix)]
fn write_executable(path: &Path, content: &str) -> anyhow::Result<()> {
    use std::os::unix::fs::PermissionsExt;
    fs::write(path, content).with_context(|| format!("write {}", path.display()))?;
    let mut perms = fs::metadata(path)
        .with_context(|| format!("stat {}", path.display()))?
        .permissions();
    perms.set_mode(0o755);
    fs::set_permissions(path, perms).with_context(|| format!("chmod {}", path.display()))?;
    Ok(())
}

#[cfg(not(unix))]
fn write_executable(path: &Path, content: &str) -> anyhow::Result<()> {
    fs::write(path, content).with_context(|| format!("write {}", path.display()))
}

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

    fn init_repo(dir: &Path) {
        // Real `git init` so `git rev-parse --git-path hooks` resolves
        // correctly. The hooks dir git creates includes `.sample` files;
        // our installer writes `post-commit` and `post-merge` alongside
        // them, so the existing assertions still hold.
        git_in(dir, &["init", "--quiet"]);
    }

    fn git_in(dir: &Path, args: &[&str]) {
        let out = std::process::Command::new("git")
            .arg("-C")
            .arg(dir)
            .args(args)
            .output()
            .unwrap_or_else(|e| panic!("invoke git {args:?}: {e}"));
        assert!(
            out.status.success(),
            "git {args:?} in {} failed: {}",
            dir.display(),
            String::from_utf8_lossy(&out.stderr)
        );
    }

    fn count_managed_blocks(text: &str) -> usize {
        text.match_indices(BLOCK_START).count()
    }

    #[test]
    fn install_in_fresh_repo_writes_both_hook_files() {
        let dir = tempfile::tempdir().expect("tempdir");
        init_repo(dir.path());
        cmd_hook_install(HookArgs {
            repo: Some(dir.path().into()),
        })
        .expect("install");
        for name in HOOK_FILES {
            let body = fs::read_to_string(dir.path().join(".git/hooks").join(name))
                .unwrap_or_else(|_| panic!("read {name}"));
            assert!(body.starts_with("#!/bin/sh"), "{name} missing shebang");
            assert_eq!(count_managed_blocks(&body), 1, "{name} block count");
            assert!(body.contains(HOOK_BODY), "{name} missing hook body");
        }
    }

    #[test]
    fn install_twice_leaves_single_managed_block_per_hook() {
        let dir = tempfile::tempdir().expect("tempdir");
        init_repo(dir.path());
        let args = HookArgs {
            repo: Some(dir.path().into()),
        };
        cmd_hook_install(args.clone()).expect("first install");
        cmd_hook_install(args).expect("second install");
        for name in HOOK_FILES {
            let body = fs::read_to_string(dir.path().join(".git/hooks").join(name))
                .unwrap_or_else(|_| panic!("read {name}"));
            assert_eq!(
                count_managed_blocks(&body),
                1,
                "{name} must contain one block"
            );
        }
    }

    #[test]
    fn uninstall_removes_managed_block_and_drops_empty_hook_file() {
        let dir = tempfile::tempdir().expect("tempdir");
        init_repo(dir.path());
        let args = HookArgs {
            repo: Some(dir.path().into()),
        };
        cmd_hook_install(args.clone()).expect("install");
        cmd_hook_uninstall(args).expect("uninstall");
        for name in HOOK_FILES {
            let path = dir.path().join(".git/hooks").join(name);
            assert!(
                !path.exists(),
                "{name} file should be removed when only managed block remained"
            );
        }
    }

    #[test]
    fn uninstall_preserves_user_lines_outside_managed_block() {
        let dir = tempfile::tempdir().expect("tempdir");
        init_repo(dir.path());
        let post_commit = dir.path().join(".git/hooks/post-commit");
        fs::write(&post_commit, "#!/bin/sh\necho 'user line'\n").expect("seed");
        let args = HookArgs {
            repo: Some(dir.path().into()),
        };
        cmd_hook_install(args.clone()).expect("install");
        cmd_hook_uninstall(args).expect("uninstall");
        let body = fs::read_to_string(&post_commit).expect("read post-commit");
        assert!(body.contains("echo 'user line'"), "user line lost: {body}");
        assert_eq!(count_managed_blocks(&body), 0, "managed block remains");
    }

    #[test]
    fn install_appends_block_to_existing_hook_with_user_content() {
        let dir = tempfile::tempdir().expect("tempdir");
        init_repo(dir.path());
        let post_merge = dir.path().join(".git/hooks/post-merge");
        fs::write(&post_merge, "#!/bin/sh\necho 'pre-existing'\n").expect("seed");
        cmd_hook_install(HookArgs {
            repo: Some(dir.path().into()),
        })
        .expect("install");
        let body = fs::read_to_string(&post_merge).expect("read post-merge");
        assert!(body.contains("echo 'pre-existing'"), "user line lost");
        assert_eq!(count_managed_blocks(&body), 1, "exactly one block");
        assert!(body.contains(HOOK_BODY), "missing hook body");
    }

    #[test]
    fn install_errors_when_target_is_not_a_git_repo() {
        let dir = tempfile::tempdir().expect("tempdir");
        let err = cmd_hook_install(HookArgs {
            repo: Some(dir.path().into()),
        })
        .expect_err("not a git repo");
        assert!(err.to_string().contains("not a git repository"), "{err}");
    }

    #[test]
    fn install_in_worktree_writes_to_main_repo_hooks() {
        // Regression for PR #8 age review: git worktrees use a `.git` file
        // (containing `gitdir: <main>/.git/worktrees/<name>`) instead of a
        // `.git` directory. The previous `<root>/.git/hooks` assumption
        // broke in worktrees; `git rev-parse --git-path hooks` resolves to
        // the main repo's `.git/hooks/` so install must land there.
        let dir = tempfile::tempdir().expect("tempdir");
        let main = dir.path().join("main");
        let wt = dir.path().join("wt");
        fs::create_dir_all(&main).expect("mkdir main");
        init_repo(&main);
        // `git worktree add` needs a starting commit; give it an empty one
        // with an inline identity (CI runners don't have user.email set).
        git_in(
            &main,
            &[
                "-c",
                "user.email=test@example.com",
                "-c",
                "user.name=Test",
                "commit",
                "--quiet",
                "--allow-empty",
                "-m",
                "init",
            ],
        );
        git_in(
            &main,
            &[
                "worktree",
                "add",
                "--quiet",
                "--detach",
                wt.to_str().expect("utf8 path"),
            ],
        );
        // Sanity: confirm the worktree really does have a `.git` *file*,
        // not a directory, so the test exercises the bug it's defending.
        let wt_git = wt.join(".git");
        assert!(wt_git.is_file(), "worktree .git must be a file");

        cmd_hook_install(HookArgs {
            repo: Some(wt.clone()),
        })
        .expect("install from worktree");

        for name in HOOK_FILES {
            let in_main = main.join(".git/hooks").join(name);
            assert!(
                in_main.exists(),
                "hook {name} must be installed at the main repo's .git/hooks; \
                 missing: {}",
                in_main.display()
            );
            let body = fs::read_to_string(&in_main).expect("read main hook");
            assert_eq!(
                count_managed_blocks(&body),
                1,
                "{name} must contain one managed block"
            );
        }
    }

    #[test]
    fn uninstall_is_noop_when_no_hooks_exist() {
        let dir = tempfile::tempdir().expect("tempdir");
        init_repo(dir.path());
        cmd_hook_uninstall(HookArgs {
            repo: Some(dir.path().into()),
        })
        .expect("uninstall noop");
    }

    #[test]
    fn strip_managed_block_errors_when_block_is_unterminated() {
        // Regression for PR #8 Copilot review: an orphan BLOCK_START with no
        // matching BLOCK_END must NOT silently drop the user's lines that
        // follow it. The function refuses to rewrite and surfaces the error.
        let text = format!("#!/bin/sh\n{BLOCK_START}\nold body\necho 'kept content'\n");
        let err = strip_managed_block(&text).expect_err("unterminated block must error");
        let msg = err.to_string();
        assert!(msg.contains(BLOCK_START), "missing BLOCK_START in: {msg}");
        assert!(msg.contains(BLOCK_END), "missing BLOCK_END in: {msg}");
    }

    #[test]
    fn install_refuses_when_existing_hook_has_unterminated_managed_block() {
        let dir = tempfile::tempdir().expect("tempdir");
        init_repo(dir.path());
        let hooks = dir.path().join(".git/hooks");
        fs::create_dir_all(&hooks).expect("mkdir hooks");
        let post_commit = hooks.join("post-commit");
        let seeded = format!("#!/bin/sh\n{BLOCK_START}\nlegacy body\necho 'must-not-be-dropped'\n");
        fs::write(&post_commit, &seeded).expect("seed");

        let err = cmd_hook_install(HookArgs {
            repo: Some(dir.path().into()),
        })
        .expect_err("install must refuse to rewrite a malformed hook");
        let chain = format!("{err:#}");
        assert!(chain.contains("malformed hook"), "{chain}");

        // The original file must be left untouched so the user can see and
        // resolve the corruption rather than discover lost content later.
        let after = fs::read_to_string(&post_commit).expect("read post-commit");
        assert_eq!(after, seeded, "hook file must be untouched on refusal");
    }
}