stax 0.78.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
//! Branch fold command integration tests.
//!
//! Covers the post-graphite-parity rewrite: commits are preserved (not
//! squashed), descendants of the folded branch are reparented onto the
//! surviving branch, siblings are rebased, and `--keep` lets the current
//! branch's name survive while the parent ref is deleted.

mod common;

use common::{OutputAssertions, TestRepo};

fn combined(output: &std::process::Output) -> String {
    format!("{}{}", TestRepo::stdout(output), TestRepo::stderr(output))
}

// =============================================================================
// Preconditions — fold bails before the confirmation prompt
// =============================================================================

#[test]
fn test_fold_into_trunk_not_allowed() {
    let repo = TestRepo::new();
    repo.create_stack(&["feature-1"]);

    let output = repo.run_stax(&["branch", "fold"]);
    assert!(
        combined(&output).contains("Cannot fold into trunk"),
        "expected 'Cannot fold into trunk' message, got: {}",
        combined(&output)
    );
}

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

    repo.git(&["checkout", "-b", "untracked-branch"]);
    repo.create_file("test.txt", "content");
    repo.commit("Untracked commit");

    let output = repo.run_stax(&["branch", "fold"]);
    output.assert_failure();
    assert!(
        combined(&output).contains("not tracked"),
        "expected 'not tracked' in: {}",
        combined(&output)
    );
}

#[test]
fn test_fold_when_on_trunk_bails() {
    let repo = TestRepo::new();
    repo.create_stack(&["feature-1"]);
    repo.run_stax(&["t"]);
    assert_eq!(repo.current_branch(), "main");

    let output = repo.run_stax(&["branch", "fold"]);
    output.assert_failure();
    assert!(
        combined(&output).contains("Cannot fold trunk"),
        "expected 'Cannot fold trunk' in: {}",
        combined(&output)
    );
}

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

    repo.run_stax(&["bc", "feature-1"]);
    let feature1 = repo.current_branch();
    repo.create_file("f1.txt", "content");
    repo.commit("Feature 1");

    // Empty branch off feature-1 (same SHA, no kids, no siblings)
    repo.git(&["checkout", "-b", "empty-branch"]);
    repo.run_stax(&["branch", "track", "--parent", &feature1]);

    let output = repo.run_stax(&["branch", "fold", "--yes"]);
    output.assert_success();
    assert!(
        combined(&output).contains("Nothing to fold"),
        "expected 'Nothing to fold' in: {}",
        combined(&output)
    );
}

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

    repo.run_stax(&["bc", "feature-1"]);
    repo.create_file("f1.txt", "content");
    repo.commit("Feature 1");

    repo.run_stax(&["bc", "feature-2"]);
    repo.create_file("f2.txt", "content");
    repo.commit("Feature 2");

    repo.create_file("dirty.txt", "uncommitted");

    let output = repo.run_stax(&["branch", "fold", "--yes"]);
    output.assert_failure();
    assert!(
        combined(&output).contains("uncommitted"),
        "expected 'uncommitted' in: {}",
        combined(&output)
    );
}

// =============================================================================
// Help / alias surface
// =============================================================================

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

    for args in [
        &["branch", "fold", "--help"][..],
        &["b", "f", "--help"][..],
        &["fold", "--help"][..],
    ] {
        let output = repo.run_stax(args);
        output.assert_success();
        let stdout = TestRepo::stdout(&output);
        assert!(
            stdout.contains("--keep") || stdout.contains("-k"),
            "expected --keep in `{:?}` help: {}",
            args,
            stdout
        );
    }
}

// =============================================================================
// Behaviour
// =============================================================================

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

    repo.run_stax(&["bc", "A"]);
    let a = repo.current_branch();
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    repo.run_stax(&["branch", "fold", "--yes"]).assert_success();

    let branches = repo.list_branches();
    assert!(
        !branches.iter().any(|n| n == &b),
        "B should be deleted, branches: {:?}",
        branches
    );
    assert_eq!(repo.current_branch(), a, "should end up on A");
    assert!(repo.path().join("a.txt").exists(), "a.txt should be on A");
    assert!(
        repo.path().join("b.txt").exists(),
        "b.txt (from B) should now live on A — commits preserved, not squashed"
    );
}

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

    repo.run_stax(&["bc", "A"]);
    let a = repo.current_branch();
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    repo.run_stax(&["branch", "fold", "--keep", "--yes"])
        .assert_success();

    let branches = repo.list_branches();
    assert!(
        !branches.iter().any(|n| n == &a),
        "--keep should delete the parent ref '{}', branches: {:?}",
        a,
        branches
    );
    assert!(
        branches.iter().any(|n| n == &b),
        "--keep should preserve '{}', branches: {:?}",
        b,
        branches
    );
    assert_eq!(repo.current_branch(), b);
    assert_eq!(
        repo.get_current_parent().as_deref(),
        Some("main"),
        "B's parent should now be the grandparent (main)"
    );
}

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

    // main → A → B → C
    repo.run_stax(&["bc", "A"]);
    let a = repo.current_branch();
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    repo.run_stax(&["bc", "C"]);
    let c = repo.current_branch();
    repo.create_file("c.txt", "from C");
    repo.commit("C commit");

    repo.run_stax(&["checkout", &b]);
    repo.run_stax(&["branch", "fold", "--yes"]).assert_success();

    assert!(
        !repo.list_branches().iter().any(|n| n == &b),
        "B should be deleted"
    );

    let json = repo.get_status_json();
    let c_parent = json["branches"]
        .as_array()
        .unwrap()
        .iter()
        .find(|br| br["name"].as_str() == Some(&c))
        .expect("C should still be tracked")["parent"]
        .as_str()
        .unwrap_or("")
        .to_string();
    assert_eq!(c_parent, a, "C's parent should be A after fold");
}

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

    // main → A → B → C, then `fold --keep` from B.
    // Survivor=B; A is deleted. C's parent_branch_name was B (now survivor)
    // and stays B — kids of `current` need no metadata change in --keep mode.
    repo.run_stax(&["bc", "A"]);
    let a = repo.current_branch();
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    repo.run_stax(&["bc", "C"]);
    let c = repo.current_branch();
    repo.create_file("c.txt", "from C");
    repo.commit("C commit");

    repo.run_stax(&["checkout", &b]);
    repo.run_stax(&["branch", "fold", "--keep", "--yes"])
        .assert_success();

    let branches = repo.list_branches();
    assert!(branches.iter().any(|n| n == &b), "B should survive --keep");
    assert!(
        !branches.iter().any(|n| n == &a),
        "A ('{}') should be deleted, branches: {:?}",
        a,
        branches
    );

    let json = repo.get_status_json();
    let c_parent = json["branches"]
        .as_array()
        .unwrap()
        .iter()
        .find(|br| br["name"].as_str() == Some(&c))
        .expect("C should still be tracked")["parent"]
        .as_str()
        .unwrap_or("")
        .to_string();
    assert_eq!(c_parent, b, "C's parent should still be B (the survivor)");
}

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

    // main → A → B  (target of fold)
    //          → S  (sibling — touches different files so rebase is conflict-free)
    repo.run_stax(&["bc", "A"]);
    let a = repo.current_branch();
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    repo.run_stax(&["checkout", &a]);
    repo.run_stax(&["bc", "S"]);
    let s = repo.current_branch();
    repo.create_file("s.txt", "from S");
    repo.commit("S commit");

    repo.run_stax(&["checkout", &b]);
    repo.run_stax(&["branch", "fold", "--yes"]).assert_success();

    let branches = repo.list_branches();
    assert!(
        branches.iter().any(|n| n == &s),
        "sibling S should survive, branches: {:?}",
        branches
    );

    repo.run_stax(&["checkout", &s]);
    assert!(
        repo.path().join("s.txt").exists(),
        "S's own file should remain"
    );
    assert!(
        repo.path().join("b.txt").exists(),
        "S should now have B's commits underneath (rebased onto survivor)"
    );

    let json = repo.get_status_json();
    let s_parent = json["branches"]
        .as_array()
        .unwrap()
        .iter()
        .find(|br| br["name"].as_str() == Some(&s))
        .expect("S should be tracked")["parent"]
        .as_str()
        .unwrap_or("")
        .to_string();
    assert_eq!(s_parent, a, "S's parent should be the survivor (A)");
}

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

    repo.run_stax(&["bc", "A"]);
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    repo.run_stax(&["fold", "--yes"]).assert_success();
    assert!(
        !repo.list_branches().iter().any(|n| n == &b),
        "B should be deleted via top-level fold"
    );
}

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

    repo.run_stax(&["bc", "A"]);
    let a = repo.current_branch();
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    let b_sha = repo.get_commit_sha(&b);
    let a_sha = repo.get_commit_sha(&a);

    repo.run_stax(&["branch", "fold", "--yes"]).assert_success();
    repo.run_stax(&["undo", "--yes"]).assert_success();

    assert!(
        repo.list_branches().iter().any(|n| n == &b),
        "undo should restore B"
    );
    assert_eq!(repo.get_commit_sha(&b), b_sha, "B back to original SHA");
    assert_eq!(repo.get_commit_sha(&a), a_sha, "A back to original SHA");
}

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

    repo.run_stax(&["bc", "A"]);
    let a = repo.current_branch();
    repo.create_file("a.txt", "from A");
    repo.commit("A commit");

    repo.run_stax(&["bc", "B"]);
    let b = repo.current_branch();
    repo.create_file("b.txt", "from B");
    repo.commit("B commit");

    // Inject pr_info onto B by writing a metadata blob directly. Avoids
    // needing a real GitHub remote in the test.
    let pr_json = format!(
        r#"{{"parentBranchName":"{}","parentBranchRevision":"{}","prInfo":{{"number":4242,"state":"OPEN","isDraft":false}}}}"#,
        a,
        repo.get_commit_sha(&a)
    );
    let mut child = std::process::Command::new("git")
        .args(["hash-object", "-w", "--stdin"])
        .current_dir(repo.path())
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    use std::io::Write;
    child
        .stdin
        .as_mut()
        .unwrap()
        .write_all(pr_json.as_bytes())
        .unwrap();
    let out = child.wait_with_output().unwrap();
    let blob_hash = String::from_utf8(out.stdout).unwrap().trim().to_string();
    repo.git(&[
        "update-ref",
        &format!("refs/branch-metadata/{}", b),
        &blob_hash,
    ]);

    let output = repo.run_stax(&["branch", "fold", "--yes"]);
    output.assert_success();
    let out = combined(&output);
    assert!(
        out.contains("PR #4242"),
        "expected orphaned-PR hint to mention PR #4242, got: {}",
        out
    );
    assert!(
        out.contains("gh pr close 4242"),
        "expected gh pr close hint, got: {}",
        out
    );
}