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_provider, 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_for_branch(&branch)?;
28 let children = stack::children_for_branch(&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 => println!("parent: none"),
34 }
35 if children.is_empty() {
36 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 = detect_provider()?;
46 println!("provider: {} ({})", provider.kind, provider.source);
47 let review_provider = review_provider(provider.kind);
48
49 let review = review_provider.review_for_branch_including_closed(&branch)?;
52 match &review {
53 Some(review) => {
54 anstream::println!(
55 "review: {} {} {} -> {}",
56 review.id,
57 style::state(&review.state),
58 style::paint(style::BRANCH, &review.branch),
59 style::paint(style::BRANCH, &review.base)
60 );
61 anstream::println!("url: {}", style::paint(style::DIM, &review.url));
62
63 if let Some(parent) = parent.as_deref()
64 && parent != review.base
65 {
66 anstream::println!(
67 "{} review base is {}, local parent is {parent} - run `git stk submit`",
68 style::paint(style::WARN, "warning:"),
69 review.base
70 );
71 }
72 }
73 None => println!("review: none"),
74 }
75
76 let mut hints = Vec::new();
79 match &review {
80 Some(review) if review.state == ReviewState::Merged => {
81 hints.push(format!(
82 "review {} is merged - run `git stk sync`",
83 review.id
84 ));
85 }
86 Some(review) if review.state == ReviewState::Closed => {
87 hints.push(format!(
88 "review {} was closed without merging - `git stk submit` opens a new review",
89 review.id
90 ));
91 }
92 _ => {}
93 }
94 if let Some(parent) = parent.as_deref() {
95 match review_provider.review_for_branch_including_closed(parent) {
96 Ok(Some(parent_review)) if parent_review.branch == parent => {
97 match parent_review.state {
98 ReviewState::Merged => hints.push(format!(
99 "parent review {} is merged - run `git stk sync`",
100 parent_review.id
101 )),
102 ReviewState::Closed => hints.push(format!(
103 "parent review {} was closed without merging - \
104 retarget {branch} with `git stk adopt`",
105 parent_review.id
106 )),
107 _ => {}
108 }
109 }
110 _ => {}
111 }
112
113 if hints.is_empty()
114 && let Some(hint) = stack::behind_parent_hint(&branch, parent)
115 {
116 hints.push(hint);
117 }
118 }
119 for hint in hints {
120 anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
121 }
122
123 Ok(())
124}