stax 0.77.0

Fast stacked Git branches and PRs
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
mod common;

use common::{OutputAssertions, TestRepo};

#[cfg(unix)]
fn install_failing_pre_commit_hook(repo: &TestRepo) {
    use std::os::unix::fs::PermissionsExt;

    let hooks_dir = repo.path().join(".git").join("hooks");
    std::fs::create_dir_all(&hooks_dir).expect("create hooks dir");
    let hook = hooks_dir.join("pre-commit");
    std::fs::write(&hook, "#!/bin/sh\necho hook failed >&2\nexit 1\n").expect("write failing hook");
    std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755))
        .expect("chmod failing hook");
}

fn assert_current_parent_contains(repo: &TestRepo, expected: &str) {
    let parent = repo.get_current_parent();
    let expected_lower = expected.to_lowercase();
    assert!(
        parent
            .as_ref()
            .is_some_and(|p| p.to_lowercase().contains(&expected_lower)),
        "Expected current parent to contain '{}', got: {:?}",
        expected,
        parent
    );
}

fn branch_needs_restack(repo: &TestRepo, branch: &str) -> bool {
    repo.get_status_json()["branches"]
        .as_array()
        .and_then(|branches| {
            branches
                .iter()
                .find(|entry| entry["name"].as_str() == Some(branch))
        })
        .and_then(|entry| entry["needs_restack"].as_bool())
        .unwrap_or(false)
}

fn create_shared_file_stack(
    repo: &TestRepo,
    parent_name: &str,
    current_name: &str,
) -> (String, String) {
    repo.run_stax(&["bc", parent_name]).assert_success();
    repo.create_file("shared.txt", "safe line\nmiddle\nfeature no\n");
    repo.commit(&format!("Add {}", parent_name));
    let parent = repo.current_branch();

    repo.run_stax(&["bc", current_name]).assert_success();
    repo.create_file("shared.txt", "safe line\nmiddle\nfeature yes\n");
    repo.commit(&format!("Add {}", current_name));
    let current = repo.current_branch();

    (parent, current)
}

fn parent_for_branch(repo: &TestRepo, branch: &str) -> Option<String> {
    repo.get_status_json()["branches"]
        .as_array()
        .and_then(|branches| {
            branches
                .iter()
                .find(|entry| entry["name"].as_str() == Some(branch))
        })
        .and_then(|entry| entry["parent"].as_str())
        .map(ToString::to_string)
}

#[test]
fn test_create_below_reparents_current_branch_and_preserves_descendants() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    // main -> below-parent -> below-current -> below-child
    let branches = repo.create_stack(&["below-parent", "below-current", "below-child"]);
    let parent = &branches[0];
    let current = &branches[1];
    let child = &branches[2];

    repo.run_stax(&["checkout", current]).assert_success();
    let output = repo.run_stax(&["create", "below-mid", "--below"]);
    output.assert_success();

    let stdout = TestRepo::stdout(&output);
    assert!(
        stdout.contains("Reparented") && stdout.contains("restack"),
        "Expected reparent summary and restack hint, got: {}",
        stdout
    );

    let below_mid = repo.current_branch();
    assert!(
        below_mid.contains("below-mid"),
        "Expected to switch to new below branch, got: {}",
        below_mid
    );
    assert_current_parent_contains(&repo, "below-parent");

    repo.run_stax(&["checkout", current]).assert_success();
    assert_current_parent_contains(&repo, "below-mid");

    repo.run_stax(&["checkout", child]).assert_success();
    assert_current_parent_contains(&repo, "below-current");

    repo.run_stax(&["checkout", parent]).assert_success();
    let children = repo.get_children(parent);
    assert!(
        children.iter().any(|name| name.contains("below-mid")),
        "Expected parent to have below-mid as a direct child, got: {:?}",
        children
    );
}

#[test]
fn test_create_below_works_for_direct_child_of_trunk_via_bc_alias() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let branches = repo.create_stack(&["below-root"]);
    let original = &branches[0];

    repo.run_stax(&["checkout", original]).assert_success();
    repo.run_stax(&["bc", "below-root-mid", "--below"])
        .assert_success();

    let below_root_mid = repo.current_branch();
    assert!(
        below_root_mid.contains("below-root-mid"),
        "Expected to switch to new below branch, got: {}",
        below_root_mid
    );
    assert_current_parent_contains(&repo, "main");

    repo.run_stax(&["checkout", original]).assert_success();
    assert_current_parent_contains(&repo, "below-root-mid");
}

#[test]
fn test_create_below_works_via_branch_create() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let branches = repo.create_stack(&["below-branch-create"]);
    let original = &branches[0];

    repo.run_stax(&["branch", "create", "below-via-branch", "--below"])
        .assert_success();

    let new_branch = repo.current_branch();
    assert!(
        new_branch.contains("below-via-branch"),
        "Expected branch create --below to switch to new branch, got: {}",
        new_branch
    );
    assert_current_parent_contains(&repo, "main");

    repo.run_stax(&["checkout", original]).assert_success();
    assert_current_parent_contains(&repo, "below-via-branch");
}

#[test]
fn test_create_below_with_message_commits_on_new_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let branches = repo.create_stack(&["below-message-parent", "below-message-current"]);
    let parent = &branches[0];
    let current = &branches[1];
    let parent_before = repo.get_commit_sha(parent);
    let current_before = repo.get_commit_sha(current);

    repo.run_stax(&["checkout", current]).assert_success();
    repo.create_file("prep.txt", "prep work\n");

    let output = repo.run_stax(&["create", "-a", "-m", "Prep below", "--below"]);
    output.assert_success();
    output.assert_stdout_contains("Committed: Prep below");

    let prep_branch = repo.current_branch();
    assert!(
        prep_branch.to_lowercase().contains("prep-below"),
        "Expected generated prep-below branch, got: {}",
        prep_branch
    );

    let subject = repo.git(&["log", "-1", "--pretty=%s"]);
    assert!(subject.status.success(), "{}", TestRepo::stderr(&subject));
    assert_eq!(TestRepo::stdout(&subject).trim(), "Prep below");
    let commit_parent = repo.git(&["rev-parse", "HEAD^"]);
    assert!(
        commit_parent.status.success(),
        "{}",
        TestRepo::stderr(&commit_parent)
    );
    assert_eq!(
        TestRepo::stdout(&commit_parent).trim(),
        parent_before,
        "below commit should be based on the original parent"
    );
    assert_eq!(
        repo.get_commit_sha(current),
        current_before,
        "original branch should not advance when committing below it"
    );

    repo.run_stax(&["checkout", current]).assert_success();
    assert_current_parent_contains(&repo, "prep-below");
    assert!(
        branch_needs_restack(&repo, current),
        "original branch should need restack after the below branch gets a commit"
    );
}

#[test]
fn test_create_below_with_message_rejects_generated_name_collision_in_stack() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let branches = repo.create_stack(&[
        "below-collision-parent",
        "below-collision-current",
        "below-collision-hotfix",
    ]);
    let current = &branches[1];

    repo.run_stax(&["checkout", current]).assert_success();
    let output = repo.run_stax(&["create", "--below", "-m", "below collision hotfix"]);
    output.assert_failure();

    let stderr = TestRepo::stderr(&output);
    assert!(
        stderr.contains("already exists")
            && stderr.contains("Generated branch names are not auto-suffixed")
            && stderr.contains("explicit different branch name"),
        "Expected actionable generated-name collision error, got: {}",
        stderr
    );
    assert_eq!(
        repo.current_branch(),
        *current,
        "failed below create should leave the current branch checked out"
    );
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|branch| branch.contains("below-collision-hotfix-2")),
        "below create must not silently create a suffixed duplicate branch"
    );
    assert_current_parent_contains(&repo, "below-collision-parent");
}

#[test]
fn test_create_below_with_explicit_name_collision_uses_normal_conflict_error() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let branches = repo.create_stack(&["below-explicit-current", "below-explicit-existing"]);
    let current = &branches[0];

    repo.run_stax(&["checkout", current]).assert_success();
    let output = repo.run_stax(&["create", "below-explicit-existing", "--below"]);
    output.assert_failure();

    let stderr = TestRepo::stderr(&output);
    assert!(
        stderr.contains("already exists"),
        "Expected normal branch conflict error, got: {}",
        stderr
    );
    assert!(
        !stderr.contains("Generated branch names"),
        "Explicit branch-name collisions should not mention generated names: {}",
        stderr
    );
}

#[test]
fn test_create_below_auto_stashes_dirty_worktree_onto_new_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let (_parent, current) =
        create_shared_file_stack(&repo, "below-stash-parent", "below-stash-current");

    repo.create_file("shared.txt", "hotfix line\nmiddle\nfeature yes\n");
    repo.create_file("cve-notes.txt", "untracked hotfix notes\n");

    let output = repo.run_stax(&["create", "below-hotfix", "--below"]);
    output.assert_success();
    output.assert_stdout_contains("Stashed working tree changes");
    output.assert_stdout_contains("Restored stashed changes on new branch");

    let new_branch = repo.current_branch();
    assert!(
        new_branch.contains("below-hotfix"),
        "Expected to switch to below-hotfix branch, got: {}",
        new_branch
    );
    assert_eq!(
        std::fs::read_to_string(repo.path().join("shared.txt")).expect("read shared.txt"),
        "hotfix line\nmiddle\nfeature no\n",
        "prepared changes should be reapplied to the lower branch base"
    );
    assert_eq!(
        std::fs::read_to_string(repo.path().join("cve-notes.txt")).expect("read cve-notes.txt"),
        "untracked hotfix notes\n",
        "untracked prepared files should be carried to the new lower branch"
    );
    let status = TestRepo::stdout(&repo.git(&["status", "--porcelain"]));
    assert!(
        status.contains(" M shared.txt"),
        "prepared change should remain uncommitted on the new lower branch"
    );
    assert!(
        status.contains("?? cve-notes.txt"),
        "untracked prepared file should remain uncommitted on the new lower branch"
    );
    assert_eq!(
        parent_for_branch(&repo, &current).as_deref(),
        Some(new_branch.as_str()),
        "original current branch should be reparented onto the new below branch"
    );
    assert!(
        TestRepo::stdout(&repo.git(&["stash", "list"]))
            .trim()
            .is_empty(),
        "auto-stash should be dropped after a clean restore"
    );
}

#[test]
fn test_create_below_with_message_auto_stashes_staged_worktree_before_detach() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let (parent, current) = create_shared_file_stack(
        &repo,
        "below-message-stash-parent",
        "below-message-stash-current",
    );
    let parent_before = repo.get_commit_sha(&parent);
    let current_before = repo.get_commit_sha(&current);

    repo.create_file("shared.txt", "hotfix line\nmiddle\nfeature yes\n");
    assert!(repo.git(&["add", "shared.txt"]).status.success());

    let output = repo.run_stax(&["create", "--below", "-m", "Fix staged CVE"]);
    output.assert_success();
    output.assert_stdout_contains("Stashed working tree changes");
    output.assert_stdout_contains("Committed: Fix staged CVE");

    let new_branch = repo.current_branch();
    assert!(
        new_branch.to_lowercase().contains("fix-staged-cve"),
        "Expected generated fix-staged-cve branch, got: {}",
        new_branch
    );
    assert_eq!(
        std::fs::read_to_string(repo.path().join("shared.txt")).expect("read shared.txt"),
        "hotfix line\nmiddle\nfeature no\n",
        "commit should be made from the lower branch base"
    );
    assert!(
        TestRepo::stdout(&repo.git(&["status", "--porcelain"]))
            .trim()
            .is_empty(),
        "staged prepared change should be committed"
    );

    let subject = repo.git(&["log", "-1", "--pretty=%s"]);
    assert!(subject.status.success(), "{}", TestRepo::stderr(&subject));
    assert_eq!(TestRepo::stdout(&subject).trim(), "Fix staged CVE");
    let commit_parent = repo.git(&["rev-parse", "HEAD^"]);
    assert!(
        commit_parent.status.success(),
        "{}",
        TestRepo::stderr(&commit_parent)
    );
    assert_eq!(
        TestRepo::stdout(&commit_parent).trim(),
        parent_before,
        "below commit should be based on the original parent"
    );
    assert_eq!(
        repo.get_commit_sha(&current),
        current_before,
        "original branch should not advance when committing below it"
    );
    assert_eq!(
        parent_for_branch(&repo, &current).as_deref(),
        Some(new_branch.as_str()),
        "original current branch should be reparented onto the generated below branch"
    );
    assert!(
        TestRepo::stdout(&repo.git(&["stash", "list"]))
            .trim()
            .is_empty(),
        "auto-stash should be dropped after a successful commit"
    );
}

#[test]
fn test_create_below_with_message_restores_original_branch_when_auto_stash_conflicts() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let (_parent, current) =
        create_shared_file_stack(&repo, "below-conflict-parent", "below-conflict-current");
    let current_before = repo.get_commit_sha(&current);

    repo.create_file("shared.txt", "safe line\nmiddle\nfeature hotfix\n");
    assert!(repo.git(&["add", "shared.txt"]).status.success());

    let output = repo.run_stax(&["create", "--below", "-m", "Conflicting hotfix"]);
    output.assert_failure();
    output.assert_stderr_contains("Could not apply stashed changes");

    assert_eq!(
        repo.current_branch(),
        current,
        "auto-stash conflict should restore the original branch"
    );
    assert_eq!(
        repo.get_commit_sha(&current),
        current_before,
        "failed below create should not advance the original branch"
    );
    assert_eq!(
        std::fs::read_to_string(repo.path().join("shared.txt")).expect("read shared.txt"),
        "safe line\nmiddle\nfeature hotfix\n",
        "prepared changes should be restored on the original branch"
    );
    assert!(
        TestRepo::stdout(&repo.git(&["status", "--porcelain"])).contains("M  shared.txt"),
        "original staged state should be restored"
    );
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|branch| branch.to_lowercase().contains("conflicting-hotfix")),
        "failed below create must not leave the destination branch"
    );
    assert!(
        TestRepo::stdout(&repo.git(&["stash", "list"]))
            .trim()
            .is_empty(),
        "restored auto-stash should be dropped"
    );
}

#[test]
#[cfg(unix)]
fn test_create_below_restores_original_metadata_when_commit_fails() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let branches = repo.create_stack(&["below-fail-parent", "below-fail-current"]);
    let parent = &branches[0];
    let current = &branches[1];
    let current_before = repo.get_commit_sha(current);

    repo.run_stax(&["checkout", current]).assert_success();
    repo.create_file("fail-below.txt", "work that should stay\n");
    install_failing_pre_commit_hook(&repo);

    let output = repo.run_stax(&["create", "-a", "-m", "Fail below", "--below"]);
    output.assert_failure();
    output.assert_stderr_contains("No branch was created");

    assert_eq!(
        repo.current_branch(),
        *current,
        "rollback should return to the original branch"
    );
    assert_eq!(
        repo.get_commit_sha(current),
        current_before,
        "original branch should not advance after failed below commit"
    );
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|branch| branch.to_lowercase().contains("fail-below")),
        "failed below branch should be deleted"
    );
    assert_current_parent_contains(&repo, parent);
    assert!(
        repo.path().join("fail-below.txt").exists(),
        "rollback should preserve the user's working-tree file"
    );

    repo.run_stax(&["create", "-a", "-m", "Fail below", "--below"])
        .assert_failure();
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|branch| branch.to_lowercase().contains("fail-below-2")),
        "retry should not drift into a suffixed branch"
    );
}

#[test]
#[cfg(unix)]
fn test_create_below_no_verify_skips_hook() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let branches = repo.create_stack(&["below-no-verify-parent", "below-no-verify-current"]);
    let current = &branches[1];
    let current_before = repo.get_commit_sha(current);

    repo.run_stax(&["checkout", current]).assert_success();
    repo.create_file("skip-below-hook.txt", "work that should commit\n");
    install_failing_pre_commit_hook(&repo);

    let output = repo.run_stax(&[
        "create",
        "-a",
        "-m",
        "Skip below hook",
        "--below",
        "--no-verify",
    ]);
    output.assert_success();
    output.assert_stdout_contains("Committed: Skip below hook");

    let new_branch = repo.current_branch();
    assert!(
        new_branch.to_lowercase().contains("skip-below-hook"),
        "Expected generated skip-below-hook branch, got: {}",
        new_branch
    );

    let subject = repo.git(&["log", "-1", "--pretty=%s"]);
    assert!(subject.status.success(), "{}", TestRepo::stderr(&subject));
    assert_eq!(TestRepo::stdout(&subject).trim(), "Skip below hook");
    assert_eq!(
        repo.get_commit_sha(current),
        current_before,
        "original branch should not advance when committing below it"
    );

    repo.run_stax(&["checkout", current]).assert_success();
    assert_current_parent_contains(&repo, "skip-below-hook");
}

#[test]
fn test_create_below_rejects_from() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();
    repo.create_stack(&["below-from-conflict"]);

    let output = repo.run_stax(&["create", "bad", "--below", "--from", "main"]);
    output.assert_failure();

    let stderr = TestRepo::stderr(&output);
    assert!(
        stderr.contains("cannot be used") || stderr.contains("conflict"),
        "Expected conflicting --from error, got: {}",
        stderr
    );
}

#[test]
fn test_create_below_untracked_branch_fails() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    repo.git(&["checkout", "-b", "manual-untracked"]);
    let output = repo.run_stax(&["create", "bad", "--below"]);
    output.assert_failure();

    let stderr = TestRepo::stderr(&output);
    assert!(
        stderr.contains("not tracked") || stderr.contains("branch track"),
        "Expected untracked branch guidance, got: {}",
        stderr
    );
}

#[test]
fn test_create_below_from_trunk_fails() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    let output = repo.run_stax(&["create", "below-main", "--below"]);
    output.assert_failure();

    let stderr = TestRepo::stderr(&output);
    assert!(
        stderr.contains("below trunk") || stderr.contains("Checkout a stacked branch"),
        "Expected below-trunk guidance, got: {}",
        stderr
    );
}

#[test]
fn test_create_below_rejects_conflicting_placement_flags() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();
    repo.create_stack(&["below-conflict"]);

    let output = repo.run_stax(&["create", "bad", "--below", "--insert"]);
    output.assert_failure();

    let stderr = TestRepo::stderr(&output);
    assert!(
        stderr.contains("cannot be used") || stderr.contains("conflict"),
        "Expected conflicting flag error, got: {}",
        stderr
    );
}