diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-31 13:38:07 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-31 13:38:07 +0100 |
| commit | cf8e2b059f94739c6f93369f73334b5227e349e1 (patch) | |
| tree | cc4f2442a141c18c47110fba1b36aa52c2170412 | |
| parent | 497a9fa40bffe8ce41a1c658a520be0690d98377 (diff) | |
test(remote): add `recreate_as_bare` helper
as gitlib2 cannot push to a non bare git repo in a directory
| -rw-r--r-- | test_utils/src/git.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test_utils/src/git.rs b/test_utils/src/git.rs index 5e8aed5..c7047cf 100644 --- a/test_utils/src/git.rs +++ b/test_utils/src/git.rs | |||
| @@ -58,6 +58,49 @@ impl GitTestRepo { | |||
| 58 | Self::new("main").unwrap() | 58 | Self::new("main").unwrap() |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | fn clone(existing_repo: &GitTestRepo) -> Result<Self> { | ||
| 62 | 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())?; | ||
| 64 | Ok(Self { | ||
| 65 | dir: path, | ||
| 66 | git_repo, | ||
| 67 | }) | ||
| 68 | } | ||
| 69 | |||
| 70 | pub fn recreate_as_bare(existing_repo: &GitTestRepo) -> Result<Self> { | ||
| 71 | // create bare repo | ||
| 72 | let path = current_dir()?.join(format!("tmpgit-{}", rand::random::<u64>())); | ||
| 73 | let git_repo = git2::Repository::init_opts( | ||
| 74 | &path, | ||
| 75 | RepositoryInitOptions::new() | ||
| 76 | .initial_head("main") | ||
| 77 | .bare(true) | ||
| 78 | .mkpath(true), | ||
| 79 | )?; | ||
| 80 | // clone existing to a temp repo | ||
| 81 | let tmp_repo = Self::clone(existing_repo)?; | ||
| 82 | // add bare as a remote and push branches | ||
| 83 | let mut remote = tmp_repo.git_repo.remote("tmp", path.to_str().unwrap())?; | ||
| 84 | let refspecs = tmp_repo | ||
| 85 | .git_repo | ||
| 86 | .branches(Some(git2::BranchType::Local))? | ||
| 87 | .filter_map(|b| b.ok()) | ||
| 88 | .map(|(b, _)| { | ||
| 89 | format!( | ||
| 90 | "refs/heads/{}:refs/heads/{}", | ||
| 91 | b.name().unwrap().unwrap(), | ||
| 92 | b.name().unwrap().unwrap(), | ||
| 93 | ) | ||
| 94 | }) | ||
| 95 | .collect::<Vec<String>>(); | ||
| 96 | remote.push(&refspecs, None)?; | ||
| 97 | // TODO: push tags | ||
| 98 | Ok(Self { | ||
| 99 | dir: path, | ||
| 100 | git_repo, | ||
| 101 | }) | ||
| 102 | } | ||
| 103 | |||
| 61 | pub fn initial_commit(&self) -> Result<Oid> { | 104 | pub fn initial_commit(&self) -> Result<Oid> { |
| 62 | let oid = self.git_repo.index()?.write_tree()?; | 105 | let oid = self.git_repo.index()?.write_tree()?; |
| 63 | let tree = self.git_repo.find_tree(oid)?; | 106 | let tree = self.git_repo.find_tree(oid)?; |