kono-wt 1.3.0

A single-binary CLI + TUI for managing Git worktrees and their GitHub pull requests.
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
//! `wt new <branch>` — create a linked worktree (spec §6/§7/§8/§13).

use std::path::{Path, PathBuf};

use crate::cli::NewArgs;
use crate::commands::{
    emit_worktree, maybe_init_submodules_interactive, open_session, render_target, resolve_target,
    rollback_worktree, same_path,
};
use crate::config::wtconfig;
use crate::copy::copy_ignored_files;
use crate::cx::Cx;
use crate::error::{Error, Result};
use crate::git::cli::GitCli;
use crate::git::discover::Repo;
use crate::git::{branch_ref, default_branch, ops, resolve_hex};
use crate::hooks::{HookContext, HookRunner, run_post_create};
use crate::model::Worktree;
use crate::query::{self, Resolved};
use crate::slug::slugify_with_fallback;
use crate::worktree_service::enumerate_worktrees;

/// Creates a linked worktree for `branch`, prompting first when the base it would
/// fork from is behind its origin counterpart (issue #56): the user can update the
/// base, proceed off the stale base, or cancel. The check is skipped offline, for
/// an existing branch, or when the base has no upstream. Delegates to [`run_core`]
/// for the actual creation.
pub(crate) fn run(cx: &mut Cx, hooks: &dyn HookRunner, args: &NewArgs, json: bool) -> Result<u8> {
    let git = cx.git.clone();
    let git = git.as_ref();
    // Pre-flight staleness check in its own scope so the session is dropped before
    // `run_core` opens its own.
    {
        let session = open_session(cx, git)?;
        if let Some(base) = prospective_base(cx, &session.repo, args, &session.config) {
            let dir = session
                .repo
                .current_workdir()
                .unwrap_or_else(|| session.primary_root.clone());
            if let Some(stale) =
                crate::commands::staleness::check_base_behind(cx, git, &session.repo, &dir, &base)?
            {
                let prompt = format!(
                    "base {base:?} is {} commit(s) behind {}; [u]pdate / [p]roceed / [c]ancel (default cancel): ",
                    stale.behind, stale.upstream_display
                );
                match crate::commands::choose(cx, &prompt)? {
                    crate::commands::Choice::Update => {
                        crate::commands::staleness::fast_forward_base(
                            cx,
                            git,
                            &session.repo,
                            &session.primary_root,
                            &base,
                            &stale,
                        )?
                    }
                    crate::commands::Choice::Proceed => {}
                    crate::commands::Choice::Cancel => {
                        cx.err.line("aborted: base branch is behind origin")?;
                        return Ok(1);
                    }
                }
            }
        }
    }
    // The CLI may prompt before initializing submodules (issue #50); the TUI
    // passes `false` to `run_core` and drives its own modal instead.
    run_core(cx, hooks, args, json, true)
}

/// Creates a linked worktree for `branch` (creating the branch if needed), runs
/// the copy step and post-create hook, and prints the new path (unless
/// `--no-switch`/`--json`). The base-staleness check (issue #56) is the caller's
/// responsibility — [`run`] does it for the CLI, the TUI before this is reached.
/// `prompt` enables the interactive submodule confirmation (issue #50): the CLI
/// passes `true`; the TUI passes `false` and runs its own modal afterwards.
pub(crate) fn run_core(
    cx: &mut Cx,
    hooks: &dyn HookRunner,
    args: &NewArgs,
    json: bool,
    prompt: bool,
) -> Result<u8> {
    let git = cx.git.clone();
    let git = git.as_ref();
    let session = open_session(cx, git)?;
    let repo = &session.repo;
    let root = session.primary_root.clone();
    let branch = args.branch.clone();

    let worktrees = enumerate_worktrees(repo, git)?;
    let branch_exists = resolve_hex(repo.gix(), &branch_ref(&branch)).is_some();

    let base_ref = if branch_exists {
        None
    } else {
        Some(resolve_base_ref(
            cx,
            repo,
            args.from.as_deref(),
            &session.config.default_base,
        ))
    };
    let base_commit = match &base_ref {
        Some(base) => resolve_hex(repo.gix(), base)
            .ok_or_else(|| Error::operation(format!("base ref {base:?} not found")))?,
        None => resolve_hex(repo.gix(), &branch_ref(&branch)).unwrap_or_default(),
    };
    let short_hash = base_commit.get(..7).unwrap_or(&base_commit).to_string();
    let slug = slugify_with_fallback(&branch, &short_hash);

    // If the branch is already checked out, either no-op (same target) or refuse.
    if let Some(existing) = worktrees
        .iter()
        .find(|w| w.branch.as_deref() == Some(branch.as_str()))
    {
        let preview = render_target(&session.config, &root, &branch, &slug, &cx.env)?;
        if same_path(&existing.path, &preview) {
            let path = existing.path.clone();
            return emit_worktree(
                cx,
                &path,
                json,
                args.no_switch,
                "worktree already exists at",
            );
        }
        return Err(Error::operation(format!(
            "branch {branch:?} is already checked out at {}",
            existing.path.display()
        )));
    }

    let target = resolve_target(
        &session.config,
        &root,
        &branch,
        &slug,
        &short_hash,
        &cx.env,
        repo.is_bare(),
    )?;
    if let Some(parent) = target.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Create the worktree (git is atomic here).
    let target_str = target.to_string_lossy().into_owned();
    if let Some(base) = &base_ref {
        // `--no-track` keeps the new branch from inheriting the base as its
        // upstream (issue #43): git's `branch.autoSetupMerge` would otherwise make
        // a remote-tracking base the upstream. `--track` opts into an explicit one.
        ops::worktree_add_branch(git, &root, &branch, &target_str, base, true)?;
    } else {
        ops::worktree_add(git, &root, &target_str, &branch)?;
    }

    // Steps after creation but before the hook are rolled back on failure (§13).
    let copy_outcome = match post_create_steps(
        git,
        repo,
        &worktrees,
        &session.config,
        &root,
        &branch,
        &base_ref,
        &target,
        args.track.as_deref(),
        args.copy_from.as_deref(),
    ) {
        Ok(outcome) => outcome,
        Err(e) => {
            // Metadata is written only for a wt-created branch, so delete the
            // branch and clear metadata together on that condition.
            let created = base_ref.is_some();
            rollback_worktree(git, &root, &target, &branch, created, created);
            return Err(e);
        }
    };
    crate::commands::log_copy_outcome(cx, &copy_outcome);

    // The post-create hook: a failure is a warning, not a rollback (§8).
    let ctx = HookContext {
        worktree_path: target.clone(),
        branch: branch.clone(),
        repo_root: root.clone(),
        base_ref: base_ref.clone(),
        pr_number: None,
    };
    run_post_create(
        hooks,
        cx,
        session.config.hooks_post_create.as_deref(),
        &ctx,
        args.no_hooks,
    )?;

    // Initialize submodules per the policy/flag, prompting (default yes) at an
    // interactive terminal when the policy is left at its default (issue #50).
    // Non-fatal — the worktree already exists.
    maybe_init_submodules_interactive(
        cx,
        git,
        &target,
        session.config.submodules_init,
        args.submodule_override(),
        prompt,
    )?;

    emit_worktree(cx, &target, json, args.no_switch, "created worktree at")
}

/// Records metadata and runs the copy step (rolled back on error), returning the
/// copy outcome for `-v` logging.
#[allow(clippy::too_many_arguments)]
fn post_create_steps(
    git: &dyn GitCli,
    repo: &Repo,
    worktrees: &[Worktree],
    config: &crate::config::Config,
    root: &Path,
    branch: &str,
    base_ref: &Option<String>,
    target: &Path,
    track: Option<&str>,
    copy_from: Option<&str>,
) -> Result<crate::copy::CopyOutcome> {
    if let Some(base) = base_ref {
        // A wt-created branch records its base and "created by wt" (§3/§10).
        wtconfig::write_base_ref(git, root, branch, base)?;
        wtconfig::mark_created_by_wt(git, root, branch)?;
    }
    // `--track <REF>` sets an explicit upstream (issue #43); a bad ref fails here,
    // inside the rolled-back region, so the half-created worktree is torn down.
    if let Some(upstream) = track {
        ops::set_upstream(git, root, branch, upstream)?;
    }
    let source = copy_source(repo, worktrees, copy_from, root)?;
    copy_ignored_files(git, &source, target, &config.copy)
}

/// The base ref a `new` invocation would fork from, for the pre-flight staleness
/// check (issue #56), or `None` when the branch already exists — then there is no
/// fork and no base to check.
pub(crate) fn prospective_base(
    cx: &mut Cx,
    repo: &Repo,
    args: &NewArgs,
    config: &crate::config::Config,
) -> Option<String> {
    if resolve_hex(repo.gix(), &branch_ref(&args.branch)).is_some() {
        return None;
    }
    Some(resolve_base_ref(
        cx,
        repo,
        args.from.as_deref(),
        &config.default_base,
    ))
}

/// Detects whether the base `args` would fork from is behind its upstream (issue
/// #56), for the TUI create pre-flight. `Ok(None)` when there is nothing to warn
/// about (existing branch, no upstream, up to date, or offline).
pub(crate) fn detect_stale_base(
    cx: &mut Cx,
    args: &NewArgs,
) -> Result<Option<crate::commands::staleness::StaleBase>> {
    let git = cx.git.clone();
    let git = git.as_ref();
    let session = open_session(cx, git)?;
    let Some(base) = prospective_base(cx, &session.repo, args, &session.config) else {
        return Ok(None);
    };
    let dir = session
        .repo
        .current_workdir()
        .unwrap_or_else(|| session.primary_root.clone());
    crate::commands::staleness::check_base_behind(cx, git, &session.repo, &dir, &base)
}

/// Fast-forwards the base `args` would fork from to its upstream (issue #56, the
/// TUI "update" action). A no-op when there is no stale base.
pub(crate) fn update_stale_base(cx: &mut Cx, args: &NewArgs) -> Result<()> {
    let git = cx.git.clone();
    let git = git.as_ref();
    let session = open_session(cx, git)?;
    let Some(base) = prospective_base(cx, &session.repo, args, &session.config) else {
        return Ok(());
    };
    let dir = session
        .repo
        .current_workdir()
        .unwrap_or_else(|| session.primary_root.clone());
    if let Some(stale) =
        crate::commands::staleness::check_base_behind(cx, git, &session.repo, &dir, &base)?
    {
        crate::commands::staleness::fast_forward_base(
            cx,
            git,
            &session.repo,
            &session.primary_root,
            &base,
            &stale,
        )?;
    }
    Ok(())
}

/// Resolves the base ref for a new branch: `--from`, then `default_base`, then
/// the repo default branch, then `HEAD` (warning when falling back).
fn resolve_base_ref(
    cx: &mut Cx,
    repo: &Repo,
    from: Option<&str>,
    default_base: &Option<String>,
) -> String {
    if let Some(from) = from {
        return from.to_string();
    }
    if let Some(base) = default_base {
        return base.clone();
    }
    if let Some(branch) = default_branch(repo.gix()) {
        return branch;
    }
    let _ = cx
        .err
        .line("warning: no default branch; basing the new branch on HEAD");
    "HEAD".to_string()
}

/// Resolves the copy source worktree: `--copy-from`, else the current worktree,
/// else the primary root (spec §8).
fn copy_source(
    repo: &Repo,
    worktrees: &[Worktree],
    copy_from: Option<&str>,
    root: &Path,
) -> Result<PathBuf> {
    if let Some(query) = copy_from {
        return match query::resolve(worktrees, query) {
            Resolved::One(index) => Ok(worktrees[index].path.clone()),
            Resolved::Ambiguous(_) => Err(Error::operation(format!(
                "--copy-from {query:?} is ambiguous"
            ))),
            Resolved::NotFound => Err(Error::NotFound {
                query: query.to_string(),
            }),
        };
    }
    Ok(repo.current_workdir().unwrap_or_else(|| root.to_path_buf()))
}

#[cfg(test)]
mod tests {
    use crate::cli::NewArgs;
    use crate::hooks::RealHookRunner;
    use crate::testutil::TestRepo;
    use std::path::Path;

    fn args(branch: &str) -> NewArgs {
        NewArgs {
            branch: branch.to_string(),
            from: None,
            track: None,
            no_track: false,
            no_switch: false,
            no_hooks: true,
            copy_from: None,
            init_submodules: false,
            no_init_submodules: false,
        }
    }

    fn run(repo: &TestRepo, a: &NewArgs, json: bool) -> (u8, String, String) {
        let mut t = crate::testutil::test_cx(&[], repo.root().to_str().unwrap());
        let code = super::run(&mut t.cx, &RealHookRunner, a, json).unwrap();
        (code, t.out.contents(), t.err.contents())
    }

    #[test]
    fn creates_new_branch_and_worktree() {
        let repo = TestRepo::init();
        let (code, out, _) = run(&repo, &args("feature/login"), false);
        assert_eq!(code, 0);
        let path = out.trim();
        assert!(Path::new(path).is_dir());
        assert!(path.ends_with("feature-login"));
        assert!(
            !repo
                .git(&["rev-parse", "--verify", "refs/heads/feature/login"])
                .is_empty()
        );
        assert_eq!(
            repo.git(&["config", "--get", "wt.feature/login.createdByWt"])
                .trim(),
            "true"
        );
        assert_eq!(
            repo.git(&["config", "--get", "wt.feature/login.baseRef"])
                .trim(),
            "main"
        );
    }

    #[test]
    fn checks_out_existing_branch_without_marking_created() {
        let repo = TestRepo::init();
        repo.git(&["branch", "existing"]);
        let (code, out, _) = run(&repo, &args("existing"), false);
        assert_eq!(code, 0);
        assert!(Path::new(out.trim()).is_dir());
        let all = repo.git(&["config", "--list"]);
        assert!(!all.contains("wt.existing"), "unexpected metadata: {all}");
    }

    #[test]
    fn idempotent_when_branch_already_at_target() {
        let repo = TestRepo::init();
        run(&repo, &args("feature/x"), false);
        let (code, out, _) = run(&repo, &args("feature/x"), false);
        assert_eq!(code, 0);
        assert!(out.trim().ends_with("feature-x"));
    }

    #[test]
    fn refuses_branch_checked_out_elsewhere() {
        let repo = TestRepo::init();
        repo.add_worktree("dup", "../manual-dup");
        let mut t = crate::testutil::test_cx(&[], repo.root().to_str().unwrap());
        let err = super::run(&mut t.cx, &RealHookRunner, &args("dup"), false).unwrap_err();
        assert!(err.to_string().contains("already checked out"));
    }

    #[test]
    fn no_switch_prints_to_stderr_not_stdout() {
        let repo = TestRepo::init();
        let mut a = args("topic");
        a.no_switch = true;
        let (code, out, err) = run(&repo, &a, false);
        assert_eq!(code, 0);
        assert!(out.is_empty());
        assert!(err.contains("created worktree at"));
    }

    #[test]
    fn json_emits_result_object() {
        let repo = TestRepo::init();
        let (code, out, _) = run(&repo, &args("feature/j"), true);
        assert_eq!(code, 0);
        let v: serde_json::Value = serde_json::from_str(out.trim()).unwrap();
        assert_eq!(v["branch"], serde_json::json!("feature/j"));
        assert_eq!(v["base_ref"], serde_json::json!("main"));
        assert_eq!(v["schema_version"], serde_json::json!(1));
    }

    #[test]
    fn from_base_ref_is_used() {
        let repo = TestRepo::init();
        repo.write("f.txt", "x\n");
        repo.commit_all("second");
        repo.git(&["branch", "base-branch"]);
        let mut a = args("derived");
        a.from = Some("base-branch".to_string());
        let (code, _, _) = run(&repo, &a, false);
        assert_eq!(code, 0);
        assert_eq!(
            repo.git(&["config", "--get", "wt.derived.baseRef"]).trim(),
            "base-branch"
        );
    }

    /// A repo with a real `origin` remote (itself) and a fetched
    /// `refs/remotes/origin/main`, so `origin/main` is a genuine
    /// remote-tracking branch for upstream/autoSetupMerge purposes.
    fn repo_with_origin() -> TestRepo {
        let repo = TestRepo::init();
        repo.git(&["remote", "add", "origin", repo.root().to_str().unwrap()]);
        repo.git(&["fetch", "-q", "origin"]);
        repo
    }

    #[test]
    fn new_branch_does_not_inherit_base_upstream() {
        // Forking from a remote-tracking base must not make that base the new
        // branch's upstream (issue #43); git's autoSetupMerge would otherwise.
        let repo = repo_with_origin();
        let mut a = args("feat");
        a.from = Some("origin/main".to_string());
        let (code, _, _) = run(&repo, &a, false);
        assert_eq!(code, 0);
        // No upstream is configured for the new branch (`--get` exits non-zero on
        // a missing key, so check the full listing instead).
        let all = repo.git(&["config", "--list"]);
        assert!(
            !all.contains("branch.feat.remote"),
            "new branch should not track the base: {all}"
        );
    }

    #[test]
    fn track_sets_explicit_upstream() {
        // `--track <REF>` records an explicit upstream for the new branch.
        let repo = repo_with_origin();
        let mut a = args("feat");
        a.track = Some("origin/main".to_string());
        let (code, _, _) = run(&repo, &a, false);
        assert_eq!(code, 0);
        assert_eq!(
            repo.git(&["config", "--get", "branch.feat.remote"]).trim(),
            "origin"
        );
        assert_eq!(
            repo.git(&["config", "--get", "branch.feat.merge"]).trim(),
            "refs/heads/main"
        );
    }

    #[test]
    fn rolls_back_worktree_when_a_post_add_step_fails() {
        use crate::git::cli::{GitCli, GitOutput, RealGit};
        use std::path::Path as StdPath;
        use std::sync::Arc;

        struct FailConfig(RealGit);
        impl GitCli for FailConfig {
            fn run_raw(&self, repo: &StdPath, args: &[&str]) -> crate::error::Result<GitOutput> {
                if args.first() == Some(&"config") && args.iter().any(|a| a.starts_with("wt.")) {
                    return Ok(GitOutput {
                        success: false,
                        stdout: String::new(),
                        stderr: "simulated failure".into(),
                    });
                }
                self.0.run_raw(repo, args)
            }
        }

        let repo = TestRepo::init();
        let mut t = crate::testutil::test_cx_with_git(
            &[],
            repo.root().to_str().unwrap(),
            Arc::new(FailConfig(RealGit)),
        );
        let err = super::run(&mut t.cx, &RealHookRunner, &args("rollme"), false).unwrap_err();
        assert!(err.to_string().contains("simulated failure"));

        let repo_name = repo
            .root()
            .file_name()
            .unwrap()
            .to_string_lossy()
            .into_owned();
        let target = repo
            .root()
            .parent()
            .unwrap()
            .join(format!("{repo_name}.worktrees"));
        let leaf = format!("{repo_name}-rollme");
        assert!(!target.join(leaf).exists(), "worktree not rolled back");
        assert!(repo.git(&["branch", "--list", "rollme"]).trim().is_empty());
    }

    #[test]
    fn copies_ignored_files_into_new_worktree() {
        let repo = TestRepo::init();
        std::fs::write(repo.root().join(".wt.toml"), "copy = [\".env\"]\n").unwrap();
        repo.write(".env", "SECRET=1\n");
        let (code, out, err) = run(&repo, &args("withenv"), false);
        assert_eq!(code, 0);
        let env_path = Path::new(out.trim()).join(".env");
        assert!(env_path.exists());
        assert_eq!(std::fs::read_to_string(env_path).unwrap(), "SECRET=1\n");
        // Silent at the default verbosity (spec §8).
        assert!(!err.contains("copied"));
    }

    /// A repo with a committed submodule on `main`, so a new worktree inherits
    /// the `.gitmodules` definition (uninitialized until populated).
    fn repo_with_submodule() -> TestRepo {
        let repo = TestRepo::init();
        repo.add_submodule("libs/sub");
        repo
    }

    #[test]
    fn new_default_does_not_init_submodules() {
        let repo = repo_with_submodule();
        let (code, out, err) = run(&repo, &args("feat"), false);
        assert_eq!(code, 0);
        // No policy/flag: submodules are left alone and nothing is logged.
        assert!(!err.contains("initializing"));
        assert!(!Path::new(out.trim()).join("libs/sub/sub.txt").exists());
    }

    #[test]
    fn new_init_submodules_flag_runs_init() {
        let repo = repo_with_submodule();
        let mut a = args("feat");
        a.init_submodules = true;
        let (code, _out, err) = run(&repo, &a, false);
        // `--init-submodules` runs the init (non-fatal even if a file-protocol
        // clone is later refused), proving `new` wires the policy through.
        assert_eq!(code, 0);
        assert!(err.contains("initializing 1 submodule"));
    }

    #[test]
    fn new_no_init_submodules_flag_overrides_always_config() {
        let repo = repo_with_submodule();
        std::fs::write(
            repo.root().join(".wt.toml"),
            "[submodules]\ninit = \"always\"\n",
        )
        .unwrap();
        let mut a = args("feat");
        a.no_init_submodules = true;
        let (code, out, err) = run(&repo, &a, false);
        assert_eq!(code, 0);
        // The flag overrides `init = "always"`: no init runs.
        assert!(!err.contains("initializing"));
        assert!(!Path::new(out.trim()).join("libs/sub/sub.txt").exists());
    }

    #[test]
    fn verbose_logs_copied_files() {
        let repo = TestRepo::init();
        std::fs::write(repo.root().join(".wt.toml"), "copy = [\".env\"]\n").unwrap();
        repo.write(".env", "SECRET=1\n");
        let mut t = crate::testutil::test_cx(&[], repo.root().to_str().unwrap());
        t.cx.verbose = 1;
        super::run(&mut t.cx, &RealHookRunner, &args("withenv2"), false).unwrap();
        let err = t.err.contents();
        assert!(err.contains("copied"), "expected copy log at -v: {err}");
        assert!(err.contains(".env"));
    }

    /// Runs `new` with seeded prompt answers, returning `(code, out, err)`.
    fn run_with_input(repo: &TestRepo, a: &NewArgs, inputs: &[&str]) -> (u8, String, String) {
        let mut t = crate::testutil::test_cx(&[], repo.root().to_str().unwrap());
        t.cx.input = Box::new(crate::testutil::CannedInput::new(inputs));
        let code = super::run(&mut t.cx, &RealHookRunner, a, false).unwrap();
        (code, t.out.contents(), t.err.contents())
    }

    /// Runs `new` with a TTY stderr and seeded prompt answers, returning
    /// `(code, stdout, stderr)`. The TTY makes the submodule prompt fire (issue #50).
    fn run_with_tty_input(repo: &TestRepo, a: &NewArgs, inputs: &[&str]) -> (u8, String, String) {
        use crate::cx::Stream;
        use crate::testutil::{CannedInput, SharedBuf};
        let mut t = crate::testutil::test_cx(&[], repo.root().to_str().unwrap());
        let err = SharedBuf::new();
        t.cx.err = Stream::new(Box::new(err.clone()), true);
        t.cx.input = Box::new(CannedInput::new(inputs));
        let code = super::run(&mut t.cx, &RealHookRunner, a, false).unwrap();
        (code, t.out.contents(), err.contents())
    }

    #[test]
    fn new_prompts_and_initializes_submodules_on_yes() {
        // The default policy at a TTY asks; `y` runs the recursive init (issue #50).
        let repo = repo_with_submodule();
        let (code, _out, err) = run_with_tty_input(&repo, &args("feat"), &["y"]);
        assert_eq!(code, 0);
        assert!(err.contains("uninitialized submodule"));
        assert!(err.contains("initializing 1 submodule"));
    }

    #[test]
    fn new_prompt_defaults_to_yes_on_empty_answer() {
        let repo = repo_with_submodule();
        let (code, _out, err) = run_with_tty_input(&repo, &args("feat"), &[""]);
        assert_eq!(code, 0);
        assert!(err.contains("initializing 1 submodule"));
    }

    #[test]
    fn new_prompt_no_leaves_submodules_uninitialized() {
        let repo = repo_with_submodule();
        let (code, out, err) = run_with_tty_input(&repo, &args("feat"), &["n"]);
        assert_eq!(code, 0);
        assert!(err.contains("uninitialized submodule"));
        assert!(!err.contains("initializing"));
        assert!(!Path::new(out.trim()).join("libs/sub/sub.txt").exists());
    }

    /// Leaves local `main` one commit behind `origin/main` (with the upstream
    /// configured but no fetchable remote, so the check's fetch is skipped).
    /// Returns the `origin/main` commit.
    fn make_main_behind(repo: &TestRepo) -> String {
        let c1 = repo.git(&["rev-parse", "HEAD"]).trim().to_string();
        repo.write("upstream.txt", "1\n");
        repo.commit_all("ahead on origin");
        let c2 = repo.git(&["rev-parse", "HEAD"]).trim().to_string();
        repo.git(&["update-ref", "refs/remotes/origin/main", &c2]);
        repo.git(&["reset", "-q", "--hard", &c1]);
        repo.git(&["config", "branch.main.remote", "origin"]);
        repo.git(&["config", "branch.main.merge", "refs/heads/main"]);
        c2
    }

    #[test]
    fn stale_base_cancel_aborts_create() {
        let repo = TestRepo::init();
        make_main_behind(&repo);
        // An empty answer defaults to cancel (issue #56).
        let (code, out, err) = run_with_input(&repo, &args("feature"), &[""]);
        assert_eq!(code, 1);
        assert!(out.is_empty());
        assert!(err.contains("aborted"));
        assert!(repo.git(&["branch", "--list", "feature"]).trim().is_empty());
    }

    #[test]
    fn stale_base_proceed_creates_off_stale_base() {
        let repo = TestRepo::init();
        let c2 = make_main_behind(&repo);
        let c1 = repo
            .git(&["rev-parse", "refs/heads/main"])
            .trim()
            .to_string();
        let (code, _, _) = run_with_input(&repo, &args("feature"), &["proceed"]);
        assert_eq!(code, 0);
        // Forked off the stale local main, not origin/main.
        assert_eq!(repo.git(&["rev-parse", "refs/heads/feature"]).trim(), c1);
        assert_ne!(c1, c2);
    }

    #[test]
    fn stale_base_update_fast_forwards_then_creates() {
        let repo = TestRepo::init();
        let c2 = make_main_behind(&repo);
        let (code, _, err) = run_with_input(&repo, &args("feature"), &["update"]);
        assert_eq!(code, 0);
        assert!(err.contains("updated main"));
        // main was fast-forwarded to origin/main, and feature forks from it.
        assert_eq!(repo.git(&["rev-parse", "refs/heads/main"]).trim(), c2);
        assert_eq!(repo.git(&["rev-parse", "refs/heads/feature"]).trim(), c2);
    }
}