remove core::git::Reference type

This commit is contained in:
Kiril Videlov 2024-06-02 19:03:03 +02:00
parent 63fb9e9299
commit 4ae1847a28
11 changed files with 45 additions and 94 deletions

View File

@ -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<git2::Reference<'repo>> for Reference<'repo> {
fn from(reference: git2::Reference<'repo>) -> Self {
Reference { reference }
}
}
impl<'repo> Reference<'repo> {
pub fn name(&self) -> Option<Refname> {
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<Oid> {
self.reference.target().map(Into::into)
}
pub fn peel_to_commit(&self) -> Result<git2::Commit<'repo>> {
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<Reference<'repo>> {
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()
}
}

View File

@ -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<Reference> {
self.0
.find_reference(&name.to_string())
.map(Reference::from)
.map_err(Into::into)
pub fn find_reference(&self, name: &Refname) -> Result<git2::Reference> {
self.0.find_reference(&name.to_string()).map_err(Into::into)
}
pub fn head(&self) -> Result<Reference> {
self.0.head().map(Reference::from).map_err(Into::into)
pub fn head(&self) -> Result<git2::Reference> {
self.0.head().map_err(Into::into)
}
pub fn find_tree(&self, id: Oid) -> Result<git2::Tree> {
@ -489,7 +486,7 @@ impl Repository {
id: Oid,
force: bool,
log_message: &str,
) -> Result<Reference> {
) -> Result<git2::Reference> {
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<impl Iterator<Item = Result<Reference>>> {
pub fn references(&self) -> Result<impl Iterator<Item = Result<git2::Reference>>> {
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<impl Iterator<Item = Result<Reference>>> {
pub fn references_glob(
&self,
glob: &str,
) -> Result<impl Iterator<Item = Result<git2::Reference>>> {
self.0
.references_glob(glob)
.map(|iter| iter.map(|reference| reference.map(Into::into).map_err(Into::into)))

View File

@ -122,7 +122,7 @@ impl Repository {
Ok(head)
}
pub fn get_head(&self) -> Result<git::Reference, git::Error> {
pub fn get_head(&self) -> Result<git2::Reference, git::Error> {
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)),

View File

@ -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::<Vec<_>>())
}

View File

@ -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()

View File

@ -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()

View File

@ -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)?;

View File

@ -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");

View File

@ -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",

View File

@ -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<git::Reference<'_>> {
pub fn references(&self) -> Vec<git2::Reference<'_>> {
self.local_repository
.references()
.expect("failed to get references")

View File

@ -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(