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    branch_and_descendants, children_map, children_of, 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 children = children_map(&parents);
157    let trunk = trunk_branch(&git::local_branches()?);
158
159    let descendants = branch_and_descendants(&root)?;
160    // A lone branch (or the bare trunk) is not a stack - say so rather than
161    // drawing a one-node "stack".
162    if descendants.len() == 1 {
163        anstream::println!("no stacked branches");
164        anstream::println!(
165            "{}",
166            style::dim("create one on top of the current branch with `git stk new <branch>`")
167        );
168        return Ok(());
169    }
170    let sizes = diff_sizes(descendants.iter().cloned(), &parents);
171    let ctx = TreeCtx {
172        current: &current,
173        trunk: trunk.as_deref(),
174        children: &children,
175        parents: &parents,
176        reviews,
177        sizes: &sizes,
178        commits,
179        width: term_width(),
180    };
181    let mut lines = Vec::new();
182    collect_tree_lines(&ctx, &root, 0, &mut BTreeSet::new(), &mut lines);
183
184    // Leaf-first, trunk last: the stack reads like a pile sitting on its
185    // base, matching the up/down direction of navigation.
186    for line in lines.iter().rev() {
187        anstream::println!("{line}");
188    }
189
190    for branch in &descendants {
191        if let Some(parent) = parents.get(branch)
192            && let Some(hint) = behind_parent_hint(branch, parent)
193        {
194            anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
195        }
196    }
197    Ok(())
198}
199
200/// Print every stack, not just the current one: the trunk-rooted forest with
201/// a single trunk line at the bottom, and each rootless fragment as its own
202/// tree above it. The branch you are on is marked wherever it appears.
203pub fn print_all_stacks(reviews: &BTreeMap<String, String>, commits: bool) -> Result<()> {
204    let current = git::current_branch()?;
205    let parents = parent_map()?;
206    let children = children_map(&parents);
207    let trunk = trunk_branch(&git::local_branches()?);
208
209    // The root of every stack: each branch with a stack relationship, walked
210    // up to its topmost ancestor. Trunk-anchored stacks all resolve to the
211    // trunk; rootless fragments resolve to their own top. Lone branches with
212    // no parent or children never enter `parents`, so they are left out.
213    let mut roots = Vec::new();
214    let mut seen = BTreeSet::new();
215    for branch in parents
216        .iter()
217        .flat_map(|(child, parent)| [child.clone(), parent.clone()])
218    {
219        let root = root_for(&branch, &parents);
220        if seen.insert(root.clone()) {
221            roots.push(root);
222        }
223    }
224
225    if roots.is_empty() {
226        anstream::println!("no stacked branches");
227        return Ok(());
228    }
229
230    // Render the trunk-rooted forest last so its trunk line sits at the very
231    // bottom; rootless fragments stack above it.
232    roots.sort_by_key(|root| Some(root.as_str()) == trunk.as_deref());
233
234    let sizes = diff_sizes(parents.keys().cloned(), &parents);
235    let ctx = TreeCtx {
236        current: &current,
237        trunk: trunk.as_deref(),
238        children: &children,
239        parents: &parents,
240        reviews,
241        sizes: &sizes,
242        commits,
243        width: term_width(),
244    };
245    for (index, root) in roots.iter().enumerate() {
246        if index > 0 {
247            anstream::println!();
248        }
249        let mut lines = Vec::new();
250        collect_tree_lines(&ctx, root, 0, &mut BTreeSet::new(), &mut lines);
251        for line in lines.iter().rev() {
252            anstream::println!("{line}");
253        }
254    }
255
256    // Behind-parent hints span every stack.
257    for (branch, parent) in &parents {
258        if let Some(hint) = behind_parent_hint(branch, parent) {
259            anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
260        }
261    }
262    Ok(())
263}
264
265/// A restack nudge when `branch` is missing commits from its parent's tip.
266/// Local-only; a missing parent yields nothing.
267pub fn behind_parent_hint(branch: &str, parent: &str) -> Option<String> {
268    let behind = git::commits_behind(branch, parent)
269        .ok()
270        .filter(|count| *count > 0)?;
271    Some(format!(
272        "{branch} is {behind} commit{} behind {parent} - run `git stk restack`",
273        if behind == 1 { "" } else { "s" }
274    ))
275}
276
277/// The read-only context for rendering a stack tree, threaded through the
278/// recursion so each call only varies `branch`/`depth` and the accumulators.
279struct TreeCtx<'a> {
280    current: &'a str,
281    trunk: Option<&'a str>,
282    children: &'a BTreeMap<String, Vec<String>>,
283    parents: &'a BTreeMap<String, String>,
284    reviews: &'a BTreeMap<String, String>,
285    sizes: &'a BTreeMap<String, (usize, usize)>,
286    /// `--commits`: list each branch's own commits beneath it.
287    commits: bool,
288    /// Terminal width, for truncating commit subjects.
289    width: usize,
290}
291
292/// Terminal width for truncation, defaulting to 80 when not a terminal.
293fn term_width() -> usize {
294    console::Term::stdout()
295        .size_checked()
296        .map_or(80, |(_, cols)| cols as usize)
297}
298
299/// Per-branch diff size (added, deleted lines) against its stack parent, for
300/// each branch that has one. Best effort: a branch with no parent, or whose
301/// diff cannot be read, is left out of the map and simply shows no size.
302fn diff_sizes(
303    branches: impl IntoIterator<Item = String>,
304    parents: &BTreeMap<String, String>,
305) -> BTreeMap<String, (usize, usize)> {
306    let mut sizes = BTreeMap::new();
307    for branch in branches {
308        if let Some(parent) = parents.get(&branch)
309            && let Ok(size) = git::diff_numstat(parent, &branch)
310        {
311            sizes.insert(branch, size);
312        }
313    }
314    sizes
315}
316
317fn collect_tree_lines(
318    ctx: &TreeCtx,
319    branch: &str,
320    depth: usize,
321    seen: &mut BTreeSet<String>,
322    lines: &mut Vec<String>,
323) {
324    // A graphite-style rail: a filled marker on the branch you are on.
325    let mut line = "  ".repeat(depth);
326    if branch == ctx.current {
327        line.push_str(&style::paint(style::CURRENT, &format!("\u{25c9} {branch}")));
328    } else {
329        line.push_str("\u{25cb} ");
330        line.push_str(&style::paint(style::BRANCH, branch));
331    }
332    if Some(branch) == ctx.trunk {
333        line.push_str(&style::paint(style::DIM, " (trunk)"));
334    }
335    // Optional annotations in one paren group: the dimmed open review number,
336    // then the diff size against the parent in faded green/red, like a diff.
337    let mut tags: Vec<String> = Vec::new();
338    if let Some(id) = ctx.reviews.get(branch) {
339        tags.push(style::paint(style::DIM, id));
340    }
341    // An empty branch (same tip as its parent) shows no size rather than a
342    // noisy "+0/-0".
343    if let Some((added, deleted)) = ctx.sizes.get(branch)
344        && (*added > 0 || *deleted > 0)
345    {
346        tags.push(format!(
347            "{}{}{}",
348            style::paint(style::ADDED, &format!("+{added}")),
349            style::paint(style::DIM, "/"),
350            style::paint(style::REMOVED, &format!("-{deleted}")),
351        ));
352    }
353    if !tags.is_empty() {
354        let separator = style::paint(style::DIM, ", ");
355        line.push_str(&style::paint(style::DIM, " ("));
356        line.push_str(&tags.join(&separator));
357        line.push_str(&style::paint(style::DIM, ")"));
358    }
359
360    // With --commits, list the branch's own commits (parent..branch) under it.
361    // `lines` is printed reversed, so push them here (before the branch line)
362    // oldest-first: the reversal then shows them newest-first - git log order -
363    // directly below the branch. The trunk and parentless roots have no "own"
364    // commits to show.
365    if ctx.commits
366        && Some(branch) != ctx.trunk
367        && let Some(parent) = ctx.parents.get(branch)
368    {
369        let indent = "  ".repeat(depth + 1);
370        match git::log_oneline(&format!("{parent}..{branch}")) {
371            Ok(commits) if !commits.is_empty() => {
372                for (sha, subject) in commits.iter().rev() {
373                    let budget = ctx
374                        .width
375                        .saturating_sub(indent.len() + sha.len() + 2)
376                        .max(16);
377                    let subject = console::truncate_str(subject, budget, "…");
378                    lines.push(format!(
379                        "{indent}{}  {}",
380                        style::paint(style::DIM, sha),
381                        style::paint(style::DIM, &subject)
382                    ));
383                }
384            }
385            Ok(_) => lines.push(format!(
386                "{indent}{}",
387                style::paint(style::DIM, "(no commits)")
388            )),
389            Err(_) => {}
390        }
391    }
392
393    lines.push(line);
394
395    if !seen.insert(branch.to_owned()) {
396        lines.push(format!("{}<cycle detected>", "  ".repeat(depth + 1)));
397        return;
398    }
399
400    if let Some(branch_children) = ctx.children.get(branch) {
401        for child in branch_children {
402            collect_tree_lines(ctx, child, depth + 1, seen, lines);
403        }
404    }
405}