aboutsummaryrefslogtreecommitdiffstats
path: root/src/vcs/mod.rs
blob: f68e4ef9096ff13986c73a16e3c27b4406f559fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
mod git;

#[derive(Debug,Copy,Clone)]
pub enum VcsType {
    Git,
}

#[derive(Debug,Copy,Clone)]
pub enum ActiveOperation {
    None,
    Merge,
    Revert,
    CherryPick,
    Bisect,
    Rebase,
}

pub trait VcsInfo {
    fn vcs(&self) -> VcsType;
    fn has_modified_files(&self) -> bool;
    fn has_staged_files(&self) -> bool;
    fn has_new_files(&self) -> bool;
    fn has_commits(&self) -> bool;
    fn active_operation(&self) -> ActiveOperation;
    fn branch(&self) -> Option<String>;
    fn remote_branch_diff(&self) -> Option<(usize, usize)>;

    fn is_dirty(&self) -> bool {
        let diff = self.remote_branch_diff();
        self.has_modified_files()
            || self.has_staged_files()
            || self.has_new_files()
            || !diff.is_some()
            || diff
                .map(|(local, remote)| local > 0 || remote > 0)
                .unwrap_or(false)
    }

    fn is_error(&self) -> bool {
        !self.has_commits()
    }
}

pub fn detect() -> Option<Box<VcsInfo>> {
    if let Some(git) = git::detect() {
        Some(git)
    }
    else {
        None
    }
}