git-stk 0.10.2

Git-native stacked branch workflow helper
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
mod common;

use common::TestRepo;
use predicates::prelude::PredicateBooleanExt;

#[test]
fn new_records_parent_and_supports_navigation() {
    let repo = TestRepo::new();

    repo.stack()
        .args(["new", "feature/a"])
        .assert()
        .success()
        .stdout("created feature/a with parent main\n");

    assert_eq!(repo.git(["branch", "--show-current"]), "feature/a");
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/a.stkParent"]),
        "main"
    );

    repo.stack()
        .arg("parent")
        .assert()
        .success()
        .stdout("main\n");

    repo.stack()
        .args(["children", "main"])
        .assert()
        .success()
        .stdout("feature/a\n");

    repo.stack()
        .arg("down")
        .assert()
        .success()
        .stdout("switched to main\n");
    assert_eq!(repo.git(["branch", "--show-current"]), "main");

    repo.stack()
        .arg("up")
        .assert()
        .success()
        .stdout("switched to feature/a\n");
    assert_eq!(repo.git(["branch", "--show-current"]), "feature/a");
}

#[test]
fn adopt_list_and_detach_manage_existing_branches() {
    let repo = TestRepo::new();

    repo.git(["switch", "-c", "feature/a"]);
    repo.git(["switch", "main"]);
    repo.git(["switch", "-c", "feature/b"]);
    repo.git(["switch", "main"]);

    repo.stack()
        .args(["adopt", "feature/a", "--parent", "main"])
        .assert()
        .success()
        .stdout("attached feature/a to main\n");

    repo.stack()
        .args(["adopt", "feature/b", "--parent", "feature/a"])
        .assert()
        .success()
        .stdout("attached feature/b to feature/a\n");

    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout("    \u{25cb} feature/b\n  \u{25cb} feature/a\n\u{25c9} main (trunk)\n");

    repo.stack()
        .args(["detach", "feature/b"])
        .assert()
        .success()
        .stdout("detached feature/b\n");

    repo.stack()
        .args(["children", "feature/a"])
        .assert()
        .success()
        .stdout("");
}

#[test]
fn adopt_refuses_to_form_a_cycle() {
    let repo = TestRepo::new();

    repo.git(["switch", "-c", "feature/a"]);
    repo.git(["switch", "main"]);
    repo.git(["switch", "-c", "feature/b"]);
    repo.git(["switch", "main"]);

    repo.stack()
        .args(["adopt", "feature/a", "--parent", "main"])
        .assert()
        .success();
    repo.stack()
        .args(["adopt", "feature/b", "--parent", "feature/a"])
        .assert()
        .success();

    // feature/b sits above feature/a, so making it feature/a's parent would
    // close a loop. Refuse rather than write cyclic metadata.
    repo.stack()
        .args(["adopt", "feature/a", "--parent", "feature/b"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("cycle"));

    // The original parent is untouched.
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/a.stkParent"]),
        "main"
    );
}

/// Cyclic metadata can still exist (e.g. written by an older version, or by
/// raw `git config`). Commands that walk descendants must terminate cleanly
/// rather than recurse forever and overflow the stack.
#[test]
fn cyclic_metadata_does_not_crash_descendant_walk() {
    let repo = TestRepo::new();

    repo.git(["switch", "-c", "feature/a"]);
    repo.git(["switch", "main"]);
    repo.git(["switch", "-c", "feature/b"]);

    // Forge a 2-cycle directly, bypassing the adopt guard: a <-> b.
    repo.git(["config", "branch.feature/a.stkParent", "feature/b"]);
    repo.git(["config", "branch.feature/b.stkParent", "feature/a"]);

    repo.git(["switch", "feature/a"]);

    // `list --format markdown` collects via branch_and_descendants; before the
    // visited-set guard this overflowed the stack and core-dumped.
    repo.stack()
        .args(["list", "--format", "markdown"])
        .assert()
        .success();
}

#[test]
fn adopt_with_no_args_uses_current_branch_and_trunk() {
    let repo = TestRepo::new();
    // A branch made with raw git, switched to but not yet in a stack.
    repo.git(["switch", "-c", "feature/x"]);

    repo.stack()
        .arg("adopt")
        .assert()
        .success()
        .stdout("attached feature/x to main\n");

    assert_eq!(
        repo.git(["config", "--get", "branch.feature/x.stkParent"]),
        "main"
    );
}

#[test]
fn new_on_an_existing_branch_points_at_adopt() {
    let repo = TestRepo::new();
    repo.git(["switch", "-c", "feature/x"]);
    repo.git(["switch", "main"]);

    repo.stack()
        .args(["new", "feature/x"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("already exists"))
        .stderr(predicates::str::contains(
            "git stk adopt feature/x --parent main",
        ));
}

#[test]
fn up_requires_branch_when_multiple_children_exist() {
    let repo = TestRepo::new();

    repo.git(["switch", "-c", "feature/a"]);
    repo.git(["switch", "main"]);
    repo.git(["switch", "-c", "feature/b"]);
    repo.git(["switch", "main"]);

    repo.stack()
        .args(["adopt", "feature/a", "--parent", "main"])
        .assert()
        .success();
    repo.stack()
        .args(["adopt", "feature/b", "--parent", "main"])
        .assert()
        .success();

    repo.stack()
        .arg("up")
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "choose one with `git stk up <branch>`",
        ));

    repo.stack().args(["up", "feature/b"]).assert().success();
    assert_eq!(repo.git(["branch", "--show-current"]), "feature/b");
}

#[test]
fn up_picker_chooses_among_multiple_children() {
    let repo = TestRepo::new();

    repo.git(["switch", "-c", "feature/a"]);
    repo.git(["switch", "main"]);
    repo.git(["switch", "-c", "feature/b"]);
    repo.git(["switch", "main"]);
    repo.stack()
        .args(["adopt", "feature/a", "--parent", "main"])
        .assert()
        .success();
    repo.stack()
        .args(["adopt", "feature/b", "--parent", "main"])
        .assert()
        .success();

    // Children list alphabetically: 2 picks feature/b.
    repo.stack()
        .arg("up")
        .write_stdin("2\n")
        .assert()
        .success()
        .stderr(predicates::str::contains("1."))
        .stderr(predicates::str::contains("pick [1-2]:"));
    assert_eq!(repo.git(["branch", "--show-current"]), "feature/b");

    // An answer off the menu falls back to the error.
    repo.git(["switch", "main"]);
    repo.stack()
        .arg("up")
        .write_stdin("9\n")
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "choose one with `git stk up <branch>`",
        ));
    assert_eq!(repo.git(["branch", "--show-current"]), "main");
}

#[test]
fn top_picker_resolves_the_fork_and_keeps_climbing() {
    let repo = TestRepo::new();

    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "feature/a"]);
    repo.stack().args(["new", "feature/c"]).assert().success();
    repo.stack().args(["new", "feature/d"]).assert().success();
    repo.git(["switch", "main"]);

    // Pick the feature/c side of the fork; the climb continues to its leaf.
    repo.stack()
        .arg("top")
        .write_stdin("2\n")
        .assert()
        .success();
    assert_eq!(repo.git(["branch", "--show-current"]), "feature/d");
}

#[test]
fn top_and_bottom_jump_across_the_stack() {
    let repo = TestRepo::new();

    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.stack().args(["new", "feature/c"]).assert().success();

    repo.stack().arg("bottom").assert().success();
    assert_eq!(repo.git(["branch", "--show-current"]), "feature/a");

    repo.stack().arg("top").assert().success();
    assert_eq!(repo.git(["branch", "--show-current"]), "feature/c");

    // Already there: a friendly no-op, not an error.
    repo.stack()
        .arg("top")
        .assert()
        .success()
        .stdout("feature/c is already at the top of the stack\n");
}

#[test]
fn bottom_from_the_trunk_follows_a_single_stack() {
    let repo = TestRepo::new();

    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "main"]);

    repo.stack().arg("bottom").assert().success();
    assert_eq!(repo.git(["branch", "--show-current"]), "feature/a");
}

#[test]
fn top_stops_at_a_fork_and_lists_the_choices() {
    let repo = TestRepo::new();

    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "feature/a"]);
    repo.stack().args(["new", "feature/c"]).assert().success();
    repo.git(["switch", "main"]);

    repo.stack()
        .arg("top")
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "feature/a has multiple stack children",
        ))
        .stderr(predicates::str::contains(
            "walk up from feature/a with `git stk up <branch>`",
        ));
}

#[test]
fn top_and_bottom_require_a_stack() {
    let repo = TestRepo::new();

    repo.stack()
        .arg("top")
        .assert()
        .failure()
        .stderr(predicates::str::contains("main is not in a stack"));

    repo.stack()
        .arg("bottom")
        .assert()
        .failure()
        .stderr(predicates::str::contains("main has no stacked branches"));
}

#[test]
fn new_and_restack_record_stack_base() {
    let repo = TestRepo::new();

    let main_tip = repo.git(["rev-parse", "main"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/a.stkBase"]),
        main_tip
    );

    repo.commit_file("a.txt", "a\n", "feature work");
    repo.git(["switch", "main"]);
    repo.commit_file("main.txt", "main\n", "main moves on");

    repo.git(["switch", "feature/a"]);
    repo.stack().arg("restack").assert().success();

    let new_main_tip = repo.git(["rev-parse", "main"]);
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/a.stkBase"]),
        new_main_tip
    );
}

#[test]
fn detach_clears_stack_base() {
    let repo = TestRepo::new();

    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["detach"]).assert().success();

    assert_eq!(
        repo.git_status(["config", "--get", "branch.feature/a.stkBase"])
            .status
            .code(),
        Some(1)
    );
}

#[test]
fn list_hints_restack_when_a_branch_is_behind() {
    let repo = TestRepo::new();

    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "a work");
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "feature/a"]);
    repo.commit_file("a.txt", "a\nmore\n", "a moves on");

    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "hint: feature/b is 1 commit behind feature/a - run `git stk restack`",
        ));
}

#[test]
fn list_prints_leaf_first_without_trunk_label_for_fragments() {
    let repo = TestRepo::new();

    // A stack fragment not rooted at the trunk gets no (trunk) label.
    repo.git(["switch", "-c", "feature/x"]);
    repo.git(["switch", "-c", "feature/y"]);
    repo.stack()
        .args(["adopt", "feature/y", "--parent", "feature/x"])
        .assert()
        .success();

    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout("  \u{25c9} feature/y\n\u{25cb} feature/x\n");
}

#[test]
fn list_colors_only_when_the_terminal_wants_it() {
    let repo = TestRepo::new();
    repo.stack().args(["new", "feature/a"]).assert().success();

    // Captured output (not a tty) stays plain; forcing color emits codes.
    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout(predicates::str::contains("\u{1b}[").not());

    repo.stack()
        .arg("list")
        .env("CLICOLOR_FORCE", "1")
        .assert()
        .success()
        .stdout(predicates::str::contains("\u{1b}["));

    // The swept commands speak the style layer too.
    repo.stack()
        .args(["new", "feature/b"])
        .env("CLICOLOR_FORCE", "1")
        .assert()
        .success()
        .stdout(predicates::str::contains("\u{1b}["))
        .stdout(predicates::str::contains("created"));
}

/// A trunk-anchored stack (main <- feature/a) plus a rootless fragment
/// (feature/x <- feature/y, adopted with no trunk anchor).
fn two_stacks() -> TestRepo {
    let repo = TestRepo::new();
    repo.stack().args(["new", "feature/a"]).assert().success();

    repo.git(["switch", "main"]);
    repo.git(["switch", "-c", "feature/x"]);
    repo.git(["switch", "-c", "feature/y"]);
    repo.stack()
        .args(["adopt", "feature/y", "--parent", "feature/x"])
        .assert()
        .success();

    repo.git(["switch", "feature/a"]);
    repo
}

#[test]
fn list_all_shows_every_stack() {
    let repo = two_stacks();

    let output = repo
        .stack()
        .args(["list", "--all"])
        .output()
        .expect("list --all");
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    for needle in ["feature/a", "feature/x", "feature/y", "main (trunk)"] {
        assert!(stdout.contains(needle), "missing {needle} in:\n{stdout}");
    }
    // The trunk appears once, at the bottom of its forest.
    assert_eq!(stdout.matches("(trunk)").count(), 1);
    // The branch we are on is marked wherever it appears.
    assert!(
        stdout.contains("\u{25c9} feature/a"),
        "current marker:\n{stdout}"
    );
}

#[test]
fn list_without_all_shows_only_the_current_stack() {
    let repo = two_stacks();

    // Standing on the trunk-anchored stack, the rootless fragment is hidden.
    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout(predicates::str::contains("feature/a"))
        .stdout(predicates::str::contains("feature/x").not());
}

#[test]
fn list_without_all_hides_sibling_stacks_sharing_the_trunk() {
    let repo = TestRepo::new();

    // Two independent stacks, both anchored on main.
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.git(["switch", "main"]);
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "feature/a"]);

    // The default view is the one stack you're on, sitting on its trunk - the
    // sibling that only shares the trunk is left for its own `list`.
    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout(predicates::str::contains("feature/a"))
        .stdout(predicates::str::contains("main (trunk)"))
        .stdout(predicates::str::contains("feature/b").not());

    // --all still shows both.
    let output = repo
        .stack()
        .args(["list", "--all"])
        .output()
        .expect("list --all");
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("feature/a") && stdout.contains("feature/b"),
        "both stacks expected:\n{stdout}"
    );
}

#[test]
fn list_all_conflicts_with_format() {
    let repo = two_stacks();

    repo.stack()
        .args(["list", "--all", "--format", "markdown"])
        .assert()
        .failure();
}

#[test]
fn list_reports_no_stacked_branches_on_a_fresh_repo() {
    let repo = TestRepo::new();
    // Just the trunk: not a stack. Must not draw a one-node "stack".
    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout(predicates::str::contains("no stacked branches"))
        .stdout(predicates::str::contains("git stk new"))
        .stdout(predicates::str::contains("(trunk)").not());
}

#[test]
fn commands_outside_a_git_repo_give_a_clean_error() {
    let outside = tempfile::tempdir().expect("temp dir");
    // A repo-requiring command run outside any repo: clean message, no raw
    // git "Stopping at filesystem boundary" noise.
    assert_cmd::Command::cargo_bin("git-stk")
        .expect("git-stk binary")
        .arg("status")
        .current_dir(outside.path())
        .assert()
        .failure()
        .stderr(predicates::str::contains("not a git repository"))
        .stderr(predicates::str::contains("Stopping at filesystem boundary").not());
}

#[test]
fn help_points_newcomers_at_the_guide() {
    let repo = TestRepo::new();
    repo.stack()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicates::str::contains("git stk guide"));
}

#[test]
fn list_commits_shows_each_branch_commits_newest_first() {
    let repo = TestRepo::new();
    repo.stack().args(["new", "feat/a"]).assert().success();
    repo.commit_file("a1.txt", "1\n", "add the first thing");
    repo.commit_file("a2.txt", "2\n", "add the second thing");
    repo.stack().args(["new", "feat/b"]).assert().success();
    repo.commit_file("b.txt", "b\n", "the b change");

    // Without --commits, commit subjects are not shown.
    repo.stack()
        .arg("list")
        .assert()
        .success()
        .stdout(predicates::str::contains("add the first thing").not());

    // With --commits, each branch's own commits appear beneath it.
    repo.stack()
        .args(["list", "--commits"])
        .assert()
        .success()
        .stdout(predicates::str::contains("add the first thing"))
        .stdout(predicates::str::contains("add the second thing"))
        .stdout(predicates::str::contains("the b change"));
}

#[test]
fn list_commits_marks_an_empty_branch() {
    let repo = TestRepo::new();
    repo.stack().args(["new", "feat/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "a work");
    // A freshly created branch shares its parent's tip - no commits of its own.
    repo.stack().args(["new", "feat/empty"]).assert().success();

    repo.stack()
        .args(["list", "--commits"])
        .assert()
        .success()
        .stdout(predicates::str::contains("(no commits)"));
}

#[test]
fn list_commits_conflicts_with_format() {
    let repo = TestRepo::new();
    repo.stack().args(["new", "feat/a"]).assert().success();

    repo.stack()
        .args(["list", "--commits", "--format", "markdown"])
        .assert()
        .failure();
}