-
Notifications
You must be signed in to change notification settings - Fork 89
/
status.rs
302 lines (285 loc) · 9.44 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use super::Screen;
use crate::{
config::Config,
git::{self, diff::Diff},
git2_opts,
items::{self, Item, TargetData},
Res,
};
use git2::Repository;
use ratatui::{
prelude::Rect,
text::{Line, Span},
};
use std::{path::PathBuf, rc::Rc};
pub(crate) fn create(config: Rc<Config>, repo: Rc<Repository>, size: Rect) -> Res<Screen> {
Screen::new(
Rc::clone(&config),
size,
Box::new(move || {
let style = &config.style;
let statuses = repo.statuses(Some(&mut git2_opts::status(&repo)?))?;
let untracked_files = statuses
.iter()
.filter(|status| status.status().is_wt_new())
.map(|status| PathBuf::from(status.path().unwrap()))
.collect::<Vec<_>>();
let unmerged_files = statuses
.iter()
.filter(|status| status.status().is_conflicted())
.map(|status| PathBuf::from(status.path().unwrap()))
.collect::<Vec<_>>();
let untracked = items_list(&config, untracked_files.clone());
let unmerged = items_list(&config, unmerged_files);
let items = if let Some(rebase) = git::rebase_status(&repo)? {
vec![Item {
id: "rebase_status".into(),
display: Line::styled(
format!("Rebasing {} onto {}", rebase.head_name, &rebase.onto),
&style.section_header,
),
..Default::default()
}]
.into_iter()
} else if let Some(merge) = git::merge_status(&repo)? {
vec![Item {
id: "merge_status".into(),
display: Line::styled(
format!("Merging {}", &merge.head),
&style.section_header,
),
..Default::default()
}]
.into_iter()
} else if let Some(revert) = git::revert_status(&repo)? {
vec![Item {
id: "revert_status".into(),
display: Line::styled(
format!("Reverting {}", &revert.head),
&style.section_header,
),
..Default::default()
}]
.into_iter()
} else {
branch_status_items(&config, &repo)?.into_iter()
}
.chain(if untracked.is_empty() {
vec![]
} else {
vec![
items::blank_line(),
Item {
id: "untracked".into(),
display: Line::styled("Untracked files", &style.section_header),
section: true,
depth: 0,
target_data: Some(TargetData::AllUntracked(untracked_files)),
..Default::default()
},
]
})
.chain(untracked)
.chain(if unmerged.is_empty() {
vec![]
} else {
vec![
items::blank_line(),
Item {
id: "unmerged".into(),
display: Line::styled("Unmerged", &style.section_header),
section: true,
depth: 0,
..Default::default()
},
]
})
.chain(unmerged)
.chain(create_status_section_items(
Rc::clone(&config),
"Unstaged changes",
Some(TargetData::AllUnstaged),
&git::diff_unstaged(&config, repo.as_ref())?,
))
.chain(create_status_section_items(
Rc::clone(&config),
"Staged changes",
Some(TargetData::AllStaged),
&git::diff_staged(&config, repo.as_ref())?,
))
.chain(create_stash_list_section_items(
Rc::clone(&config),
repo.as_ref(),
"Stashes",
))
.chain(create_log_section_items(
Rc::clone(&config),
repo.as_ref(),
"Recent commits",
))
.collect();
Ok(items)
}),
)
}
fn items_list(config: &Config, files: Vec<PathBuf>) -> Vec<Item> {
let style = &config.style;
files
.into_iter()
.map(|path| Item {
id: path.to_string_lossy().to_string().into(),
display: Line::styled(path.to_string_lossy().to_string(), &style.file_header),
depth: 1,
target_data: Some(items::TargetData::File(path)),
..Default::default()
})
.collect::<Vec<_>>()
}
fn branch_status_items(config: &Config, repo: &Repository) -> Res<Vec<Item>> {
let style = &config.style;
let Ok(head) = repo.head() else {
return Ok(vec![Item {
id: "branch_status".into(),
display: Line::styled("No branch", &style.section_header),
section: true,
depth: 0,
..Default::default()
}]);
};
let mut items = vec![Item {
id: "branch_status".into(),
display: Line::styled(
format!("On branch {}", head.shorthand().unwrap()),
&style.section_header,
),
section: true,
depth: 0,
..Default::default()
}];
let Ok(upstream) = repo.branch_upstream_name(head.name().unwrap()) else {
return Ok(items);
};
let upstream_name = upstream.as_str().unwrap().to_string();
let upstream_shortname = upstream_name
.strip_prefix("refs/remotes/")
.unwrap_or(&upstream_name)
.to_string();
let Ok(upstream_id) = repo.refname_to_id(&upstream_name) else {
items.push(Item {
id: "branch_status".into(),
display: format!(
"Your branch is based on '{}', but the upstream is gone.",
upstream_shortname
)
.into(),
depth: 1,
unselectable: true,
..Default::default()
});
return Ok(items);
};
let (ahead, behind) = repo.graph_ahead_behind(head.target().unwrap(), upstream_id)?;
items.push(Item {
id: "branch_status".into(),
display: if ahead == 0 && behind == 0 {
Line::raw(format!("Your branch is up to date with '{}'.", upstream_shortname))
} else if ahead > 0 && behind == 0 {
Line::raw(format!(
"Your branch is ahead of '{}' by {} commit.",
upstream_shortname, ahead
))
} else if ahead == 0 && behind > 0 {
Line::raw(format!(
"Your branch is behind '{}' by {} commit.",
upstream_shortname, behind
))
} else {
Line::raw(format!("Your branch and '{}' have diverged,\nand have {} and {} different commits each, respectively.", upstream_shortname, ahead, behind))
},
depth: 1,
unselectable: true,
..Default::default()
});
Ok(items)
}
fn create_status_section_items<'a>(
config: Rc<Config>,
header: &str,
header_data: Option<TargetData>,
diff: &'a Diff,
) -> impl Iterator<Item = Item> + 'a {
let style = &config.style;
if diff.deltas.is_empty() {
vec![]
} else {
vec![
Item {
display: Line::raw(""),
unselectable: true,
depth: 0,
..Default::default()
},
Item {
id: header.to_string().into(),
display: Line::from(vec![
Span::styled(header.to_string(), &style.section_header),
format!(" ({})", diff.deltas.len()).into(),
]),
section: true,
depth: 0,
target_data: header_data,
..Default::default()
},
]
}
.into_iter()
.chain(items::create_diff_items(config, diff, &1, true))
}
fn create_stash_list_section_items<'a>(
config: Rc<Config>,
repo: &Repository,
header: &str,
) -> impl Iterator<Item = Item> + 'a {
let stashes = items::stash_list(&config, repo, 10).unwrap();
if stashes.is_empty() {
vec![]
} else {
let style = &config.style;
vec![
items::blank_line(),
Item {
id: header.to_string().into(),
display: Line::styled(header.to_string(), &style.section_header),
section: true,
depth: 0,
..Default::default()
},
]
}
.into_iter()
.chain(stashes)
}
fn create_log_section_items<'a>(
config: Rc<Config>,
repo: &Repository,
header: &str,
) -> impl Iterator<Item = Item> + 'a {
let style = &config.style;
[
Item {
display: Line::raw(""),
depth: 0,
unselectable: true,
..Default::default()
},
Item {
id: header.to_string().into(),
display: Line::styled(header.to_string(), &style.section_header),
section: true,
depth: 0,
..Default::default()
},
]
.into_iter()
.chain(items::log(&config, repo, 10, None, None).unwrap())
}