heddle-cli 0.15.1

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0
//! Shared process harness for CLI integration-test binaries.
#![allow(dead_code)]
// Re-exports exist for the themed binaries' glob imports, not for use here.
#![allow(unused_imports)]

// The harness types are re-exported (not just imported) so themed
// binaries reach them through `use support::*`.
pub use std::{
    io::Write,
    path::Path,
    process::{Command, Output, Stdio},
    str,
};

const TEST_PRINCIPAL_NAME: &str = "Heddle Test";
const TEST_PRINCIPAL_EMAIL: &str = "test@heddle.dev";

/// Run the Heddle binary with the deterministic test principal.
///
/// Extra environment values are applied last so tests that exercise identity
/// resolution can intentionally override the defaults without mutating the
/// process-wide environment.
pub fn heddle_output_env(
    args: &[&str],
    cwd: Option<&Path>,
    envs: &[(&str, &str)],
) -> Result<Output, String> {
    let mut command = std::process::Command::new(env!("CARGO_BIN_EXE_heddle"));
    for (key, _) in std::env::vars_os() {
        let key = key.to_string_lossy();
        if key.starts_with("CODEX_")
            || key.starts_with("CLAUDE")
            || key.starts_with("OPENCODE_")
            || matches!(key.as_ref(), "HEDDLE_AGENT_PROVIDER" | "HEDDLE_AGENT_MODEL")
        {
            command.env_remove(key.as_ref());
        }
    }
    command
        .args(args)
        .env("HEDDLE_PRINCIPAL_NAME", TEST_PRINCIPAL_NAME)
        .env("HEDDLE_PRINCIPAL_EMAIL", TEST_PRINCIPAL_EMAIL)
        .env("HEDDLE_FSMONITOR", "off")
        .envs(envs.iter().copied());
    if let Some(cwd) = cwd {
        command.current_dir(cwd);
    }
    command.output().map_err(|error| error.to_string())
}

/// Run Heddle and return UTF-8-lossy stdout on success or a diagnostic carrying
/// the complete exit status/stdout/stderr contract on failure.
pub fn heddle_env(
    args: &[&str],
    cwd: Option<&Path>,
    envs: &[(&str, &str)],
) -> Result<String, String> {
    let output = heddle_output_env(args, cwd, envs)?;
    let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
    let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
    if output.status.success() {
        Ok(stdout)
    } else {
        Err(format!(
            "Exit code: {:?}\nstdout: {}\nstderr: {}",
            output.status.code(),
            stdout,
            stderr
        ))
    }
}

/// Create a deterministic many-small-files fixture shared by CLI and storage
/// performance tests.
pub fn write_many_small_files(root: &Path, file_count: usize) {
    for index in 0..file_count {
        std::fs::write(
            root.join(format!("file_{index:05}.txt")),
            format!("content {index}\n{}\n", "x".repeat(48)),
        )
        .unwrap();
    }
}

// ---------------------------------------------------------------------------
// Spawn harness shared by the themed CLI integration binaries.
//
// Moved verbatim from the former single `cli_integration.rs` target so the
// split binaries (basics / overlay+remote / output contracts / workflow)
// share one harness instead of four copies. The in-process help-render
// helper (`heddle_help`) lives here too. The older env-var harness above
// keeps its `_env`-suffixed names.
// ---------------------------------------------------------------------------

// Re-exported so the themed test binaries share one set of harness types.
pub use objects::store::ObjectStore;
pub use repo::Repository;
pub use serde_json::Value;
pub use sley::{
    CommitObject, EntryKind, GitObjectType, GitTime, ObjectId, RefPrecondition, ReferenceTarget,
    Repository as SleyRepository, Signature, TagObject,
    plumbing::{sley_object::EncodedObject, sley_refs::ReflogEntry},
    BString as GitByteString,
};
pub use tempfile::TempDir;

pub trait SleyIntegrationRepoExt {
    fn find_commit(&self, oid: ObjectId) -> Result<TestCommit, String>;
    fn find_object(&self, oid: ObjectId) -> Result<(), String>;
    fn head_id(&self) -> Result<ObjectId, String>;
    fn head_commit(&self) -> Result<TestCommit, String>;
    fn merge_base(&self, a: ObjectId, b: ObjectId) -> Result<ObjectId, String>;
    fn rev_walk<I>(&self, starts: I) -> TestRevWalk
    where
        I: IntoIterator<Item = ObjectId>;
}

impl SleyIntegrationRepoExt for SleyRepository {
    fn find_commit(&self, oid: ObjectId) -> Result<TestCommit, String> {
        let commit = self.read_commit(&oid).map_err(|err| err.to_string())?;
        let tree = self
            .read_tree(&commit.tree)
            .map_err(|err| err.to_string())?;
        Ok(TestCommit {
            id: oid,
            commit,
            tree,
        })
    }

    fn find_object(&self, oid: ObjectId) -> Result<(), String> {
        self.read_object(&oid)
            .map(|_| ())
            .map_err(|err| err.to_string())
    }

    fn head_id(&self) -> Result<ObjectId, String> {
        self.head()
            .map_err(|err| err.to_string())?
            .oid
            .ok_or_else(|| "HEAD is unborn".to_string())
    }

    fn head_commit(&self) -> Result<TestCommit, String> {
        let oid = self.head_id()?;
        self.find_commit(oid)
    }

    fn merge_base(&self, a: ObjectId, b: ObjectId) -> Result<ObjectId, String> {
        let ancestors = commit_ancestors(self, a);
        let mut pending = std::collections::VecDeque::from([b]);
        let mut seen = std::collections::HashSet::new();
        while let Some(oid) = pending.pop_front() {
            if ancestors.contains(&oid) {
                return Ok(oid);
            }
            if !seen.insert(oid) {
                continue;
            }
            if let Ok(commit) = self.read_commit(&oid) {
                pending.extend(commit.parents);
            }
        }
        Err(format!("no merge base for {a} and {b}"))
    }

    fn rev_walk<I>(&self, starts: I) -> TestRevWalk
    where
        I: IntoIterator<Item = ObjectId>,
    {
        let mut pending: std::collections::VecDeque<ObjectId> = starts.into_iter().collect();
        let mut seen = std::collections::HashSet::new();
        let mut items = Vec::new();
        while let Some(oid) = pending.pop_front() {
            if !seen.insert(oid) {
                continue;
            }
            match self.read_commit(&oid) {
                Ok(commit) => {
                    pending.extend(commit.parents.iter().copied());
                    items.push(Ok(TestCommitInfo { id: oid }));
                }
                Err(err) => items.push(Err(err.to_string())),
            }
        }
        TestRevWalk { items }
    }
}

pub fn commit_ancestors(
    repo: &SleyRepository,
    start: ObjectId,
) -> std::collections::HashSet<ObjectId> {
    let mut pending = std::collections::VecDeque::from([start]);
    let mut seen = std::collections::HashSet::new();
    while let Some(oid) = pending.pop_front() {
        if !seen.insert(oid) {
            continue;
        }
        if let Ok(commit) = repo.read_commit(&oid) {
            pending.extend(commit.parents);
        }
    }
    seen
}

pub struct TestCommit {
    pub id: ObjectId,
    pub commit: CommitObject,
    pub tree: sley::TreeObject,
}

pub struct TestCommitInfo {
    pub id: ObjectId,
}

pub struct TestRevWalk {
    items: Vec<Result<TestCommitInfo, String>>,
}

impl TestRevWalk {
    pub fn all(self) -> Result<std::vec::IntoIter<Result<TestCommitInfo, String>>, String> {
        Ok(self.items.into_iter())
    }
}

pub struct TestTag {
    pub id: ObjectId,
}

impl TestTag {
    pub fn id(&self) -> ObjectId {
        self.id
    }
}

impl TestCommit {
    pub fn id(&self) -> ObjectId {
        self.id
    }

    pub fn tree_id(&self) -> Option<ObjectId> {
        Some(self.commit.tree)
    }

    pub fn tree(&self) -> Result<TestTree, String> {
        Ok(TestTree {
            entries: self
                .tree
                .entries
                .iter()
                .map(|entry| entry.name.as_bytes().to_vec())
                .collect(),
        })
    }

    pub fn message_raw_sloppy(&self) -> TestRawMessage<'_> {
        TestRawMessage(&self.commit.message)
    }
}

pub struct TestTree {
    entries: Vec<Vec<u8>>,
}

impl TestTree {
    pub fn lookup_entry_by_path(&self, path: &str) -> Result<Option<()>, String> {
        if path.contains('/') {
            return Err(format!(
                "nested lookup not implemented for test path {path}"
            ));
        }
        Ok(self
            .entries
            .iter()
            .any(|entry| entry.as_slice() == path.as_bytes())
            .then_some(()))
    }
}

pub struct TestRawMessage<'a>(&'a [u8]);

impl std::fmt::Display for TestRawMessage<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", String::from_utf8_lossy(self.0))
    }
}

pub fn find_reference<'a>(
    repo: &'a SleyRepository,
    name: &str,
) -> Result<TestReference<'a>, String> {
    let full_name = if name == "HEAD" || name.starts_with("refs/") {
        name.to_string()
    } else {
        format!("refs/heads/{name}")
    };
    let target = repo
        .references()
        .read_ref(&full_name)
        .map_err(|err| err.to_string())?
        .ok_or_else(|| format!("reference {full_name} not found"))?;
    Ok(TestReference { repo, target })
}

pub struct TestReference<'a> {
    repo: &'a SleyRepository,
    pub target: ReferenceTarget,
}

impl TestReference<'_> {
    pub fn peel_to_id(&mut self) -> Result<ObjectId, String> {
        let oid = match &self.target {
            ReferenceTarget::Direct(oid) => *oid,
            ReferenceTarget::Symbolic(name) => {
                let reference = find_reference(self.repo, name)?;
                match reference.target {
                    ReferenceTarget::Direct(oid) => oid,
                    ReferenceTarget::Symbolic(_) => {
                        return Err(format!("nested symbolic reference {name} is unsupported"));
                    }
                }
            }
        };
        Ok(oid)
    }

    pub fn target(&self) -> TestReferenceTarget<'_> {
        TestReferenceTarget {
            target: &self.target,
        }
    }
}

pub struct TestReferenceTarget<'a> {
    target: &'a ReferenceTarget,
}

impl<'a> TestReferenceTarget<'a> {
    pub fn try_id(&self) -> Option<&'a ObjectId> {
        match self.target {
            ReferenceTarget::Direct(oid) => Some(oid),
            ReferenceTarget::Symbolic(_) => None,
        }
    }
}

pub fn assert_json_recovery_advice_fields(envelope: &Value, context: &str) {
    for field in [
        "unsafe_condition",
        "would_change",
        "preserved",
        "primary_command",
        "recovery_commands",
        "hint",
    ] {
        assert!(
            envelope[field]
                .as_str()
                .is_some_and(|value| !value.trim().is_empty())
                || envelope[field]
                    .as_array()
                    .is_some_and(|value| !value.is_empty()),
            "JSON recovery advice should expose `{field}` through structured fields: {context}"
        );
    }
    assert!(
        envelope["error"].as_str().is_some_and(|error| {
            !error.contains("Unsafe:")
                && !error.contains("Would change:")
                && !error.contains("Preserved:")
                && !error.contains("Primary recovery:")
                && !error.contains("Other recovery:")
        }),
        "JSON `error` should stay concise; recovery detail belongs in structured fields: {context}"
    );
    assert!(
        envelope
            .get("primary_command_template")
            .is_some_and(|template| template.is_null() || template.is_object()),
        "JSON recovery advice should expose `primary_command_template` as object or null: {context}"
    );
    assert!(
        envelope["recovery_action_templates"]
            .as_array()
            .is_some_and(|templates| templates.iter().all(|template| template.is_object())),
        "JSON recovery advice should expose `recovery_action_templates` as an array of template objects: {context}"
    );
}

pub fn assert_undo_requires_hard(cwd: &Path) -> Value {
    let output = heddle_output(&["--output", "json", "undo"], Some(cwd))
        .expect("plain undo should return typed safety advice");
    assert_eq!(
        output.status.code(),
        Some(74),
        "destructive plain undo should use the safety-refusal exit code: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        output.stdout.is_empty(),
        "JSON safety refusal should keep stdout empty: {}",
        String::from_utf8_lossy(&output.stdout)
    );
    let envelope: Value =
        serde_json::from_slice(&output.stderr).expect("undo refusal should be JSON");
    assert_eq!(envelope["kind"], "undo_requires_hard", "{envelope}");
    assert_eq!(
        envelope["unsafe_condition"],
        "the selected undo operation materializes an earlier saved tree",
        "{envelope}"
    );
    assert_eq!(
        envelope["would_change"],
        "captured worktree files would be replaced by the selected operation's prior tree",
        "{envelope}"
    );
    assert_eq!(
        envelope["preserved"], "repository state and worktree files were left unchanged",
        "{envelope}"
    );
    assert_json_recovery_advice_fields(&envelope, "undo --hard refusal");
    envelope
}

pub fn heddle(args: &[&str], cwd: Option<&std::path::Path>) -> Result<String, String> {
    let output = heddle_output(args, cwd)?;
    let stdout = str::from_utf8(&output.stdout).unwrap_or("").to_string();
    let stderr = str::from_utf8(&output.stderr).unwrap_or("").to_string();

    if output.status.success() {
        Ok(stdout)
    } else {
        Err(format!(
            "Exit code: {:?}\nstdout: {}\nstderr: {}",
            output.status.code(),
            stdout,
            stderr
        ))
    }
}

/// Render the help that `heddle <args>` would print, **in-process**, without
/// spawning the binary.
///
/// `heddle <verb> --help`, `heddle help <topic>`, and `heddle capture
/// --help-agent` are pure presentation: they render text from the clap command
/// tree + curated topic strings with no repo, cwd, or env dependence, and exit
/// before any command body runs. So they don't need a real subprocess — calling
/// `cli::cli::help::render_for_args` gives the byte-identical stdout the binary
/// produces (the binary's `print_*` helpers are `write_stdout(&render_*(..))`
/// wrappers; see `help_render_matches_spawned_binary` for the equivalence
/// guard). This skips one process spawn per help assertion (HeddleCo/heddle#381).
///
/// Help output carries no ANSI styling here (matching the non-TTY piped
/// subprocess the spawn helper used), so substring assertions transfer
/// unchanged.
pub fn heddle_help(args: &[&str]) -> String {
    cli::cli::help::render_for_args(args).unwrap_or_else(|| {
        panic!(
            "`heddle {}` is not an in-process help request",
            args.join(" ")
        )
    })
}

/// Look up a registered JSON Schema **in-process**, without spawning the
/// binary. The user-facing `heddle schemas <verb>` verb was culled; the
/// registry (`schema_for_verb`) is the library contract that remains, so
/// schema-shape assertions read it directly.
pub fn heddle_schema(verb: &str) -> Value {
    let schema = cli::cli::commands::schema_for_verb(verb)
        .unwrap_or_else(|| panic!("no schema registered for `{verb}`"));
    serde_json::from_str(&schema.to_string()).expect("registered schema should be JSON")
}

pub fn heddle_output(args: &[&str], cwd: Option<&std::path::Path>) -> Result<Output, String> {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_heddle"));
    cmd.args(args);

    let temp;
    let dir = if let Some(dir) = cwd {
        dir.to_path_buf()
    } else {
        temp = TempDir::new().map_err(|e| e.to_string())?;
        temp.path().to_path_buf()
    };
    cmd.current_dir(&dir);
    let config_path = default_test_user_config_path(&dir);
    seed_default_test_user_config(&config_path, &dir)?;
    cmd.env("HEDDLE_CONFIG", config_path);
    cmd.env("HOME", default_test_home_path(&dir));
    cmd.env("HEDDLE_FSMONITOR", "off");
    cmd.env("GIT_CONFIG_GLOBAL", "/dev/null");
    cmd.env("GIT_CONFIG_SYSTEM", "/dev/null");
    cmd.env("GIT_CONFIG_NOSYSTEM", "1");

    cmd.output().map_err(|e| e.to_string())
}

pub fn heddle_argv_json<I, S>(args: I) -> Value
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let argv = std::iter::once(env!("CARGO_BIN_EXE_heddle").to_string())
        .chain(args.into_iter().map(|arg| arg.as_ref().to_string()))
        .collect::<Vec<_>>();
    serde_json::json!(argv)
}

pub fn canonical_path_string(path: &std::path::Path) -> String {
    path.canonicalize()
        .unwrap_or_else(|_| path.to_path_buf())
        .display()
        .to_string()
}

pub fn heddle_output_with_env(
    args: &[&str],
    cwd: Option<&std::path::Path>,
    envs: &[(&str, &str)],
) -> Result<Output, String> {
    heddle_output_with_env_removed(args, cwd, envs, &[])
}

pub fn heddle_output_with_env_removed(
    args: &[&str],
    cwd: Option<&std::path::Path>,
    envs: &[(&str, &str)],
    remove_envs: &[&str],
) -> Result<Output, String> {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_heddle"));
    cmd.args(args);

    let temp;
    let dir = if let Some(dir) = cwd {
        dir.to_path_buf()
    } else {
        temp = TempDir::new().map_err(|e| e.to_string())?;
        temp.path().to_path_buf()
    };
    cmd.current_dir(&dir);
    let config_path = default_test_user_config_path(&dir);
    seed_default_test_user_config(&config_path, &dir)?;
    cmd.env("HEDDLE_CONFIG", config_path);
    cmd.env("HOME", default_test_home_path(&dir));
    cmd.env("HEDDLE_FSMONITOR", "off");
    cmd.env_remove("NO_COLOR");
    for key in remove_envs {
        cmd.env_remove(key);
    }
    for (key, value) in envs {
        cmd.env(key, value);
    }

    cmd.output().map_err(|e| e.to_string())
}

pub fn heddle_output_with_stdin(
    args: &[&str],
    cwd: &std::path::Path,
    stdin: &str,
) -> Result<Output, String> {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_heddle"));
    cmd.args(args);
    cmd.current_dir(cwd);
    let config_path = default_test_user_config_path(cwd);
    seed_default_test_user_config(&config_path, cwd)?;
    cmd.env("HEDDLE_CONFIG", config_path);
    cmd.env("HOME", default_test_home_path(cwd));
    cmd.env("HEDDLE_FSMONITOR", "off");
    cmd.stdin(Stdio::piped());
    cmd.stdout(Stdio::piped());
    cmd.stderr(Stdio::piped());

    let mut child = cmd.spawn().map_err(|e| e.to_string())?;
    let mut stdin_pipe = child
        .stdin
        .take()
        .ok_or_else(|| "missing stdin pipe".to_string())?;
    stdin_pipe
        .write_all(stdin.as_bytes())
        .map_err(|e| e.to_string())?;
    drop(stdin_pipe);

    child.wait_with_output().map_err(|e| e.to_string())
}

pub fn state_chain_ids(path: &std::path::Path, count: usize) -> Vec<String> {
    let repo = Repository::open(path).expect("repo should open");
    let mut ids = Vec::new();
    let mut current = repo.head().expect("head should resolve");

    while let Some(id) = current {
        ids.push(id.to_string_full());
        if ids.len() >= count {
            break;
        }
        let state = repo
            .store()
            .get_state(&id)
            .expect("state lookup should work")
            .expect("state should exist");
        current = state.first_parent().copied();
    }

    ids
}

pub fn status_json(path: &std::path::Path) -> Value {
    let output = heddle(&["status", "--output", "json"], Some(path)).unwrap();
    serde_json::from_str(&output).expect("status output should be JSON")
}

/// Run `git <args>` in `dir` under a fully isolated environment, asserting
/// success. Hermetic *by construction*: the child's environment is wiped with
/// [`Command::env_clear`] and rebuilt from a minimal explicit allowlist, so no
/// inherited variable — `GIT_DIR`, `GIT_WORK_TREE`, `GIT_INDEX_FILE`,
/// `GIT_OBJECT_DIRECTORY`, or any other `GIT_*` / ambient var — can leak in and
/// flake the tests. A blocklist would only ever chase the next leaking var;
/// clearing the slate and opting variables back in closes the whole class.
/// Identity is pinned via `-c` so commits don't depend on a global
/// `user.name`/`user.email`. Shared by the exit-code and error-envelope
/// fixtures (HeddleCo/heddle#252) so the isolation lives in one place.
pub fn git_hermetic(args: &[&str], dir: &std::path::Path) {
    let mut command = Command::new("git");
    command.env_clear();
    // Minimal allowlist — everything the child legitimately needs, nothing else.
    if let Some(path) = std::env::var_os("PATH") {
        command.env("PATH", path);
    }
    command
        .env("HOME", dir)
        .env("GIT_CONFIG_GLOBAL", "/dev/null")
        .env("GIT_CONFIG_SYSTEM", "/dev/null")
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .env("GIT_AUTHOR_NAME", "test")
        .env("GIT_AUTHOR_EMAIL", "test@example.com")
        .env("GIT_COMMITTER_NAME", "test")
        .env("GIT_COMMITTER_EMAIL", "test@example.com")
        .env("LANG", "C")
        .env("LC_ALL", "C")
        .env("TERM", "dumb")
        .args([
            "-c",
            "core.hooksPath=/dev/null",
            "-c",
            "commit.gpgsign=false",
            "-c",
            "user.name=test",
            "-c",
            "user.email=test@example.com",
            "-c",
            "init.defaultBranch=main",
        ])
        .args(args)
        .current_dir(dir);
    let status = command
        .status()
        .unwrap_or_else(|err| panic!("spawn git {args:?}: {err}"));
    assert!(status.success(), "git {args:?} failed in {}", dir.display());
}

pub fn open_git(path: impl AsRef<Path>) -> Result<SleyRepository, String> {
    SleyRepository::open(path.as_ref())
        .or_else(|_| SleyRepository::discover(path.as_ref()))
        .map_err(|err| err.to_string())
}

pub fn git_test_signature() -> Signature {
    Signature {
        name: GitByteString::new(b"Heddle Test".to_vec()),
        email: GitByteString::new(b"heddle@test".to_vec()),
        time: GitTime::new(0, 0),
        raw: b"Heddle Test <heddle@test> 0 +0000".to_vec(),
    }
}

pub fn seed_default_test_user_config(
    config_path: &std::path::Path,
    cwd: &std::path::Path,
) -> Result<(), String> {
    if config_path.exists() {
        return Ok(());
    }
    if cwd.join(".git").exists() {
        return Ok(());
    }
    if let Some(parent) = config_path.parent() {
        std::fs::create_dir_all(parent).map_err(|err| err.to_string())?;
    }
    std::fs::write(
        config_path,
        "[principal]\nname = \"Heddle Test\"\nemail = \"heddle@example.com\"\n",
    )
    .map_err(|err| err.to_string())
}

#[derive(PartialEq, Eq)]
pub enum TestTempPath {
    Directory(std::path::PathBuf),
    File(std::path::PathBuf),
}

#[derive(Default)]
pub struct TestTempPaths(Vec<TestTempPath>);

impl Drop for TestTempPaths {
    fn drop(&mut self) {
        for path in self.0.drain(..).rev() {
            let result = match &path {
                TestTempPath::Directory(path) => std::fs::remove_dir_all(path),
                TestTempPath::File(path) => std::fs::remove_file(path),
            };
            if let Err(err) = result
                && err.kind() != std::io::ErrorKind::NotFound
            {
                let path = match path {
                    TestTempPath::Directory(path) | TestTempPath::File(path) => path,
                };
                eprintln!("failed to remove test temp path {}: {err}", path.display());
            }
        }
    }
}

thread_local! {
    static TEST_TEMP_PATHS: std::cell::RefCell<TestTempPaths> =
        std::cell::RefCell::new(TestTempPaths::default());
}

pub fn track_test_temp_path(path: std::path::PathBuf, directory: bool) -> std::path::PathBuf {
    TEST_TEMP_PATHS.with(|paths| {
        let tracked = if directory {
            TestTempPath::Directory(path.clone())
        } else {
            TestTempPath::File(path.clone())
        };
        let mut paths = paths.borrow_mut();
        if !paths.0.contains(&tracked) {
            paths.0.push(tracked);
        }
    });
    path
}

pub fn default_test_user_config_path(cwd: &std::path::Path) -> std::path::PathBuf {
    let path = std::env::temp_dir().join(format!(
        "heddle-cli-test-user-{}-{:016x}.toml",
        std::process::id(),
        test_path_hash(cwd)
    ));
    track_test_temp_path(path, false)
}

pub fn default_test_home_path(cwd: &std::path::Path) -> std::path::PathBuf {
    let path = std::env::temp_dir().join(format!(
        "heddle-cli-test-home-{}-{:016x}",
        std::process::id(),
        test_path_hash(cwd)
    ));
    track_test_temp_path(path, true)
}

pub fn test_path_hash(path: &std::path::Path) -> u64 {
    use std::{
        collections::hash_map::DefaultHasher,
        hash::{Hash, Hasher},
    };

    let mut hasher = DefaultHasher::new();
    path.hash(&mut hasher);
    hasher.finish()
}

pub fn git_empty_tree_oid(repo: &SleyRepository) -> ObjectId {
    repo.write_tree(sley::TreeEditor::new())
        .expect("write empty tree")
}

pub fn git_set_reference(repo: &SleyRepository, name: &str, target: ObjectId) {
    let sig = git_test_signature();
    let refs = repo.references();
    let old_oid = match refs.read_ref(name).expect("read ref") {
        Some(ReferenceTarget::Direct(oid)) => oid,
        _ => ObjectId::null(repo.object_format()),
    };
    let mut tx = refs.transaction();
    tx.update_to(
        name.to_string(),
        ReferenceTarget::Direct(target),
        RefPrecondition::Any,
        Some(ReflogEntry {
            old_oid,
            new_oid: target,
            committer: sig.to_ident_bytes(),
            message: b"test: update ref".to_vec(),
        }),
    );
    tx.commit().expect("update ref");
}

pub fn git_commit_with_tree(
    repo: &SleyRepository,
    reference: Option<&str>,
    tree_oid: ObjectId,
    message: &str,
    parents: &[ObjectId],
) -> ObjectId {
    let sig = git_test_signature();
    let commit = CommitObject {
        tree: tree_oid,
        parents: parents.to_vec(),
        author: sig.to_ident_bytes(),
        committer: sig.to_ident_bytes(),
        encoding: None,
        message: message.as_bytes().to_vec(),
    };
    let commit_id = repo
        .write_object(EncodedObject::new(GitObjectType::Commit, commit.write()))
        .expect("commit");
    if let Some(reference) = reference {
        git_set_reference(repo, reference, commit_id);
    }
    commit_id
}

pub fn git_create_annotated_tag(
    repo: &SleyRepository,
    name: &str,
    target: ObjectId,
    object_type: GitObjectType,
    message: &str,
    precondition: RefPrecondition,
) -> TestTag {
    let tag = TagObject {
        object: target,
        object_type,
        name: name.as_bytes().to_vec(),
        tagger: Some(git_test_signature().to_ident_bytes()),
        message: message.as_bytes().to_vec(),
        raw_body: None,
    };
    let tag_id = repo
        .write_object(EncodedObject::new(GitObjectType::Tag, tag.write()))
        .expect("write annotated tag");
    let refs = repo.references();
    let mut tx = refs.transaction();
    tx.update_to(
        format!("refs/tags/{name}"),
        ReferenceTarget::Direct(tag_id),
        precondition,
        None,
    );
    tx.commit().expect("update tag ref");
    TestTag { id: tag_id }
}