upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/test_utils
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils')
-rw-r--r--test_utils/src/git.rs43
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)?;