omni-dev 0.39.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, and Datadog.
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
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
//! Logged wrappers over `git worktree` (issue #1392).
//!
//! Each verb is a thin shell over the real `git worktree <verb>`; the value is
//! the `kind: "worktree"` request-log record it writes, carrying the
//! recovery-relevant metadata (path/branch/commit/dirtiness) that lets
//! `omni-dev log --command 'git worktree remove'` answer "what worktree did I
//! just remove?" — the row needed to run `git branch <branch> <commit>` and
//! re-attach. Metadata capture is strictly best-effort: enrichment failures
//! never change the wrapped git operation's behaviour or exit status.
//!
//! Distinct from the top-level `omni-dev worktrees` command, which lists the
//! worktrees open across VS Code windows via the daemon.

use std::collections::BTreeMap;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use serde::Serialize;

use crate::request_log::{self, WorktreeOutcome};

/// Worktree operations: logged wrappers over `git worktree`.
#[derive(Parser)]
#[command(
    long_about = "Worktree operations: thin wrappers over `git worktree` that record \
recovery-relevant metadata (path, branch, commit, uncommitted status) to the omni-dev \
request log. If a worktree is removed by mistake, `omni-dev log --command 'git worktree \
remove'` returns the row needed to recover the branch.\n\nNot to be confused with \
`omni-dev worktrees`, which lists worktrees open across VS Code windows via the daemon."
)]
pub struct WorktreeCommand {
    /// Worktree subcommand to execute.
    #[command(subcommand)]
    pub command: WorktreeSubcommands,
}

/// Worktree subcommands.
#[derive(Subcommand)]
pub enum WorktreeSubcommands {
    /// Creates a worktree at `<path>` (wraps `git worktree add`).
    Add(AddArgs),
    /// Removes a worktree, recording branch/commit/dirtiness first (wraps `git worktree remove`).
    Remove(RemoveArgs),
    /// Lists worktrees (wraps `git worktree list`).
    List(ListArgs),
    /// Moves a worktree to a new location (wraps `git worktree move`).
    Move(MoveArgs),
    /// Prunes stale worktree metadata, recording what was pruned (wraps `git worktree prune`).
    Prune(PruneArgs),
    /// Repairs worktree administrative files (wraps `git worktree repair`).
    Repair(RepairArgs),
}

/// Arguments for `git worktree add`.
#[derive(Parser)]
pub struct AddArgs {
    /// Path for the new worktree.
    pub path: PathBuf,
    /// Commit-ish to check out (defaults to HEAD).
    pub commit_ish: Option<String>,
    /// Create a new branch for the worktree.
    #[arg(short = 'b', long = "branch", value_name = "BRANCH")]
    pub branch: Option<String>,
    /// Checkout even if another worktree holds the branch.
    #[arg(short = 'f', long)]
    pub force: bool,
    /// Detach HEAD in the new worktree.
    #[arg(long)]
    pub detach: bool,
}

/// Arguments for `git worktree remove`.
#[derive(Parser)]
pub struct RemoveArgs {
    /// Path of the worktree to remove.
    pub path: PathBuf,
    /// Remove even if the worktree is dirty or locked.
    #[arg(short = 'f', long)]
    pub force: bool,
}

/// Arguments for `git worktree list`.
#[derive(Parser)]
pub struct ListArgs {
    /// Emit git's machine-readable porcelain output verbatim.
    #[arg(long, conflicts_with = "output_json")]
    pub porcelain: bool,
    /// Emit the porcelain data as JSON.
    #[arg(long)]
    pub output_json: bool,
}

/// Arguments for `git worktree move`.
#[derive(Parser)]
pub struct MoveArgs {
    /// Current path of the worktree.
    pub from: PathBuf,
    /// New path for the worktree.
    pub to: PathBuf,
    /// Move even if the worktree is dirty or locked.
    #[arg(short = 'f', long)]
    pub force: bool,
}

/// Arguments for `git worktree prune`.
#[derive(Parser)]
pub struct PruneArgs {
    /// Do not remove anything; report what would be removed.
    #[arg(short = 'n', long)]
    pub dry_run: bool,
    /// Report all removals.
    #[arg(short = 'v', long)]
    pub verbose: bool,
    /// Expire working trees older than TIME.
    #[arg(long, value_name = "TIME")]
    pub expire: Option<String>,
}

/// Arguments for `git worktree repair`.
#[derive(Parser)]
pub struct RepairArgs {
    /// Worktree paths to repair (all, when omitted).
    pub paths: Vec<PathBuf>,
}

impl WorktreeCommand {
    /// Executes the worktree command.
    ///
    /// `repo` is the CLI-level `--repo` location; git runs with it as the
    /// working directory, so relative worktree paths resolve against it
    /// (matching `git -C` semantics).
    pub fn execute(self, repo: Option<&Path>) -> Result<()> {
        let base = base_dir(repo)?;
        match self.command {
            WorktreeSubcommands::Add(args) => run_add(&base, &args),
            WorktreeSubcommands::Remove(args) => run_remove(&base, &args),
            WorktreeSubcommands::List(args) => run_list(&base, &args),
            WorktreeSubcommands::Move(args) => run_move(&base, &args),
            WorktreeSubcommands::Prune(args) => run_prune(&base, &args),
            WorktreeSubcommands::Repair(args) => run_repair(&base, &args),
        }
    }
}

// --- per-verb flows -------------------------------------------------------

fn run_add(base: &Path, args: &AddArgs) -> Result<()> {
    let argv = add_argv(args);
    let mut context = base_context(base);
    let abs = absolutize(base, &args.path);
    let run = run_git(base, &argv);
    echo(&run);
    context.insert("path".to_string(), display_canonical(&abs));
    if run.exit_code == Some(0) {
        // The branch and HEAD only exist after a successful checkout.
        insert_snapshot(&mut context, &snapshot(&abs), false);
    }
    finish("add", argv, run, context)
}

fn run_remove(base: &Path, args: &RemoveArgs) -> Result<()> {
    let argv = remove_argv(args);
    let mut context = base_context(base);
    let abs = absolutize(base, &args.path);
    // Captured BEFORE git deletes the working directory — this is the record's
    // entire recovery value; on failure it is still written.
    context.insert("path".to_string(), display_canonical(&abs));
    context.insert("used_force".to_string(), args.force.to_string());
    let snap = snapshot(&abs);
    insert_snapshot(&mut context, &snap, true);
    let run = run_git(base, &argv);
    echo(&run);
    finish("remove", argv, run, context)
}

fn run_list(base: &Path, args: &ListArgs) -> Result<()> {
    let porcelain = args.porcelain || args.output_json;
    let argv = list_argv(porcelain);
    let mut context = base_context(base);
    let run = run_git(base, &argv);
    if args.output_json {
        // JSON replaces git's stdout; stderr still passes through.
        echo_stderr(&run);
        if run.exit_code == Some(0) {
            let stdout = String::from_utf8_lossy(&run.stdout);
            let entries = parse_worktree_porcelain(&stdout);
            context.insert("count".to_string(), entries.len().to_string());
            let json = serde_json::to_string_pretty(&entries)
                .context("Failed to serialize worktree list as JSON")?;
            println!("{json}");
        }
    } else {
        echo(&run);
        if run.exit_code == Some(0) {
            let stdout = String::from_utf8_lossy(&run.stdout);
            let count = if porcelain {
                parse_worktree_porcelain(&stdout).len()
            } else {
                stdout.lines().filter(|l| !l.trim().is_empty()).count()
            };
            context.insert("count".to_string(), count.to_string());
        }
    }
    finish("list", argv, run, context)
}

fn run_move(base: &Path, args: &MoveArgs) -> Result<()> {
    let argv = move_argv(args);
    let mut context = base_context(base);
    let from_abs = absolutize(base, &args.from);
    context.insert("from_path".to_string(), display_canonical(&from_abs));
    context.insert(
        "to_path".to_string(),
        absolutize(base, &args.to).display().to_string(),
    );
    // Branch captured before the move, in case the move fails midway.
    let snap = snapshot(&from_abs);
    if let Some(branch) = &snap.branch {
        context.insert("branch".to_string(), branch.clone());
    }
    let run = run_git(base, &argv);
    echo(&run);
    finish("move", argv, run, context)
}

fn run_prune(base: &Path, args: &PruneArgs) -> Result<()> {
    let argv = prune_argv(args);
    let mut context = base_context(base);
    if args.dry_run {
        context.insert("dry_run".to_string(), "true".to_string());
    }
    // Diffing the porcelain list before vs after yields the pruned entries
    // with the branch/commit that git still reports for prunable stanzas.
    let before = list_entries(base);
    let run = run_git(base, &argv);
    echo(&run);
    if let (Some(before), Some(after)) = (before, list_entries(base)) {
        let pruned = pruned_entries(&before, &after);
        match serde_json::to_string(&pruned) {
            Ok(json) => {
                context.insert("pruned".to_string(), json);
            }
            Err(e) => tracing::debug!("Cannot serialize pruned worktree list: {e}"),
        }
    }
    finish("prune", argv, run, context)
}

fn run_repair(base: &Path, args: &RepairArgs) -> Result<()> {
    let argv = repair_argv(args);
    let mut context = base_context(base);
    let run = run_git(base, &argv);
    echo(&run);
    // git reports repairs on both streams depending on the verb/version.
    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr)
    );
    let repaired = parse_repair_paths(&combined);
    if !repaired.is_empty() {
        match serde_json::to_string(&repaired) {
            Ok(json) => {
                context.insert("repaired".to_string(), json);
            }
            Err(e) => tracing::debug!("Cannot serialize repaired worktree list: {e}"),
        }
    }
    finish("repair", argv, run, context)
}

/// Records the outcome and converts the subprocess result into the command's
/// result: success on exit 0, an error (non-zero omni-dev exit) otherwise.
fn finish(
    verb: &str,
    argv: Vec<String>,
    run: GitRun,
    context: BTreeMap<String, String>,
) -> Result<()> {
    request_log::record_worktree(WorktreeOutcome {
        verb: verb.to_string(),
        argv,
        exit_code: run.exit_code,
        duration: run.duration,
        error: run.error.clone(),
        context,
    });
    match (run.exit_code, run.error) {
        (Some(0), _) => Ok(()),
        // Stderr was already echoed, so the error chain stays short.
        (Some(code), _) => anyhow::bail!("git worktree {verb} failed with exit code {code}"),
        (None, err) => anyhow::bail!(
            "Failed to run git worktree {verb}: {}",
            err.unwrap_or_else(|| "unknown spawn error".to_string())
        ),
    }
}

// --- argv builders (pure, unit-tested) ------------------------------------

fn add_argv(args: &AddArgs) -> Vec<String> {
    let mut argv = vec!["worktree".to_string(), "add".to_string()];
    if args.force {
        argv.push("--force".to_string());
    }
    if args.detach {
        argv.push("--detach".to_string());
    }
    if let Some(branch) = &args.branch {
        argv.push("-b".to_string());
        argv.push(branch.clone());
    }
    argv.push(args.path.display().to_string());
    if let Some(commit_ish) = &args.commit_ish {
        argv.push(commit_ish.clone());
    }
    argv
}

fn remove_argv(args: &RemoveArgs) -> Vec<String> {
    let mut argv = vec!["worktree".to_string(), "remove".to_string()];
    if args.force {
        argv.push("--force".to_string());
    }
    argv.push(args.path.display().to_string());
    argv
}

fn list_argv(porcelain: bool) -> Vec<String> {
    if porcelain {
        // `core.quotePath=false` keeps non-ASCII paths literal for the parser.
        vec![
            "-c".to_string(),
            "core.quotePath=false".to_string(),
            "worktree".to_string(),
            "list".to_string(),
            "--porcelain".to_string(),
        ]
    } else {
        vec!["worktree".to_string(), "list".to_string()]
    }
}

fn move_argv(args: &MoveArgs) -> Vec<String> {
    let mut argv = vec!["worktree".to_string(), "move".to_string()];
    if args.force {
        argv.push("--force".to_string());
    }
    argv.push(args.from.display().to_string());
    argv.push(args.to.display().to_string());
    argv
}

fn prune_argv(args: &PruneArgs) -> Vec<String> {
    let mut argv = vec!["worktree".to_string(), "prune".to_string()];
    if args.dry_run {
        argv.push("--dry-run".to_string());
    }
    if args.verbose {
        argv.push("--verbose".to_string());
    }
    if let Some(expire) = &args.expire {
        argv.push("--expire".to_string());
        argv.push(expire.clone());
    }
    argv
}

fn repair_argv(args: &RepairArgs) -> Vec<String> {
    let mut argv = vec!["worktree".to_string(), "repair".to_string()];
    argv.extend(args.paths.iter().map(|p| p.display().to_string()));
    argv
}

// --- subprocess runner ----------------------------------------------------

/// The observed result of one `git` subprocess.
struct GitRun {
    exit_code: Option<i32>,
    duration: Duration,
    error: Option<String>,
    stdout: Vec<u8>,
    stderr: Vec<u8>,
}

/// Builds a `git` [`Command`] whose child receives a snapshot of the current
/// environment (`env_clear` + `envs`), keeping the spawn out of the data race
/// against concurrent `std::env::set_var` (issue #1022; same idiom as
/// `crate::cli::ai::claude::skills`).
fn git_command() -> Command {
    let mut cmd = Command::new("git");
    cmd.env_clear();
    cmd.envs(std::env::vars_os());
    cmd
}

/// Runs `git <argv>` in `base`, timing it. Never fails: a spawn error lands in
/// [`GitRun::error`] so the caller can record it before reporting.
fn run_git(base: &Path, argv: &[String]) -> GitRun {
    let started = Instant::now();
    let result = git_command().args(argv).current_dir(base).output();
    let duration = started.elapsed();
    match result {
        Ok(output) => GitRun {
            exit_code: output.status.code(),
            duration,
            error: None,
            stdout: output.stdout,
            stderr: output.stderr,
        },
        Err(e) => GitRun {
            exit_code: None,
            duration,
            error: Some(e.to_string()),
            stdout: Vec::new(),
            stderr: Vec::new(),
        },
    }
}

/// Echoes the captured git output to our own streams (thin-wrapper UX).
fn echo(run: &GitRun) {
    // Best-effort: a write failure here (e.g. EPIPE from a closed pager) is
    // the downstream consumer's condition, not ours.
    let _ = std::io::stdout().write_all(&run.stdout);
    echo_stderr(run);
}

/// Echoes only git's stderr (used when stdout is replaced, e.g. `--output-json`).
fn echo_stderr(run: &GitRun) {
    // Best-effort, as in `echo`.
    let _ = std::io::stderr().write_all(&run.stderr);
}

// --- metadata capture (best-effort) ---------------------------------------

/// Resolves the directory git runs in: `--repo` when given, else the cwd.
fn base_dir(repo: Option<&Path>) -> Result<PathBuf> {
    match repo {
        Some(p) => Ok(p.to_path_buf()),
        None => std::env::current_dir().context("Failed to resolve working directory"),
    }
}

/// Context common to every verb: the main worktree root, when resolvable.
fn base_context(base: &Path) -> BTreeMap<String, String> {
    let mut context = BTreeMap::new();
    if let Some(toplevel) = toplevel(base) {
        context.insert("repo".to_string(), toplevel);
    }
    context
}

/// Best-effort `git rev-parse --show-toplevel` from `base`.
fn toplevel(base: &Path) -> Option<String> {
    let output = git_command()
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(base)
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let stdout = String::from_utf8(output.stdout).ok()?;
    let trimmed = stdout.trim();
    (!trimmed.is_empty()).then(|| trimmed.to_string())
}

/// Best-effort git2 snapshot of a worktree; every field degrades to absent.
#[derive(Default)]
struct WtSnapshot {
    branch: Option<String>,
    detached: bool,
    commit: Option<String>,
    dirty: Option<bool>,
}

/// Reads branch, HEAD commit, and dirtiness from the worktree at `path`.
fn snapshot(path: &Path) -> WtSnapshot {
    let mut snap = WtSnapshot::default();
    let repo = match git2::Repository::open(path) {
        Ok(repo) => repo,
        Err(e) => {
            tracing::debug!("Cannot open {} as a git repository: {e}", path.display());
            return snap;
        }
    };
    match repo.head() {
        Ok(head) => {
            if head.is_branch() {
                // Err = non-UTF-8 ref name; omit the branch rather than mangle it.
                snap.branch = head.shorthand().ok().map(str::to_string);
            } else {
                snap.detached = true;
            }
            snap.commit = head.target().map(|oid| oid.to_string());
        }
        Err(e) => tracing::debug!("Cannot read HEAD of {}: {e}", path.display()),
    }
    // Untracked files count as dirty: they are exactly what `remove --force`
    // destroys. Same StatusOptions recipe as the daemon worktrees service.
    let mut opts = git2::StatusOptions::new();
    opts.include_untracked(true)
        .recurse_untracked_dirs(true)
        .include_ignored(false)
        .exclude_submodules(true);
    match repo.statuses(Some(&mut opts)) {
        Ok(statuses) => snap.dirty = Some(!statuses.is_empty()),
        Err(e) => tracing::debug!("Cannot read status of {}: {e}", path.display()),
    }
    snap
}

/// Inserts the snapshot's fields into `context`, skipping unknown ones.
fn insert_snapshot(context: &mut BTreeMap<String, String>, snap: &WtSnapshot, with_dirty: bool) {
    if let Some(branch) = &snap.branch {
        context.insert("branch".to_string(), branch.clone());
    }
    if snap.detached {
        context.insert("detached".to_string(), "true".to_string());
    }
    if let Some(commit) = &snap.commit {
        context.insert("commit".to_string(), commit.clone());
    }
    if with_dirty {
        if let Some(dirty) = snap.dirty {
            context.insert("had_uncommitted".to_string(), dirty.to_string());
        }
    }
}

/// Resolves `path` against `base` when relative (matching `git -C` semantics).
fn absolutize(base: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        base.join(path)
    }
}

/// Canonical display form of `path`; falls back to the joined path when the
/// target does not exist (yet, or any more).
fn display_canonical(path: &Path) -> String {
    std::fs::canonicalize(path)
        .unwrap_or_else(|_| path.to_path_buf())
        .display()
        .to_string()
}

// --- porcelain parsing (pure, unit-tested) --------------------------------

/// One stanza of `git worktree list --porcelain`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct WorktreeEntry {
    /// Absolute worktree root path.
    pub path: String,
    /// HEAD commit sha.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub head: Option<String>,
    /// Checked-out branch, `refs/heads/` prefix stripped.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch: Option<String>,
    /// True when HEAD is detached.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub detached: bool,
    /// True for a bare checkout.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub bare: bool,
    /// Lock reason; empty string when locked without a reason.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locked: Option<String>,
    /// Prunable reason.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prunable: Option<String>,
}

impl WorktreeEntry {
    fn new(path: String) -> Self {
        Self {
            path,
            head: None,
            branch: None,
            detached: false,
            bare: false,
            locked: None,
            prunable: None,
        }
    }
}

/// Parses `git worktree list --porcelain` output. Unknown attributes are
/// ignored so future git versions cannot break the parse.
fn parse_worktree_porcelain(out: &str) -> Vec<WorktreeEntry> {
    let mut entries = Vec::new();
    let mut current: Option<WorktreeEntry> = None;
    for line in out.lines() {
        if let Some(path) = line.strip_prefix("worktree ") {
            entries.extend(current.take());
            current = Some(WorktreeEntry::new(path.to_string()));
            continue;
        }
        let Some(entry) = current.as_mut() else {
            continue;
        };
        if let Some(head) = line.strip_prefix("HEAD ") {
            entry.head = Some(head.to_string());
        } else if let Some(branch) = line.strip_prefix("branch ") {
            entry.branch = Some(
                branch
                    .strip_prefix("refs/heads/")
                    .unwrap_or(branch)
                    .to_string(),
            );
        } else if line == "detached" {
            entry.detached = true;
        } else if line == "bare" {
            entry.bare = true;
        } else if line == "locked" {
            entry.locked = Some(String::new());
        } else if let Some(reason) = line.strip_prefix("locked ") {
            entry.locked = Some(reason.to_string());
        } else if let Some(reason) = line.strip_prefix("prunable ") {
            entry.prunable = Some(reason.to_string());
        }
        // Blank lines end a stanza implicitly; the next `worktree ` flushes.
    }
    entries.extend(current);
    entries
}

/// Best-effort porcelain listing of the repo at `base` (for the prune diff).
fn list_entries(base: &Path) -> Option<Vec<WorktreeEntry>> {
    let output = git_command()
        .args([
            "-c",
            "core.quotePath=false",
            "worktree",
            "list",
            "--porcelain",
        ])
        .current_dir(base)
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    Some(parse_worktree_porcelain(&String::from_utf8_lossy(
        &output.stdout,
    )))
}

/// A pruned worktree, as recorded in the `pruned` context field.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct PrunedEntry {
    path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    branch: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    commit: Option<String>,
}

/// The entries present in `before` but gone from `after` — what prune removed,
/// with the branch/commit git still reported for the prunable stanzas.
fn pruned_entries(before: &[WorktreeEntry], after: &[WorktreeEntry]) -> Vec<PrunedEntry> {
    before
        .iter()
        .filter(|b| !after.iter().any(|a| a.path == b.path))
        .map(|b| PrunedEntry {
            path: b.path.clone(),
            branch: b.branch.clone(),
            commit: b.head.clone(),
        })
        .collect()
}

/// Extracts the paths from `git worktree repair` report lines, best-effort.
/// The messages are human-readable (`repair: <description>: <path>`), so an
/// upstream wording change yields an empty list rather than an error.
fn parse_repair_paths(out: &str) -> Vec<String> {
    out.lines()
        .filter_map(|line| line.strip_prefix("repair: "))
        .filter_map(|rest| rest.rsplit_once(": ").map(|(_, path)| path.to_string()))
        .collect()
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    fn argv(args: &[&str]) -> Vec<String> {
        args.iter().copied().map(String::from).collect()
    }

    // --- porcelain parser ---

    #[test]
    fn porcelain_parses_main_linked_and_detached_stanzas() {
        let out = "\
worktree /repo
HEAD 1111111111111111111111111111111111111111
branch refs/heads/main

worktree /wt/feature
HEAD 2222222222222222222222222222222222222222
branch refs/heads/issue-1392

worktree /wt/detached
HEAD 3333333333333333333333333333333333333333
detached
";
        let entries = parse_worktree_porcelain(out);
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].path, "/repo");
        assert_eq!(entries[0].branch.as_deref(), Some("main"));
        assert_eq!(
            entries[0].head.as_deref(),
            Some("1111111111111111111111111111111111111111")
        );
        assert_eq!(entries[1].branch.as_deref(), Some("issue-1392"));
        assert!(entries[2].detached);
        assert!(entries[2].branch.is_none());
    }

    #[test]
    fn porcelain_parses_bare_locked_prunable_and_ignores_unknown() {
        let out = "\
worktree /bare
bare

worktree /locked-no-reason
HEAD 4444444444444444444444444444444444444444
locked

worktree /locked-with-reason
locked worktree is on a portable device
future-attribute some value

worktree /stale
prunable gitdir file points to non-existent location";
        let entries = parse_worktree_porcelain(out);
        assert_eq!(entries.len(), 4);
        assert!(entries[0].bare);
        assert_eq!(entries[1].locked.as_deref(), Some(""));
        assert_eq!(
            entries[2].locked.as_deref(),
            Some("worktree is on a portable device")
        );
        assert_eq!(
            entries[3].prunable.as_deref(),
            Some("gitdir file points to non-existent location")
        );
    }

    #[test]
    fn porcelain_entry_serializes_compactly() {
        let entries = parse_worktree_porcelain("worktree /repo\nbranch refs/heads/main\n");
        let json = serde_json::to_string(&entries).unwrap();
        // Absent/default fields are skipped.
        assert_eq!(json, r#"[{"path":"/repo","branch":"main"}]"#);
    }

    #[test]
    fn porcelain_ignores_attributes_before_the_first_stanza() {
        // A stray attribute line with no open stanza is dropped, not a panic.
        let entries = parse_worktree_porcelain("HEAD 9999\nworktree /a\n");
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].path, "/a");
        assert!(entries[0].head.is_none());
    }

    // --- metadata capture ---

    #[test]
    fn snapshot_degrades_on_non_repo_unborn_and_detached_trees() {
        let tmp = tempfile::tempdir().unwrap();

        // Not a repository: every field degrades to absent.
        let snap = snapshot(tmp.path());
        assert!(snap.branch.is_none());
        assert!(snap.commit.is_none());
        assert!(snap.dirty.is_none());
        assert!(!snap.detached);

        // Unborn HEAD (init, no commit): head() fails, statuses still answer.
        let unborn = tmp.path().join("unborn");
        git2::Repository::init(&unborn).unwrap();
        let snap = snapshot(&unborn);
        assert!(snap.branch.is_none());
        assert!(snap.commit.is_none());
        assert_eq!(snap.dirty, Some(false));

        // Detached HEAD: branch omitted, detached flagged, commit present.
        let detached = tmp.path().join("detached");
        let repo = git2::Repository::init(&detached).unwrap();
        let sig = git2::Signature::now("Test User", "test@example.com").unwrap();
        let tree_id = repo.index().unwrap().write_tree().unwrap();
        let tree = repo.find_tree(tree_id).unwrap();
        let oid = repo
            .commit(Some("HEAD"), &sig, &sig, "init", &tree, &[])
            .unwrap();
        repo.set_head_detached(oid).unwrap();
        let snap = snapshot(&detached);
        assert!(snap.detached);
        assert!(snap.branch.is_none());
        assert_eq!(snap.commit, Some(oid.to_string()));
    }

    #[test]
    fn insert_snapshot_records_detached_and_gates_dirty() {
        let snap = WtSnapshot {
            branch: None,
            detached: true,
            commit: Some("abc".to_string()),
            dirty: Some(false),
        };
        let mut ctx = BTreeMap::new();
        insert_snapshot(&mut ctx, &snap, false);
        assert_eq!(ctx.get("detached").map(String::as_str), Some("true"));
        assert_eq!(ctx.get("commit").map(String::as_str), Some("abc"));
        assert!(!ctx.contains_key("branch"));
        assert!(!ctx.contains_key("had_uncommitted"));

        insert_snapshot(&mut ctx, &snap, true);
        assert_eq!(
            ctx.get("had_uncommitted").map(String::as_str),
            Some("false")
        );
    }

    #[test]
    fn absolutize_and_display_canonical_handle_relative_and_missing_paths() {
        let base = Path::new("/base");
        assert_eq!(absolutize(base, Path::new("/abs")), PathBuf::from("/abs"));
        assert_eq!(
            absolutize(base, Path::new("rel")),
            PathBuf::from("/base/rel")
        );
        // A nonexistent path falls back to the un-canonicalized form verbatim.
        assert_eq!(
            display_canonical(Path::new("/no/such/dir/x")),
            "/no/such/dir/x"
        );
    }

    // --- argv builders ---

    #[test]
    fn add_argv_covers_flags_positionals_and_order() {
        let args = AddArgs {
            path: PathBuf::from("../wt"),
            commit_ish: Some("origin/main".to_string()),
            branch: Some("issue-1".to_string()),
            force: true,
            detach: false,
        };
        assert_eq!(
            add_argv(&args),
            argv(&[
                "worktree",
                "add",
                "--force",
                "-b",
                "issue-1",
                "../wt",
                "origin/main"
            ])
        );
        let minimal = AddArgs {
            path: PathBuf::from("wt"),
            commit_ish: None,
            branch: None,
            force: false,
            detach: true,
        };
        assert_eq!(
            add_argv(&minimal),
            argv(&["worktree", "add", "--detach", "wt"])
        );
    }

    #[test]
    fn remove_move_prune_repair_argv() {
        assert_eq!(
            remove_argv(&RemoveArgs {
                path: PathBuf::from("/wt"),
                force: true,
            }),
            argv(&["worktree", "remove", "--force", "/wt"])
        );
        assert_eq!(
            move_argv(&MoveArgs {
                from: PathBuf::from("a"),
                to: PathBuf::from("b"),
                force: false,
            }),
            argv(&["worktree", "move", "a", "b"])
        );
        assert_eq!(
            move_argv(&MoveArgs {
                from: PathBuf::from("a"),
                to: PathBuf::from("b"),
                force: true,
            }),
            argv(&["worktree", "move", "--force", "a", "b"])
        );
        assert_eq!(
            prune_argv(&PruneArgs {
                dry_run: true,
                verbose: true,
                expire: Some("1.day".to_string()),
            }),
            argv(&[
                "worktree",
                "prune",
                "--dry-run",
                "--verbose",
                "--expire",
                "1.day"
            ])
        );
        assert_eq!(
            repair_argv(&RepairArgs {
                paths: vec![PathBuf::from("a"), PathBuf::from("b")],
            }),
            argv(&["worktree", "repair", "a", "b"])
        );
    }

    #[test]
    fn list_argv_adds_quote_path_only_for_porcelain() {
        assert_eq!(list_argv(false), argv(&["worktree", "list"]));
        assert_eq!(
            list_argv(true),
            argv(&[
                "-c",
                "core.quotePath=false",
                "worktree",
                "list",
                "--porcelain"
            ])
        );
    }

    // --- prune diff + repair parse ---

    #[test]
    fn pruned_entries_diffs_by_path_and_keeps_branch_commit() {
        let before = parse_worktree_porcelain(
            "worktree /repo\nbranch refs/heads/main\n\n\
             worktree /stale\nHEAD 5555555555555555555555555555555555555555\n\
             branch refs/heads/gone\nprunable gitdir gone\n",
        );
        let after = parse_worktree_porcelain("worktree /repo\nbranch refs/heads/main\n");
        let pruned = pruned_entries(&before, &after);
        assert_eq!(pruned.len(), 1);
        assert_eq!(pruned[0].path, "/stale");
        assert_eq!(pruned[0].branch.as_deref(), Some("gone"));
        assert_eq!(
            pruned[0].commit.as_deref(),
            Some("5555555555555555555555555555555555555555")
        );
        assert!(pruned_entries(&after, &after).is_empty());
    }

    #[test]
    fn repair_paths_parsed_from_report_lines_best_effort() {
        let out = "\
repair: gitdir file points to non-existent location: /wt/one/.git
not a repair line
repair: .git file broken: /wt/two/.git";
        assert_eq!(
            parse_repair_paths(out),
            argv(&["/wt/one/.git", "/wt/two/.git"])
        );
        assert!(parse_repair_paths("nothing to repair").is_empty());
    }

    // --- clap surface ---

    #[derive(Parser)]
    struct Wrapper {
        #[command(subcommand)]
        cmd: WorktreeSubcommands,
    }

    #[test]
    fn clap_parses_every_verb() {
        for args in [
            vec!["omni-dev", "add", "wt", "-b", "branch", "origin/main"],
            vec!["omni-dev", "remove", "--force", "wt"],
            vec!["omni-dev", "list", "--porcelain"],
            vec!["omni-dev", "list", "--output-json"],
            vec!["omni-dev", "move", "a", "b"],
            vec!["omni-dev", "prune", "-n", "--expire", "1.day"],
            vec!["omni-dev", "repair", "a", "b"],
            vec!["omni-dev", "repair"],
        ] {
            Wrapper::try_parse_from(&args)
                .unwrap_or_else(|e| panic!("failed to parse {args:?}: {e}"));
        }
    }

    #[test]
    fn clap_rejects_porcelain_with_output_json() {
        assert!(
            Wrapper::try_parse_from(["omni-dev", "list", "--porcelain", "--output-json"]).is_err()
        );
    }
}