Skip to main content

git_stk/stack/
nav.rs

1//! Moving around the stack and printing it.
2
3use std::collections::{BTreeMap, BTreeSet};
4
5use anyhow::{Result, bail};
6
7use super::{
8    children_map, children_of, current_stack_branches, parent_map, parent_of, root_for,
9    trunk_branch,
10};
11use crate::git;
12use crate::prompt;
13use crate::style;
14
15/// Offer a numbered pick of `children`; None when nothing was chosen
16/// (non-interactive stdin, or an invalid answer).
17fn pick_child(title: &str, children: &[String]) -> anyhow::Result<Option<String>> {
18    let painted: Vec<String> = children
19        .iter()
20        .map(|child| style::paint(style::BRANCH, child))
21        .collect();
22    Ok(prompt::pick(title, &painted)?.map(|index| children[index].clone()))
23}
24
25pub fn print_parent(branch: Option<&str>) -> Result<()> {
26    let branch = branch
27        .map(str::to_owned)
28        .map_or_else(git::current_branch, Ok)?;
29    match parent_of(&branch)? {
30        Some(parent) => println!("{parent}"),
31        None => bail!("{branch} has no stack parent"),
32    }
33    Ok(())
34}
35
36pub fn print_children(branch: Option<&str>) -> Result<()> {
37    let branch = branch
38        .map(str::to_owned)
39        .map_or_else(git::current_branch, Ok)?;
40    for child in children_of(&branch)? {
41        println!("{child}");
42    }
43    Ok(())
44}
45
46pub fn checkout_parent() -> Result<()> {
47    let current = git::current_branch()?;
48    let Some(parent) = parent_of(&current)? else {
49        bail!("{current} has no stack parent");
50    };
51
52    git::checkout(&parent)
53}
54
55pub fn checkout_child(branch: Option<&str>) -> Result<()> {
56    let current = git::current_branch()?;
57    let children = children_of(&current)?;
58    let child = match (branch, children.as_slice()) {
59        (Some(branch), _) => {
60            if children.iter().any(|child| child == branch) {
61                branch.to_owned()
62            } else {
63                bail!("{branch} is not a stack child of {current}");
64            }
65        }
66        (None, [child]) => child.to_owned(),
67        (None, []) => bail!("{current} has no stack children"),
68        (None, _) => {
69            match pick_child(
70                &format!("{current} has multiple stack children:"),
71                &children,
72            )? {
73                Some(child) => child,
74                None => bail!("choose one with `git stk up <branch>`"),
75            }
76        }
77    };
78
79    git::checkout(&child)
80}
81
82/// Check out the leaf of the current stack, following single children. A
83/// fork is ambiguous, like `up` without a branch.
84pub fn checkout_top() -> Result<()> {
85    let current = git::current_branch()?;
86    let mut top = current.clone();
87    loop {
88        let children = children_of(&top)?;
89        match children.as_slice() {
90            [] => break,
91            [child] => top = child.clone(),
92            // A pick resolves the fork and the climb continues from there.
93            _ => match pick_child(&format!("{top} has multiple stack children:"), &children)? {
94                Some(child) => top = child,
95                None => bail!("walk up from {top} with `git stk up <branch>`"),
96            },
97        }
98    }
99
100    if top == current {
101        if children_of(&current)?.is_empty() && parent_of(&current)?.is_none() {
102            bail!("{current} is not in a stack");
103        }
104        anstream::println!("{current} is already at the top of the stack");
105        return Ok(());
106    }
107    git::checkout(&top)
108}
109
110/// Check out the bottom of the current stack: the branch just above the
111/// trunk. From the trunk itself, a single stacked child is unambiguous.
112pub fn checkout_bottom() -> Result<()> {
113    let current = git::current_branch()?;
114    let trunk = trunk_branch(&git::local_branches()?);
115
116    let bottom = if Some(&current) == trunk.as_ref() {
117        let children = children_of(&current)?;
118        match children.as_slice() {
119            [child] => child.clone(),
120            [] => bail!("{current} has no stacked branches"),
121            _ => {
122                match pick_child(
123                    &format!("{current} has multiple stack children:"),
124                    &children,
125                )? {
126                    Some(child) => child,
127                    None => bail!("choose one with `git stk up <branch>`"),
128                }
129            }
130        }
131    } else {
132        let mut bottom = current.clone();
133        while let Some(parent) = parent_of(&bottom)? {
134            if Some(&parent) == trunk.as_ref() {
135                break;
136            }
137            bottom = parent;
138        }
139        bottom
140    };
141
142    if bottom == current {
143        if parent_of(&current)?.is_none() && children_of(&current)?.is_empty() {
144            bail!("{current} is not in a stack");
145        }
146        anstream::println!("{current} is already at the bottom of the stack");
147        return Ok(());
148    }
149    git::checkout(&bottom)
150}
151
152pub fn print_stack(reviews: &BTreeMap<String, String>, commits: bool) -> Result<()> {
153    let current = git::current_branch()?;
154    let parents = parent_map()?;
155    let root = root_for(&current, &parents);
156    let trunk = trunk_branch(&git::local_branches()?);
157
158    // A lone branch (or the bare trunk) is not a stack - say so rather than
159    // drawing a one-node "stack".
160    if parent_of(&current)?.is_none() && children_of(&current)?.is_empty() {
161        anstream::println!("no stacked branches");
162        anstream::println!(
163            "{}",
164            style::dim("create one on top of the current branch with `git stk new <branch>`")
165        );
166        return Ok(());
167    }
168
169    // Scope to the current branch's own line (fork siblings included, stacks
170    // that merely share the trunk excluded), so `list` shows just the stack you
171    // are on; `--all` is for the rest. The trunk stays as the rendered root so
172    // the stack still reads as sitting on its base.
173    let stack: BTreeSet<String> = current_stack_branches(&current)?.into_iter().collect();
174    let children: BTreeMap<String, Vec<String>> = children_map(&parents)
175        .into_iter()
176        .map(|(parent, kids)| {
177            let kept = kids.into_iter().filter(|kid| stack.contains(kid)).collect();
178            (parent, kept)
179        })
180        .collect();
181
182    let sizes = diff_sizes(stack.iter().cloned(), &parents);
183    let ctx = TreeCtx {
184        current: &current,
185        trunk: trunk.as_deref(),
186        children: &children,
187        parents: &parents,
188        reviews,
189        sizes: &sizes,
190        commits,
191        width: term_width(),
192    };
193    let mut lines = Vec::new();
194    collect_tree_lines(&ctx, &root, 0, &mut BTreeSet::new(), &mut lines);
195
196    // Leaf-first, trunk last: the stack reads like a pile sitting on its
197    // base, matching the up/down direction of navigation.
198    for line in lines.iter().rev() {
199        anstream::println!("{line}");
200    }
201
202    for branch in &stack {
203        if let Some(parent) = parents.get(branch)
204            && let Some(hint) = behind_parent_hint(branch, parent)
205        {
206            anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
207        }
208    }
209    Ok(())
210}
211
212/// Print every stack, not just the current one: the trunk-rooted forest with
213/// a single trunk line at the bottom, and each rootless fragment as its own
214/// tree above it. The branch you are on is marked wherever it appears.
215pub fn print_all_stacks(reviews: &BTreeMap<String, String>, commits: bool) -> Result<()> {
216    let current = git::current_branch()?;
217    let parents = parent_map()?;
218    let children = children_map(&parents);
219    let trunk = trunk_branch(&git::local_branches()?);
220
221    // The root of every stack: each branch with a stack relationship, walked
222    // up to its topmost ancestor. Trunk-anchored stacks all resolve to the
223    // trunk; rootless fragments resolve to their own top. Lone branches with
224    // no parent or children never enter `parents`, so they are left out.
225    let mut roots = Vec::new();
226    let mut seen = BTreeSet::new();
227    for branch in parents
228        .iter()
229        .flat_map(|(child, parent)| [child.clone(), parent.clone()])
230    {
231        let root = root_for(&branch, &parents);
232        if seen.insert(root.clone()) {
233            roots.push(root);
234        }
235    }
236
237    if roots.is_empty() {
238        anstream::println!("no stacked branches");
239        return Ok(());
240    }
241
242    // Render the trunk-rooted forest last so its trunk line sits at the very
243    // bottom; rootless fragments stack above it.
244    roots.sort_by_key(|root| Some(root.as_str()) == trunk.as_deref());
245
246    let sizes = diff_sizes(parents.keys().cloned(), &parents);
247    let ctx = TreeCtx {
248        current: &current,
249        trunk: trunk.as_deref(),
250        children: &children,
251        parents: &parents,
252        reviews,
253        sizes: &sizes,
254        commits,
255        width: term_width(),
256    };
257    for (index, root) in roots.iter().enumerate() {
258        if index > 0 {
259            anstream::println!();
260        }
261        let mut lines = Vec::new();
262        collect_tree_lines(&ctx, root, 0, &mut BTreeSet::new(), &mut lines);
263        for line in lines.iter().rev() {
264            anstream::println!("{line}");
265        }
266    }
267
268    // Behind-parent hints span every stack.
269    for (branch, parent) in &parents {
270        if let Some(hint) = behind_parent_hint(branch, parent) {
271            anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
272        }
273    }
274    Ok(())
275}
276
277/// A restack nudge when `branch` is missing commits from its parent's tip.
278/// Local-only; a missing parent yields nothing.
279pub fn behind_parent_hint(branch: &str, parent: &str) -> Option<String> {
280    let behind = git::commits_behind(branch, parent)
281        .ok()
282        .filter(|count| *count > 0)?;
283    Some(format!(
284        "{branch} is {behind} commit{} behind {parent} - run `git stk restack`",
285        if behind == 1 { "" } else { "s" }
286    ))
287}
288
289/// The read-only context for rendering a stack tree, threaded through the
290/// recursion so each call only varies `branch`/`depth` and the accumulators.
291struct TreeCtx<'a> {
292    current: &'a str,
293    trunk: Option<&'a str>,
294    children: &'a BTreeMap<String, Vec<String>>,
295    parents: &'a BTreeMap<String, String>,
296    reviews: &'a BTreeMap<String, String>,
297    sizes: &'a BTreeMap<String, (usize, usize)>,
298    /// `--commits`: list each branch's own commits beneath it.
299    commits: bool,
300    /// Terminal width, for truncating commit subjects.
301    width: usize,
302}
303
304/// Terminal width for truncation, defaulting to 80 when not a terminal.
305fn term_width() -> usize {
306    console::Term::stdout()
307        .size_checked()
308        .map_or(80, |(_, cols)| cols as usize)
309}
310
311/// Per-branch diff size (added, deleted lines) against its stack parent, for
312/// each branch that has one. Best effort: a branch with no parent, or whose
313/// diff cannot be read, is left out of the map and simply shows no size.
314fn diff_sizes(
315    branches: impl IntoIterator<Item = String>,
316    parents: &BTreeMap<String, String>,
317) -> BTreeMap<String, (usize, usize)> {
318    let mut sizes = BTreeMap::new();
319    for branch in branches {
320        if let Some(parent) = parents.get(&branch)
321            && let Ok(size) = git::diff_numstat(parent, &branch)
322        {
323            sizes.insert(branch, size);
324        }
325    }
326    sizes
327}
328
329fn collect_tree_lines(
330    ctx: &TreeCtx,
331    branch: &str,
332    depth: usize,
333    seen: &mut BTreeSet<String>,
334    lines: &mut Vec<String>,
335) {
336    // A graphite-style rail: a filled marker on the branch you are on.
337    let mut line = "  ".repeat(depth);
338    if branch == ctx.current {
339        line.push_str(&style::paint(style::CURRENT, &format!("\u{25c9} {branch}")));
340    } else {
341        line.push_str("\u{25cb} ");
342        line.push_str(&style::paint(style::BRANCH, branch));
343    }
344    if Some(branch) == ctx.trunk {
345        line.push_str(&style::paint(style::DIM, " (trunk)"));
346    }
347    // Optional annotations in one paren group: the dimmed open review number,
348    // then the diff size against the parent in faded green/red, like a diff.
349    let mut tags: Vec<String> = Vec::new();
350    if let Some(id) = ctx.reviews.get(branch) {
351        tags.push(style::paint(style::DIM, id));
352    }
353    // An empty branch (same tip as its parent) shows no size rather than a
354    // noisy "+0/-0".
355    if let Some((added, deleted)) = ctx.sizes.get(branch)
356        && (*added > 0 || *deleted > 0)
357    {
358        tags.push(format!(
359            "{}{}{}",
360            style::paint(style::ADDED, &format!("+{added}")),
361            style::paint(style::DIM, "/"),
362            style::paint(style::REMOVED, &format!("-{deleted}")),
363        ));
364    }
365    if !tags.is_empty() {
366        let separator = style::paint(style::DIM, ", ");
367        line.push_str(&style::paint(style::DIM, " ("));
368        line.push_str(&tags.join(&separator));
369        line.push_str(&style::paint(style::DIM, ")"));
370    }
371
372    // With --commits, list the branch's own commits (parent..branch) under it.
373    // `lines` is printed reversed, so push them here (before the branch line)
374    // oldest-first: the reversal then shows them newest-first - git log order -
375    // directly below the branch. The trunk and parentless roots have no "own"
376    // commits to show.
377    if ctx.commits
378        && Some(branch) != ctx.trunk
379        && let Some(parent) = ctx.parents.get(branch)
380    {
381        let indent = "  ".repeat(depth + 1);
382        match git::log_oneline(&format!("{parent}..{branch}")) {
383            Ok(commits) if !commits.is_empty() => {
384                for (sha, subject) in commits.iter().rev() {
385                    let budget = ctx
386                        .width
387                        .saturating_sub(indent.len() + sha.len() + 2)
388                        .max(16);
389                    let subject = console::truncate_str(subject, budget, "…");
390                    lines.push(format!(
391                        "{indent}{}  {}",
392                        style::paint(style::DIM, sha),
393                        style::paint(style::DIM, &subject)
394                    ));
395                }
396            }
397            Ok(_) => lines.push(format!(
398                "{indent}{}",
399                style::paint(style::DIM, "(no commits)")
400            )),
401            Err(_) => {}
402        }
403    }
404
405    lines.push(line);
406
407    if !seen.insert(branch.to_owned()) {
408        lines.push(format!("{}<cycle detected>", "  ".repeat(depth + 1)));
409        return;
410    }
411
412    if let Some(branch_children) = ctx.children.get(branch) {
413        for child in branch_children {
414            collect_tree_lines(ctx, child, depth + 1, seen, lines);
415        }
416    }
417}