Skip to main content

git_workflow/state/
working_dir.rs

1//! Working directory state
2
3use crate::git;
4
5/// State of the working directory
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum WorkingDirState {
8    /// No uncommitted changes
9    Clean,
10    /// Has unstaged changes only
11    HasUnstagedChanges,
12    /// Has staged changes only
13    HasStagedChanges,
14    /// Has both staged and unstaged changes
15    HasMixedChanges,
16}
17
18impl WorkingDirState {
19    /// Detect the current working directory state
20    pub fn detect() -> Self {
21        let has_unstaged = git::has_unstaged_changes();
22        let has_staged = git::has_staged_changes();
23
24        match (has_unstaged, has_staged) {
25            (false, false) => WorkingDirState::Clean,
26            (true, false) => WorkingDirState::HasUnstagedChanges,
27            (false, true) => WorkingDirState::HasStagedChanges,
28            (true, true) => WorkingDirState::HasMixedChanges,
29        }
30    }
31
32    /// Check if the working directory is clean
33    pub fn is_clean(&self) -> bool {
34        matches!(self, WorkingDirState::Clean)
35    }
36
37    /// Get a human-readable description
38    pub fn description(&self) -> &'static str {
39        match self {
40            WorkingDirState::Clean => "clean",
41            WorkingDirState::HasUnstagedChanges => "has unstaged changes",
42            WorkingDirState::HasStagedChanges => "has staged changes",
43            WorkingDirState::HasMixedChanges => "has staged and unstaged changes",
44        }
45    }
46}