1use 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
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 children = children_map(&parents);
157 let trunk = trunk_branch(&git::local_branches()?);
158
159 let descendants = branch_and_descendants(&root)?;
160 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: ¤t,
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 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
200pub 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 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 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: ¤t,
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 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
265pub 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
277struct 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: bool,
288 width: usize,
290}
291
292fn term_width() -> usize {
294 console::Term::stdout()
295 .size_checked()
296 .map_or(80, |(_, cols)| cols as usize)
297}
298
299fn 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 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 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 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 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}