git-stk 0.9.0

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
mod common;

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

#[test]
fn demo_provider_runs_the_full_merge_loop_offline() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);

    repo.stack()
        .args(["new", "feature/login"])
        .assert()
        .success();
    repo.commit_file("login.txt", "login form\n", "add login form");
    repo.stack()
        .args(["new", "feature/avatar"])
        .assert()
        .success();
    repo.commit_file("avatar.txt", "avatars\n", "add avatars");

    // Submit opens demo reviews and writes the stack overview into them.
    repo.stack()
        .args(["submit", "--stack"])
        .assert()
        .success()
        .stdout(predicates::str::contains("created feature/login -> main"))
        .stdout(predicates::str::contains(
            "created feature/avatar -> feature/login",
        ))
        .stdout(predicates::str::contains("updated stack note in #1"))
        .stdout(predicates::str::contains("updated stack note in #2"));

    repo.stack()
        .args(["status", "feature/login"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "review: #1 open feature/login -> main",
        ))
        .stdout(predicates::str::contains("url: demo://review/1"));

    // The whole landing, no network: real squashes onto main, real cleanup.
    repo.stack()
        .args(["merge", "--all", "-y"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "squashed feature/login into main",
        ))
        .stdout(predicates::str::contains("merged add login form (#1)"))
        .stdout(predicates::str::contains(
            "squashed feature/avatar into main",
        ))
        .stdout(predicates::str::contains("merged add avatars (#2)"))
        .stdout(predicates::str::contains(
            "stack complete: everything merged into main",
        ))
        .stdout(predicates::str::contains(
            "merge complete: 2 of 2 reviews merged",
        ));

    // The squashed work is genuinely on main.
    assert_eq!(repo.git(["branch", "--show-current"]), "main");
    assert_eq!(repo.git(["show", "main:login.txt"]), "login form");
    assert_eq!(repo.git(["show", "main:avatar.txt"]), "avatars");
    assert_eq!(
        repo.git_status(["branch", "--list", "feature/login", "feature/avatar"])
            .stdout
            .len(),
        0
    );
}

#[test]
fn demo_provider_is_never_auto_detected() {
    let repo = TestRepo::new();
    repo.git(["remote", "add", "origin", "git@github.com:owner/repo.git"]);

    repo.stack()
        .arg("provider")
        .assert()
        .success()
        .stdout(predicates::str::contains("github"))
        .stdout(predicates::str::contains("demo").not());
}

#[test]
fn list_plain_format_uses_plain_text_and_bare_urls() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "add feature a");
    repo.stack().args(["submit"]).assert().success();

    repo.stack()
        .args(["list", "--format", "plain"])
        .assert()
        .success()
        // Unquoted base; bare URL on its own line for chat apps to auto-link.
        .stdout(predicates::str::contains("1 PR, base main"))
        .stdout(predicates::str::contains("1. add feature a (#1) - open"))
        .stdout(predicates::str::contains("   demo://review/1"))
        // No markdown link syntax.
        .stdout(predicates::str::contains("](").not());
}

#[test]
fn undo_restores_branch_tips_after_a_restack() {
    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.commit_file("b.txt", "b\n", "b work");
    let before = repo.git(["rev-parse", "feature/b"]);

    // Move the parent so the restack actually rewrites feature/b.
    repo.git(["switch", "feature/a"]);
    repo.commit_file("a2.txt", "more\n", "a moves on");
    repo.stack().arg("restack").assert().success();
    repo.git(["switch", "feature/b"]);
    assert_ne!(repo.git(["rev-parse", "feature/b"]), before);

    repo.stack()
        .arg("undo")
        .assert()
        .success()
        .stdout(predicates::str::contains("undid restack"))
        .stdout(predicates::str::contains(
            "pushes and merged reviews are not reverted",
        ));

    assert_eq!(repo.git(["rev-parse", "feature/b"]), before);
    // One-shot: a second undo has nothing to restore.
    repo.stack()
        .arg("undo")
        .assert()
        .failure()
        .stderr(predicates::str::contains("nothing to undo"));
}

#[test]
fn undo_recreates_a_branch_cleanup_deleted() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "a work");
    repo.stack().args(["submit"]).assert().success();
    let sha = repo.git(["rev-parse", "feature/a"]);

    // Merge lands feature/a on main and cleanup deletes it.
    repo.stack().args(["merge", "-y"]).assert().success();
    assert_eq!(
        repo.git_status(["rev-parse", "--verify", "feature/a"])
            .status
            .code(),
        Some(128),
        "feature/a should be gone after merge"
    );

    repo.stack().arg("undo").assert().success();
    // The deleted branch is back at its pre-merge tip.
    assert_eq!(repo.git(["rev-parse", "feature/a"]), sha);
}

#[test]
fn undo_refuses_with_a_dirty_worktree() {
    let repo = TestRepo::new();
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "a work");
    repo.git(["switch", "feature/a"]);
    repo.commit_file("a2.txt", "more\n", "a moves on");
    repo.git(["switch", "main"]); // make feature/a the stack, restack a no-op-free case
    repo.git(["switch", "feature/a"]);
    repo.stack().arg("restack").assert().success();

    // Dirty the tree, then undo must refuse rather than reset over it.
    repo.write("uncommitted.txt", "work in progress\n");
    repo.git(["add", "uncommitted.txt"]);
    repo.stack()
        .arg("undo")
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "worktree has uncommitted changes",
        ));
}

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

    // A failing command: no stack to merge. Captured (non-tty) stderr is
    // plain, but carries the `error:` prefix and exits nonzero.
    repo.stack()
        .arg("merge")
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "error: no stacked branches to merge",
        ))
        .stderr(predicates::str::contains("\u{1b}[").not());

    // Forced color paints the prefix red.
    repo.stack()
        .arg("merge")
        .env("CLICOLOR_FORCE", "1")
        .assert()
        .failure()
        .stderr(predicates::str::contains("\u{1b}["))
        .stderr(predicates::str::contains("error:"));
}

#[test]
fn view_reports_no_review_without_one() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);
    repo.stack().args(["new", "feature/a"]).assert().success();

    repo.stack()
        .args(["view", "feature/a"])
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "no demo review found for feature/a; submit it first with `git stk submit`",
        ));
}

#[test]
fn view_opens_the_current_branch_review() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "a work");
    repo.stack().args(["submit"]).assert().success();

    // The demo has no browser, but the command resolves the review, prints
    // the opening line, and the provider's graceful note.
    repo.stack()
        .args(["view"])
        .assert()
        .success()
        .stdout(predicates::str::contains("opening #1"))
        .stdout(predicates::str::contains("demo reviews have no web page"));
}

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

    repo.stack()
        .arg("guide")
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "the guide is interactive; run it from a terminal",
        ));

    // A named tour still needs the terminal.
    repo.stack()
        .args(["guide", "conflicts"])
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "the guide is interactive; run it from a terminal",
        ));
}

// The two scripted tours run interactively, so their exact recipes are
// proven here instead: same commands, same files, same expected outcomes.

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

    repo.stack()
        .args(["new", "feature/payment"])
        .assert()
        .success();
    repo.commit_file("notes.txt", "use stripe\n", "choose payment provider");
    repo.stack()
        .args(["new", "feature/receipts"])
        .assert()
        .success();
    repo.commit_file("notes.txt", "use stripe with receipts\n", "email receipts");
    repo.git(["switch", "feature/payment"]);
    repo.commit_file("notes.txt", "use paypal\n", "switch to paypal");

    repo.stack()
        .arg("restack")
        .assert()
        .failure()
        .stderr(predicates::str::contains(
            "resolve conflicts, then run `git stk continue`",
        ));

    repo.write("notes.txt", "use paypal with receipts\n");
    repo.git(["add", "notes.txt"]);
    repo.stack()
        .arg("continue")
        .assert()
        .success()
        .stdout(predicates::str::contains("restack complete"));

    assert_eq!(
        repo.git(["show", "feature/receipts:notes.txt"]),
        "use paypal with receipts"
    );
}

#[test]
fn guide_repair_recipe_recovers_from_the_demo_review() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);

    repo.stack().args(["new", "feature/api"]).assert().success();
    repo.commit_file("api.txt", "endpoints\n", "add api");
    repo.stack().args(["new", "feature/ui"]).assert().success();
    repo.commit_file("ui.txt", "buttons\n", "add ui");
    repo.stack().args(["submit", "--stack"]).assert().success();

    repo.git(["config", "--unset", "branch.feature/ui.stkParent"]);

    repo.stack()
        .arg("repair")
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "feature/ui: set parent feature/api (from demo review #2)",
        ));
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/ui.stkParent"]),
        "feature/api"
    );
}

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

    repo.stack()
        .args(["guide", "bogus"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("invalid value"))
        .stderr(predicates::str::contains("intro"));
}

#[test]
fn submit_stack_does_not_sweep_sibling_stacks_on_the_trunk() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);

    // Two independent stacks rooted on the trunk.
    repo.stack().args(["new", "feature/x"]).assert().success();
    repo.commit_file("x.txt", "x\n", "add x");
    repo.git(["switch", "main"]);
    repo.stack().args(["new", "feature/y"]).assert().success();
    repo.commit_file("y.txt", "y\n", "add y");

    // Submitting feature/x's stack opens a review for it alone - feature/y is
    // a separate stack that merely shares the trunk.
    repo.git(["switch", "feature/x"]);
    repo.stack()
        .args(["submit", "--stack"])
        .assert()
        .success()
        .stdout(predicates::str::contains("created feature/x -> main"))
        .stdout(predicates::str::contains("feature/y").not());

    // feature/y still has no review until its own stack is submitted.
    repo.git(["switch", "feature/y"]);
    repo.stack()
        .args(["submit", "--stack"])
        .assert()
        .success()
        .stdout(predicates::str::contains("created feature/y -> main"));
}

#[test]
fn rename_then_submit_replaces_and_prunes_the_old_review() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);

    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "add a");
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.commit_file("b.txt", "b\n", "add b");
    repo.stack().args(["submit", "--stack"]).assert().success();

    // Rename the leaf; its open review (#2) still heads the old name, so the
    // rename records the supersession for the next submit to resolve.
    repo.stack()
        .args(["rename", "feature/b2"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "opens a fresh review for feature/b2 and closes #2",
        ));
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/b2.stkRenamedFrom"]),
        "feature/b"
    );

    // Resubmit opens a fresh review for feature/b2, then retires the stale #2.
    repo.stack()
        .args(["submit", "--stack"])
        .assert()
        .success()
        .stdout(predicates::str::contains("created feature/b2 -> feature/a"))
        .stdout(predicates::str::contains(
            "closed superseded review #2 for feature/b",
        ));

    // The marker is consumed once handled.
    assert!(
        repo.git_status(["config", "--get", "branch.feature/b2.stkRenamedFrom"])
            .stdout
            .is_empty()
    );

    // feature/a's overview now lists the replacement review, not the stale one.
    let raw = std::fs::read_to_string(repo.path().join(".git/stk-demo-reviews"))
        .expect("demo review state");
    let state: serde_json::Value = serde_json::from_str(&raw).expect("parse demo state");
    let body_a = state["reviews"]
        .as_array()
        .unwrap()
        .iter()
        .find(|review| review["id"] == 1)
        .expect("review #1")["body"]
        .as_str()
        .unwrap();
    assert!(
        body_a.contains("demo://review/3"),
        "overview should list the renamed branch's fresh review"
    );
    assert!(
        !body_a.contains("demo://review/2"),
        "overview should drop the superseded review"
    );
}

/// The stored review body for demo review `id`, for asserting overview content.
fn demo_review_body(repo: &TestRepo, id: u64) -> String {
    let raw = std::fs::read_to_string(repo.path().join(".git/stk-demo-reviews"))
        .expect("demo review state");
    let state: serde_json::Value = serde_json::from_str(&raw).expect("parse demo state");
    state["reviews"]
        .as_array()
        .unwrap()
        .iter()
        .find(|review| review["id"].as_u64() == Some(id))
        .unwrap_or_else(|| panic!("review #{id}"))["body"]
        .as_str()
        .unwrap()
        .to_owned()
}

#[test]
fn rebuild_overview_drops_orphaned_rows_keeping_the_live_stack() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "add a");
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.commit_file("b.txt", "b\n", "add b");
    repo.stack().args(["submit", "--stack"]).assert().success();

    // Drift the ledger like a pre-fix rename would: drop the marker so #2 is
    // left orphaned in the overview instead of being closed and pruned.
    repo.stack()
        .args(["rename", "feature/b2"])
        .assert()
        .success();
    repo.git(["config", "--unset", "branch.feature/b2.stkRenamedFrom"]);
    repo.stack().args(["submit", "--stack"]).assert().success();

    // Dry run reports the drifted row it would drop.
    repo.stack()
        .args(["submit", "--stack", "--rebuild-overview", "--dry-run"])
        .assert()
        .success()
        .stdout(predicates::str::contains("would drop drifted entry #2"));

    repo.stack()
        .args(["submit", "--stack", "--rebuild-overview"])
        .assert()
        .success();

    // feature/a's overview now lists only the live stack, not the orphaned #2.
    let body = demo_review_body(&repo, 1);
    assert!(body.contains("demo://review/1"));
    assert!(body.contains("demo://review/3"));
    assert!(
        !body.contains("demo://review/2"),
        "rebuild should drop the orphaned entry"
    );
}

#[test]
fn rebuild_overview_keeps_merged_history() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "demo"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.commit_file("a.txt", "a\n", "add a");
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.commit_file("b.txt", "b\n", "add b");
    repo.stack().args(["submit", "--stack"]).assert().success();

    // Land feature/a so #1 becomes merged history and a leaves the stack.
    repo.git(["switch", "feature/a"]);
    repo.stack().args(["merge", "-y"]).assert().success();

    // Rebuilding from the survivor keeps the merged #1 alongside the live #2.
    repo.git(["switch", "feature/b"]);
    repo.stack()
        .args(["submit", "--stack", "--rebuild-overview"])
        .assert()
        .success();

    let body = demo_review_body(&repo, 2);
    assert!(
        body.contains("demo://review/1"),
        "rebuild should keep merged history"
    );
    assert!(body.contains("demo://review/2"));
}