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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#![deny(missing_docs)]
use std::ffi::OsString;
use std::{
collections::BTreeMap,
convert::Infallible,
io::Read,
path::{Path, PathBuf},
time::Duration,
};
pub use bstr;
use bstr::{BStr, ByteSlice};
use io_close::Close;
pub use is_ci;
use nom::error::VerboseError;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
pub use tempfile;
pub type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
static SCRIPT_IDENTITY: Lazy<Mutex<BTreeMap<PathBuf, u32>>> = Lazy::new(|| Mutex::new(BTreeMap::new()));
static EXCLUDE_LUT: Lazy<Mutex<Option<git_worktree::fs::Cache<'static>>>> = Lazy::new(|| {
let cache = (|| {
let (repo_path, _) = git_discover::upwards(Path::new(".")).ok()?;
let (git_dir, work_tree) = repo_path.into_repository_and_work_tree_directories();
let work_tree = work_tree?.canonicalize().ok()?;
let mut buf = Vec::with_capacity(512);
let case = git_worktree::fs::Capabilities::probe(&work_tree)
.ignore_case
.then(|| git_attributes::glob::pattern::Case::Fold)
.unwrap_or_default();
let state = git_worktree::fs::cache::State::IgnoreStack(git_worktree::fs::cache::state::Ignore::new(
Default::default(),
git_attributes::MatchGroup::<git_attributes::Ignore>::from_git_dir(git_dir, None, &mut buf).ok()?,
None,
case,
));
Some(git_worktree::fs::Cache::new(
work_tree,
state,
case,
buf,
Default::default(),
))
})();
Mutex::new(cache)
});
pub enum Creation {
CopyFromReadOnly,
ExecuteScript,
}
pub fn run_git(working_dir: &Path, args: &[&str]) -> std::io::Result<std::process::ExitStatus> {
std::process::Command::new("git")
.current_dir(working_dir)
.args(args)
.status()
}
pub fn hex_to_id(hex: &str) -> git_hash::ObjectId {
git_hash::ObjectId::from_hex(hex.as_bytes()).expect("40 bytes hex")
}
pub fn fixture_path(path: impl AsRef<Path>) -> PathBuf {
PathBuf::from("tests").join("fixtures").join(path.as_ref())
}
pub fn fixture_bytes(path: impl AsRef<Path>) -> Vec<u8> {
match std::fs::read(fixture_path(path.as_ref())) {
Ok(res) => res,
Err(_) => panic!("File at '{}' not found", path.as_ref().display()),
}
}
pub fn scripted_fixture_repo_read_only(script_name: impl AsRef<Path>) -> Result<PathBuf> {
scripted_fixture_repo_read_only_with_args(script_name, None)
}
pub fn scripted_fixture_repo_writable(script_name: &str) -> Result<tempfile::TempDir> {
scripted_fixture_repo_writable_with_args(script_name, None, Creation::CopyFromReadOnly)
}
pub fn scripted_fixture_repo_writable_with_args(
script_name: &str,
args: impl IntoIterator<Item = &'static str>,
mode: Creation,
) -> Result<tempfile::TempDir> {
let dst = tempfile::TempDir::new()?;
Ok(match mode {
Creation::CopyFromReadOnly => {
let ro_dir = scripted_fixture_repo_read_only_with_args_inner(script_name, args, None)?;
copy_recursively_into_existing_dir(&ro_dir, dst.path())?;
dst
}
Creation::ExecuteScript => {
scripted_fixture_repo_read_only_with_args_inner(script_name, args, dst.path().into())?;
dst
}
})
}
pub fn copy_recursively_into_existing_dir(src_dir: impl AsRef<Path>, dst_dir: impl AsRef<Path>) -> std::io::Result<()> {
fs_extra::copy_items(
&std::fs::read_dir(src_dir)?
.map(|e| e.map(|e| e.path()))
.collect::<std::result::Result<Vec<_>, _>>()?,
dst_dir,
&fs_extra::dir::CopyOptions {
overwrite: false,
skip_exist: false,
copy_inside: false,
content_only: false,
..Default::default()
},
)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
Ok(())
}
pub fn scripted_fixture_repo_read_only_with_args(
script_name: impl AsRef<Path>,
args: impl IntoIterator<Item = &'static str>,
) -> Result<PathBuf> {
scripted_fixture_repo_read_only_with_args_inner(script_name, args, None)
}
fn scripted_fixture_repo_read_only_with_args_inner(
script_name: impl AsRef<Path>,
args: impl IntoIterator<Item = &'static str>,
destination_dir: Option<&Path>,
) -> Result<PathBuf> {
git_lock::tempfile::setup(
git_lock::tempfile::SignalHandlerMode::DeleteTempfilesOnTerminationAndRestoreDefaultBehaviour,
);
let script_location = script_name.as_ref();
let script_path = fixture_path(script_location);
let args: Vec<String> = args.into_iter().map(Into::into).collect();
let mut map = SCRIPT_IDENTITY.lock();
let script_identity = map
.entry(args.iter().fold(script_path.clone(), |p, a| p.join(a)))
.or_insert_with(|| {
let crc_value = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);
let mut crc_digest = crc_value.digest();
crc_digest.update(&std::fs::read(&script_path).expect("file can be read entirely"));
for arg in args.iter() {
crc_digest.update(arg.as_bytes());
}
crc_digest.finalize()
})
.to_owned();
let script_basename = script_location.file_stem().unwrap_or(script_location.as_os_str());
let archive_file_path = fixture_path(
Path::new("generated-archives").join(format!("{}.tar.xz", script_basename.to_str().expect("valid UTF-8"))),
);
let (force_run, script_result_directory) = destination_dir.map(|d| (true, d.to_owned())).unwrap_or_else(|| {
let dir = fixture_path(Path::new("generated-do-not-edit").join(script_basename).join(format!(
"{}-{}",
script_identity,
family_name()
)));
(false, dir)
});
let _marker = git_lock::Marker::acquire_to_hold_resource(
script_basename,
git_lock::acquire::Fail::AfterDurationWithBackoff(Duration::from_secs(if cfg!(windows) { 3 * 60 } else { 30 })),
None,
)?;
let failure_marker = script_result_directory.join("_invalid_state_due_to_script_failure_");
if force_run || !script_result_directory.is_dir() || failure_marker.is_file() {
if failure_marker.is_file() {
std::fs::remove_dir_all(&script_result_directory)?;
}
std::fs::create_dir_all(&script_result_directory)?;
match extract_archive(&archive_file_path, &script_result_directory, script_identity) {
Ok((archive_id, platform)) => {
eprintln!(
"Extracted fixture from archive '{}' ({}, {:?})",
archive_file_path.display(),
archive_id,
platform
)
}
Err(err) => {
if err.kind() != std::io::ErrorKind::NotFound {
eprintln!("failed to extract '{}': {}", archive_file_path.display(), err);
} else if !is_excluded(&archive_file_path) {
eprintln!(
"Archive at '{}' not found, creating fixture using script '{}'",
archive_file_path.display(),
script_location.display()
);
}
let script_absolute_path = std::env::current_dir()?.join(script_path);
let output = std::process::Command::new("bash")
.arg(script_absolute_path)
.args(args)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.current_dir(&script_result_directory)
.env_remove("GIT_DIR")
.env("GIT_AUTHOR_DATE", "2000-01-01 00:00:00 +0000")
.env("GIT_AUTHOR_EMAIL", "author@example.com")
.env("GIT_AUTHOR_NAME", "author")
.env("GIT_COMMITTER_DATE", "2000-01-02 00:00:00 +0000")
.env("GIT_COMMITTER_EMAIL", "committer@example.com")
.env("GIT_COMMITTER_NAME", "committer")
.env("GIT_CONFIG_COUNT", "2")
.env("GIT_CONFIG_KEY_0", "commit.gpgsign")
.env("GIT_CONFIG_VALUE_0", "false")
.env("GIT_CONFIG_KEY_1", "init.defaultBranch")
.env("GIT_CONFIG_VALUE_1", "main")
.output()?;
if !output.status.success() {
write_failure_marker(&failure_marker);
}
assert!(
output.status.success(),
"repo script failed: stdout: {}\nstderr: {}",
output.stdout.as_bstr(),
output.stderr.as_bstr()
);
create_archive_if_not_on_ci(&script_result_directory, &archive_file_path, script_identity).map_err(
|err| {
write_failure_marker(&failure_marker);
err
},
)?;
}
}
}
Ok(script_result_directory)
}
fn write_failure_marker(failure_marker: &Path) {
std::fs::write(failure_marker, &[]).ok();
}
fn create_archive_if_not_on_ci(source_dir: &Path, archive: &Path, script_identity: u32) -> std::io::Result<()> {
if is_ci::cached() {
return Ok(());
}
if is_excluded(archive) {
return Ok(());
}
std::fs::create_dir_all(archive.parent().expect("archive is a file"))?;
let meta_dir = populate_meta_dir(source_dir, script_identity)?;
let res = (move || {
let mut buf = Vec::<u8>::new();
{
let mut ar = tar::Builder::new(&mut buf);
ar.mode(tar::HeaderMode::Deterministic);
ar.follow_symlinks(false);
ar.append_dir_all(".", source_dir)?;
ar.finish()?;
}
let archive = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(archive)?;
let mut xz_write = xz2::write::XzEncoder::new(archive, 3);
std::io::copy(&mut &*buf, &mut xz_write)?;
xz_write.finish()?.close()
})();
#[cfg(not(windows))]
std::fs::remove_dir_all(meta_dir)?;
#[cfg(windows)]
std::fs::remove_dir_all(meta_dir).ok();
res
}
fn is_excluded(archive: &Path) -> bool {
let mut lut = EXCLUDE_LUT.lock();
lut.as_mut()
.and_then(|cache| {
let archive = std::env::current_dir().ok()?.join(archive);
let relative_path = archive.strip_prefix(cache.base()).ok()?;
cache
.at_path(
relative_path,
Some(false),
|_oid, _buf| -> std::result::Result<_, Infallible> { unreachable!("") },
)
.ok()?
.is_excluded()
.into()
})
.unwrap_or(false)
}
const META_DIR_NAME: &str = "__gitoxide_meta__";
const META_IDENTITY: &str = "identity";
const META_GIT_VERSION: &str = "git-version";
fn populate_meta_dir(destination_dir: &Path, script_identity: u32) -> std::io::Result<PathBuf> {
let meta_dir = destination_dir.join(META_DIR_NAME);
std::fs::create_dir_all(&meta_dir)?;
std::fs::write(
meta_dir.join(META_IDENTITY),
format!("{}-{}", script_identity, family_name()).as_bytes(),
)?;
std::fs::write(
meta_dir.join(META_GIT_VERSION),
&std::process::Command::new("git").arg("--version").output()?.stdout,
)?;
Ok(meta_dir)
}
fn extract_archive(
archive: &Path,
destination_dir: &Path,
required_script_identity: u32,
) -> std::io::Result<(u32, Option<String>)> {
let archive_buf: Vec<u8> = {
let mut buf = Vec::new();
let input_archive = std::fs::File::open(archive)?;
if std::env::var_os("GITOXIDE_TEST_IGNORE_ARCHIVES").is_some() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Ignoring archive at '{}' as GITOXIDE_TEST_IGNORE_ARCHIVES is set.",
archive.display()
),
));
}
let mut decoder = xz2::bufread::XzDecoder::new(std::io::BufReader::new(input_archive));
std::io::copy(&mut decoder, &mut buf)?;
buf
};
let mut entry_buf = Vec::<u8>::new();
let (archive_identity, platform): (u32, _) = tar::Archive::new(std::io::Cursor::new(&mut &*archive_buf))
.entries_with_seek()?
.filter_map(|e| e.ok())
.find_map(|mut e: tar::Entry<'_, _>| {
let path = e.path().ok()?;
if path.parent()?.file_name()? == META_DIR_NAME && path.file_name()? == META_IDENTITY {
entry_buf.clear();
e.read_to_end(&mut entry_buf).ok()?;
let mut tokens = entry_buf.to_str().ok()?.trim().splitn(2, '-');
match (tokens.next(), tokens.next()) {
(Some(id), platform) => Some((id.parse().ok()?, platform.map(ToOwned::to_owned))),
_ => None,
}
} else {
None
}
})
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::Other,
"BUG: Could not find meta directory in our own archive",
)
})?;
if archive_identity != required_script_identity {
return Err(std::io::ErrorKind::NotFound.into());
}
for entry in tar::Archive::new(&mut &*archive_buf).entries()? {
let mut entry = entry?;
let path = entry.path()?;
if path.to_str() == Some(META_DIR_NAME) || path.parent().and_then(|p| p.to_str()) == Some(META_DIR_NAME) {
continue;
}
entry.unpack_in(&destination_dir)?;
}
Ok((archive_identity, platform))
}
pub fn to_bstr_err(err: nom::Err<VerboseError<&[u8]>>) -> VerboseError<&BStr> {
let err = match err {
nom::Err::Error(err) | nom::Err::Failure(err) => err,
nom::Err::Incomplete(_) => unreachable!("not a streaming parser"),
};
VerboseError {
errors: err.errors.into_iter().map(|(i, v)| (i.as_bstr(), v)).collect(),
}
}
fn family_name() -> &'static str {
if cfg!(windows) {
"windows"
} else {
"unix"
}
}
#[derive(Default)]
pub struct Env<'a> {
altered_vars: Vec<(&'a str, Option<OsString>)>,
}
impl<'a> Env<'a> {
pub fn new() -> Self {
Env {
altered_vars: Vec::new(),
}
}
pub fn set(mut self, var: &'a str, value: impl Into<String>) -> Self {
let prev = std::env::var_os(var);
std::env::set_var(var, value.into());
self.altered_vars.push((var, prev));
self
}
}
impl<'a> Drop for Env<'a> {
fn drop(&mut self) {
for (var, prev_value) in &self.altered_vars {
match prev_value {
Some(value) => std::env::set_var(var, value),
None => std::env::remove_var(var),
}
}
}
}