stax 0.91.1

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
use crate::engine::Stack;
use crate::git::GitRepo;
use anyhow::{Result, bail};
use colored::Colorize;
use std::collections::HashMap;
use std::io::Write;
use std::path::Path;
use std::process::Command;

/// Result of file-level attribution: which branch owns each changed file.
struct AbsorbPlan {
    /// Files grouped by target branch name.
    groups: Vec<(String, Vec<String>)>,
    /// Files that could not be attributed to any stack branch.
    unattributed: Vec<String>,
}

pub fn run(dry_run: bool, all: bool) -> Result<()> {
    let repo = GitRepo::open()?;
    let stack = Stack::load(&repo)?;
    let current = repo.current_branch()?;

    if current == stack.trunk {
        bail!("Cannot absorb on trunk. Checkout a stacked branch first.");
    }

    let workdir = repo.workdir()?;

    // Stage all if requested
    if all {
        let status = Command::new("git")
            .args(["add", "-A"])
            .current_dir(workdir)
            .status()?;
        if !status.success() {
            bail!("Failed to stage changes");
        }
    }

    // Get list of staged files
    let staged_output = Command::new("git")
        .args(["diff", "--cached", "--name-only"])
        .current_dir(workdir)
        .output()?;

    if !staged_output.status.success() {
        bail!("Failed to list staged files");
    }

    let staged_files: Vec<String> = String::from_utf8_lossy(&staged_output.stdout)
        .lines()
        .filter(|l| !l.is_empty())
        .map(|s| s.to_string())
        .collect();

    if staged_files.is_empty() {
        println!(
            "{}",
            "No staged changes to absorb. Stage files or use `st absorb -a`.".yellow()
        );
        return Ok(());
    }

    // Build (branch, parent) pairs from the Stack (no extra metadata reads needed)
    let ancestors = stack.ancestors(&current);
    let mut stack_branches: Vec<String> = ancestors.into_iter().rev().collect();
    stack_branches.push(current.clone());
    stack_branches.retain(|b| *b != stack.trunk);

    if stack_branches.is_empty() {
        bail!("No stack branches found above trunk.");
    }

    let branch_boundaries: Vec<(String, String)> = stack_branches
        .iter()
        .map(|branch| {
            let parent = stack
                .branches
                .get(branch)
                .and_then(|b| b.parent.clone())
                .unwrap_or_else(|| stack.trunk.clone());
            (branch.clone(), parent)
        })
        .collect();

    // Attribute each file to a branch
    let plan = attribute_files(workdir, &staged_files, &branch_boundaries)?;

    // Display the plan
    if plan.groups.is_empty() && plan.unattributed.is_empty() {
        println!("{}", "No changes to absorb.".yellow());
        return Ok(());
    }

    println!("{}", "Absorb plan:".bold());
    for (branch, files) in &plan.groups {
        let marker = if *branch == current {
            " (current)".dimmed().to_string()
        } else {
            String::new()
        };
        println!("  {} {}{}", "→".green(), branch.cyan(), marker);
        for file in files {
            println!("    {}", file);
        }
    }
    if !plan.unattributed.is_empty() {
        println!(
            "  {} {}",
            "?".yellow(),
            "unattributed (staying staged)".dimmed()
        );
        for file in &plan.unattributed {
            println!("    {}", file);
        }
    }
    println!();

    if dry_run {
        println!("{}", "Dry run — no changes made.".dimmed());
        return Ok(());
    }

    // Check if there are changes targeting other branches (not just current)
    let has_other_targets = plan.groups.iter().any(|(b, _)| *b != current);

    if !has_other_targets {
        println!(
            "{}",
            "All changes already target the current branch. Nothing to absorb.".dimmed()
        );
        return Ok(());
    }

    // Extract patches as raw bytes (preserves binary diffs)
    let mut patches: Vec<(String, Vec<u8>, Vec<String>)> = Vec::new();

    for (branch, files) in &plan.groups {
        if *branch == current {
            continue;
        }

        let mut diff_args = vec!["diff".to_string(), "--cached".to_string(), "--".to_string()];
        diff_args.extend(files.iter().cloned());

        let diff_output = Command::new("git")
            .args(&diff_args)
            .current_dir(workdir)
            .output()?;

        if diff_output.status.success() && !diff_output.stdout.is_empty() {
            patches.push((branch.clone(), diff_output.stdout, files.clone()));
        }
    }

    if patches.is_empty() {
        println!("{}", "No changes to move to other branches.".dimmed());
        return Ok(());
    }

    // Stash current state (include untracked files with -u)
    let stash_output = Command::new("git")
        .args(["stash", "push", "-u", "-m", "stax-absorb"])
        .current_dir(workdir)
        .output()?;

    let stash_msg = String::from_utf8_lossy(&stash_output.stdout);
    let stashed = stash_output.status.success() && !stash_msg.contains("No local changes to save");

    if !stashed {
        bail!("Failed to stash changes before absorbing");
    }

    let mut absorbed_files: Vec<String> = Vec::new();
    let mut errors: Vec<String> = Vec::new();

    for (branch, patch_bytes, files) in &patches {
        // Checkout target branch
        let co = Command::new("git")
            .args(["checkout", branch])
            .current_dir(workdir)
            .status()?;

        if !co.success() {
            errors.push(format!("Failed to checkout '{}'", branch));
            break; // Can't continue safely if checkout fails
        }

        // Apply the patch (git apply reads from stdin when no path is given)
        let mut apply_cmd = Command::new("git")
            .args(["apply", "--cached"])
            .current_dir(workdir)
            .stdin(std::process::Stdio::piped())
            .spawn()?;

        if let Some(mut stdin) = apply_cmd.stdin.take() {
            stdin.write_all(patch_bytes)?;
            drop(stdin);
        }
        let apply_status = apply_cmd.wait()?;

        if !apply_status.success() {
            errors.push(format!(
                "Failed to apply patch to '{}' (files may have diverged)",
                branch
            ));
            let _ = Command::new("git")
                .args(["reset"])
                .current_dir(workdir)
                .status();
        } else {
            // Get the tip commit message for the fixup label
            let tip_msg = get_branch_tip_message(workdir, branch);
            let commit_msg = format!("fixup! {}", tip_msg.unwrap_or_else(|| branch.clone()));
            let commit_status = Command::new("git")
                .args(["commit", "-m", &commit_msg])
                .current_dir(workdir)
                .status()?;

            if !commit_status.success() {
                errors.push(format!("Failed to commit fixup on '{}'", branch));
                let _ = Command::new("git")
                    .args(["reset"])
                    .current_dir(workdir)
                    .status();
            } else {
                let reset_status = Command::new("git")
                    .args(["reset", "--hard", "HEAD"])
                    .current_dir(workdir)
                    .status()?;

                if !reset_status.success() {
                    errors.push(format!(
                        "Failed to clean worktree after committing absorbed changes on '{}'",
                        branch
                    ));
                    break;
                }

                absorbed_files.extend(files.iter().cloned());
                println!(
                    "  {} {} file(s) → {}",
                    "✓".green(),
                    files.len(),
                    branch.cyan()
                );
            }
        }

        // Return to original branch -- abort if this fails
        let co_back = Command::new("git")
            .args(["checkout", &current])
            .current_dir(workdir)
            .status()?;

        if !co_back.success() {
            errors.push(format!(
                "Failed to return to '{}'. Repository may be on wrong branch.",
                current
            ));
            break;
        }
    }

    if repo.current_branch()? != current {
        let co_back = Command::new("git")
            .args(["checkout", &current])
            .current_dir(workdir)
            .status()?;

        if !co_back.success() {
            errors.push(format!(
                "Failed to return to '{}'. Repository may be on wrong branch.",
                current
            ));
        }
    }

    // Restore stash
    let pop = Command::new("git")
        .args(["stash", "pop"])
        .current_dir(workdir)
        .status()?;

    if !pop.success() {
        println!(
            "{}",
            "Warning: failed to pop stash. Run `git stash pop` manually.".yellow()
        );
    }

    // Unstage and discard absorbed files.
    // For tracked files: reset index + checkout from HEAD.
    // For untracked/new files: reset index + remove from working tree.
    for file in &absorbed_files {
        let _ = Command::new("git")
            .args(["reset", "HEAD", "--", file])
            .current_dir(workdir)
            .status();

        let checkout = Command::new("git")
            .args(["checkout", "HEAD", "--", file])
            .current_dir(workdir)
            .status();

        if checkout.map(|s| !s.success()).unwrap_or(true) {
            let _ = std::fs::remove_file(workdir.join(file));
        }
    }

    if !errors.is_empty() {
        println!();
        println!("{}", "Some files could not be absorbed:".yellow());
        for e in &errors {
            println!("  {}", e);
        }
    }

    println!();
    println!("{}", "Absorb complete.".green());

    Ok(())
}

/// Attribute each staged file to a stack branch based on which branch most recently
/// modified it (file-level attribution via `git log`).
fn attribute_files(
    workdir: &Path,
    files: &[String],
    branch_boundaries: &[(String, String)],
) -> Result<AbsorbPlan> {
    let mut branch_files: HashMap<String, Vec<String>> = HashMap::new();
    let mut unattributed: Vec<String> = Vec::new();
    let branch_by_file = collect_file_attribution(workdir, branch_boundaries)?;

    for file in files {
        match branch_by_file.get(file) {
            Some(branch) => branch_files
                .entry(branch.clone())
                .or_default()
                .push(file.clone()),
            None => unattributed.push(file.clone()),
        }
    }

    let groups: Vec<(String, Vec<String>)> = branch_boundaries
        .iter()
        .filter_map(|(branch, _)| {
            branch_files
                .get(branch)
                .map(|files| (branch.clone(), files.clone()))
        })
        .collect();

    Ok(AbsorbPlan {
        groups,
        unattributed,
    })
}

fn collect_branch_tips(
    workdir: &Path,
    branch_boundaries: &[(String, String)],
) -> Result<HashMap<String, String>> {
    let mut branch_tips = HashMap::new();

    for (branch, _) in branch_boundaries {
        let output = Command::new("git")
            .args(["rev-parse", branch])
            .current_dir(workdir)
            .output()?;

        if !output.status.success() {
            bail!("Failed to resolve branch tip for '{}'", branch);
        }

        let tip = String::from_utf8_lossy(&output.stdout).trim().to_string();
        branch_tips.insert(tip, branch.clone());
    }

    Ok(branch_tips)
}

fn next_branch_after(branch: &str, branch_boundaries: &[(String, String)]) -> Option<String> {
    let idx = branch_boundaries
        .iter()
        .position(|(candidate, _)| candidate == branch)?;
    branch_boundaries
        .get(idx + 1)
        .map(|(next_branch, _)| next_branch.clone())
}

fn collect_file_attribution(
    workdir: &Path,
    branch_boundaries: &[(String, String)],
) -> Result<HashMap<String, String>> {
    let mut branch_by_file = HashMap::new();
    let Some((first_branch, base)) = branch_boundaries.first() else {
        return Ok(branch_by_file);
    };
    let top_branch = branch_boundaries
        .last()
        .map(|(branch, _)| branch.as_str())
        .unwrap_or(base);
    let branch_tips = collect_branch_tips(workdir, branch_boundaries)?;
    let mut current_branch = first_branch.clone();
    let mut branch_after_current_commit: Option<String> = None;

    let output = Command::new("git")
        .args([
            "log",
            "--format=commit:%H",
            "--name-only",
            "--reverse",
            "--ancestry-path",
            &format!("{}..{}", base, top_branch),
        ])
        .current_dir(workdir)
        .output()?;

    if !output.status.success() {
        bail!("Failed to collect file attribution for absorb");
    }

    for line in String::from_utf8_lossy(&output.stdout).lines() {
        if line.is_empty() {
            continue;
        }

        if let Some(commit) = line.strip_prefix("commit:") {
            if let Some(next_branch) = branch_after_current_commit.take() {
                current_branch = next_branch;
            }
            if let Some(tip_branch) = branch_tips.get(commit) {
                branch_after_current_commit = next_branch_after(tip_branch, branch_boundaries);
            }
            continue;
        }

        branch_by_file.insert(line.to_string(), current_branch.clone());
    }

    Ok(branch_by_file)
}

/// Get the first-line commit message of a branch's tip.
fn get_branch_tip_message(workdir: &Path, branch: &str) -> Option<String> {
    Command::new("git")
        .args(["log", "-1", "--format=%s", branch])
        .current_dir(workdir)
        .output()
        .ok()
        .and_then(|o| {
            if o.status.success() {
                let msg = String::from_utf8_lossy(&o.stdout).trim().to_string();
                if msg.is_empty() { None } else { Some(msg) }
            } else {
                None
            }
        })
}