stax 0.96.7

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
use crate::common::{OutputAssertions, TestRepo};
use std::fs;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

struct BranchSnapshot {
    name: String,
    before: String,
}

struct RemoteSyncedParentStack {
    submitted: BranchSnapshot,
    leaf: Option<BranchSnapshot>,
    parent_after: String,
}

fn remote_synced_parent_stack(repo: &TestRepo, names: &[&str]) -> RemoteSyncedParentStack {
    assert!(
        names.len() >= 2,
        "fixture requires at least a parent and submitted branch"
    );

    let branches = repo.create_stack(names);
    let parent = branches[0].clone();
    let submitted = BranchSnapshot {
        name: branches[1].clone(),
        before: repo.get_commit_sha(&branches[1]),
    };
    let leaf = branches.get(2).map(|branch| BranchSnapshot {
        name: branch.clone(),
        before: repo.get_commit_sha(branch),
    });

    repo.run_stax(&["checkout", &parent]).assert_success();
    repo.git(&["push", "-u", "origin", &parent])
        .assert_success();

    repo.create_file(&format!("{}-remote-update.txt", names[0]), "parent v2\n");
    repo.commit("Parent update");
    repo.git(&["push", "-u", "origin", &parent])
        .assert_success();
    let parent_after = repo.get_commit_sha(&parent);

    repo.run_stax(&["checkout", &submitted.name])
        .assert_success();

    RemoteSyncedParentStack {
        submitted,
        leaf,
        parent_after,
    }
}

fn fetch_origin(repo: &TestRepo) {
    repo.git(&["fetch", "origin"]).assert_success();
}

fn run_git(cwd: &Path, args: &[&str]) -> String {
    let output = Command::new("git")
        .args(args)
        .current_dir(cwd)
        .output()
        .expect("run git");
    output.assert_success();
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

fn push_remote_only_branch(repo: &TestRepo, branch: &str, file: &str, content: &str) {
    repo.git(&["checkout", "-B", branch, "main"])
        .assert_success();
    repo.create_file(file, content);
    repo.commit(&format!("Commit {branch}"));
    repo.git(&["push", "-u", "origin", branch]).assert_success();
    repo.git(&["checkout", "main"]).assert_success();
    repo.git(&["branch", "-D", branch]).assert_success();
}

fn remote_branch_sha(repo: &TestRepo, branch: &str) -> String {
    let output = repo.git(&["ls-remote", "origin", &format!("refs/heads/{branch}")]);
    output.assert_success();
    TestRepo::stdout(&output)
        .split_whitespace()
        .next()
        .expect("remote branch sha")
        .to_string()
}

fn simulate_remote_owner_update(
    repo: &TestRepo,
    branch: &str,
    file: &str,
    content: &str,
) -> String {
    let remote_path = repo.remote_path().expect("remote path");
    let clone = TempDir::new().expect("temp clone");

    run_git(clone.path(), &["clone", remote_path.to_str().unwrap(), "."]);
    run_git(
        clone.path(),
        &["checkout", "-B", branch, &format!("origin/{branch}")],
    );
    run_git(clone.path(), &["config", "user.email", "other@test.com"]);
    run_git(clone.path(), &["config", "user.name", "Other User"]);
    fs::write(clone.path().join(file), content).expect("write remote update");
    run_git(clone.path(), &["add", "-A"]);
    run_git(clone.path(), &["commit", "-m", "Update imported branch"]);
    run_git(clone.path(), &["push", "origin", branch]);
    run_git(clone.path(), &["rev-parse", branch])
}

fn assert_contains_commit(repo: &TestRepo, ancestor: &str, descendant: &str, message: &str) {
    let output = repo.git(&["merge-base", "--is-ancestor", ancestor, descendant]);
    assert!(
        output.status.success(),
        "{message}\nstdout: {}\nstderr: {}",
        TestRepo::stdout(&output),
        TestRepo::stderr(&output)
    );
}

fn assert_does_not_contain_commit(
    repo: &TestRepo,
    ancestor: &str,
    descendant: &str,
    message: &str,
) {
    let output = repo.git(&["merge-base", "--is-ancestor", ancestor, descendant]);
    assert!(
        !output.status.success(),
        "{message}\nstdout: {}\nstderr: {}",
        TestRepo::stdout(&output),
        TestRepo::stderr(&output)
    );
}

fn assert_no_temporary_submit_refs(repo: &TestRepo) {
    let temp_refs = repo.git(&["for-each-ref", "--format=%(refname)", "refs/stax/submit"]);
    assert!(
        TestRepo::stdout(&temp_refs).trim().is_empty(),
        "temporary submit refs should be cleaned up"
    );
}

fn assert_no_temporary_submit_worktrees(repo: &TestRepo) {
    let worktrees = repo.git(&["worktree", "list", "--porcelain"]);
    worktrees.assert_success();
    assert!(
        !TestRepo::stdout(&worktrees).contains("stax-submit-"),
        "temporary submit worktrees should be cleaned up"
    );
}

fn write_test_config(home: &Path, api_base_url: &str) {
    let config_dir = home.join(".config").join("stax");
    fs::create_dir_all(&config_dir).expect("failed to create test config dir");
    fs::write(
        config_dir.join("config.toml"),
        format!("[remote]\napi_base_url = \"{api_base_url}\"\n\n[submit]\nstack_links = \"off\"\n"),
    )
    .expect("failed to write test config");
}

async fn mock_github_pr_create(mock_server: &MockServer) {
    Mock::given(method("GET"))
        .and(path("/repos/test-owner/test-repo/pulls"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([])))
        .mount(mock_server)
        .await;

    Mock::given(method("POST"))
        .and(path("/repos/test-owner/test-repo/pulls"))
        .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({
            "url": "https://api.github.com/repos/test-owner/test-repo/pulls/42",
            "id": 42,
            "number": 42,
            "state": "open",
            "title": "created",
            "body": "",
            "draft": false,
            "head": { "ref": "created", "sha": "aaaa", "label": "test-owner:created" },
            "base": { "ref": "main", "sha": "bbbb" },
            "html_url": "https://github.com/test-owner/test-repo/pull/42"
        })))
        .mount(mock_server)
        .await;

    Mock::given(method("GET"))
        .and(path("/repos/test-owner/test-repo/issues/42/comments"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!([])))
        .mount(mock_server)
        .await;

    Mock::given(method("GET"))
        .and(path("/repos/test-owner/test-repo/pulls/42"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "url": "https://api.github.com/repos/test-owner/test-repo/pulls/42",
            "id": 42,
            "number": 42,
            "state": "open",
            "title": "created",
            "body": "",
            "draft": false,
            "head": { "ref": "created", "sha": "aaaa", "label": "test-owner:created" },
            "base": { "ref": "main", "sha": "bbbb" },
            "html_url": "https://github.com/test-owner/test-repo/pull/42"
        })))
        .mount(mock_server)
        .await;
}

#[test]
fn branch_submit_temporarily_restacks_when_parent_is_remote_synced() {
    let repo = TestRepo::new_with_remote();
    repo.configure_github_like_submit_remote();

    let stack = remote_synced_parent_stack(&repo, &["temp-parent", "temp-child"]);
    repo.create_file("child-extra.txt", "manual extra child commit\n");
    repo.commit("Manual extra child commit");
    let local_child_before = repo.get_commit_sha(&stack.submitted.name);

    repo.run_stax(&["branch", "submit", "--no-pr", "--yes"])
        .assert_success();

    assert_eq!(
        repo.get_commit_sha(&stack.submitted.name),
        local_child_before,
        "scoped submit should not move the local stale child"
    );

    fetch_origin(&repo);
    let remote_child = format!("origin/{}", stack.submitted.name);
    assert_ne!(
        repo.get_commit_sha(&remote_child),
        local_child_before,
        "remote child should be the temporary rebased head"
    );
    repo.git(&["show", &format!("{}:child-extra.txt", remote_child)])
        .assert_success();

    assert_contains_commit(
        &repo,
        &stack.parent_after,
        &remote_child,
        "remote child should include the synced parent update",
    );
    assert_does_not_contain_commit(
        &repo,
        &stack.parent_after,
        &stack.submitted.name,
        "local child should still need restack",
    );
    assert_no_temporary_submit_refs(&repo);
    assert_no_temporary_submit_worktrees(&repo);
}

#[tokio::test]
async fn upstack_submit_pr_defaults_exclude_parent_commits_from_temporary_parent() {
    let mock_server = MockServer::start().await;
    mock_github_pr_create(&mock_server).await;

    let repo = TestRepo::new_with_remote();
    let home = repo.clean_home();
    write_test_config(Path::new(&home), &mock_server.uri());
    repo.configure_github_like_submit_remote();

    let stack =
        remote_synced_parent_stack(&repo, &["temp-pr-parent", "temp-pr-middle", "temp-pr-leaf"]);
    let leaf = stack.leaf.as_ref().expect("fixture should include a leaf");

    let output = repo.run_stax_with_env(
        &[
            "upstack",
            "submit",
            "--yes",
            "--no-prompt",
            "--publish",
            "--no-template",
        ],
        &[("STAX_GITHUB_TOKEN", "test-token")],
    );
    assert!(output.status.success(), "{}", TestRepo::stderr(&output));

    let requests = mock_server.received_requests().await.unwrap();
    let payloads = requests
        .iter()
        .filter(|request| {
            request.method.as_str() == "POST"
                && request.url.path() == "/repos/test-owner/test-repo/pulls"
        })
        .map(|request| serde_json::from_slice::<serde_json::Value>(&request.body).unwrap())
        .collect::<Vec<_>>();

    let middle = payloads
        .iter()
        .find(|payload| payload["head"].as_str() == Some(stack.submitted.name.as_str()))
        .expect("missing middle PR create payload");
    assert_eq!(middle["title"], "Commit for temp-pr-middle");
    assert!(
        !middle["body"]
            .as_str()
            .unwrap_or_default()
            .contains("Parent update"),
        "middle PR body should not include excluded parent commits: {middle}"
    );

    let leaf_payload = payloads
        .iter()
        .find(|payload| payload["head"].as_str() == Some(leaf.name.as_str()))
        .expect("missing leaf PR create payload");
    assert_eq!(leaf_payload["title"], "Commit for temp-pr-leaf");
    let leaf_body = leaf_payload["body"].as_str().unwrap_or_default();
    assert!(
        !leaf_body.contains("Parent update"),
        "leaf PR body should not include excluded grandparent commits: {leaf_payload}"
    );
    assert!(
        !leaf_body.contains("Commit for temp-pr-middle"),
        "leaf PR body should not include its temporary parent commit: {leaf_payload}"
    );
}

#[tokio::test]
async fn stack_submit_pr_defaults_exclude_rewritten_parent_commits() {
    let mock_server = MockServer::start().await;
    mock_github_pr_create(&mock_server).await;

    let repo = TestRepo::new_with_remote();
    let home = repo.clean_home();
    write_test_config(Path::new(&home), &mock_server.uri());
    repo.configure_github_like_submit_remote();

    let branches = repo.create_stack(&["rewrite-parent", "rewrite-child"]);
    let parent = &branches[0];
    let child = &branches[1];

    repo.run_stax(&["checkout", parent]).assert_success();
    repo.create_file("rewrite-parent.txt", "rewritten parent\n");
    repo.git(&["add", "-A"]).assert_success();
    repo.git(&["commit", "--amend", "-m", "Rewritten parent"])
        .assert_success();

    repo.run_stax(&["checkout", child]).assert_success();
    let output = repo.run_stax_with_env(
        &[
            "submit",
            "--yes",
            "--no-prompt",
            "--publish",
            "--no-template",
        ],
        &[("STAX_GITHUB_TOKEN", "test-token")],
    );
    assert!(output.status.success(), "{}", TestRepo::stderr(&output));

    let requests = mock_server.received_requests().await.unwrap();
    let child_payload = requests
        .iter()
        .filter(|request| {
            request.method.as_str() == "POST"
                && request.url.path() == "/repos/test-owner/test-repo/pulls"
        })
        .map(|request| serde_json::from_slice::<serde_json::Value>(&request.body).unwrap())
        .find(|payload| payload["head"].as_str() == Some(child.as_str()))
        .expect("missing child PR create payload");

    assert_eq!(child_payload["title"], "Commit for rewrite-child");
    assert!(
        !child_payload["body"]
            .as_str()
            .unwrap_or_default()
            .contains("Commit for rewrite-parent"),
        "child PR body should not include stale parent commits: {child_payload}"
    );
}

#[tokio::test]
async fn branch_submit_pr_defaults_exclude_imported_parent_commits_after_temporary_restack() {
    let mock_server = MockServer::start().await;
    mock_github_pr_create(&mock_server).await;

    let repo = TestRepo::new_with_remote();
    let home = repo.clean_home();
    write_test_config(Path::new(&home), &mock_server.uri());
    repo.configure_github_like_submit_remote();

    push_remote_only_branch(
        &repo,
        "imported-pr-parent",
        "imported-parent.txt",
        "remote parent\n",
    );
    repo.run_stax(&["get", "imported-pr-parent"])
        .assert_success();
    repo.run_stax(&["create", "imported-pr-child"])
        .assert_success();
    repo.create_file("child.txt", "child\n");
    repo.commit("Child change");
    let child = repo.current_branch();

    let imported_parent_remote_after = simulate_remote_owner_update(
        &repo,
        "imported-pr-parent",
        "imported-parent-update.txt",
        "remote parent update\n",
    );
    fetch_origin(&repo);
    repo.git(&[
        "branch",
        "-f",
        "imported-pr-parent",
        "origin/imported-pr-parent",
    ])
    .assert_success();

    let output = repo.run_stax_with_env(
        &[
            "branch",
            "submit",
            "--yes",
            "--no-prompt",
            "--publish",
            "--no-template",
        ],
        &[("STAX_GITHUB_TOKEN", "test-token")],
    );
    assert!(output.status.success(), "{}", TestRepo::stderr(&output));
    assert_eq!(
        remote_branch_sha(&repo, "imported-pr-parent"),
        imported_parent_remote_after,
        "branch submit should not push or rewrite the imported parent remote"
    );

    let requests = mock_server.received_requests().await.unwrap();
    let payload = requests
        .iter()
        .find(|request| {
            request.method.as_str() == "POST"
                && request.url.path() == "/repos/test-owner/test-repo/pulls"
        })
        .map(|request| serde_json::from_slice::<serde_json::Value>(&request.body).unwrap())
        .expect("missing child PR create payload");

    assert_eq!(payload["head"], child);
    assert_eq!(payload["title"], "Child change");
    let body = payload["body"].as_str().unwrap_or_default();
    assert!(
        !body.contains("Commit imported-pr-parent"),
        "child PR body should not include imported parent commit: {payload}"
    );
    assert!(
        !body.contains("Update imported branch"),
        "child PR body should not include imported parent update: {payload}"
    );
}

#[test]
fn upstack_submit_temporarily_restacks_descendants() {
    let repo = TestRepo::new_with_remote();
    repo.configure_github_like_submit_remote();

    let stack =
        remote_synced_parent_stack(&repo, &["temp-us-parent", "temp-us-middle", "temp-us-leaf"]);
    let leaf = stack.leaf.as_ref().expect("fixture should include a leaf");

    repo.run_stax(&["upstack", "submit", "--no-pr", "--yes"])
        .assert_success();

    assert_eq!(
        repo.get_commit_sha(&stack.submitted.name),
        stack.submitted.before
    );
    assert_eq!(repo.get_commit_sha(&leaf.name), leaf.before);

    fetch_origin(&repo);
    let remote_middle = format!("origin/{}", stack.submitted.name);
    let remote_leaf = format!("origin/{}", leaf.name);
    assert_ne!(repo.get_commit_sha(&remote_middle), stack.submitted.before);
    assert_ne!(repo.get_commit_sha(&remote_leaf), leaf.before);

    assert_contains_commit(
        &repo,
        &stack.parent_after,
        &remote_middle,
        "remote middle should include the synced parent update",
    );
    assert_contains_commit(
        &repo,
        &remote_middle,
        &remote_leaf,
        "remote leaf should be pushed on top of the temporary middle head",
    );
    assert_no_temporary_submit_refs(&repo);
    assert_no_temporary_submit_worktrees(&repo);
}

#[test]
fn branch_submit_cleans_up_temporary_state_when_temporary_restack_conflicts() {
    let repo = TestRepo::new_with_remote();
    repo.configure_github_like_submit_remote();

    repo.run_stax(&["bc", "temp-conflict-parent"])
        .assert_success();
    let parent = repo.current_branch();
    repo.create_file("conflict.txt", "parent v1\n");
    repo.commit("Parent commit");
    repo.git(&["push", "-u", "origin", &parent])
        .assert_success();

    repo.run_stax(&["bc", "temp-conflict-child"])
        .assert_success();
    repo.create_file("conflict.txt", "child edit\n");
    repo.commit("Child conflicting commit");
    let child = repo.current_branch();

    repo.run_stax(&["checkout", &parent]).assert_success();
    repo.create_file("conflict.txt", "parent v2\n");
    repo.commit("Parent update");
    repo.git(&["push", "-u", "origin", &parent])
        .assert_success();

    repo.run_stax(&["checkout", &child]).assert_success();
    repo.run_stax(&["branch", "submit", "--no-pr", "--yes"])
        .assert_failure();

    assert_no_temporary_submit_refs(&repo);
    assert_no_temporary_submit_worktrees(&repo);
}