Skip to main content

git_stk/commands/
status.rs

1use anyhow::Result;
2use clap_complete::engine::ArgValueCompleter;
3
4use crate::commands::Run;
5use crate::completions;
6use crate::providers::{CheckStatus, ReviewState, detect_review_provider};
7use crate::style;
8use crate::{git, stack};
9
10/// Print local and remote stack status for a branch.
11#[derive(Debug, clap::Args)]
12pub struct Status {
13    /// Branch to report on (defaults to the current branch).
14    #[arg(add = ArgValueCompleter::new(completions::branch_candidates))]
15    branch: Option<String>,
16}
17
18impl Run for Status {
19    fn run(self) -> Result<()> {
20        print_status(self.branch.as_deref())
21    }
22}
23
24pub fn print_status(branch: Option<&str>) -> Result<()> {
25    let branch = branch
26        .map(str::to_owned)
27        .map_or_else(git::current_branch, Ok)?;
28    let parent = stack::parent_of(&branch)?;
29    let children = stack::children_of(&branch)?;
30
31    anstream::println!("branch: {}", style::paint(style::CURRENT, &branch));
32    match parent.as_deref() {
33        Some(parent) => anstream::println!("parent: {}", style::paint(style::BRANCH, parent)),
34        None => anstream::println!("parent: none"),
35    }
36    if children.is_empty() {
37        anstream::println!("children: none");
38    } else {
39        let children: Vec<String> = children
40            .iter()
41            .map(|child| style::paint(style::BRANCH, child))
42            .collect();
43        anstream::println!("children: {}", children.join(", "));
44    }
45
46    // Provider state is best-effort: a repo with no remote (or no provider
47    // configured) still shows its local stack rather than hard-failing.
48    let detected = detect_review_provider().ok();
49    let review = match &detected {
50        Some((provider, review_provider)) => {
51            anstream::println!("provider: {} ({})", provider.kind, provider.source);
52            // Closed-inclusive: a review closed without merging is part of the
53            // branch's story, not "no review".
54            let review = review_provider.review_for_branch_including_closed(&branch)?;
55            match &review {
56                Some(review) => {
57                    // A queued review shows just the clock (it is waiting to
58                    // land); otherwise the CI dot. Both best-effort - a failed
59                    // lookup just omits the marker. When queued, the CI dot is
60                    // suppressed, so skip fetching it.
61                    let queued = review_provider
62                        .enqueued_branches(std::slice::from_ref(&review.branch))
63                        .map(|set| set.contains(&review.branch))
64                        .unwrap_or(false);
65                    let marker = if queued {
66                        crate::providers::QUEUED_MARK
67                    } else {
68                        review_provider
69                            .check_status(review)
70                            .unwrap_or(CheckStatus::None)
71                            .dot()
72                    };
73                    anstream::println!(
74                        "review: {marker}{} {} {} -> {}",
75                        review.id,
76                        style::state(&review.state),
77                        style::paint(style::BRANCH, &review.branch),
78                        style::paint(style::BRANCH, &review.base)
79                    );
80                    anstream::println!("url: {}", style::paint(style::DIM, &review.url));
81
82                    if let Some(parent) = parent.as_deref()
83                        && parent != review.base
84                    {
85                        anstream::println!(
86                            "{} review base is {}, local parent is {parent} - run `git stk submit`",
87                            style::paint(style::WARN, "warning:"),
88                            review.base
89                        );
90                    }
91                }
92                None => anstream::println!("review: none"),
93            }
94            review
95        }
96        None => {
97            anstream::println!("{}", style::dim("provider: not detected (no review info)"));
98            None
99        }
100    };
101
102    // Teach the loop: the next command, derived from review states and
103    // local drift. A sync covers the restack, so the nudges don't stack.
104    let mut hints = Vec::new();
105    match &review {
106        Some(review) if review.state == ReviewState::Merged => {
107            hints.push(format!(
108                "review {} is merged - run `git stk sync`",
109                review.id
110            ));
111        }
112        Some(review) if review.state == ReviewState::Closed => {
113            hints.push(format!(
114                "review {} was closed without merging - `git stk submit` opens a new review",
115                review.id
116            ));
117        }
118        _ => {}
119    }
120    if let Some(parent) = parent.as_deref() {
121        if let Some((_, review_provider)) = &detected {
122            match review_provider.review_for_branch_including_closed(parent) {
123                Ok(Some(parent_review)) if parent_review.branch == parent => {
124                    match parent_review.state {
125                        ReviewState::Merged => hints.push(format!(
126                            "parent review {} is merged - run `git stk sync`",
127                            parent_review.id
128                        )),
129                        ReviewState::Closed => hints.push(format!(
130                            "parent review {} was closed without merging - \
131                             retarget {branch} with `git stk adopt`",
132                            parent_review.id
133                        )),
134                        _ => {}
135                    }
136                }
137                _ => {}
138            }
139        }
140
141        if hints.is_empty()
142            && let Some(hint) = stack::behind_parent_hint(&branch, parent)
143        {
144            hints.push(hint);
145        }
146    }
147    for hint in hints {
148        anstream::println!("{} {hint}", style::paint(style::HINT, "hint:"));
149    }
150
151    Ok(())
152}