stax 0.87.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
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
//! Tests for `st create -m` behaviour when the commit step fails.
//!
//! The graphite-style commit-first flow runs `git commit` on the current branch
//! before creating the new branch. When a pre-commit hook rejects the commit
//! (or the user interrupts with Ctrl+C) the command exits with no refs touched
//! — no orphan branch is created, the current branch's HEAD is unchanged, and
//! retrying with the same message does not drift into `mybranch-2`,
//! `mybranch-3`, etc.
//!
//! The same safety property applies when `-m` commits from a non-current
//! parent (`--from <other>` / `--below`): stax commits before creating the
//! destination branch, then writes refs and metadata only after success.
//!
//! Most tests require Unix (pre-commit hooks use `#!/bin/sh` and chmod).

use crate::common;

#[cfg(unix)]
use common::stax_bin;
use common::{OutputAssertions, TestRepo};
#[cfg(unix)]
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
#[cfg(unix)]
use std::process::{Command, Stdio};
#[cfg(unix)]
use std::thread;
#[cfg(unix)]
use std::time::{Duration, Instant};

/// Install a pre-commit hook that always fails.
#[cfg(unix)]
fn install_failing_pre_commit_hook(repo: &TestRepo) {
    let hooks_dir = repo.path().join(".git").join("hooks");
    fs::create_dir_all(&hooks_dir).unwrap();
    let hook_path = hooks_dir.join("pre-commit");
    fs::write(&hook_path, "#!/bin/sh\nexit 1\n").unwrap();
    fs::set_permissions(&hook_path, fs::Permissions::from_mode(0o755)).unwrap();
}

#[cfg(unix)]
fn install_waiting_pre_commit_hook(repo: &TestRepo) {
    let hooks_dir = repo.path().join(".git").join("hooks");
    fs::create_dir_all(&hooks_dir).unwrap();
    let hook_path = hooks_dir.join("pre-commit");
    fs::write(
        &hook_path,
        "#!/bin/sh\n\
         touch .git/hooks/pre-commit-started\n\
         while [ ! -f .git/hooks/pre-commit-release ]; do sleep 1; done\n",
    )
    .unwrap();
    fs::set_permissions(&hook_path, fs::Permissions::from_mode(0o755)).unwrap();
}

/// Remove the pre-commit hook.
#[cfg(unix)]
fn remove_pre_commit_hook(repo: &TestRepo) {
    let hook_path = repo.path().join(".git").join("hooks").join("pre-commit");
    let _ = std::fs::remove_file(hook_path);
}

#[cfg(unix)]
fn wait_for_hook_start(repo: &TestRepo) {
    let marker = repo
        .path()
        .join(".git")
        .join("hooks")
        .join("pre-commit-started");
    let deadline = Instant::now() + Duration::from_secs(10);
    while Instant::now() < deadline {
        if marker.exists() {
            return;
        }
        thread::sleep(Duration::from_millis(50));
    }
    panic!("pre-commit hook did not start");
}

#[cfg(unix)]
fn run_stax_until_hook_then_interrupt(repo: &TestRepo, args: &[&str]) -> std::process::Output {
    let null_path = if cfg!(windows) { "NUL" } else { "/dev/null" };
    let child = Command::new(stax_bin())
        .args(args)
        .current_dir(repo.path())
        .env_remove("GITHUB_TOKEN")
        .env_remove("STAX_GITHUB_TOKEN")
        .env_remove("STAX_SHELL_INTEGRATION")
        .env_remove("GH_TOKEN")
        .env("GIT_CONFIG_GLOBAL", null_path)
        .env("GIT_CONFIG_SYSTEM", null_path)
        .env("STAX_DISABLE_UPDATE_CHECK", "1")
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn stax");

    wait_for_hook_start(repo);

    let pid = child.id().to_string();
    let status = Command::new("kill")
        .args(["-INT", &pid])
        .status()
        .expect("send SIGINT to stax process");
    assert!(status.success(), "failed to interrupt stax process {pid}");

    fs::write(
        repo.path()
            .join(".git")
            .join("hooks")
            .join("pre-commit-release"),
        "",
    )
    .expect("release waiting pre-commit hook");

    child.wait_with_output().expect("wait for interrupted stax")
}

#[test]
#[cfg(unix)]
fn create_with_failing_hook_leaves_no_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    repo.create_file("test.txt", "hello");

    install_failing_pre_commit_hook(&repo);

    let main_before = repo.head_sha();

    let output = repo.run_stax(&["create", "-a", "-m", "my feature"]);
    output.assert_failure();

    // No branch should have been created at all — commit-first means the
    // branch is never made if the commit fails.
    let branches = repo.list_branches();
    assert!(
        !branches.iter().any(|b| b.contains("my-feature")),
        "No orphan branch should exist when commit fails. Branches: {:?}",
        branches
    );

    // User stays on main — no switch happened.
    assert_eq!(repo.current_branch(), "main");

    // Main's HEAD must not have moved: the commit either didn't happen or was
    // undone. Nothing stacked on top.
    assert_eq!(
        repo.head_sha(),
        main_before,
        "main's HEAD must not advance when the commit fails",
    );

    // Error message should guide the user to retry.
    output.assert_stderr_contains("No branch was created");
}

#[test]
#[cfg(unix)]
fn create_retry_after_hook_failure_uses_same_name() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    repo.create_file("test.txt", "hello");

    install_failing_pre_commit_hook(&repo);

    // First attempt: should fail and roll back
    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_failure();

    // Second attempt: should also fail and roll back (no -2 suffix)
    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_failure();

    // No branches with suffixes should exist
    let branches = repo.list_branches();
    assert!(
        !branches.iter().any(|b| b.contains("my-feature-2")),
        "No suffixed branch should exist. Branches: {:?}",
        branches
    );
}

#[test]
fn create_with_successful_commit_still_works() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    repo.create_file("test.txt", "hello");

    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_success();

    assert!(
        repo.current_branch().contains("my-feature"),
        "Should be on the new branch. Current: {}",
        repo.current_branch()
    );
}

#[test]
#[cfg(unix)]
fn create_no_verify_skips_hook_in_commit_first_flow() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    repo.create_file("test.txt", "hello");
    install_failing_pre_commit_hook(&repo);

    let output = repo.run_stax(&["create", "-a", "-m", "skip hook", "--no-verify"]);
    output.assert_success();

    assert!(
        repo.current_branch().contains("skip-hook"),
        "Should be on the created branch. Current: {}",
        repo.current_branch()
    );
    let subject = repo.git(&["log", "-1", "--pretty=%s"]);
    assert!(subject.status.success(), "{}", TestRepo::stderr(&subject));
    assert_eq!(TestRepo::stdout(&subject).trim(), "skip hook");
}

#[test]
#[cfg(unix)]
fn create_no_verify_works_via_bc_alias() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    repo.create_file("alias.txt", "hello");
    install_failing_pre_commit_hook(&repo);

    repo.run_stax(&["bc", "-a", "-m", "alias skip hook", "-n"])
        .assert_success();

    assert!(
        repo.current_branch().contains("alias-skip-hook"),
        "Should be on the created branch. Current: {}",
        repo.current_branch()
    );
}

/// Verifies the commit-first ordering: on success, the new commit lives only
/// on the new branch and `main`'s HEAD does not advance.
#[test]
fn create_commit_lives_only_on_new_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    let main_before = repo.get_commit_sha("main");

    repo.create_file("test.txt", "hello");
    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_success();

    let main_after = repo.get_commit_sha("main");
    assert_eq!(
        main_before, main_after,
        "main's HEAD must not move when `st create -m` splits the commit off",
    );

    let new_branch = repo.current_branch();
    let new_branch_sha = repo.get_commit_sha(&new_branch);
    assert_ne!(
        new_branch_sha, main_before,
        "new branch must point at a new commit, not at main's HEAD",
    );
}

/// `--insert` + `-m` exercises the commit-first flow AND the reparenting path.
/// Verifies children get reparented to the newly-created (and now committed)
/// branch, and the split-off commit still only lives on that branch.
#[test]
fn create_commit_first_with_insert_reparents_children_onto_new_commit() {
    let repo = TestRepo::new();
    repo.run_stax(&["status"]).assert_success();

    // main -> insert-a -> insert-b
    let branches = repo.create_stack(&["insert-a", "insert-b"]);

    // Back on insert-a, create a new branch WITH a commit, inserting above A.
    repo.run_stax(&["checkout", &branches[0]]).assert_success();
    repo.create_file("insert_mid.txt", "new work");

    let insert_a_before = repo.get_commit_sha(&branches[0]);

    let output = repo.run_stax(&["create", "-a", "-m", "mid feature", "--insert"]);
    output.assert_success();

    // insert-a (the parent) must not have moved — the new commit lives only on
    // the inserted branch.
    assert_eq!(
        repo.get_commit_sha(&branches[0]),
        insert_a_before,
        "parent branch must not advance in commit-first + --insert",
    );

    // The previously-direct child of insert-a should now be reparented.
    let stdout = TestRepo::stdout(&output);
    assert!(
        stdout.contains("Reparented"),
        "Expected reparent message, got: {}",
        stdout
    );

    // insert-b's parent should now be the inserted branch.
    repo.run_stax(&["checkout", &branches[1]]).assert_success();
    let b_parent = repo.get_current_parent();
    assert!(
        b_parent
            .as_deref()
            .is_some_and(|p| p.contains("mid-feature")),
        "insert-b should be reparented onto the mid-feature branch, got: {:?}",
        b_parent,
    );
}

/// After a failing pre-commit hook, the user's working-tree changes must still
/// be present so they can fix the hook and retry with the same command — the
/// whole point of commit-first is that retrying is a clean operation.
#[test]
#[cfg(unix)]
fn create_failing_hook_preserves_working_tree_changes() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    repo.create_file("notes.txt", "draft idea");

    install_failing_pre_commit_hook(&repo);

    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_failure();

    // The file the user wanted committed must still be on disk, unchanged.
    let contents = std::fs::read_to_string(repo.path().join("notes.txt"))
        .expect("notes.txt should still exist");
    assert_eq!(contents, "draft idea");

    // Remove hook and retry — must succeed with the original branch name (no
    // `-2` suffix) because nothing was left behind.
    remove_pre_commit_hook(&repo);
    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_success();

    let branch = repo.current_branch();
    assert!(
        branch.contains("my-feature") && !branch.contains("my-feature-2"),
        "Retry should use the original branch name, got: {}",
        branch,
    );
}

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

    install_failing_pre_commit_hook(&repo);

    // Create with just a name (no -m, no commit attempt)
    repo.run_stax(&["create", "my-branch"]).assert_success();

    assert!(
        repo.current_branch().contains("my-branch"),
        "Branch should be created. Current: {}",
        repo.current_branch()
    );
}

#[test]
#[cfg(unix)]
fn create_rollback_then_succeed_after_hook_removed() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();
    repo.create_file("test.txt", "hello");

    // Install failing hook -> create fails
    install_failing_pre_commit_hook(&repo);
    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_failure();

    // Remove hook -> retry should succeed with the same branch name
    remove_pre_commit_hook(&repo);
    repo.run_stax(&["create", "-a", "-m", "my feature"])
        .assert_success();

    let branch = repo.current_branch();
    assert!(
        branch.contains("my-feature"),
        "Should be on the feature branch: {}",
        branch
    );
    assert!(
        !branch.contains("my-feature-2"),
        "Should not have a -2 suffix: {}",
        branch
    );
}

/// `-m` is passed without a positional name so the branch name is derived
/// from the message — when both are given, stax uses the positional name and
/// discards the message, which would suppress the commit entirely.
#[test]
#[cfg(unix)]
fn create_from_other_branch_with_failing_hook_leaves_no_branch() {
    let repo = TestRepo::new();
    repo.run_stax(&["init"]).assert_success();

    // Seed a second branch so we have somewhere to point --from at.
    let branches = repo.create_stack(&["sibling"]);

    // Back on main, install a failing hook and drop an uncommitted file that
    // `-a` will pick up after the branch switch.
    repo.run_stax(&["checkout", "main"]).assert_success();
    install_failing_pre_commit_hook(&repo);
    repo.create_file("wip.txt", "wip");

    let output = repo.run_stax(&[
        "create",
        "--from",
        &branches[0],
        "-a",
        "-m",
        "off sibling feature",
    ]);
    output.assert_failure();

    // The branch must never be created — no orphan, and retries do not drift
    // into a suffixed name.
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|b| b.contains("off-sibling-feature")),
        "new branch must be rolled back after hook failure. Branches: {:?}",
        repo.list_branches(),
    );
    output.assert_stderr_contains("No branch was created");

    repo.run_stax(&[
        "create",
        "--from",
        &branches[0],
        "-a",
        "-m",
        "off sibling feature",
    ])
    .assert_failure();
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|b| b.contains("off-sibling-feature-2")),
        "retry must not drift into a suffixed branch. Branches: {:?}",
        repo.list_branches(),
    );
}

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

    let branches = repo.create_stack(&["sibling-parent"]);
    let parent = &branches[0];
    let parent_before = repo.get_commit_sha(parent);

    repo.run_stax(&["checkout", "main"]).assert_success();
    let main_before = repo.get_commit_sha("main");
    repo.create_file("from-parent.txt", "work for explicit parent\n");

    let output = repo.run_stax(&["create", "--from", parent, "-a", "-m", "off sibling parent"]);
    output.assert_success();
    output.assert_stdout_contains("Committed: off sibling parent");

    assert_eq!(
        repo.get_commit_sha("main"),
        main_before,
        "original branch should not advance when creating from another parent"
    );

    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);

    let stax_parent = repo.get_current_parent();
    assert!(
        stax_parent.as_deref().is_some_and(|p| p == parent),
        "new branch metadata should point at requested parent, got: {:?}",
        stax_parent
    );
}

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

    let branches = repo.create_stack(&["interrupt-parent"]);
    let parent = &branches[0];

    repo.run_stax(&["checkout", "main"]).assert_success();
    repo.create_file("interrupt.txt", "work interrupted during commit\n");
    install_waiting_pre_commit_hook(&repo);

    let output = run_stax_until_hook_then_interrupt(
        &repo,
        &[
            "create",
            "--from",
            parent,
            "-a",
            "-m",
            "interrupted from parent",
        ],
    );
    assert!(
        !output.status.success(),
        "interrupted create should fail\nstdout: {}\nstderr: {}",
        TestRepo::stdout(&output),
        TestRepo::stderr(&output)
    );

    assert_eq!(
        repo.current_branch(),
        "main",
        "interrupted create should restore the original branch"
    );
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|b| b.contains("interrupted-from-parent")),
        "interrupted create must not leave a destination branch. Branches: {:?}",
        repo.list_branches(),
    );

    remove_pre_commit_hook(&repo);
    repo.run_stax(&[
        "create",
        "--from",
        parent,
        "-a",
        "-m",
        "interrupted from parent",
    ])
    .assert_success();

    let branch = repo.current_branch();
    assert!(
        branch.contains("interrupted-from-parent") && !branch.contains("interrupted-from-parent-2"),
        "retry should use the original branch name, got: {}",
        branch
    );
}

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

    let branches = repo.create_stack(&["interrupt-below-parent", "interrupt-below-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(
        "interrupt-below.txt",
        "work interrupted during below commit\n",
    );
    install_waiting_pre_commit_hook(&repo);

    let output = run_stax_until_hook_then_interrupt(
        &repo,
        &["create", "--below", "-a", "-m", "interrupted below parent"],
    );
    assert!(
        !output.status.success(),
        "interrupted create --below should fail\nstdout: {}\nstderr: {}",
        TestRepo::stdout(&output),
        TestRepo::stderr(&output)
    );

    assert_eq!(
        repo.current_branch(),
        *current,
        "interrupted create --below should restore the original branch"
    );
    assert_eq!(
        repo.get_commit_sha(current),
        current_before,
        "interrupted create --below should not advance the original branch"
    );
    assert!(
        !repo
            .list_branches()
            .iter()
            .any(|b| b.contains("interrupted-below-parent")),
        "interrupted create --below must not leave a destination branch. Branches: {:?}",
        repo.list_branches(),
    );

    remove_pre_commit_hook(&repo);
    repo.run_stax(&["create", "--below", "-a", "-m", "interrupted below parent"])
        .assert_success();

    let branch = repo.current_branch();
    assert!(
        branch.contains("interrupted-below-parent")
            && !branch.contains("interrupted-below-parent-2"),
        "retry should use the original branch name, got: {}",
        branch
    );

    repo.run_stax(&["checkout", current]).assert_success();
    let stax_parent = repo.get_current_parent();
    assert!(
        stax_parent
            .as_deref()
            .is_some_and(|p| p.contains("interrupted-below-parent")),
        "original branch should be reparented onto retry branch, got: {:?}",
        stax_parent
    );

    repo.run_stax(&["checkout", parent]).assert_success();
}

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

    let branches = repo.create_stack(&["sibling"]);

    repo.run_stax(&["checkout", "main"]).assert_success();
    install_failing_pre_commit_hook(&repo);
    repo.create_file("wip.txt", "wip");

    let output = repo.run_stax(&[
        "branch",
        "create",
        "--from",
        &branches[0],
        "-a",
        "-m",
        "off sibling no verify",
        "-n",
    ]);
    output.assert_success();

    assert!(
        repo.current_branch().contains("off-sibling-no-verify"),
        "Should be on the created branch. Current: {}",
        repo.current_branch()
    );
    let subject = repo.git(&["log", "-1", "--pretty=%s"]);
    assert!(subject.status.success(), "{}", TestRepo::stderr(&subject));
    assert_eq!(TestRepo::stdout(&subject).trim(), "off sibling no verify");
}