diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-08-01 08:02:15 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-08-01 08:02:15 +0100 |
| commit | 6ba6177e895f7018b6b044a3d63442d1097a07dd (patch) | |
| tree | 8f126de09daab666a210d7d5fb94def7a7203652 /test_utils/src/git.rs | |
| parent | 1180f0b3c3157b6fd8eb7bf08a255a114e961e5f (diff) | |
test(remote): `recreate_as_bare` cp all branch
previously `recreate_as_bare` was using clone and therefore just
copying main branch
Diffstat (limited to 'test_utils/src/git.rs')
| -rw-r--r-- | test_utils/src/git.rs | 30 |
1 files changed, 26 insertions, 4 deletions
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 @@ | |||
| 1 | //create | 1 | //create |
| 2 | 2 | ||
| 3 | // implement drop? | 3 | // implement drop? |
| 4 | use std::{env::current_dir, fs, path::PathBuf}; | 4 | use std::{ |
| 5 | env::current_dir, | ||
| 6 | fs, | ||
| 7 | path::{Path, PathBuf}, | ||
| 8 | }; | ||
| 5 | 9 | ||
| 6 | use anyhow::{Context, Result}; | 10 | use anyhow::{Context, Result}; |
| 7 | use git2::{Oid, RepositoryInitOptions, Signature, Time}; | 11 | use git2::{Oid, RepositoryInitOptions, Signature, Time}; |
| @@ -58,9 +62,27 @@ impl GitTestRepo { | |||
| 58 | Self::new("main").unwrap() | 62 | Self::new("main").unwrap() |
| 59 | } | 63 | } |
| 60 | 64 | ||
| 61 | fn clone(existing_repo: &GitTestRepo) -> Result<Self> { | 65 | fn duplicate(existing_repo: &GitTestRepo) -> Result<Self> { |
| 62 | let path = current_dir()?.join(format!("tmpgit-{}", rand::random::<u64>())); | 66 | let path = current_dir()?.join(format!("tmpgit-{}", rand::random::<u64>())); |
| 63 | let git_repo = git2::Repository::clone(existing_repo.dir.to_str().unwrap(), path.clone())?; | 67 | // function source: https://stackoverflow.com/a/65192210 |
| 68 | fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> { | ||
| 69 | fs::create_dir_all(&dst)?; | ||
| 70 | for entry in fs::read_dir(src)? { | ||
| 71 | let entry = entry?; | ||
| 72 | let ty = entry.file_type()?; | ||
| 73 | if ty.is_dir() { | ||
| 74 | copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; | ||
| 75 | } else { | ||
| 76 | fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | Ok(()) | ||
| 80 | } | ||
| 81 | copy_dir_all(existing_repo.dir.clone(), path.clone())?; | ||
| 82 | let git_repo = git2::Repository::open(path.clone())?; | ||
| 83 | |||
| 84 | // let git_repo = git2::Repository::clone(existing_repo.dir.to_str().unwrap(), | ||
| 85 | // path.clone())?; | ||
| 64 | Ok(Self { | 86 | Ok(Self { |
| 65 | dir: path, | 87 | dir: path, |
| 66 | git_repo, | 88 | git_repo, |
| @@ -78,7 +100,7 @@ impl GitTestRepo { | |||
| 78 | .mkpath(true), | 100 | .mkpath(true), |
| 79 | )?; | 101 | )?; |
| 80 | // clone existing to a temp repo | 102 | // clone existing to a temp repo |
| 81 | let tmp_repo = Self::clone(existing_repo)?; | 103 | let tmp_repo = Self::duplicate(existing_repo)?; |
| 82 | // add bare as a remote and push branches | 104 | // add bare as a remote and push branches |
| 83 | let mut remote = tmp_repo.git_repo.remote("tmp", path.to_str().unwrap())?; | 105 | let mut remote = tmp_repo.git_repo.remote("tmp", path.to_str().unwrap())?; |
| 84 | let refspecs = tmp_repo | 106 | let refspecs = tmp_repo |