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
303
304
305
306
307
308
309
310
311
312
313
use super::config;
use super::path;
use super::repo;
use comfy_table::{Cell, Table};
use std::path::Path;
fn add_table_header(table: &mut Table) {
table
.load_preset(comfy_table::presets::UTF8_FULL)
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS)
.set_header(vec![
Cell::new("Repo"),
Cell::new("Worktree"),
Cell::new("Status"),
Cell::new("Branches"),
Cell::new("HEAD"),
Cell::new("Remotes"),
]);
}
fn add_repo_status(
table: &mut Table,
repo_name: &str,
repo_handle: &repo::RepoHandle,
is_worktree: bool,
) -> Result<(), String> {
let repo_status = repo_handle.status(is_worktree)?;
table.add_row(vec![
repo_name,
match is_worktree {
true => "\u{2714}",
false => "",
},
&match is_worktree {
true => String::from(""),
false => match repo_status.changes {
Some(changes) => {
let mut out = Vec::new();
if changes.files_new > 0 {
out.push(format!("New: {}\n", changes.files_new))
}
if changes.files_modified > 0 {
out.push(format!("Modified: {}\n", changes.files_modified))
}
if changes.files_deleted > 0 {
out.push(format!("Deleted: {}\n", changes.files_deleted))
}
out.into_iter().collect::<String>().trim().to_string()
}
None => String::from("\u{2714}"),
},
},
repo_status
.branches
.iter()
.map(|(branch_name, remote_branch)| {
format!(
"branch: {}{}\n",
&branch_name,
&match remote_branch {
None => String::from(" <!local>"),
Some((remote_branch_name, remote_tracking_status)) => {
format!(
" <{}>{}",
remote_branch_name,
&match remote_tracking_status {
repo::RemoteTrackingStatus::UpToDate =>
String::from(" \u{2714}"),
repo::RemoteTrackingStatus::Ahead(d) => format!(" [+{}]", &d),
repo::RemoteTrackingStatus::Behind(d) => format!(" [-{}]", &d),
repo::RemoteTrackingStatus::Diverged(d1, d2) =>
format!(" [+{}/-{}]", &d1, &d2),
}
)
}
}
)
})
.collect::<String>()
.trim(),
&match is_worktree {
true => String::from(""),
false => match repo_status.head {
Some(head) => head,
None => String::from("Empty"),
},
},
repo_status
.remotes
.iter()
.map(|r| format!("{}\n", r))
.collect::<String>()
.trim(),
]);
Ok(())
}
pub fn get_worktree_status_table(
repo: &repo::RepoHandle,
directory: &Path,
) -> Result<(impl std::fmt::Display, Vec<String>), String> {
let worktrees = repo.get_worktrees()?;
let mut table = Table::new();
let mut errors = Vec::new();
add_worktree_table_header(&mut table);
for worktree in &worktrees {
let worktree_dir = &directory.join(&worktree.name());
if worktree_dir.exists() {
let repo = match repo::RepoHandle::open(worktree_dir, false) {
Ok(repo) => repo,
Err(error) => {
errors.push(format!(
"Failed opening repo of worktree {}: {}",
&worktree.name(),
&error
));
continue;
}
};
if let Err(error) = add_worktree_status(&mut table, worktree, &repo) {
errors.push(error);
}
} else {
errors.push(format!(
"Worktree {} does not have a directory",
&worktree.name()
));
}
}
for worktree in repo::RepoHandle::find_unmanaged_worktrees(repo, directory)? {
errors.push(format!(
"Found {}, which is not a valid worktree directory!",
&worktree
));
}
Ok((table, errors))
}
pub fn get_status_table(config: config::Config) -> Result<(Vec<Table>, Vec<String>), String> {
let mut errors = Vec::new();
let mut tables = Vec::new();
for tree in config.trees()? {
let repos = tree.repos.unwrap_or_default();
let root_path = path::expand_path(Path::new(&tree.root));
let mut table = Table::new();
add_table_header(&mut table);
for repo in &repos {
let repo_path = root_path.join(&repo.name);
if !repo_path.exists() {
errors.push(format!(
"{}: Repository does not exist. Run sync?",
&repo.name
));
continue;
}
let repo_handle = repo::RepoHandle::open(&repo_path, repo.worktree_setup);
let repo_handle = match repo_handle {
Ok(repo) => repo,
Err(error) => {
if error.kind == repo::RepoErrorKind::NotFound {
errors.push(format!(
"{}: No git repository found. Run sync?",
&repo.name
));
} else {
errors.push(format!(
"{}: Opening repository failed: {}",
&repo.name, error
));
}
continue;
}
};
add_repo_status(&mut table, &repo.name, &repo_handle, repo.worktree_setup)?;
}
tables.push(table);
}
Ok((tables, errors))
}
fn add_worktree_table_header(table: &mut Table) {
table
.load_preset(comfy_table::presets::UTF8_FULL)
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS)
.set_header(vec![
Cell::new("Worktree"),
Cell::new("Status"),
Cell::new("Branch"),
Cell::new("Remote branch"),
]);
}
fn add_worktree_status(
table: &mut Table,
worktree: &repo::Worktree,
repo: &repo::RepoHandle,
) -> Result<(), String> {
let repo_status = repo.status(false)?;
let local_branch = repo
.head_branch()
.map_err(|error| format!("Failed getting head branch: {}", error))?;
let upstream_output = match local_branch.upstream() {
Ok(remote_branch) => {
let remote_branch_name = remote_branch
.name()
.map_err(|error| format!("Failed getting name of remote branch: {}", error))?;
let (ahead, behind) = repo
.graph_ahead_behind(&local_branch, &remote_branch)
.map_err(|error| format!("Failed computing branch deviation: {}", error))?;
format!(
"{}{}\n",
&remote_branch_name,
&match (ahead, behind) {
(0, 0) => String::from(""),
(d, 0) => format!(" [+{}]", &d),
(0, d) => format!(" [-{}]", &d),
(d1, d2) => format!(" [+{}/-{}]", &d1, &d2),
},
)
}
Err(_) => String::from(""),
};
table.add_row(vec![
worktree.name(),
&match repo_status.changes {
Some(changes) => {
let mut out = Vec::new();
if changes.files_new > 0 {
out.push(format!("New: {}\n", changes.files_new))
}
if changes.files_modified > 0 {
out.push(format!("Modified: {}\n", changes.files_modified))
}
if changes.files_deleted > 0 {
out.push(format!("Deleted: {}\n", changes.files_deleted))
}
out.into_iter().collect::<String>().trim().to_string()
}
None => String::from("\u{2714}"),
},
&local_branch
.name()
.map_err(|error| format!("Failed getting name of branch: {}", error))?,
&upstream_output,
]);
Ok(())
}
pub fn show_single_repo_status(
path: &Path,
) -> Result<(impl std::fmt::Display, Vec<String>), String> {
let mut table = Table::new();
let mut warnings = Vec::new();
let is_worktree = repo::RepoHandle::detect_worktree(path);
add_table_header(&mut table);
let repo_handle = repo::RepoHandle::open(path, is_worktree);
if let Err(error) = repo_handle {
if error.kind == repo::RepoErrorKind::NotFound {
return Err(String::from("Directory is not a git directory"));
} else {
return Err(format!("Opening repository failed: {}", error));
}
};
let repo_name = match path.file_name() {
None => {
warnings.push(format!(
"Cannot detect repo name for path {}. Are you working in /?",
&path.display()
));
String::from("unknown")
}
Some(file_name) => match file_name.to_str() {
None => {
warnings.push(format!(
"Name of repo directory {} is not valid UTF-8",
&path.display()
));
String::from("invalid")
}
Some(name) => name.to_string(),
},
};
add_repo_status(&mut table, &repo_name, &repo_handle.unwrap(), is_worktree)?;
Ok((table, warnings))
}