git/testutil/
with_temp_repository.rs1#![cfg(not(tarpaulin_include))]
2
3use std::path::Path;
4
5use tempfile::Builder;
6
7use crate::{testutil::JAN_2021_EPOCH, Repository};
8
9pub(crate) fn with_temporary_path<F>(callback: F)
10where F: FnOnce(&Path) {
11 let temp_repository_directory = Builder::new().prefix("interactive-rebase-tool").tempdir().unwrap();
12 let path = temp_repository_directory.path();
13 callback(path);
14 temp_repository_directory.close().unwrap();
15}
16
17#[inline]
24pub fn with_temp_repository<F>(callback: F)
25where F: FnOnce(Repository) {
26 with_temporary_path(|path| {
27 let mut opts = git2::RepositoryInitOptions::new();
28 _ = opts.initial_head("main");
29 let repo = git2::Repository::init_opts(path, &opts).unwrap();
30
31 {
32 let id = repo.index().unwrap().write_tree().unwrap();
33 let tree = repo.find_tree(id).unwrap();
34 let sig = git2::Signature::new("name", "name@example.com", &git2::Time::new(JAN_2021_EPOCH, 0)).unwrap();
35 _ = repo
36 .commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[])
37 .unwrap();
38 };
39 callback(Repository::from(repo));
40 });
41}
42
43#[inline]
49#[allow(clippy::panic)]
50pub fn with_temp_bare_repository<F>(callback: F)
51where F: FnOnce(Repository) {
52 with_temporary_path(|path| {
53 let git2_repository = git2::Repository::init_bare(path).unwrap();
54 let repository = Repository::from(git2_repository);
55 callback(repository);
56 });
57}