aboutsummaryrefslogtreecommitdiffstats
path: root/src/vcs/git.rs
blob: 749d498b01a51c27675ad1c81fa11423530ae431 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use git2;
use std;

use std::fmt::Write;

#[derive(Debug)]
pub struct GitInfo {
    modified_files: bool,
    staged_files: bool,
    new_files: bool,
    commits: bool,
    active_operation: super::ActiveOperation,
    branch: Option<String>,
    remote_branch_diff: Option<(usize, usize)>,
}

impl GitInfo {
    pub fn new(git: git2::Repository) -> GitInfo {
        let mut modified_statuses = git2::Status::empty();
        modified_statuses.insert(git2::STATUS_WT_DELETED);
        modified_statuses.insert(git2::STATUS_WT_MODIFIED);
        modified_statuses.insert(git2::STATUS_WT_RENAMED);
        modified_statuses.insert(git2::STATUS_WT_TYPECHANGE);
        let mut staged_statuses = git2::Status::empty();
        staged_statuses.insert(git2::STATUS_INDEX_DELETED);
        staged_statuses.insert(git2::STATUS_INDEX_MODIFIED);
        staged_statuses.insert(git2::STATUS_INDEX_NEW);
        staged_statuses.insert(git2::STATUS_INDEX_RENAMED);
        staged_statuses.insert(git2::STATUS_INDEX_TYPECHANGE);
        let mut new_statuses = git2::Status::empty();
        new_statuses.insert(git2::STATUS_WT_NEW);

        let mut status_options = git2::StatusOptions::new();
        status_options.include_untracked(true);
        if true { // XXX
            status_options.update_index(true);
        }
        else {
            status_options.update_index(false);
            status_options.no_refresh(true);
        }
        let status = git.statuses(Some(&mut status_options));
        let mut modified_files = false;
        let mut staged_files = false;
        let mut new_files = false;
        for status in status.iter() {
            for file in status.iter() {
                if file.status().intersects(modified_statuses) {
                    modified_files = true;
                }
                if file.status().intersects(staged_statuses) {
                    staged_files = true;
                }
                if file.status().intersects(new_statuses) {
                    new_files = true;
                }
            }
        }

        let head = git.head();
        let commits = head.is_ok();
        let branch = head.ok()
            .and_then(|head| {
                if head.is_branch() {
                    head.shorthand().map(|s| s.to_string())
                }
                else {
                    head.resolve().ok()
                        .and_then(|head| head.target())
                        .map(|oid| {
                            let mut sha = String::new();
                            for b in oid.as_bytes().iter() {
                                write!(sha, "{:02x}", b).unwrap();
                            }
                            sha.truncate(7);
                            sha
                        })
                }
            });

        let active_operation = match git.state() {
            git2::RepositoryState::Merge
                => super::ActiveOperation::Merge,
            git2::RepositoryState::Revert
                | git2::RepositoryState::RevertSequence
                => super::ActiveOperation::Revert,
            git2::RepositoryState::CherryPick
                | git2::RepositoryState::CherryPickSequence
                => super::ActiveOperation::CherryPick,
            git2::RepositoryState::Bisect
                => super::ActiveOperation::Bisect,
            git2::RepositoryState::Rebase
                | git2::RepositoryState::RebaseInteractive
                | git2::RepositoryState::RebaseMerge
                => super::ActiveOperation::Rebase,
            _ => super::ActiveOperation::None,
        };

        let remote_branch_diff = git.head().ok()
            .and_then(|head| if head.is_branch() { Some(head) } else { None })
            .and_then(|head| {
                head.resolve().ok()
            })
            .map(|head| {
                (head.target(), head.shorthand().map(|s| s.to_string()))
            })
            .and_then(|(head_id, name)| {
                head_id.and_then(|head_id| {
                    name.and_then(|name| {
                        git.refname_to_id(
                            &(String::from("refs/remotes/origin/") + &name)
                        ).ok().and_then(|remote_id| {
                            git.graph_ahead_behind(head_id, remote_id).ok()
                        })
                    })
                })
            });

        GitInfo {
            modified_files: modified_files,
            staged_files: staged_files,
            new_files: new_files,
            commits: commits,
            active_operation: active_operation,
            branch: branch,
            remote_branch_diff: remote_branch_diff,
        }
    }
}

impl super::VcsInfo for GitInfo {
    fn vcs(&self) -> super::VcsType {
        super::VcsType::Git
    }

    fn has_modified_files(&self) -> bool {
        self.modified_files
    }

    fn has_staged_files(&self) -> bool {
        self.staged_files
    }

    fn has_new_files(&self) -> bool {
        self.new_files
    }

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

    fn active_operation(&self) -> super::ActiveOperation {
        self.active_operation
    }

    fn branch(&self) -> Option<String> {
        self.branch.clone()
    }

    fn remote_branch_diff(&self) -> Option<(usize, usize)> {
        self.remote_branch_diff
    }
}

pub fn detect() -> Option<Box<super::VcsInfo>> {
    let git = std::env::current_dir().ok().and_then(|pwd| {
        git2::Repository::discover(pwd).ok()
    });

    if let Some(git) = git {
        Some(Box::new(GitInfo::new(git)))
    }
    else {
        None
    }
}