diff options
Diffstat (limited to 'test_utils/src/git.rs')
| -rw-r--r-- | test_utils/src/git.rs | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/test_utils/src/git.rs b/test_utils/src/git.rs index 166693d..af87a3a 100644 --- a/test_utils/src/git.rs +++ b/test_utils/src/git.rs | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | // implement drop? | 3 | // implement drop? |
| 4 | use std::{env::current_dir, fs, path::PathBuf}; | 4 | use std::{env::current_dir, fs, path::PathBuf}; |
| 5 | 5 | ||
| 6 | use anyhow::Result; | 6 | use anyhow::{Context, Result}; |
| 7 | use git2::{Oid, RepositoryInitOptions, Signature, Time}; | 7 | use git2::{Oid, RepositoryInitOptions, Signature, Time}; |
| 8 | 8 | ||
| 9 | pub struct GitTestRepo { | 9 | pub struct GitTestRepo { |
| @@ -53,7 +53,27 @@ impl GitTestRepo { | |||
| 53 | self.stage_and_commit("add t2.md") | 53 | self.stage_and_commit("add t2.md") |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | pub fn populate_with_test_branch(&self) -> Result<Oid> { | ||
| 57 | self.populate()?; | ||
| 58 | self.create_branch("add-example-feature")?; | ||
| 59 | fs::write(self.dir.join("f1.md"), "some content")?; | ||
| 60 | self.stage_and_commit("add f1.md")?; | ||
| 61 | fs::write(self.dir.join("f2.md"), "some content")?; | ||
| 62 | self.stage_and_commit("add f2.md")?; | ||
| 63 | fs::write(self.dir.join("f3.md"), "some content1")?; | ||
| 64 | self.stage_and_commit("add f3.md") | ||
| 65 | } | ||
| 66 | |||
| 56 | pub fn stage_and_commit(&self, message: &str) -> Result<Oid> { | 67 | pub fn stage_and_commit(&self, message: &str) -> Result<Oid> { |
| 68 | self.stage_and_commit_custom_signature(message, None, None) | ||
| 69 | } | ||
| 70 | |||
| 71 | pub fn stage_and_commit_custom_signature( | ||
| 72 | &self, | ||
| 73 | message: &str, | ||
| 74 | author: Option<&git2::Signature>, | ||
| 75 | commiter: Option<&git2::Signature>, | ||
| 76 | ) -> Result<Oid> { | ||
| 57 | let prev_oid = self.git_repo.head().unwrap().peel_to_commit()?; | 77 | let prev_oid = self.git_repo.head().unwrap().peel_to_commit()?; |
| 58 | 78 | ||
| 59 | let mut index = self.git_repo.index()?; | 79 | let mut index = self.git_repo.index()?; |
| @@ -62,8 +82,8 @@ impl GitTestRepo { | |||
| 62 | 82 | ||
| 63 | let oid = self.git_repo.commit( | 83 | let oid = self.git_repo.commit( |
| 64 | Some("HEAD"), | 84 | Some("HEAD"), |
| 65 | &joe_signature(), | 85 | author.unwrap_or(&joe_signature()), |
| 66 | &joe_signature(), | 86 | commiter.unwrap_or(&joe_signature()), |
| 67 | message, | 87 | message, |
| 68 | &self.git_repo.find_tree(index.write_tree()?)?, | 88 | &self.git_repo.find_tree(index.write_tree()?)?, |
| 69 | &[&prev_oid], | 89 | &[&prev_oid], |
| @@ -92,6 +112,40 @@ impl GitTestRepo { | |||
| 92 | let oid = self.git_repo.head()?.peel_to_commit()?.id(); | 112 | let oid = self.git_repo.head()?.peel_to_commit()?.id(); |
| 93 | Ok(oid) | 113 | Ok(oid) |
| 94 | } | 114 | } |
| 115 | |||
| 116 | pub fn get_local_branch_names(&self) -> Result<Vec<String>> { | ||
| 117 | let local_branches = self | ||
| 118 | .git_repo | ||
| 119 | .branches(Some(git2::BranchType::Local)) | ||
| 120 | .context("getting GitRepo branches should not error even for a blank repository")?; | ||
| 121 | |||
| 122 | let mut branch_names = vec![]; | ||
| 123 | |||
| 124 | for iter in local_branches { | ||
| 125 | let branch = iter?.0; | ||
| 126 | if let Some(name) = branch.name()? { | ||
| 127 | branch_names.push(name.to_string()); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | Ok(branch_names) | ||
| 131 | } | ||
| 132 | |||
| 133 | pub fn get_checked_out_branch_name(&self) -> Result<String> { | ||
| 134 | Ok(self | ||
| 135 | .git_repo | ||
| 136 | .head()? | ||
| 137 | .shorthand() | ||
| 138 | .context("an object without a shorthand is checked out")? | ||
| 139 | .to_string()) | ||
| 140 | } | ||
| 141 | |||
| 142 | pub fn get_tip_of_local_branch(&self, branch_name: &str) -> Result<Oid> { | ||
| 143 | let branch = self | ||
| 144 | .git_repo | ||
| 145 | .find_branch(branch_name, git2::BranchType::Local) | ||
| 146 | .context(format!("cannot find branch {branch_name}"))?; | ||
| 147 | Ok(branch.into_reference().peel_to_commit()?.id()) | ||
| 148 | } | ||
| 95 | } | 149 | } |
| 96 | 150 | ||
| 97 | impl Drop for GitTestRepo { | 151 | impl Drop for GitTestRepo { |