iso-code 0.1.1

Safe git worktree lifecycle management — prevents data loss bugs in AI coding agents
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
use std::path::Path;
use std::process::Command;

use crate::error::WorktreeError;
use crate::types::{GitCapabilities, GitVersion, WorktreeHandle, WorktreeState};

/// Parse the output of `git --version` into a `GitVersion`.
///
/// Handles format variations:
/// - "git version 2.43.0"
/// - "git version 2.39.3 (Apple Git-146)"
/// - "git version 2.43.0.windows.1"
pub fn parse_git_version(output: &str) -> Result<GitVersion, WorktreeError> {
    // Extract the version string after "git version "
    let version_str = output
        .trim()
        .strip_prefix("git version ")
        .ok_or_else(|| WorktreeError::GitCommandFailed {
            command: "git --version".to_string(),
            stderr: format!("unexpected output format: {output}"),
            exit_code: 0,
        })?;

    // Take the first space-delimited token (drops "(Apple Git-146)" suffix)
    let version_token = version_str.split_whitespace().next().unwrap_or(version_str);

    // Split by '.' and parse first three components (drops ".windows.1" suffix)
    let parts: Vec<&str> = version_token.split('.').collect();
    if parts.len() < 3 {
        return Err(WorktreeError::GitCommandFailed {
            command: "git --version".to_string(),
            stderr: format!("cannot parse version: {version_token}"),
            exit_code: 0,
        });
    }

    let major = parts[0].parse::<u32>().map_err(|_| WorktreeError::GitCommandFailed {
        command: "git --version".to_string(),
        stderr: format!("cannot parse major version: {}", parts[0]),
        exit_code: 0,
    })?;
    let minor = parts[1].parse::<u32>().map_err(|_| WorktreeError::GitCommandFailed {
        command: "git --version".to_string(),
        stderr: format!("cannot parse minor version: {}", parts[1]),
        exit_code: 0,
    })?;
    let patch = parts[2].parse::<u32>().map_err(|_| WorktreeError::GitCommandFailed {
        command: "git --version".to_string(),
        stderr: format!("cannot parse patch version: {}", parts[2]),
        exit_code: 0,
    })?;

    Ok(GitVersion { major, minor, patch })
}

/// Build a `GitCapabilities` struct from a detected `GitVersion`.
pub fn detect_capabilities(version: &GitVersion) -> GitCapabilities {
    let has_repair = *version >= GitVersion::HAS_REPAIR;               // 2.30+
    let has_list_nul = *version >= GitVersion::HAS_LIST_NUL;           // 2.36+
    let has_merge_tree_write = *version >= GitVersion::HAS_MERGE_TREE_WRITE; // 2.38+
    let has_orphan = *version >= GitVersion { major: 2, minor: 42, patch: 0 }; // 2.42+
    let has_relative_paths = *version >= GitVersion { major: 2, minor: 48, patch: 0 }; // 2.48+

    GitCapabilities::new(
        version.clone(),
        has_list_nul,
        has_repair,
        has_orphan,
        has_relative_paths,
        has_merge_tree_write,
    )
}

/// Run `git --version`, parse the result, and validate against minimum version.
/// Returns `GitCapabilities` on success.
pub fn detect_git_version() -> Result<GitCapabilities, WorktreeError> {
    let output = Command::new("git")
        .arg("--version")
        .output()
        .map_err(|_| WorktreeError::GitNotFound)?;

    if !output.status.success() {
        return Err(WorktreeError::GitNotFound);
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let version = parse_git_version(&stdout)?;

    if version < GitVersion::MINIMUM {
        return Err(WorktreeError::GitVersionTooOld {
            required: format!(
                "{}.{}.{}",
                GitVersion::MINIMUM.major,
                GitVersion::MINIMUM.minor,
                GitVersion::MINIMUM.patch
            ),
            found: format!("{}.{}.{}", version.major, version.minor, version.patch),
        });
    }

    Ok(detect_capabilities(&version))
}

/// Parse the porcelain output of `git worktree list` into [`WorktreeHandle`]s.
///
/// Handles both NUL-delimited (`git -z`, available on Git >= 2.36) and
/// newline-delimited layouts: each worktree is one block of key-value lines,
/// and blocks are separated by double-NUL (or a blank line).
///
/// Path bytes are preserved end-to-end on Unix via `OsStr::from_bytes` so
/// non-UTF8 paths survive through to [`std::path::PathBuf`]. Branch names,
/// HEAD SHAs, and keyword markers are ASCII in practice and are decoded lossily.
pub fn parse_worktree_list_porcelain(
    output: &[u8],
    nul_delimited: bool,
) -> Result<Vec<WorktreeHandle>, WorktreeError> {
    // Block separator: double-NUL in -z mode, blank line (double-LF) otherwise.
    let block_sep: &[u8] = if nul_delimited { b"\0\0" } else { b"\n\n" };
    // Field separator within a block: NUL in -z mode, LF otherwise.
    let field_sep: u8 = if nul_delimited { 0 } else { b'\n' };

    let mut handles = Vec::new();
    for block in split_bytes(output, block_sep) {
        // Trim leading/trailing separator bytes + whitespace from the block.
        let block = trim_block(block);
        if block.is_empty() {
            continue;
        }

        let mut path: Option<std::path::PathBuf> = None;
        let mut head_sha = String::new();
        let mut branch = String::new();
        let mut is_bare = false;
        let mut is_detached = false;
        let mut is_locked = false;
        let mut is_prunable = false;

        for field in block.split(|b| *b == field_sep) {
            let field = trim_field(field);
            if field.is_empty() {
                continue;
            }

            if let Some(p) = strip_prefix_bytes(field, b"worktree ") {
                if !nul_delimited && p.contains(&b'\n') {
                    eprintln!(
                        "WARNING: Worktree path may contain newlines — upgrade to git 2.36 for safe parsing"
                    );
                }
                path = Some(path_from_bytes(p));
            } else if let Some(sha) = strip_prefix_bytes(field, b"HEAD ") {
                head_sha = String::from_utf8_lossy(sha).into_owned();
            } else if let Some(b) = strip_prefix_bytes(field, b"branch ") {
                let s = String::from_utf8_lossy(b);
                branch = s
                    .strip_prefix("refs/heads/")
                    .unwrap_or(&s)
                    .to_string();
            } else if field == b"detached" {
                is_detached = true;
            } else if field == b"bare" {
                is_bare = true;
            } else if field == b"locked" || strip_prefix_bytes(field, b"locked ").is_some() {
                is_locked = true;
            } else if field == b"prunable" || strip_prefix_bytes(field, b"prunable ").is_some() {
                is_prunable = true;
            }
        }

        let Some(wt_path) = path else {
            continue;
        };

        // locked > prunable(orphaned) > active
        let state = if is_locked {
            WorktreeState::Locked
        } else if is_prunable {
            WorktreeState::Orphaned
        } else {
            WorktreeState::Active
        };

        if is_bare || is_detached {
            branch = String::new();
        }

        handles.push(WorktreeHandle::new(
            wt_path,
            branch,
            head_sha,
            state,
            String::new(), // created_at — populated from state.json
            0,             // creator_pid
            String::new(), // creator_name
            None,          // adapter
            false,         // setup_complete
            None,          // port
            String::new(), // session_uuid
        ));
    }

    Ok(handles)
}

/// Build a `PathBuf` from raw bytes. On Unix, preserves non-UTF8 bytes via
/// `OsStr::from_bytes`. On other targets, falls back to lossy UTF-8 decoding
/// (Windows paths are UTF-16 anyway, so non-UTF8 bytes from git there would
/// already be broken).
fn path_from_bytes(b: &[u8]) -> std::path::PathBuf {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;
        std::path::PathBuf::from(std::ffi::OsStr::from_bytes(b))
    }
    #[cfg(not(unix))]
    {
        std::path::PathBuf::from(String::from_utf8_lossy(b).into_owned())
    }
}

fn strip_prefix_bytes<'a>(s: &'a [u8], prefix: &[u8]) -> Option<&'a [u8]> {
    if s.len() >= prefix.len() && &s[..prefix.len()] == prefix {
        Some(&s[prefix.len()..])
    } else {
        None
    }
}

fn trim_field(b: &[u8]) -> &[u8] {
    let mut start = 0;
    while start < b.len() && (b[start] == b' ' || b[start] == b'\t' || b[start] == b'\r') {
        start += 1;
    }
    let mut end = b.len();
    while end > start && (b[end - 1] == b' ' || b[end - 1] == b'\t' || b[end - 1] == b'\r') {
        end -= 1;
    }
    &b[start..end]
}

fn trim_block(b: &[u8]) -> &[u8] {
    let mut start = 0;
    while start < b.len() && matches!(b[start], 0 | b'\n' | b'\r' | b' ' | b'\t') {
        start += 1;
    }
    let mut end = b.len();
    while end > start && matches!(b[end - 1], 0 | b'\n' | b'\r' | b' ' | b'\t') {
        end -= 1;
    }
    &b[start..end]
}

/// Split `haystack` on every occurrence of `needle`, returning the between-slices.
fn split_bytes<'a>(haystack: &'a [u8], needle: &[u8]) -> Vec<&'a [u8]> {
    if needle.is_empty() || haystack.is_empty() {
        return vec![haystack];
    }
    let mut out = Vec::new();
    let mut i = 0;
    let mut start = 0;
    while i + needle.len() <= haystack.len() {
        if &haystack[i..i + needle.len()] == needle {
            out.push(&haystack[start..i]);
            i += needle.len();
            start = i;
        } else {
            i += 1;
        }
    }
    out.push(&haystack[start..]);
    out
}

/// Run `git worktree list --porcelain [-z]` and parse the output.
pub fn run_worktree_list(
    repo: &Path,
    caps: &GitCapabilities,
) -> Result<Vec<WorktreeHandle>, WorktreeError> {
    let mut cmd = Command::new("git");
    cmd.arg("worktree").arg("list").arg("--porcelain");
    cmd.current_dir(repo);

    if caps.has_list_nul {
        cmd.arg("-z");
    }

    let output = cmd.output().map_err(|_| WorktreeError::GitNotFound)?;

    if !output.status.success() {
        return Err(WorktreeError::GitCommandFailed {
            command: format!("git worktree list --porcelain{}", if caps.has_list_nul { " -z" } else { "" }),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
        });
    }

    parse_worktree_list_porcelain(&output.stdout, caps.has_list_nul)
}

/// Resolve a ref to its 40-char SHA.
pub fn resolve_ref(repo: &Path, refspec: &str) -> Result<String, WorktreeError> {
    let output = Command::new("git")
        .args(["rev-parse", refspec])
        .current_dir(repo)
        .output()
        .map_err(|_| WorktreeError::GitNotFound)?;

    if !output.status.success() {
        return Err(WorktreeError::GitCommandFailed {
            command: format!("git rev-parse {refspec}"),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
        });
    }

    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

/// Check if a branch already exists.
pub fn branch_exists(repo: &Path, branch: &str) -> Result<bool, WorktreeError> {
    let output = Command::new("git")
        .args(["rev-parse", "--verify", &format!("refs/heads/{branch}")])
        .current_dir(repo)
        .output()
        .map_err(|_| WorktreeError::GitNotFound)?;

    Ok(output.status.success())
}

/// Run `git worktree add` with the appropriate flags.
/// Returns Ok(()) on success.
pub fn worktree_add(
    repo: &Path,
    path: &Path,
    branch: &str,
    base: Option<&str>,
    new_branch: bool,
    lock: bool,
    lock_reason: Option<&str>,
) -> Result<(), WorktreeError> {
    let mut cmd = Command::new("git");
    cmd.arg("worktree").arg("add");
    cmd.current_dir(repo);

    if lock {
        cmd.arg("--lock");
        if let Some(reason) = lock_reason {
            cmd.arg("--reason").arg(reason);
        }
    }

    cmd.arg(path);

    if new_branch {
        cmd.arg("-b").arg(branch);
        if let Some(base_ref) = base {
            cmd.arg(base_ref);
        }
    } else {
        cmd.arg(branch);
    }

    let output = cmd.output().map_err(|_| WorktreeError::GitNotFound)?;

    if !output.status.success() {
        return Err(WorktreeError::GitCommandFailed {
            command: format!("git worktree add {}", path.display()),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
        });
    }

    Ok(())
}

/// Run `git worktree remove --force <path>`.
pub fn worktree_remove_force(repo: &Path, path: &Path) -> Result<(), WorktreeError> {
    let output = Command::new("git")
        .args(["worktree", "remove", "--force"])
        .arg(path)
        .current_dir(repo)
        .output()
        .map_err(|_| WorktreeError::GitNotFound)?;

    if !output.status.success() {
        return Err(WorktreeError::GitCommandFailed {
            command: format!("git worktree remove --force {}", path.display()),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
        });
    }

    Ok(())
}

/// Run `git worktree remove <path>` (non-force).
pub fn worktree_remove(repo: &Path, path: &Path) -> Result<(), WorktreeError> {
    let output = Command::new("git")
        .args(["worktree", "remove"])
        .arg(path)
        .current_dir(repo)
        .output()
        .map_err(|_| WorktreeError::GitNotFound)?;

    if !output.status.success() {
        return Err(WorktreeError::GitCommandFailed {
            command: format!("git worktree remove {}", path.display()),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
        });
    }

    Ok(())
}

/// Post-create git-crypt check on the new worktree path.
/// Returns Ok(()) if the worktree is safe, Err(GitCryptLocked) if encrypted files detected.
pub fn post_create_git_crypt_check(worktree_path: &Path) -> Result<(), WorktreeError> {
    let gitattributes = worktree_path.join(".gitattributes");
    if !gitattributes.exists() {
        return Ok(());
    }

    let content = match std::fs::read_to_string(&gitattributes) {
        Ok(c) => c,
        Err(_) => return Ok(()),
    };

    let has_git_crypt = content.lines().any(|l| l.contains("filter=git-crypt"));
    if !has_git_crypt {
        return Ok(());
    }

    const GIT_CRYPT_MAGIC: &[u8; 10] = b"\x00GITCRYPT\x00";

    for line in content.lines() {
        if !line.contains("filter=git-crypt") {
            continue;
        }
        let pattern = line.split_whitespace().next().unwrap_or("");
        if pattern.is_empty() {
            continue;
        }

        let ls_output = Command::new("git")
            .args(["ls-files", "--", pattern])
            .current_dir(worktree_path)
            .output();

        if let Ok(ls) = ls_output {
            for file_path in String::from_utf8_lossy(&ls.stdout).lines() {
                let full_path = worktree_path.join(file_path);
                if full_path.exists() {
                    if let Ok(true) = is_encrypted(&full_path, GIT_CRYPT_MAGIC) {
                        return Err(WorktreeError::GitCryptLocked);
                    }
                }
            }
        }
    }

    Ok(())
}

/// Check if a file starts with the git-crypt magic header bytes.
pub(crate) fn is_encrypted(path: &Path, magic: &[u8; 10]) -> std::io::Result<bool> {
    use std::io::Read;
    let mut file = std::fs::File::open(path)?;
    let mut header = [0u8; 10];
    match file.read_exact(&mut header) {
        Ok(_) => Ok(&header == magic),
        Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => Ok(false),
        Err(e) => Err(e),
    }
}

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

    // ── Version parsing ─────────────────────────────────────────────

    #[test]
    fn parse_standard_version() {
        let v = parse_git_version("git version 2.43.0").unwrap();
        assert_eq!(v, GitVersion { major: 2, minor: 43, patch: 0 });
    }

    #[test]
    fn parse_apple_version() {
        let v = parse_git_version("git version 2.39.3 (Apple Git-146)").unwrap();
        assert_eq!(v, GitVersion { major: 2, minor: 39, patch: 3 });
    }

    #[test]
    fn parse_windows_version() {
        let v = parse_git_version("git version 2.43.0.windows.1").unwrap();
        assert_eq!(v, GitVersion { major: 2, minor: 43, patch: 0 });
    }

    #[test]
    fn parse_with_trailing_newline() {
        let v = parse_git_version("git version 2.20.0\n").unwrap();
        assert_eq!(v, GitVersion { major: 2, minor: 20, patch: 0 });
    }

    #[test]
    fn parse_garbage_input() {
        assert!(parse_git_version("not git output").is_err());
    }

    // ── Minimum version check ───────────────────────────────────────

    #[test]
    fn version_2_19_is_too_old() {
        let v = GitVersion { major: 2, minor: 19, patch: 9 };
        assert!(v < GitVersion::MINIMUM);
    }

    #[test]
    fn version_2_20_is_ok() {
        let v = GitVersion { major: 2, minor: 20, patch: 0 };
        assert!(v >= GitVersion::MINIMUM);
    }

    // ── Capability thresholds ───────────────────────────────────────

    #[test]
    fn capabilities_at_2_20() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 20, patch: 0 });
        assert!(!caps.has_repair);
        assert!(!caps.has_list_nul);
        assert!(!caps.has_merge_tree_write);
        assert!(!caps.has_orphan);
        assert!(!caps.has_relative_paths);
    }

    #[test]
    fn capabilities_at_2_29_no_repair() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 29, patch: 9 });
        assert!(!caps.has_repair);
    }

    #[test]
    fn capabilities_at_2_30_has_repair() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 30, patch: 0 });
        assert!(caps.has_repair);
        assert!(!caps.has_list_nul);
    }

    #[test]
    fn capabilities_at_2_35_no_list_nul() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 35, patch: 9 });
        assert!(caps.has_repair);
        assert!(!caps.has_list_nul);
    }

    #[test]
    fn capabilities_at_2_36_has_list_nul() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 36, patch: 0 });
        assert!(caps.has_repair);
        assert!(caps.has_list_nul);
        assert!(!caps.has_merge_tree_write);
    }

    #[test]
    fn capabilities_at_2_38_has_merge_tree() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 38, patch: 0 });
        assert!(caps.has_merge_tree_write);
        assert!(!caps.has_orphan);
    }

    #[test]
    fn capabilities_at_2_42_has_orphan() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 42, patch: 0 });
        assert!(caps.has_orphan);
        assert!(!caps.has_relative_paths);
    }

    #[test]
    fn capabilities_at_2_48_has_relative_paths() {
        let caps = detect_capabilities(&GitVersion { major: 2, minor: 48, patch: 0 });
        assert!(caps.has_relative_paths);
        // All other caps should be true too
        assert!(caps.has_repair);
        assert!(caps.has_list_nul);
        assert!(caps.has_merge_tree_write);
        assert!(caps.has_orphan);
    }

    // ── Integration: actual git on this machine ─────────────────────

    #[test]
    fn detect_real_git_version() {
        let caps = detect_git_version().expect("git should be installed on CI");
        assert!(caps.version >= GitVersion::MINIMUM);
    }

    // ── Worktree list parser tests ──────────────────────────────────

    #[test]
    fn parse_empty_output() {
        let result = parse_worktree_list_porcelain(b"", false).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn parse_single_worktree_newline_mode() {
        let output = b"worktree /home/user/project\nHEAD abc1234abc1234abc1234abc1234abc1234abc1234\nbranch refs/heads/main\n\n";
        let result = parse_worktree_list_porcelain(output, false).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].path, std::path::PathBuf::from("/home/user/project"));
        assert_eq!(result[0].branch, "main");
        assert_eq!(result[0].base_commit, "abc1234abc1234abc1234abc1234abc1234abc1234");
        assert_eq!(result[0].state, WorktreeState::Active);
    }

    #[test]
    fn parse_multi_block_newline_mode() {
        let output = b"worktree /home/user/project\nHEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbranch refs/heads/main\n\nworktree /home/user/project-feature\nHEAD bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\nbranch refs/heads/feature/test\n\nworktree /home/user/project-detached\nHEAD cccccccccccccccccccccccccccccccccccccccc\ndetached\n\n";
        let result = parse_worktree_list_porcelain(output, false).unwrap();
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].branch, "main");
        assert_eq!(result[1].branch, "feature/test");
        assert_eq!(result[2].branch, ""); // detached
        assert_eq!(result[2].state, WorktreeState::Active);
    }

    #[test]
    fn parse_locked_worktree_no_reason() {
        let output = b"worktree /tmp/wt\nHEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbranch refs/heads/test\nlocked\n\n";
        let result = parse_worktree_list_porcelain(output, false).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].state, WorktreeState::Locked);
    }

    #[test]
    fn parse_locked_worktree_with_reason() {
        let output = b"worktree /tmp/wt\nHEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbranch refs/heads/test\nlocked important work in progress\n\n";
        let result = parse_worktree_list_porcelain(output, false).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].state, WorktreeState::Locked);
    }

    #[test]
    fn parse_prunable_worktree() {
        let output = b"worktree /tmp/wt\nHEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbranch refs/heads/test\nprunable gitdir file points to non-existent location\n\n";
        let result = parse_worktree_list_porcelain(output, false).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].state, WorktreeState::Orphaned);
    }

    #[test]
    fn parse_bare_worktree() {
        let output = b"worktree /tmp/bare.git\nbare\n\n";
        let result = parse_worktree_list_porcelain(output, false).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].branch, "");
    }

    #[test]
    fn parse_nul_delimited_mode() {
        // Real git -z format: fields separated by NUL within a block,
        // blocks separated by double NUL.
        let output = b"worktree /home/user/project\0HEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\0branch refs/heads/main\0\0worktree /home/user/project-feature\0HEAD bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\0branch refs/heads/feature\0\0";
        let result = parse_worktree_list_porcelain(output, true).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].branch, "main");
        assert_eq!(result[1].branch, "feature");
    }

    #[test]
    fn parse_nul_delimited_path_with_spaces() {
        let output = b"worktree /home/user/my project\0HEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\0branch refs/heads/main\0\0";
        let result = parse_worktree_list_porcelain(output, true).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].path, std::path::PathBuf::from("/home/user/my project"));
    }

    #[cfg(unix)]
    #[test]
    fn parse_nul_delimited_preserves_non_utf8_path_bytes() {
        // A path byte that's valid on Unix but not valid UTF-8 (0xff).
        let mut output: Vec<u8> = Vec::new();
        output.extend_from_slice(b"worktree /tmp/wt-");
        output.push(0xff);
        output.extend_from_slice(b"-end\0HEAD aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\0branch refs/heads/x\0\0");

        let result = parse_worktree_list_porcelain(&output, true).unwrap();
        assert_eq!(result.len(), 1);

        use std::os::unix::ffi::OsStrExt;
        let bytes = result[0].path.as_os_str().as_bytes();
        assert!(bytes.contains(&0xff), "non-UTF8 byte should survive");
    }

    #[test]
    fn parse_integration_real_repo() {
        // Run against the actual ISO repo
        let caps = detect_git_version().expect("git should be installed");
        let result = run_worktree_list(std::path::Path::new("."), &caps);
        // Should succeed — we're in a git repo
        assert!(result.is_ok());
        let handles = result.unwrap();
        // At minimum, the main worktree should be present
        assert!(!handles.is_empty());
    }
}