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
|
//! **libgfold** provides the ability to find a minimal set of user-relevant information for git
//! repositories on a local filesystem.
//!
//! This library powers [**gfold**](https://github.com/nickgerace/gfold).
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub,
bad_style,
dead_code,
improper_ctypes,
non_shorthand_field_patterns,
no_mangle_generic_items,
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
private_in_public,
unconditional_recursion,
unused,
unused_allocation,
unused_comparisons,
unused_parens,
while_true,
clippy::missing_panics_doc
)]
pub mod collector;
pub mod repository_view;
pub mod status;
pub use collector::RepositoryCollection;
pub use collector::RepositoryCollector;
pub use repository_view::RepositoryView;
pub use status::Status;
#[cfg(test)]
mod tests {
use super::*;
use git2::ErrorCode;
use git2::Oid;
use git2::Repository;
use git2::Signature;
use log::LevelFilter;
use pretty_assertions::assert_eq;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::{fs, io};
use tempfile::tempdir;
/// This integration test for `gfold` covers an end-to-end usage scenario. It uses the
/// [`tempfile`](tempfile) crate to create some repositories with varying states and levels
/// of nesting.
#[test]
fn integration() -> anyhow::Result<()> {
env_logger::builder()
.is_test(true)
.filter_level(LevelFilter::Info)
.try_init()?;
// Temporary directory structure:
// └── root
// ├── one (repo)
// │ └── file
// ├── two (repo)
// ├── three (repo)
// └── nested
// ├── four (repo)
// ├── five (repo)
// │ └── file
// ├── six (repo)
// └── seven (repo)
let root = tempdir()?;
let repo_one = create_directory(&root, "one")?;
let repo_two = create_directory(&root, "two")?;
let repo_three = create_directory(&root, "three")?;
let nested = create_directory(&root, "nested")?;
let repo_four = create_directory(&nested, "four")?;
let repo_five = create_directory(&nested, "five")?;
let repo_six = create_directory(&nested, "six")?;
let repo_seven = create_directory(&nested, "seven")?;
// Repo One
Repository::init(&repo_one)?;
create_file(&repo_one)?;
// Repo Two
Repository::init(&repo_two)?;
// Repo Three
Repository::init(&repo_three)?;
// Repo Four
let repository = Repository::init(&repo_four)?;
if let Err(e) = repository.remote("origin", "https://github.com/nickgerace/gfold") {
if e.code() != ErrorCode::Exists {
return Err(e.into());
}
}
// Repo Five
Repository::init(&repo_five)?;
create_file(&repo_five)?;
// Repo Six
let repository = Repository::init(&repo_six)?;
if let Err(e) = repository.remote("fork", "https://github.com/nickgerace/gfold") {
if e.code() != ErrorCode::Exists {
return Err(e.into());
}
}
commit_head_and_create_branch(&repository, "feat")?;
// Repo Seven
let repository = Repository::init(&repo_seven)?;
if let Err(e) = repository.remote("origin", "https://github.com/nickgerace/gfold") {
if e.code() != ErrorCode::Exists {
return Err(e.into());
}
}
commit_head_and_create_branch(&repository, "needtopush")?;
repository.set_head("refs/heads/needtopush")?;
// Generate the collection directly with a default config and ensure the resulting views
// match what we expect.
let mut expected_collection = RepositoryCollection::new();
let expected_views_key = root
.path()
.to_str()
.expect("could not convert PathBuf to &str")
.to_string();
let mut expected_views = vec![
RepositoryView::finalize(
&repo_one,
Some("HEAD".to_string()),
Status::Unclean,
None,
None,
Vec::with_capacity(0),
)?,
RepositoryView::finalize(
&repo_two,
Some("HEAD".to_string()),
Status::Clean,
None,
None,
Vec::with_capacity(0),
)?,
RepositoryView::finalize(
&repo_three,
Some("HEAD".to_string()),
Status::Clean,
None,
None,
Vec::with_capacity(0),
)?,
];
expected_views.sort_by(|a, b| a.name.cmp(&b.name));
expected_collection.insert(Some(expected_views_key), expected_views);
// Add nested views to the expected collection.
let nested_expected_views_key = nested
.to_str()
.expect("could not convert PathBuf to &str")
.to_string();
let mut nested_expected_views_raw = vec![
RepositoryView::finalize(
&repo_four,
Some("HEAD".to_string()),
Status::Clean,
Some("https://github.com/nickgerace/gfold".to_string()),
None,
Vec::with_capacity(0),
)?,
RepositoryView::finalize(
&repo_five,
Some("HEAD".to_string()),
Status::Unclean,
None,
None,
Vec::with_capacity(0),
)?,
RepositoryView::finalize(
&repo_six,
Some("master".to_string()),
Status::Unpushed,
Some("https://github.com/nickgerace/gfold".to_string()),
None,
Vec::with_capacity(0),
)?,
RepositoryView::finalize(
&repo_seven,
Some("needtopush".to_string()),
Status::Unpushed,
Some("https://github.com/nickgerace/gfold".to_string()),
None,
Vec::with_capacity(0),
)?,
];
nested_expected_views_raw.sort_by(|a, b| a.name.cmp(&b.name));
expected_collection.insert(Some(nested_expected_views_key), nested_expected_views_raw);
// Generate a collection.
let found_collection = RepositoryCollector::run(root.path(), false, false)?;
// Ensure the found collection matches our expected one. Sort the collection for the
// assertion.
let mut found_collection_sorted = RepositoryCollection::new();
for (key, mut value) in found_collection {
value.sort_by(|a, b| a.name.cmp(&b.name));
found_collection_sorted.insert(key, value);
}
assert_eq!(
expected_collection, // expected
found_collection_sorted // actual
);
Ok(())
}
fn create_directory<P: AsRef<Path>>(parent: P, name: &str) -> io::Result<PathBuf> {
let parent = parent.as_ref();
let new_directory = parent.join(name);
if let Err(e) = fs::create_dir(&new_directory) {
if e.kind() != io::ErrorKind::AlreadyExists {
return Err(e);
}
}
Ok(new_directory)
}
fn create_file<P: AsRef<Path>>(parent: P) -> io::Result<()> {
let parent = parent.as_ref();
File::create(parent.join("file"))?;
Ok(())
}
fn commit_head_and_create_branch(repository: &Repository, name: &str) -> anyhow::Result<()> {
// We need to commit at least once before branching.
let commit_oid = commit(repository, "HEAD")?;
let commit = repository.find_commit(commit_oid)?;
repository.branch(name, &commit, true)?;
Ok(())
}
// Source: https://github.com/rust-lang/git2-rs/pull/885
fn commit(repository: &Repository, update_ref: &str) -> anyhow::Result<Oid> {
// We will commit the contents of the index.
let mut index = repository.index()?;
let tree_oid = index.write_tree()?;
let tree = repository.find_tree(tree_oid)?;
// If this is the first commit, there is no parent. If the object returned by
// "revparse_single" cannot be converted into a commit, then it isn't a commit and we know
// there is no parent _commit_.
let maybe_parent = match repository.revparse_single("HEAD") {
Ok(object) => match object.into_commit() {
Ok(commit) => Some(commit),
Err(_) => None,
},
Err(e) if e.code() == ErrorCode::NotFound => None,
Err(e) => return Err(e.into()),
};
let mut parents = Vec::new();
if let Some(parent) = maybe_parent.as_ref() {
parents.push(parent);
};
let signature = Signature::now("Bob", "bob@bob")?;
Ok(repository.commit(
Some(update_ref),
&signature,
&signature,
"hello",
&tree,
parents.as_ref(),
)?)
}
}
|