Skip to main content

grit_lib/status/
summary.rs

1//! Dashboard summary types for [`super::Status::assemble_summary`].
2
3use crate::branch_tracking::{
4    status_integration_hint, BranchStanding, StandingOptions, StatusHeader, StatusIntegrationHint,
5};
6use crate::error::Result;
7use crate::porcelain::status::{ConflictGroup, StatusGuidance, StatusModel};
8use crate::repo::Repository;
9use crate::state::InProgressOperation;
10
11/// Options for [`super::Status::assemble_summary`].
12#[derive(Debug, Clone, Copy)]
13#[non_exhaustive]
14pub struct StatusSummaryOptions {
15    /// How far ahead to walk and whether to compute ahead/behind counts.
16    pub standing: StandingOptions,
17}
18
19impl StatusSummaryOptions {
20    /// Set how far ahead to walk and whether to compute ahead/behind counts.
21    #[must_use]
22    pub fn standing(mut self, standing: StandingOptions) -> Self {
23        self.standing = standing;
24        self
25    }
26}
27
28impl Default for StatusSummaryOptions {
29    fn default() -> Self {
30        Self {
31            standing: StandingOptions {
32                newest_ahead_limit: 10,
33                counts: true,
34            },
35        }
36    }
37}
38
39/// Typed dashboard outcome: worktree model plus branch standing and follow-up guidance.
40///
41/// Built by [`super::Status::assemble_summary`] after [`super::Status::run`].
42#[derive(Debug, Clone)]
43#[non_exhaustive]
44pub struct StatusSummary {
45    /// Index and worktree scan (staged, unstaged, conflicts, operation state).
46    pub model: StatusModel,
47    /// Target/upstream comparison for the current branch.
48    pub standing: BranchStanding,
49    /// Resolved header line (branch standing vs in-progress operation).
50    pub header: StatusHeader,
51    /// Suggested next steps for the user or agent.
52    pub guidance: StatusGuidance,
53    /// Unmerged paths grouped by conflict kind.
54    pub conflict_groups: Vec<ConflictGroup>,
55    /// When the branch should merge or pull to integrate, which ref to use.
56    pub integration_hint: Option<StatusIntegrationHint>,
57    /// Paused operations detected in the repository (merge, rebase, …).
58    pub in_progress: Vec<InProgressOperation>,
59}
60
61/// Derive dashboard fields from a [`StatusModel`] without re-scanning the worktree.
62pub(crate) fn assemble_status_summary(
63    repo: &Repository,
64    model: StatusModel,
65    options: StatusSummaryOptions,
66) -> Result<StatusSummary> {
67    let in_progress = repo.operation().in_progress();
68    let standing = BranchStanding::compute(repo, &model.head, options.standing)?;
69    let header = StatusHeader::resolve(&standing, &model.state, &in_progress);
70    let conflict_groups = model.conflict_groups();
71    let guidance = model.guidance(&standing);
72    let integration_hint = standing
73        .branch
74        .as_deref()
75        .and_then(|branch| status_integration_hint(&standing, Some((repo, branch))));
76    Ok(StatusSummary {
77        model,
78        standing,
79        header,
80        guidance,
81        conflict_groups,
82        integration_hint,
83        in_progress,
84    })
85}