-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.rs
57 lines (53 loc) · 2.11 KB
/
testing.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
#[macro_export]
macro_rules! git_test {
($($name:literal: [$($path:literal => $content:expr),*])* $(staged: [$($spath:literal => $scontent:expr),*])? $(working: [$($wdpath:literal => $wdcontent:expr),*])?) => {{
let tempdir = ::tempfile::tempdir().unwrap();
let repo = ::git2::Repository::init(tempdir.path()).unwrap();
#[allow(unused_variables)]
let signature = ::git2::Signature::new("Example User", "test@example.com", &::git2::Time::new(0, 0)).unwrap();
#[allow(unused_variables, unused_mut)]
let mut index = repo.index().unwrap();
$({
$({
let path = tempdir.path().join($path);
::std::fs::create_dir_all(path.parent().unwrap()).unwrap();
::std::fs::write(path, $content).unwrap();
})*
index
.add_all(["."].iter(), git2::IndexAddOption::DEFAULT, None)
.unwrap();
index.write().unwrap();
let oid = index.write_tree().unwrap();
let tree = repo.find_tree(oid).unwrap();
let parents = if let Ok(Ok(parent_commit)) = repo.head().map(|head| head.peel_to_commit()) {
vec![parent_commit]
} else {
vec![]
};
repo.commit(
Some("HEAD"),
&signature,
&signature,
$name,
&tree,
&parents.iter().collect::<Vec<_>>(),
).unwrap();
})*
$($({
let path = tempdir.path().join($spath);
::std::fs::create_dir_all(path.parent().unwrap()).unwrap();
::std::fs::write(path, $scontent).unwrap();
index
.add_all(["."].iter(), git2::IndexAddOption::DEFAULT, None)
.unwrap();
index.write().unwrap();
})*)?
$($({
let path = tempdir.path().join($wdpath);
::std::fs::create_dir_all(path.parent().unwrap()).unwrap();
::std::fs::write(path, $wdcontent).unwrap();
})*)?
(tempdir, repo)
}}
}
pub use git_test;