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>) -> 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        reviews,
176        sizes: &sizes,
177    };
178    let mut lines = Vec::new();
179    collect_tree_lines(&ctx, &root, 0, &mut BTreeSet::new(), &mut lines);
180
181    // Leaf-first, trunk last: the stack reads like a pile sitting on its
182    // base, matching the up/down direction of navigation.
183    for line in lines.iter().rev() {
184        anstream::println!("{line}");
185    }
186
187    for branch in &descendants {
188        if let Some(parent) = parents.get(branch)
189            && let Some(hint) = behind_parent_hint(branch, parent)
190        {
191            anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
192        }
193    }
194    Ok(())
195}
196
197/// Print every stack, not just the current one: the trunk-rooted forest with
198/// a single trunk line at the bottom, and each rootless fragment as its own
199/// tree above it. The branch you are on is marked wherever it appears.
200pub fn print_all_stacks(reviews: &BTreeMap<String, String>) -> Result<()> {
201    let current = git::current_branch()?;
202    let parents = parent_map()?;
203    let children = children_map(&parents);
204    let trunk = trunk_branch(&git::local_branches()?);
205
206    // The root of every stack: each branch with a stack relationship, walked
207    // up to its topmost ancestor. Trunk-anchored stacks all resolve to the
208    // trunk; rootless fragments resolve to their own top. Lone branches with
209    // no parent or children never enter `parents`, so they are left out.
210    let mut roots = Vec::new();
211    let mut seen = BTreeSet::new();
212    for branch in parents
213        .iter()
214        .flat_map(|(child, parent)| [child.clone(), parent.clone()])
215    {
216        let root = root_for(&branch, &parents);
217        if seen.insert(root.clone()) {
218            roots.push(root);
219        }
220    }
221
222    if roots.is_empty() {
223        anstream::println!("no stacked branches");
224        return Ok(());
225    }
226
227    // Render the trunk-rooted forest last so its trunk line sits at the very
228    // bottom; rootless fragments stack above it.
229    roots.sort_by_key(|root| Some(root.as_str()) == trunk.as_deref());
230
231    let sizes = diff_sizes(parents.keys().cloned(), &parents);
232    let ctx = TreeCtx {
233        current: &current,
234        trunk: trunk.as_deref(),
235        children: &children,
236        reviews,
237        sizes: &sizes,
238    };
239    for (index, root) in roots.iter().enumerate() {
240        if index > 0 {
241            anstream::println!();
242        }
243        let mut lines = Vec::new();
244        collect_tree_lines(&ctx, root, 0, &mut BTreeSet::new(), &mut lines);
245        for line in lines.iter().rev() {
246            anstream::println!("{line}");
247        }
248    }
249
250    // Behind-parent hints span every stack.
251    for (branch, parent) in &parents {
252        if let Some(hint) = behind_parent_hint(branch, parent) {
253            anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
254        }
255    }
256    Ok(())
257}
258
259/// A restack nudge when `branch` is missing commits from its parent's tip.
260/// Local-only; a missing parent yields nothing.
261pub fn behind_parent_hint(branch: &str, parent: &str) -> Option<String> {
262    let behind = git::commits_behind(branch, parent)
263        .ok()
264        .filter(|count| *count > 0)?;
265    Some(format!(
266        "{branch} is {behind} commit{} behind {parent} - run `git stk restack`",
267        if behind == 1 { "" } else { "s" }
268    ))
269}
270
271/// The read-only context for rendering a stack tree, threaded through the
272/// recursion so each call only varies `branch`/`depth` and the accumulators.
273struct TreeCtx<'a> {
274    current: &'a str,
275    trunk: Option<&'a str>,
276    children: &'a BTreeMap<String, Vec<String>>,
277    reviews: &'a BTreeMap<String, String>,
278    sizes: &'a BTreeMap<String, (usize, usize)>,
279}
280
281/// Per-branch diff size (added, deleted lines) against its stack parent, for
282/// each branch that has one. Best effort: a branch with no parent, or whose
283/// diff cannot be read, is left out of the map and simply shows no size.
284fn diff_sizes(
285    branches: impl IntoIterator<Item = String>,
286    parents: &BTreeMap<String, String>,
287) -> BTreeMap<String, (usize, usize)> {
288    let mut sizes = BTreeMap::new();
289    for branch in branches {
290        if let Some(parent) = parents.get(&branch)
291            && let Ok(size) = git::diff_numstat(parent, &branch)
292        {
293            sizes.insert(branch, size);
294        }
295    }
296    sizes
297}
298
299fn collect_tree_lines(
300    ctx: &TreeCtx,
301    branch: &str,
302    depth: usize,
303    seen: &mut BTreeSet<String>,
304    lines: &mut Vec<String>,
305) {
306    // A graphite-style rail: a filled marker on the branch you are on.
307    let mut line = "  ".repeat(depth);
308    if branch == ctx.current {
309        line.push_str(&style::paint(style::CURRENT, &format!("\u{25c9} {branch}")));
310    } else {
311        line.push_str("\u{25cb} ");
312        line.push_str(&style::paint(style::BRANCH, branch));
313    }
314    if Some(branch) == ctx.trunk {
315        line.push_str(&style::paint(style::DIM, " (trunk)"));
316    }
317    // Optional annotations in one paren group: the dimmed open review number,
318    // then the diff size against the parent in faded green/red, like a diff.
319    let mut tags: Vec<String> = Vec::new();
320    if let Some(id) = ctx.reviews.get(branch) {
321        tags.push(style::paint(style::DIM, id));
322    }
323    // An empty branch (same tip as its parent) shows no size rather than a
324    // noisy "+0/-0".
325    if let Some((added, deleted)) = ctx.sizes.get(branch)
326        && (*added > 0 || *deleted > 0)
327    {
328        tags.push(format!(
329            "{}{}{}",
330            style::paint(style::ADDED, &format!("+{added}")),
331            style::paint(style::DIM, "/"),
332            style::paint(style::REMOVED, &format!("-{deleted}")),
333        ));
334    }
335    if !tags.is_empty() {
336        let separator = style::paint(style::DIM, ", ");
337        line.push_str(&style::paint(style::DIM, " ("));
338        line.push_str(&tags.join(&separator));
339        line.push_str(&style::paint(style::DIM, ")"));
340    }
341    lines.push(line);
342
343    if !seen.insert(branch.to_owned()) {
344        lines.push(format!("{}<cycle detected>", "  ".repeat(depth + 1)));
345        return;
346    }
347
348    if let Some(branch_children) = ctx.children.get(branch) {
349        for child in branch_children {
350            collect_tree_lines(ctx, child, depth + 1, seen, lines);
351        }
352    }
353}