diff --git a/crates/gitbutler-core/src/git/reference/mod.rs b/crates/gitbutler-core/src/git/reference/mod.rs index 9bbb33425..e54d68e9b 100644 --- a/crates/gitbutler-core/src/git/reference/mod.rs +++ b/crates/gitbutler-core/src/git/reference/mod.rs @@ -1,57 +1,2 @@ mod refname; pub use refname::{LocalRefname, Refname, RemoteRefname, VirtualRefname}; - -use super::{Oid, Result}; - -pub struct Reference<'repo> { - reference: git2::Reference<'repo>, -} - -impl<'repo> From> for Reference<'repo> { - fn from(reference: git2::Reference<'repo>) -> Self { - Reference { reference } - } -} - -impl<'repo> Reference<'repo> { - pub fn name(&self) -> Option { - self.reference - .name() - .map(|name| name.parse().expect("libgit2 provides valid refnames")) - } - - pub fn name_bytes(&self) -> &[u8] { - self.reference.name_bytes() - } - - pub fn target(&self) -> Option { - self.reference.target().map(Into::into) - } - - pub fn peel_to_commit(&self) -> Result> { - self.reference - .peel_to_commit() - .map(Into::into) - .map_err(Into::into) - } - - pub fn rename( - &mut self, - new_name: &Refname, - force: bool, - log_message: &str, - ) -> Result> { - self.reference - .rename(&new_name.to_string(), force, log_message) - .map(Into::into) - .map_err(Into::into) - } - - pub fn delete(&mut self) -> Result<()> { - self.reference.delete().map_err(Into::into) - } - - pub fn is_remote(&self) -> bool { - self.reference.is_remote() - } -} diff --git a/crates/gitbutler-core/src/git/repository.rs b/crates/gitbutler-core/src/git/repository.rs index f5acf12e4..874cff214 100644 --- a/crates/gitbutler-core/src/git/repository.rs +++ b/crates/gitbutler-core/src/git/repository.rs @@ -1,4 +1,4 @@ -use super::{Oid, Reference, Refname, Result, Url}; +use super::{Oid, Refname, Result, Url}; use git2::{BlameOptions, Submodule}; use git2_hooks::HookResult; #[cfg(unix)] @@ -140,15 +140,12 @@ impl Repository { .map_err(Into::into) } - pub fn find_reference(&self, name: &Refname) -> Result { - self.0 - .find_reference(&name.to_string()) - .map(Reference::from) - .map_err(Into::into) + pub fn find_reference(&self, name: &Refname) -> Result { + self.0.find_reference(&name.to_string()).map_err(Into::into) } - pub fn head(&self) -> Result { - self.0.head().map(Reference::from).map_err(Into::into) + pub fn head(&self) -> Result { + self.0.head().map_err(Into::into) } pub fn find_tree(&self, id: Oid) -> Result { @@ -489,7 +486,7 @@ impl Repository { id: Oid, force: bool, log_message: &str, - ) -> Result { + ) -> Result { self.0 .reference(&name.to_string(), id.into(), force, log_message) .map(Into::into) @@ -500,14 +497,17 @@ impl Repository { self.0.remote(name, &url.to_string()).map_err(Into::into) } - pub fn references(&self) -> Result>> { + pub fn references(&self) -> Result>> { self.0 .references() .map(|iter| iter.map(|reference| reference.map(Into::into).map_err(Into::into))) .map_err(Into::into) } - pub fn references_glob(&self, glob: &str) -> Result>> { + pub fn references_glob( + &self, + glob: &str, + ) -> Result>> { self.0 .references_glob(glob) .map(|iter| iter.map(|reference| reference.map(Into::into).map_err(Into::into))) diff --git a/crates/gitbutler-core/src/project_repository/repository.rs b/crates/gitbutler-core/src/project_repository/repository.rs index 265074b9c..b8026fe83 100644 --- a/crates/gitbutler-core/src/project_repository/repository.rs +++ b/crates/gitbutler-core/src/project_repository/repository.rs @@ -122,7 +122,7 @@ impl Repository { Ok(head) } - pub fn get_head(&self) -> Result { + pub fn get_head(&self) -> Result { let head = self.git_repository.head()?; Ok(head) } @@ -191,7 +191,7 @@ impl Repository { let (should_write, with_force) = match self.git_repository.find_reference(&branch.refname().into()) { Ok(reference) => match reference.target() { - Some(head_oid) => Ok((head_oid != branch.head, true)), + Some(head_oid) => Ok((head_oid != branch.head.into(), true)), None => Ok((true, true)), }, Err(git::Error::NotFound(_)) => Ok((true, false)), diff --git a/crates/gitbutler-core/src/synchronize/mod.rs b/crates/gitbutler-core/src/synchronize/mod.rs index 03105a2d7..314fea6c4 100644 --- a/crates/gitbutler-core/src/synchronize/mod.rs +++ b/crates/gitbutler-core/src/synchronize/mod.rs @@ -139,7 +139,10 @@ fn collect_refs( .git_repository .references_glob("refs/*")? .flatten() - .filter_map(|r| r.name()) + .filter_map(|r| { + r.name() + .map(|name| name.parse().expect("libgit2 provides valid refnames")) + }) .collect::>()) } diff --git a/crates/gitbutler-core/src/virtual_branches/base.rs b/crates/gitbutler-core/src/virtual_branches/base.rs index bb507c37d..99b30f0ff 100644 --- a/crates/gitbutler-core/src/virtual_branches/base.rs +++ b/crates/gitbutler-core/src/virtual_branches/base.rs @@ -181,6 +181,7 @@ pub fn set_base_branch( // TODO: make sure this is a real branch let head_name: git::Refname = current_head .name() + .map(|name| name.parse().expect("libgit2 provides valid refnames")) .context("Failed to get HEAD reference name")?; if !head_name .to_string() diff --git a/crates/gitbutler-core/src/virtual_branches/integration.rs b/crates/gitbutler-core/src/virtual_branches/integration.rs index d4492a0b1..67e34b120 100644 --- a/crates/gitbutler-core/src/virtual_branches/integration.rs +++ b/crates/gitbutler-core/src/virtual_branches/integration.rs @@ -304,10 +304,8 @@ pub fn verify_branch(project_repository: &project_repository::Repository) -> Res impl project_repository::Repository { fn verify_head_is_set(&self) -> Result<&Self> { match self.get_head().context("failed to get head")?.name() { - Some(refname) if refname.to_string() == GITBUTLER_INTEGRATION_REFERENCE.to_string() => { - Ok(self) - } - Some(head_name) => Err(invalid_head_err(&head_name.to_string())), + Some(refname) if *refname == GITBUTLER_INTEGRATION_REFERENCE.to_string() => Ok(self), + Some(head_name) => Err(invalid_head_err(head_name)), None => Err(anyhow!( "project in detached head state. Please checkout {} to continue", GITBUTLER_INTEGRATION_REFERENCE.branch() diff --git a/crates/gitbutler-core/tests/virtual_branches/mod.rs b/crates/gitbutler-core/tests/virtual_branches/mod.rs index 18df0626a..d0ec8a236 100644 --- a/crates/gitbutler-core/tests/virtual_branches/mod.rs +++ b/crates/gitbutler-core/tests/virtual_branches/mod.rs @@ -691,7 +691,7 @@ fn commit_id_can_be_generated_or_specified() -> Result<()> { .unwrap(); let target = project_repository .git_repository - .find_commit(target_oid) + .find_commit(target_oid.into()) .unwrap(); let change_id = target.change_id(); @@ -714,9 +714,10 @@ fn commit_id_can_be_generated_or_specified() -> Result<()> { let oid = index.write_tree().expect("failed to write tree"); let signature = git2::Signature::now("test", "test@email.com").unwrap(); let head = repository.head().expect("failed to get head"); + let refname: git::Refname = head.name().unwrap().parse().unwrap(); repository .commit( - Some(&head.name().unwrap()), + Some(&refname), &signature, &signature, "some commit", @@ -742,7 +743,7 @@ fn commit_id_can_be_generated_or_specified() -> Result<()> { .unwrap(); let target = project_repository .git_repository - .find_commit(target_oid) + .find_commit(target_oid.into()) .unwrap(); let change_id = target.change_id(); @@ -804,7 +805,7 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> { //update repo ref refs/remotes/origin/master to up_target oid project_repository.git_repository.reference( &"refs/remotes/origin/master".parse().unwrap(), - coworker_work, + coworker_work.into(), true, "update target", )?; @@ -820,7 +821,7 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> { vb_state.set_default_target(virtual_branches::target::Target { branch: "refs/remotes/origin/master".parse().unwrap(), remote_url: "origin".to_string(), - sha: target_oid, + sha: target_oid.into(), push_remote_name: None, })?; @@ -835,7 +836,7 @@ fn merge_vbranch_upstream_clean_rebase() -> Result<()> { let mut branch = create_virtual_branch(project_repository, &BranchCreateRequest::default()) .expect("failed to create virtual branch"); branch.upstream = Some(remote_branch.clone()); - branch.head = last_push; + branch.head = last_push.into(); vb_state .set_branch(branch.clone()) .context("failed to write target branch after push")?; @@ -930,7 +931,7 @@ async fn merge_vbranch_upstream_conflict() -> Result<()> { //update repo ref refs/remotes/origin/master to up_target oid project_repository.git_repository.reference( &"refs/remotes/origin/master".parse().unwrap(), - coworker_work, + coworker_work.into(), true, "update target", )?; @@ -946,7 +947,7 @@ async fn merge_vbranch_upstream_conflict() -> Result<()> { vb_state.set_default_target(virtual_branches::target::Target { branch: "refs/remotes/origin/master".parse().unwrap(), remote_url: "origin".to_string(), - sha: target_oid, + sha: target_oid.into(), push_remote_name: None, })?; @@ -960,7 +961,7 @@ async fn merge_vbranch_upstream_conflict() -> Result<()> { let mut branch = create_virtual_branch(project_repository, &BranchCreateRequest::default()) .expect("failed to create virtual branch"); branch.upstream = Some(remote_branch.clone()); - branch.head = last_push; + branch.head = last_push.into(); vb_state .set_branch(branch.clone()) .context("failed to write target branch after push")?; @@ -1289,7 +1290,7 @@ fn detect_mergeable_branch() -> Result<()> { .unwrap(); project_repository.git_repository.reference( &"refs/remotes/origin/remote_branch".parse().unwrap(), - up_target, + up_target.into(), true, "update target", )?; @@ -1310,7 +1311,7 @@ fn detect_mergeable_branch() -> Result<()> { .unwrap(); project_repository.git_repository.reference( &"refs/remotes/origin/remote_branch2".parse().unwrap(), - up_target, + up_target.into(), true, "update target", )?; @@ -1428,7 +1429,7 @@ fn upstream_integrated_vbranch() -> Result<()> { .unwrap(); project_repository.git_repository.reference( &"refs/remotes/origin/master".parse().unwrap(), - upstream_commit, + upstream_commit.into(), true, "update target", )?; @@ -1436,7 +1437,7 @@ fn upstream_integrated_vbranch() -> Result<()> { vb_state.set_default_target(virtual_branches::target::Target { branch: "refs/remotes/origin/master".parse().unwrap(), remote_url: "http://origin.com/project".to_string(), - sha: base_commit, + sha: base_commit.into(), push_remote_name: None, })?; project_repository @@ -1810,7 +1811,7 @@ fn commit_partial_by_file() -> Result<()> { .unwrap(); let commit1 = project_repository .git_repository - .find_commit(commit1_oid) + .find_commit(commit1_oid.into()) .unwrap(); set_test_target(project_repository)?; @@ -1877,7 +1878,7 @@ fn commit_add_and_delete_files() -> Result<()> { .unwrap(); let commit1 = project_repository .git_repository - .find_commit(commit1_oid) + .find_commit(commit1_oid.into()) .unwrap(); set_test_target(project_repository)?; diff --git a/crates/gitbutler-testsupport/src/lib.rs b/crates/gitbutler-testsupport/src/lib.rs index 3359efd18..876f5cafe 100644 --- a/crates/gitbutler-testsupport/src/lib.rs +++ b/crates/gitbutler-testsupport/src/lib.rs @@ -40,7 +40,7 @@ pub mod virtual_branches { .set_default_target(virtual_branches::target::Target { branch: "refs/remotes/origin/master".parse().unwrap(), remote_url: remote_repo.path().to_str().unwrap().parse().unwrap(), - sha: remote_repo.head().unwrap().target().unwrap(), + sha: remote_repo.head().unwrap().target().unwrap().into(), push_remote_name: None, }) .expect("failed to write target"); diff --git a/crates/gitbutler-testsupport/src/suite.rs b/crates/gitbutler-testsupport/src/suite.rs index eca88720c..749e74f71 100644 --- a/crates/gitbutler-testsupport/src/suite.rs +++ b/crates/gitbutler-testsupport/src/suite.rs @@ -196,7 +196,7 @@ pub fn commit_all(repository: &gitbutler_core::git::Repository) -> gitbutler_cor let head = repository.head().expect("failed to get head"); let commit_oid = repository .commit( - Some(&head.name().unwrap()), + Some(&head.name().map(|name| name.parse().unwrap()).unwrap()), &signature, &signature, "some commit", diff --git a/crates/gitbutler-testsupport/src/test_project.rs b/crates/gitbutler-testsupport/src/test_project.rs index 5acf956c9..9d781a6b8 100644 --- a/crates/gitbutler-testsupport/src/test_project.rs +++ b/crates/gitbutler-testsupport/src/test_project.rs @@ -124,7 +124,9 @@ impl TestProject { }); let head_ref = head.name().unwrap(); - self.local_repository.find_reference(&head_ref).unwrap(); + self.local_repository + .find_reference(&head_ref.parse().expect("libgit2 provides valid refnames")) + .unwrap(); self.local_repository .reset(&commit, git2::ResetType::Hard, None) @@ -306,9 +308,10 @@ impl TestProject { index.write().expect("failed to write index"); let oid = index.write_tree().expect("failed to write tree"); let signature = git2::Signature::now("test", "test@email.com").unwrap(); + let refname: git::Refname = head.name().unwrap().parse().unwrap(); self.local_repository .commit( - head.name().as_ref(), + Some(&refname), &signature, &signature, message, @@ -329,7 +332,7 @@ impl TestProject { .expect("failed to commit") } - pub fn references(&self) -> Vec> { + pub fn references(&self) -> Vec> { self.local_repository .references() .expect("failed to get references") diff --git a/crates/gitbutler-watcher/src/handler.rs b/crates/gitbutler-watcher/src/handler.rs index d234f3366..80b744bfd 100644 --- a/crates/gitbutler-watcher/src/handler.rs +++ b/crates/gitbutler-watcher/src/handler.rs @@ -177,7 +177,7 @@ impl Handler { .get_head() .context("failed to get head")?; let head_ref_name = head_ref.name().context("failed to get head name")?; - if head_ref_name.to_string() != "refs/heads/gitbutler/integration" { + if head_ref_name != "refs/heads/gitbutler/integration" { let mut integration_reference = project_repository .git_repository .find_reference(&git::Refname::from(git::LocalRefname::new(