forked from gitui-org/gitui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.rs
197 lines (170 loc) · 3.82 KB
/
status.rs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! sync git api for fetching a status
use crate::{
error::Error,
error::Result,
sync::{config::untracked_files_config_repo, repository::repo},
};
use git2::{Delta, Status, StatusOptions, StatusShow};
use scopetime::scope_time;
use std::path::Path;
use super::{RepoPath, ShowUntrackedFilesConfig};
///
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
pub enum StatusItemType {
///
New,
///
Modified,
///
Deleted,
///
Renamed,
///
Typechange,
///
Conflicted,
}
impl From<Status> for StatusItemType {
fn from(s: Status) -> Self {
if s.is_index_new() || s.is_wt_new() {
Self::New
} else if s.is_index_deleted() || s.is_wt_deleted() {
Self::Deleted
} else if s.is_index_renamed() || s.is_wt_renamed() {
Self::Renamed
} else if s.is_index_typechange() || s.is_wt_typechange() {
Self::Typechange
} else if s.is_conflicted() {
Self::Conflicted
} else {
Self::Modified
}
}
}
impl From<Delta> for StatusItemType {
fn from(d: Delta) -> Self {
match d {
Delta::Added => Self::New,
Delta::Deleted => Self::Deleted,
Delta::Renamed => Self::Renamed,
Delta::Typechange => Self::Typechange,
_ => Self::Modified,
}
}
}
///
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub struct StatusItem {
///
pub path: String,
///
pub status: StatusItemType,
}
///
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
pub enum StatusType {
///
WorkingDir,
///
Stage,
///
Both,
}
impl Default for StatusType {
fn default() -> Self {
Self::WorkingDir
}
}
impl From<StatusType> for StatusShow {
fn from(s: StatusType) -> Self {
match s {
StatusType::WorkingDir => Self::Workdir,
StatusType::Stage => Self::Index,
StatusType::Both => Self::IndexAndWorkdir,
}
}
}
///
pub fn is_workdir_clean(
repo_path: &RepoPath,
show_untracked: Option<ShowUntrackedFilesConfig>,
) -> Result<bool> {
let repo = repo(repo_path)?;
if repo.is_bare() && !repo.is_worktree() {
return Ok(true);
}
let show_untracked = if let Some(config) = show_untracked {
config
} else {
untracked_files_config_repo(&repo)?
};
let mut options = StatusOptions::default();
options
.show(StatusShow::Workdir)
.update_index(true)
.include_untracked(show_untracked.include_untracked())
.renames_head_to_index(true)
.recurse_untracked_dirs(
show_untracked.recurse_untracked_dirs(),
);
let statuses = repo.statuses(Some(&mut options))?;
Ok(statuses.is_empty())
}
/// gurantees sorting
pub fn get_status(
repo_path: &RepoPath,
status_type: StatusType,
show_untracked: Option<ShowUntrackedFilesConfig>,
) -> Result<Vec<StatusItem>> {
scope_time!("get_status");
let repo = repo(repo_path)?;
if repo.is_bare() && !repo.is_worktree() {
return Ok(Vec::new());
}
let show_untracked = if let Some(config) = show_untracked {
config
} else {
untracked_files_config_repo(&repo)?
};
let mut options = StatusOptions::default();
options
.show(status_type.into())
.update_index(true)
.include_untracked(show_untracked.include_untracked())
.renames_head_to_index(true)
.recurse_untracked_dirs(
show_untracked.recurse_untracked_dirs(),
);
let statuses = repo.statuses(Some(&mut options))?;
let mut res = Vec::with_capacity(statuses.len());
for e in statuses.iter() {
let status: Status = e.status();
let path = match e.head_to_index() {
Some(diff) => diff
.new_file()
.path()
.and_then(Path::to_str)
.map(String::from)
.ok_or_else(|| {
Error::Generic(
"failed to get path to diff's new file."
.to_string(),
)
})?,
None => e.path().map(String::from).ok_or_else(|| {
Error::Generic(
"failed to get the path to indexed file."
.to_string(),
)
})?,
};
res.push(StatusItem {
path,
status: StatusItemType::from(status),
});
}
res.sort_by(|a, b| {
Path::new(a.path.as_str()).cmp(Path::new(b.path.as_str()))
});
Ok(res)
}