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
use std::error::Error;
use std::ffi::OsStr;
use std::path::Path;
use git2::{ErrorClass, ErrorCode, Repository, StatusOptions};
use log::debug;
use walkdir::WalkDir;
const UNKNOWN: &str = "unknown";
#[derive(Eq, PartialEq, Ord, PartialOrd)]
struct Outcome {
name: String,
status: String,
branch: String,
source: String,
}
pub fn run(path: &Path) -> Result<(), Box<dyn Error>> {
let mut vec = Vec::new();
let mut max_name: usize = 0;
let mut max_status: usize = 0;
let mut max_branch: usize = 0;
let mut max_source: usize = 0;
for item in WalkDir::new(path)
.into_iter()
.filter_entry(|e| {
!e.file_name()
.to_str()
.map(|s| s.starts_with('.'))
.unwrap_or(false)
&& match e.path().parent() {
Some(s) => Repository::open(s).is_err(),
None => false,
}
})
.filter_map(|e| e.ok())
{
let entry = item.into_path();
let path = path.to_path_buf();
if let Ok(repository) = Repository::open(&entry) {
let origin = match repository.find_remote("origin") {
Ok(o) => o,
Err(e) => {
debug!("{}", e);
continue;
}
};
let source = origin.url().unwrap_or("none");
let head = match repository.head() {
Ok(o) => o,
Err(e) => {
debug!("{}", e);
continue;
}
};
let branch = head.shorthand().unwrap_or("none");
let mut opts = StatusOptions::new();
let status = match repository.statuses(Some(&mut opts)) {
Ok(statuses) if statuses.is_empty() => "clean",
Ok(_) => "unclean",
Err(error)
if error.code() == ErrorCode::BareRepo
&& error.class() == ErrorClass::Repository =>
{
"bare"
}
Err(_) => "error",
};
let name = match entry.strip_prefix(&path) {
Ok(o) => match o.to_str() {
Some(s) if s.is_empty() => path
.file_name()
.unwrap_or_else(|| OsStr::new(UNKNOWN))
.to_str()
.unwrap_or(UNKNOWN),
Some(s) => s,
None => "none",
},
Err(e) => {
debug!("{}", e);
UNKNOWN
}
};
if name.len() > max_name {
max_name = name.len();
}
if status.len() > max_status {
max_status = status.len();
}
if branch.len() > max_branch {
max_branch = branch.len();
}
if source.len() > max_source {
max_source = source.len();
}
vec.push(Outcome {
name: name.to_string(),
status: status.to_string(),
branch: branch.to_string(),
source: source.to_string(),
});
}
}
vec.sort_unstable_by_key(|k| k.name.clone());
vec.sort_by_key(|k| k.status.clone());
vec.insert(
0,
Outcome {
name: String::from("NAME"),
status: String::from("STATUS"),
branch: String::from("BRANCH"),
source: String::from("SOURCE"),
},
);
for outcome in &vec {
println!(
"{:<width_name$} {:<width_status$} {:<width_branch$} {:<width_source$}",
outcome.name,
outcome.status,
outcome.branch,
outcome.source,
width_name = max_name,
width_status = max_status,
width_branch = max_branch,
width_source = max_source
);
}
Ok(())
}