git_stk/commands/
status.rs1use anyhow::Result;
2use clap_complete::engine::ArgValueCompleter;
3
4use crate::commands::Run;
5use crate::completions;
6use crate::providers::{ReviewState, detect_review_provider};
7use crate::style;
8use crate::{git, stack};
9
10#[derive(Debug, clap::Args)]
12pub struct Status {
13 #[arg(add = ArgValueCompleter::new(completions::branch_candidates))]
14 branch: Option<String>,
15}
16
17impl Run for Status {
18 fn run(self) -> Result<()> {
19 print_status(self.branch.as_deref())
20 }
21}
22
23pub fn print_status(branch: Option<&str>) -> Result<()> {
24 let branch = branch
25 .map(str::to_owned)
26 .map_or_else(git::current_branch, Ok)?;
27 let parent = stack::parent_of(&branch)?;
28 let children = stack::children_of(&branch)?;
29
30 anstream::println!("branch: {}", style::paint(style::CURRENT, &branch));
31 match parent.as_deref() {
32 Some(parent) => anstream::println!("parent: {}", style::paint(style::BRANCH, parent)),
33 None => anstream::println!("parent: none"),
34 }
35 if children.is_empty() {
36 anstream::println!("children: none");
37 } else {
38 let children: Vec<String> = children
39 .iter()
40 .map(|child| style::paint(style::BRANCH, child))
41 .collect();
42 anstream::println!("children: {}", children.join(", "));
43 }
44
45 let (provider, review_provider) = detect_review_provider()?;
46 anstream::println!("provider: {} ({})", provider.kind, provider.source);
47
48 let review = review_provider.review_for_branch_including_closed(&branch)?;
51 match &review {
52 Some(review) => {
53 anstream::println!(
54 "review: {} {} {} -> {}",
55 review.id,
56 style::state(&review.state),
57 style::paint(style::BRANCH, &review.branch),
58 style::paint(style::BRANCH, &review.base)
59 );
60 anstream::println!("url: {}", style::paint(style::DIM, &review.url));
61
62 if let Some(parent) = parent.as_deref()
63 && parent != review.base
64 {
65 anstream::println!(
66 "{} review base is {}, local parent is {parent} - run `git stk submit`",
67 style::paint(style::WARN, "warning:"),
68 review.base
69 );
70 }
71 }
72 None => anstream::println!("review: none"),
73 }
74
75 let mut hints = Vec::new();
78 match &review {
79 Some(review) if review.state == ReviewState::Merged => {
80 hints.push(format!(
81 "review {} is merged - run `git stk sync`",
82 review.id
83 ));
84 }
85 Some(review) if review.state == ReviewState::Closed => {
86 hints.push(format!(
87 "review {} was closed without merging - `git stk submit` opens a new review",
88 review.id
89 ));
90 }
91 _ => {}
92 }
93 if let Some(parent) = parent.as_deref() {
94 match review_provider.review_for_branch_including_closed(parent) {
95 Ok(Some(parent_review)) if parent_review.branch == parent => {
96 match parent_review.state {
97 ReviewState::Merged => hints.push(format!(
98 "parent review {} is merged - run `git stk sync`",
99 parent_review.id
100 )),
101 ReviewState::Closed => hints.push(format!(
102 "parent review {} was closed without merging - \
103 retarget {branch} with `git stk adopt`",
104 parent_review.id
105 )),
106 _ => {}
107 }
108 }
109 _ => {}
110 }
111
112 if hints.is_empty()
113 && let Some(hint) = stack::behind_parent_hint(&branch, parent)
114 {
115 hints.push(hint);
116 }
117 }
118 for hint in hints {
119 anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
120 }
121
122 Ok(())
123}