From 6ba6177e895f7018b6b044a3d63442d1097a07dd Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 1 Aug 2024 08:02:15 +0100 Subject: test(remote): `recreate_as_bare` cp all branch previously `recreate_as_bare` was using clone and therefore just copying main branch --- test_utils/src/git.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'test_utils') diff --git a/test_utils/src/git.rs b/test_utils/src/git.rs index c7047cf..7dfa62b 100644 --- a/test_utils/src/git.rs +++ b/test_utils/src/git.rs @@ -1,7 +1,11 @@ //create // implement drop? -use std::{env::current_dir, fs, path::PathBuf}; +use std::{ + env::current_dir, + fs, + path::{Path, PathBuf}, +}; use anyhow::{Context, Result}; use git2::{Oid, RepositoryInitOptions, Signature, Time}; @@ -58,9 +62,27 @@ impl GitTestRepo { Self::new("main").unwrap() } - fn clone(existing_repo: &GitTestRepo) -> Result { + fn duplicate(existing_repo: &GitTestRepo) -> Result { let path = current_dir()?.join(format!("tmpgit-{}", rand::random::())); - let git_repo = git2::Repository::clone(existing_repo.dir.to_str().unwrap(), path.clone())?; + // function source: https://stackoverflow.com/a/65192210 + fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> std::io::Result<()> { + fs::create_dir_all(&dst)?; + for entry in fs::read_dir(src)? { + let entry = entry?; + let ty = entry.file_type()?; + if ty.is_dir() { + copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; + } else { + fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; + } + } + Ok(()) + } + copy_dir_all(existing_repo.dir.clone(), path.clone())?; + let git_repo = git2::Repository::open(path.clone())?; + + // let git_repo = git2::Repository::clone(existing_repo.dir.to_str().unwrap(), + // path.clone())?; Ok(Self { dir: path, git_repo, @@ -78,7 +100,7 @@ impl GitTestRepo { .mkpath(true), )?; // clone existing to a temp repo - let tmp_repo = Self::clone(existing_repo)?; + let tmp_repo = Self::duplicate(existing_repo)?; // add bare as a remote and push branches let mut remote = tmp_repo.git_repo.remote("tmp", path.to_str().unwrap())?; let refspecs = tmp_repo -- cgit v1.2.3