git-stk 0.12.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
mod common;

use common::{FakeProvider, TestRepo};
use predicates::prelude::PredicateBooleanExt;

// Shared provider responses. The URL host/owner and presence of a title vary
// per test, matching what each assertion inspects.
const MERGED_A: &str = r##"[{"number":12,"state":"MERGED","baseRefName":"main","headRefName":"feature/a","url":"https://github.com/lararosekelley/git-stk/pull/12"}]"##;
const OPEN_B: &str = r##"[{"number":13,"state":"OPEN","baseRefName":"feature/a","headRefName":"feature/b","url":"https://github.com/lararosekelley/git-stk/pull/13"}]"##;
const CLOSED_A: &str = r##"[{"number":12,"state":"CLOSED","baseRefName":"main","headRefName":"feature/a","url":"https://github.com/lararosekelley/git-stk/pull/12"}]"##;
const MERGED_A_OWNER: &str = r##"[{"number":12,"state":"MERGED","baseRefName":"main","headRefName":"feature/a","url":"https://github.com/owner/repo/pull/12"}]"##;
const OPEN_B_OWNER: &str = r##"[{"number":13,"state":"OPEN","baseRefName":"feature/a","headRefName":"feature/b","url":"https://github.com/owner/repo/pull/13"}]"##;
const MERGED_A_TITLE: &str = r##"[{"number":12,"state":"MERGED","baseRefName":"main","headRefName":"feature/a","url":"https://github.com/owner/repo/pull/12","title":"A work"}]"##;
const OPEN_B_TITLE: &str = r##"[{"number":13,"state":"OPEN","baseRefName":"feature/a","headRefName":"feature/b","url":"https://github.com/owner/repo/pull/13","title":"B work"}]"##;

#[test]
fn cleanup_retargets_children_and_detaches_merged_branch() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();
    let fake = FakeProvider::new()
        .on("feature/a --state merged", MERGED_A)
        .on("feature/a", "[]")
        .on("feature/b", OPEN_B)
        .on("pr edit", "updated child review")
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains("will retarget feature/b -> main"))
        .stdout(predicates::str::contains(
            "will update review feature/b -> main (#13)",
        ))
        .stdout(predicates::str::contains("updated child review"))
        .stdout(predicates::str::contains("will detach feature/a"))
        .stdout(predicates::str::contains(
            "skipped feature/b: review #13 is open",
        ))
        .stdout(predicates::str::contains(
            "cleanup complete: 1 cleaned, 1 skipped",
        ));

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

#[test]
fn cleanup_dry_run_leaves_stack_metadata_unchanged() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();
    let fake = FakeProvider::new()
        .on("feature/a --state merged", MERGED_A)
        .on("feature/a", "[]")
        .on("feature/b", OPEN_B)
        // A dry run must never reach a review edit.
        .fail("pr edit", "dry-run should not edit review")
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "--dry-run", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "would retarget feature/b -> main",
        ))
        .stdout(predicates::str::contains(
            "would update review feature/b -> main (#13)",
        ))
        .stdout(predicates::str::contains("would detach feature/a"))
        .stdout(predicates::str::contains("would update stack note in #12"));

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

#[test]
fn cleanup_refreshes_the_stack_overview_ledger() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "main"]);
    let fake = FakeProvider::new()
        .on("pr view 12", r#"{"body":""}"#)
        .on("pr view 13", r#"{"body":""}"#)
        .record("pr edit 12 --body", "edit-body-12.txt", "")
        .record("pr edit 13 --body", "edit-body-13.txt", "")
        .on("feature/a --state merged", MERGED_A_TITLE)
        .on("feature/a", "[]")
        .on("feature/b", OPEN_B_TITLE)
        .on("pr edit", "edited")
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains("updated stack note in #12"))
        .stdout(predicates::str::contains("updated stack note in #13"))
        .stdout(predicates::str::contains("will delete branch feature/a"));

    // The overview was refreshed before the merged branch vanished: its
    // entry is restyled in the survivor, not dropped.
    let survivor =
        std::fs::read_to_string(repo.path().join("edit-body-13.txt")).expect("survivor body");
    assert!(survivor.contains(
        "- \u{1F7E3} ~~[A work (#12)](https://github.com/owner/repo/pull/12)~~ (merged)"
    ));
    assert!(
        survivor.contains(
            "- \u{1F7E2} [B work (#13)](https://github.com/owner/repo/pull/13) \u{1F448}"
        )
    );
}

#[test]
fn cleanup_recovers_base_when_merged_parent_branch_is_gone() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    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");
    repo.git(["switch", "main"]);
    // The merged parent was deleted out-of-band: feature/b now points at a
    // branch that no longer exists, and only the review remembers its base.
    repo.git(["branch", "-D", "feature/a"]);
    repo.git(["config", "branch.feature/b.stkParent", "feature/a"]);

    let fake = FakeProvider::new()
        .on("feature/a --state merged", MERGED_A_OWNER)
        .on("feature/a", "[]")
        .record("pr edit 13 --base", "edit-base-13.txt", "")
        .on("feature/b", OPEN_B_OWNER)
        .fallback("[]")
        .install(&repo);

    // Dry run announces the retarget without writing anything.
    repo.stack_faked(&fake)
        .args(["cleanup", "--dry-run", "feature/b"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "would retarget feature/b -> main",
        ));
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/b.stkParent"]),
        "feature/a"
    );

    repo.stack_faked(&fake)
        .args(["cleanup", "feature/b"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "feature/b: parent feature/a is gone, but review #12 merged into main",
        ))
        .stdout(predicates::str::contains("will retarget feature/b -> main"))
        .stdout(predicates::str::contains(
            "will update review feature/b -> main (#13)",
        ))
        .stdout(predicates::str::contains(
            "cleanup complete: 0 cleaned, 1 skipped, 1 retargeted",
        ));

    let recorded =
        std::fs::read_to_string(repo.path().join("edit-base-13.txt")).expect("edit base args");
    assert_eq!(recorded.trim(), "pr edit 13 --base main");
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/b.stkParent"]),
        "main"
    );
}

#[test]
fn cleanup_leaves_a_gone_parent_alone_without_a_merged_review() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.git(["switch", "-c", "feature/b"]);
    repo.git(["config", "branch.feature/b.stkParent", "feature/a"]);

    // No review for the missing parent: recovery must defer to repair.
    let fake = FakeProvider::new().fallback("[]").install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "feature/b"])
        .assert()
        .success()
        .stdout(predicates::str::contains("retarget").not());

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

#[test]
fn cleanup_skips_closed_reviews_with_their_state() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.git(["switch", "main"]);
    let fake = FakeProvider::new()
        .on("--state closed", CLOSED_A)
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "skipped feature/a: review #12 is closed",
        ))
        .stdout(predicates::str::contains(
            "cleanup complete: 0 cleaned, 1 skipped",
        ));

    // Closed work is not in the trunk; the branch must survive.
    assert!(
        repo.git(["branch", "--list", "feature/a"])
            .contains("feature/a")
    );
}

#[test]
fn cleanup_deletes_cleaned_merged_branch_by_default() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    // Real commits + a squash merge: feature/a's commits are NOT
    // ancestry-merged into main afterwards, so `git branch -d` would refuse.
    // Deletion must trust the provider-verified merge state instead.
    repo.commit_file("a.txt", "one\n", "parent change one");
    repo.commit_file("a.txt", "one\ntwo\n", "parent change two");
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "main"]);
    repo.git(["merge", "--squash", "feature/a"]);
    repo.git(["commit", "-m", "parent changes (#12)"]);
    let fake = FakeProvider::new()
        .on("feature/a --state merged", MERGED_A)
        .on("feature/a", "[]")
        .on("feature/b", OPEN_B)
        .on("pr edit", "updated child review")
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains("will delete branch feature/a"))
        .stdout(predicates::str::contains(
            "cleanup complete: 1 cleaned, 1 skipped",
        ));

    assert!(
        !repo
            .git(["branch", "--list", "feature/a"])
            .contains("feature/a")
    );
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/b.stkParent"]),
        "main"
    );
}

#[test]
fn cleanup_deletes_closed_branch_only_with_config_set() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    // Real commits, none of them ancestry-merged into main, so `git branch -d`
    // would refuse: deletion trusts the provider-verified review state.
    repo.commit_file("a.txt", "one\n", "parent change one");
    repo.commit_file("a.txt", "one\ntwo\n", "parent change two");
    repo.stack().args(["new", "feature/b"]).assert().success();
    repo.git(["switch", "main"]);
    repo.git(["merge", "--squash", "feature/a"]);
    repo.git(["commit", "-m", "parent changes (#12)"]);
    let fake = FakeProvider::new()
        .on("feature/a --state closed", CLOSED_A)
        .on("feature/a", "[]")
        .on("feature/b", OPEN_B)
        .on("pr edit", "updated child review")
        .fallback("[]")
        .install(&repo);

    // Initially try without the config option set
    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "skipped feature/a: review #12 is closed",
        ))
        .stdout(predicates::str::contains(
            "cleanup complete: 0 cleaned, 2 skipped",
        ));

    assert!(
        repo.git(["branch", "--list", "feature/a"])
            .contains("feature/a")
    );
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/b.stkParent"]),
        "feature/a"
    );

    // Then try with the config option set
    repo.git(["config", "stk.cleanClosed", "true"]);
    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "will delete branch feature/a (closed, not merged - `git stk undo` restores it)",
        ))
        .stdout(predicates::str::contains(
            "cleanup complete: 1 cleaned, 1 skipped",
        ));

    assert!(
        !repo
            .git(["branch", "--list", "feature/a"])
            .contains("feature/a")
    );
    assert_eq!(
        repo.git(["config", "--get", "branch.feature/b.stkParent"]),
        "main"
    );
    // No fork point pinned past the closed parent: feature/b's next restack has
    // to replay the closed commits it was built on, not drop them.
    assert!(
        !repo
            .git_status(["config", "--get", "branch.feature/b.stkBase"])
            .status
            .success()
    );
}

#[test]
fn cleanup_dry_run_keeps_branch() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.git(["switch", "main"]);
    let fake = FakeProvider::new()
        .on("--state merged", MERGED_A)
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "--dry-run", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains("would delete branch feature/a"));

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

#[test]
fn cleanup_keeps_the_checked_out_branch() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    let fake = FakeProvider::new()
        .on("--state merged", MERGED_A)
        .fallback("[]")
        .install(&repo);

    // Nothing was cleaned: the ref stays, so its metadata stays with it and the
    // branch is still in the stack for a later run.
    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "kept feature/a: cannot delete the checked out branch \
             - still stacked, so a later cleanup can finish it",
        ))
        .stdout(predicates::str::contains(
            "cleanup complete: 0 cleaned, 0 skipped, 1 kept",
        ));

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

#[test]
fn cleanup_keep_branch_keeps_cleaned_merged_branch() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.git(["switch", "main"]);
    let fake = FakeProvider::new()
        .on("--state merged", MERGED_A)
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "--keep-branch", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains("will detach feature/a"))
        .stdout(predicates::str::contains("delete").not());

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

/// `cleanup` reaches `landing_for` by a second path that `sync` does not, so a
/// merged release PR on the stack's base would have detached the layers above
/// it and deleted the branch - the destructive half of the same hazard.
#[test]
fn cleanup_never_finishes_the_stack_base() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.git(["switch", "-c", "rc-20260817"]);
    repo.commit_file("rc.txt", "rc\n", "release commit");
    repo.stack().args(["new", "fix/shared"]).assert().success();
    repo.commit_file("shared.txt", "shared\n", "shared work");

    let fake = FakeProvider::new()
        .on("rc-20260817", r##"[{"number":99,"state":"MERGED","baseRefName":"main","headRefName":"rc-20260817","url":"https://example.com/99","title":"Release 20260817"}]"##)
        .on("fix/shared", r##"[{"number":13,"state":"OPEN","baseRefName":"rc-20260817","headRefName":"fix/shared","url":"https://example.com/13","title":"Shared fix"}]"##)
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake).args(["cleanup"]).assert().success();

    assert!(
        !repo
            .git_status(["branch", "--list", "rc-20260817"])
            .stdout
            .is_empty(),
        "the base must not be deleted"
    );
    assert_eq!(
        repo.git(["config", "--get", "branch.fix/shared.stkParent"]),
        "rc-20260817",
        "the layers above the base must not be detached"
    );
}

/// A child in a stack that cannot reach its new parent - here the stack's
/// bottom, which the platform never retargets. The quiet "the platform
/// retargets it" is false, and `cleanup` is about to delete the branch this
/// review targets, so the mismatch has to be said out loud.
#[test]
fn cleanup_says_so_when_a_stack_cannot_retarget_a_child() {
    let repo = TestRepo::new();
    repo.git(["config", "stk.provider", "github"]);
    repo.git(["config", "stk.githubStacks", "true"]);
    repo.stack().args(["new", "feature/a"]).assert().success();
    repo.stack().args(["new", "feature/b"]).assert().success();

    // feature/b is the bottom of its own stack, still targeting feature/a.
    let stacks = r##"[{"number":5,"open":true,"base":{"ref":"feature/a"},"pull_requests":[
        {"number":13,"head":{"ref":"feature/b"}},
        {"number":14,"head":{"ref":"feature/c"}}]}]"##;
    let fake = FakeProvider::new()
        // Only a *base* change is refused; the stack-note edit is ordinary.
        .record("pr edit 13 --base", "retarget.txt", "")
        .on("repo view", r##"{"nameWithOwner":"owner/repo"}"##)
        .on("repos/owner/repo/stacks", stacks)
        .on("feature/a --state merged", MERGED_A)
        .on("feature/a", "[]")
        .on("feature/b", OPEN_B)
        .fallback("[]")
        .install(&repo);

    repo.stack_faked(&fake)
        .args(["cleanup", "feature/a"])
        .assert()
        .success()
        .stdout(predicates::str::contains(
            "its stack will not move it to main",
        ))
        .stdout(predicates::str::contains("git stk unstack"))
        .stdout(predicates::str::contains("the platform retargets it").not());

    assert!(
        !repo.path().join("retarget.txt").exists(),
        "the platform refuses this call, so it must not be attempted"
    );
}