stax 0.50.2

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
//! Enqueue a stack into the forge's merge queue, wait for completion, and sync.
//!
//! Retargets all stack PRs to trunk, enqueues them via the forge's merge
//! queue API (GitHub `enqueuePullRequest`, GitLab merge trains), then
//! polls until all PRs are merged.  Finishes with auto-sync and a desktop
//! notification — the same "land and walk away" experience as Graphite.

use crate::config::Config;
use crate::engine::Stack;
use crate::forge::ForgeClient;
use crate::git::GitRepo;
use crate::progress::LiveTimer;
use crate::remote::{ForgeType, RemoteInfo};
use anyhow::{Context, Result};
use colored::Colorize;
use dialoguer::{theme::ColorfulTheme, Confirm};
use std::process::Command;
use std::time::{Duration, Instant};

#[derive(Debug)]
struct QueueBranchInfo {
    branch: String,
    pr_number: u64,
    /// The original PR base branch (stacked parent) so we can restore it if
    /// enqueue fails after retargeting.
    original_base: String,
}

pub fn run(
    all: bool,
    timeout: u64,
    interval: u64,
    no_sync: bool,
    yes: bool,
    quiet: bool,
) -> Result<()> {
    let repo = GitRepo::open()?;
    let current = repo.current_branch()?;
    let stack = Stack::load(&repo)?;
    let config = Config::load()?;

    if current == stack.trunk {
        if !quiet {
            println!(
                "{}",
                "You are on trunk. Checkout a branch in a stack to merge.".yellow()
            );
        }
        return Ok(());
    }

    if !stack.branches.contains_key(&current) {
        if !quiet {
            println!(
                "{}",
                format!(
                    "Branch '{}' is not tracked. Run 'stax branch track' first.",
                    current
                )
                .yellow()
            );
        }
        return Ok(());
    }

    let remote_info =
        RemoteInfo::from_repo(&repo, &config).context("Failed to read git remote configuration")?;
    if remote_info.forge == ForgeType::Gitea {
        anyhow::bail!(
            "`stax merge --queue` is not supported for Gitea/Forgejo — \
             Gitea does not have a merge queue feature.\n\
             Tip: use `stax merge` or `stax merge --when-ready` instead."
        );
    }

    let client = ForgeClient::new(&remote_info).context(
        "Failed to connect to the configured forge. Check your token and remote configuration.",
    )?;
    let forge_name = remote_info.forge.to_string();
    let queue_term = match remote_info.forge {
        ForgeType::GitLab => "merge train",
        _ => "merge queue",
    };

    let rt = tokio::runtime::Runtime::new()?;
    let _enter = rt.enter();

    let (to_queue, trunk) = calculate_queue_scope(&stack, &current, all);

    let fetch_timer = LiveTimer::maybe_new(!quiet, "Fetching PR info...");

    let open_prs = rt
        .block_on(async { client.list_open_prs_by_head().await })
        .ok();

    let mut branches: Vec<QueueBranchInfo> = Vec::new();
    for branch_name in &to_queue {
        let pr_number = stack
            .branches
            .get(branch_name)
            .and_then(|b| b.pr_number)
            .or_else(|| {
                open_prs
                    .as_ref()
                    .and_then(|prs| prs.get(branch_name))
                    .map(|pr| pr.info.number)
            });

        let original_base = stack
            .branches
            .get(branch_name)
            .and_then(|b| b.parent.clone())
            .unwrap_or_else(|| trunk.clone());

        match pr_number {
            Some(num) => branches.push(QueueBranchInfo {
                branch: branch_name.clone(),
                pr_number: num,
                original_base,
            }),
            None => {
                LiveTimer::maybe_finish_err(fetch_timer, "missing PR");
                anyhow::bail!(
                    "Branch '{}' has no PR. Run 'stax submit' first to create PRs.",
                    branch_name
                );
            }
        }
    }

    LiveTimer::maybe_finish_ok(fetch_timer, "done");

    if branches.is_empty() {
        if !quiet {
            println!("{}", "No branches to enqueue.".yellow());
        }
        return Ok(());
    }

    if !quiet {
        println!();
        print_header(&capitalize(queue_term));
        println!();
        let pr_word = if branches.len() == 1 { "PR" } else { "PRs" };
        println!(
            "Will retarget and enqueue {} {} into {}'s {}:",
            branches.len().to_string().bold(),
            pr_word,
            trunk.cyan(),
            queue_term,
        );
        println!();
        for (idx, branch) in branches.iter().enumerate() {
            println!(
                "  {}. {} (#{})",
                (idx + 1).to_string().bold(),
                branch.branch.bold(),
                branch.pr_number,
            );
        }
        println!();
        println!(
            "{}",
            format!(
                "{} will run CI on the combined changes and merge automatically.",
                forge_name
            )
            .dimmed()
        );
    }

    if !yes {
        let confirm = Confirm::with_theme(&ColorfulTheme::default())
            .with_prompt("Proceed with merge --queue?")
            .default(false)
            .interact()?;

        if !confirm {
            println!("{}", "Aborted.".dimmed());
            return Ok(());
        }
    }

    if !quiet {
        println!();
        print_header("Enqueuing");
    }

    let total = branches.len();
    let mut enqueued: Vec<(String, u64, Option<u32>)> = Vec::new();
    let mut failed: Option<(String, u64, String)> = None;

    for (idx, branch) in branches.iter().enumerate() {
        if !quiet {
            println!(
                "\n[{}/{}] {} (#{})",
                (idx + 1).to_string().cyan(),
                total,
                branch.branch.bold(),
                branch.pr_number
            );
        }

        match rt.block_on(async { client.is_pr_merged(branch.pr_number).await }) {
            Ok(true) => {
                if !quiet {
                    println!("      {} Already merged", "".green());
                }
                continue;
            }
            Ok(false) => {}
            Err(e) => {
                failed = Some((
                    branch.branch.clone(),
                    branch.pr_number,
                    format!("Failed to check merge status: {}", e),
                ));
                break;
            }
        }

        let retarget_timer = LiveTimer::maybe_new(
            !quiet,
            &format!("Retargeting #{} to {}...", branch.pr_number, trunk),
        );

        match rt.block_on(async { client.update_pr_base(branch.pr_number, &trunk).await }) {
            Ok(()) => LiveTimer::maybe_finish_ok(retarget_timer, "done"),
            Err(e) => {
                LiveTimer::maybe_finish_err(retarget_timer, "failed");
                failed = Some((
                    branch.branch.clone(),
                    branch.pr_number,
                    format!("Failed to retarget PR: {}", e),
                ));
                break;
            }
        }

        let enqueue_timer =
            LiveTimer::maybe_new(!quiet, &format!("Enqueuing #{}...", branch.pr_number));

        match rt.block_on(async { client.enqueue_pr(branch.pr_number).await }) {
            Ok(result) => {
                let position = result.merge_queue_entry.and_then(|e| e.position);
                let msg = match position {
                    Some(pos) => format!("queued at position {}", pos),
                    None => "queued".to_string(),
                };
                LiveTimer::maybe_finish_ok(enqueue_timer, &msg);
                enqueued.push((branch.branch.clone(), branch.pr_number, position));
            }
            Err(e) => {
                LiveTimer::maybe_finish_err(enqueue_timer, "failed");

                // Rollback: restore the original PR base since the PR was
                // retargeted to trunk but never actually enqueued.  Use
                // best-effort — if the rollback itself fails we still report
                // the original enqueue error.
                if branch.original_base != trunk {
                    let rollback_timer = LiveTimer::maybe_new(
                        !quiet,
                        &format!(
                            "Rolling back #{} base to {}...",
                            branch.pr_number, branch.original_base
                        ),
                    );
                    match rt.block_on(async {
                        client
                            .update_pr_base(branch.pr_number, &branch.original_base)
                            .await
                    }) {
                        Ok(()) => LiveTimer::maybe_finish_ok(rollback_timer, "restored"),
                        Err(rb_err) => {
                            LiveTimer::maybe_finish_err(rollback_timer, "rollback failed");
                            if !quiet {
                                println!(
                                    "      {} Could not restore original base: {}",
                                    "".yellow(),
                                    rb_err
                                );
                            }
                        }
                    }
                }

                failed = Some((
                    branch.branch.clone(),
                    branch.pr_number,
                    format!("Failed to enqueue: {}", e),
                ));
                break;
            }
        }
    }

    println!();

    if let Some((branch, pr, reason)) = &failed {
        print_header_error(&format!("{} Failed", capitalize(queue_term)));
        println!();
        println!("Progress:");
        for (queued_branch, queued_pr, _) in &enqueued {
            println!(
                "  {} #{} {} → enqueued",
                "".green(),
                queued_pr,
                queued_branch
            );
        }
        println!("  {} #{} {}{}", "".red(), pr, branch, reason);
        println!();
        println!(
            "{}",
            format!("Already enqueued PRs remain in the {}.", queue_term).dimmed()
        );
        println!(
            "{}",
            "Fix the issue and run 'stax merge --queue' to continue.".dimmed()
        );
        return Ok(());
    }

    if enqueued.is_empty() {
        if !quiet {
            println!("{}", "All PRs were already merged.".dimmed());
        }
        return Ok(());
    }

    // --- Wait for the merge queue/train to process ---

    if !quiet {
        println!(
            "{}",
            format!(
                "Enqueued {} {}. Waiting for {} to merge...",
                enqueued.len(),
                if enqueued.len() == 1 { "PR" } else { "PRs" },
                queue_term,
            )
            .dimmed()
        );
        println!();
    }

    let timeout_duration = Duration::from_secs(timeout * 60);
    let poll_interval = Duration::from_secs(interval);
    let start = Instant::now();
    let mut pending: Vec<(String, u64)> =
        enqueued.iter().map(|(b, pr, _)| (b.clone(), *pr)).collect();
    let mut timed_out = false;

    while !pending.is_empty() {
        std::thread::sleep(poll_interval);

        let elapsed = start.elapsed();
        if elapsed > timeout_duration {
            timed_out = true;
            break;
        }

        let mut still_pending = Vec::new();
        for (branch, pr) in &pending {
            match rt.block_on(async { client.is_pr_merged(*pr).await }) {
                Ok(true) => {
                    if !quiet {
                        println!(
                            "  {} #{} {} merged  {}",
                            "".green(),
                            pr,
                            branch,
                            format!("({}s)", elapsed.as_secs()).dimmed()
                        );
                    }
                }
                Ok(false) => still_pending.push((branch.clone(), *pr)),
                Err(_) => still_pending.push((branch.clone(), *pr)),
            }
        }
        pending = still_pending;

        if !pending.is_empty() && !quiet {
            let names: Vec<String> = pending.iter().map(|(_, pr)| format!("#{}", pr)).collect();
            print!(
                "\r{}  {}",
                names.join(", "),
                format!("({}s)", elapsed.as_secs()).dimmed()
            );
            // Flush without newline so the line updates in-place
            use std::io::Write;
            let _ = std::io::stdout().flush();
        }
    }

    // Clear any in-place status line
    if !quiet && !timed_out {
        print!("\r{}\r", " ".repeat(72));
    }
    println!();

    if timed_out {
        if !quiet {
            println!(
                "{} {}",
                "warning:".yellow().bold(),
                format!(
                    "Timed out after {} min waiting for {} to finish.",
                    timeout, queue_term
                )
                .yellow()
            );
            println!("{}", "Run `stax rs` manually to sync once merged.".dimmed());
        }
        return Ok(());
    }

    // --- All merged ---

    print_header_success("Stack Merged");
    println!();
    println!(
        "Merged {} {} into {} via {}:",
        enqueued.len(),
        if enqueued.len() == 1 { "PR" } else { "PRs" },
        trunk.cyan(),
        queue_term,
    );
    for (branch, pr, _) in &enqueued {
        println!("  {} #{} {}", "".green(), pr, branch);
    }

    send_notification(
        "stax merge --queue",
        &format!(
            "Merged {} {} into {}",
            enqueued.len(),
            if enqueued.len() == 1 { "PR" } else { "PRs" },
            trunk
        ),
    );

    if !no_sync {
        if !quiet {
            println!();
            println!("{}", "Running post-merge sync...".dimmed());
        }

        // Release handles before sync opens a fresh repo view.
        drop(rt);
        drop(client);
        drop(repo);

        if let Err(err) = crate::commands::sync::run(
            false, // restack
            false, // prune
            false, // full
            true,  // delete merged branches
            false, // delete upstream-gone
            true,  // force
            false, // safe
            false, // continue
            quiet, false, // verbose
            false, // auto_stash_pop
        ) {
            if !quiet {
                println!();
                println!(
                    "{} {}",
                    "warning:".yellow().bold(),
                    format!("post-merge sync failed: {}", err).yellow()
                );
                println!(
                    "{}",
                    "Run 'stax rs --force' manually to sync local state.".dimmed()
                );
            }
        }
    }

    Ok(())
}

fn calculate_queue_scope(stack: &Stack, current: &str, all: bool) -> (Vec<String>, String) {
    let mut to_queue = stack.ancestors(current);
    to_queue.reverse();
    to_queue.retain(|b| b != &stack.trunk);
    to_queue.push(current.to_string());

    if all {
        to_queue.extend(stack.descendants(current));
    }

    (to_queue, stack.trunk.clone())
}

fn send_notification(title: &str, message: &str) {
    if cfg!(target_os = "macos") {
        let script = format!(
            r#"display notification "{}" with title "{}""#,
            message.replace('"', "\\\""),
            title.replace('"', "\\\""),
        );
        let _ = Command::new("osascript").args(["-e", &script]).output();
    }
}

fn capitalize(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(c) => c.to_uppercase().to_string() + chars.as_str(),
    }
}

// --- Display helpers (same as merge_remote.rs) ---

fn strip_ansi(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut in_escape = false;
    for c in s.chars() {
        if c == '\x1b' {
            in_escape = true;
            continue;
        }
        if in_escape {
            if c == 'm' {
                in_escape = false;
            }
            continue;
        }
        result.push(c);
    }
    result
}

fn display_width(s: &str) -> usize {
    let stripped = strip_ansi(s);
    stripped
        .chars()
        .map(|c| match c {
            '\x00'..='\x1f' | '\x7f' => 0,
            '\x20'..='\x7e' => 1,
            '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | '' | ''
            | '' | '' | '' => 1,
            '' | '' | '' | '' => 1,
            '' | '' | '' | '' => 1,
            _ => 2,
        })
        .sum()
}

fn print_header(title: &str) {
    let width: usize = 56;
    let title_width = display_width(title);
    let padding = width.saturating_sub(title_width) / 2;
    println!("{}", "".repeat(width));
    println!(
        "{}{}{}",
        " ".repeat(padding),
        title.bold(),
        " ".repeat(width.saturating_sub(padding + title_width))
    );
    println!("{}", "".repeat(width));
}

fn print_header_success(title: &str) {
    let width: usize = 56;
    let full_title = format!("{}", title);
    let title_width = display_width(&full_title);
    let padding = width.saturating_sub(title_width) / 2;
    println!("{}", "".repeat(width));
    println!(
        "{}{}{}",
        " ".repeat(padding),
        full_title.green().bold(),
        " ".repeat(width.saturating_sub(padding + title_width))
    );
    println!("{}", "".repeat(width));
}

fn print_header_error(title: &str) {
    let width: usize = 56;
    let full_title = format!("{}", title);
    let title_width = display_width(&full_title);
    let padding = width.saturating_sub(title_width) / 2;
    println!("{}", "".repeat(width));
    println!(
        "{}{}{}",
        " ".repeat(padding),
        full_title.red().bold(),
        " ".repeat(width.saturating_sub(padding + title_width))
    );
    println!("{}", "".repeat(width));
}