1use 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
15fn 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(¤t)? 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(¤t)?;
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
82pub 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 _ => 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(¤t)?.is_empty() && parent_of(¤t)?.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
110pub fn checkout_bottom() -> Result<()> {
113 let current = git::current_branch()?;
114 let trunk = trunk_branch(&git::local_branches()?);
115
116 let bottom = if Some(¤t) == trunk.as_ref() {
117 let children = children_of(¤t)?;
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(¤t)?.is_none() && children_of(¤t)?.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(¤t, &parents);
156 let trunk = trunk_branch(&git::local_branches()?);
157
158 if parent_of(¤t)?.is_none() && children_of(¤t)?.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 let stack: BTreeSet<String> = current_stack_branches(¤t)?.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: ¤t,
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 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
212pub 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 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 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: ¤t,
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 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
277pub 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
289struct 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: bool,
300 width: usize,
302}
303
304fn term_width() -> usize {
306 console::Term::stdout()
307 .size_checked()
308 .map_or(80, |(_, cols)| cols as usize)
309}
310
311fn 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 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 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 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 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}