remove core::git::Index

This commit is contained in:
Kiril Videlov 2024-06-02 17:57:18 +02:00
parent 053fa6096a
commit 879a07da64
8 changed files with 45 additions and 185 deletions

View File

@ -1,142 +0,0 @@
use std::path;
use filetime::FileTime;
use super::{Oid, Repository, Result};
pub struct Index {
index: git2::Index,
}
impl<'a> From<&'a mut Index> for &'a mut git2::Index {
fn from(index: &'a mut Index) -> Self {
&mut index.index
}
}
impl From<git2::Index> for Index {
fn from(index: git2::Index) -> Self {
Self { index }
}
}
impl Index {
pub fn new() -> Result<Self> {
Ok(Index {
index: git2::Index::new()?,
})
}
pub fn add_all<I, T>(
&mut self,
pathspecs: I,
flag: git2::IndexAddOption,
cb: Option<&mut git2::IndexMatchedPath<'_>>,
) -> Result<()>
where
T: git2::IntoCString,
I: IntoIterator<Item = T>,
{
self.index.add_all(pathspecs, flag, cb).map_err(Into::into)
}
pub fn conflicts(&self) -> Result<git2::IndexConflicts> {
self.index.conflicts().map_err(Into::into)
}
pub fn write_tree_to(&mut self, repo: &Repository) -> Result<Oid> {
self.index
.write_tree_to(repo.into())
.map(Into::into)
.map_err(Into::into)
}
pub fn has_conflicts(&self) -> bool {
self.index.has_conflicts()
}
pub fn write_tree(&mut self) -> Result<Oid> {
self.index.write_tree().map(Into::into).map_err(Into::into)
}
pub fn add(&mut self, entry: &IndexEntry) -> Result<()> {
self.index.add(&entry.clone().into()).map_err(Into::into)
}
pub fn write(&mut self) -> Result<()> {
self.index.write().map_err(Into::into)
}
pub fn add_path(&mut self, path: &path::Path) -> Result<()> {
self.index.add_path(path).map_err(Into::into)
}
pub fn remove_path(&mut self, path: &path::Path) -> Result<()> {
self.index.remove_path(path).map_err(Into::into)
}
pub fn get_path(&self, path: &path::Path, stage: i32) -> Option<IndexEntry> {
self.index.get_path(path, stage).map(Into::into)
}
}
#[derive(Debug, Clone)]
pub struct IndexEntry {
pub ctime: FileTime,
pub mtime: FileTime,
pub dev: u32,
pub ino: u32,
pub mode: u32,
pub uid: u32,
pub gid: u32,
pub file_size: u32,
pub id: Oid,
pub flags: u16,
pub flags_extended: u16,
pub path: Vec<u8>,
}
impl From<git2::IndexEntry> for IndexEntry {
fn from(value: git2::IndexEntry) -> Self {
Self {
ctime: FileTime::from_unix_time(
i64::from(value.ctime.seconds()),
value.ctime.nanoseconds(),
),
mtime: FileTime::from_unix_time(
i64::from(value.mtime.seconds()),
value.mtime.nanoseconds(),
),
dev: value.dev,
ino: value.ino,
mode: value.mode,
uid: value.uid,
gid: value.gid,
file_size: value.file_size,
id: value.id.into(),
flags: value.flags,
flags_extended: value.flags_extended,
path: value.path,
}
}
}
impl From<IndexEntry> for git2::IndexEntry {
#[allow(clippy::cast_possible_truncation)]
fn from(entry: IndexEntry) -> Self {
Self {
ctime: git2::IndexTime::new(entry.ctime.seconds() as i32, entry.ctime.nanoseconds()),
mtime: git2::IndexTime::new(entry.mtime.seconds() as i32, entry.mtime.nanoseconds()),
dev: entry.dev,
ino: entry.ino,
mode: entry.mode,
uid: entry.uid,
gid: entry.gid,
file_size: entry.file_size,
id: entry.id.into(),
flags: entry.flags,
flags_extended: entry.flags_extended,
path: entry.path,
}
}
}

View File

@ -10,9 +10,6 @@ mod repository;
pub use repository::*;
mod index;
pub use index::*;
mod oid;
pub use oid::*;

View File

@ -1,4 +1,4 @@
use super::{Index, Oid, Reference, Refname, Result, Url};
use super::{Oid, Reference, Refname, Result, Url};
use git2::{BlameOptions, Submodule};
use git2_hooks::HookResult;
#[cfg(unix)]
@ -98,10 +98,9 @@ impl Repository {
ancestor_tree: &git2::Tree<'_>,
our_tree: &git2::Tree<'_>,
their_tree: &git2::Tree<'_>,
) -> Result<Index> {
) -> Result<git2::Index> {
self.0
.merge_trees(ancestor_tree, our_tree, their_tree, None)
.map(Index::from)
.map_err(Into::into)
}
@ -180,8 +179,8 @@ impl Repository {
.map_err(Into::into)
}
pub fn index(&self) -> Result<Index> {
self.0.index().map(Into::into).map_err(Into::into)
pub fn index(&self) -> Result<git2::Index> {
self.0.index().map_err(Into::into)
}
pub fn index_size(&self) -> Result<usize> {
@ -195,10 +194,9 @@ impl Repository {
.map_err(Into::into)
}
pub fn cherry_pick(&self, base: &git2::Commit, target: &git2::Commit) -> Result<Index> {
pub fn cherry_pick(&self, base: &git2::Commit, target: &git2::Commit) -> Result<git2::Index> {
self.0
.cherrypick_commit(target, base, 0, None)
.map(Into::into)
.map_err(Into::into)
}
@ -447,9 +445,9 @@ impl Repository {
self.0.checkout_head(opts).map_err(Into::into)
}
pub fn checkout_index<'a>(&'a self, index: &'a mut Index) -> CheckoutIndexBuilder {
pub fn checkout_index<'a>(&'a self, index: &'a mut git2::Index) -> CheckoutIndexBuilder {
CheckoutIndexBuilder {
index: index.into(),
index,
repo: &self.0,
checkout_builder: git2::build::CheckoutBuilder::new(),
}

View File

@ -95,11 +95,11 @@ fn go_back_to_integration(
.merge_trees(&base_tree, &final_tree, &branch_tree)
.context("failed to merge")?;
let final_tree_oid = result
.write_tree_to(&project_repository.git_repository)
.write_tree_to(project_repository.repo())
.context("failed to write tree")?;
final_tree = project_repository
.git_repository
.find_tree(final_tree_oid)
.find_tree(final_tree_oid.into())
.context("failed to find written tree")?;
}
@ -430,16 +430,16 @@ pub fn update_base_branch(
}
let branch_merge_index_tree_oid =
branch_tree_merge_index.write_tree_to(repo)?;
branch_tree_merge_index.write_tree_to(project_repository.repo())?;
if branch_merge_index_tree_oid == new_target_tree.id().into() {
if branch_merge_index_tree_oid == new_target_tree.id() {
return result_integrated_detected(branch);
}
if branch.head == target.sha {
// there are no commits on the branch, so we can just update the head to the new target and calculate the new tree
branch.head = new_target_commit.id().into();
branch.tree = branch_merge_index_tree_oid;
branch.tree = branch_merge_index_tree_oid.into();
vb_state.set_branch(branch.clone())?;
return Ok(Some(branch));
}
@ -464,7 +464,7 @@ pub fn update_base_branch(
// branch commits do not conflict with new target, so lets merge them
let branch_head_merge_tree_oid = branch_head_merge_index
.write_tree_to(repo)
.write_tree_to(project_repository.repo())
.context(format!(
"failed to write head merge index for {}",
branch.id
@ -477,7 +477,7 @@ pub fn update_base_branch(
// branch was pushed to upstream, and user doesn't like force pushing.
// create a merge commit to avoid the need of force pushing then.
let branch_head_merge_tree = repo
.find_tree(branch_head_merge_tree_oid)
.find_tree(branch_head_merge_tree_oid.into())
.context("failed to find tree")?;
let new_target_head = project_repository
@ -497,7 +497,7 @@ pub fn update_base_branch(
.context("failed to commit merge")?;
branch.head = new_target_head;
branch.tree = branch_merge_index_tree_oid;
branch.tree = branch_merge_index_tree_oid.into();
vb_state.set_branch(branch.clone())?;
Ok(Some(branch))
};
@ -522,7 +522,7 @@ pub fn update_base_branch(
if let Some(rebased_head_oid) = rebased_head_oid? {
// rebase worked out, rewrite the branch head
branch.head = rebased_head_oid;
branch.tree = branch_merge_index_tree_oid;
branch.tree = branch_merge_index_tree_oid.into();
vb_state.set_branch(branch.clone())?;
return Ok(Some(branch));
}

View File

@ -316,11 +316,11 @@ pub fn apply_branch(
.context("failed to find head commit")?;
let merged_branch_tree_oid = merge_index
.write_tree_to(repo)
.write_tree_to(project_repository.repo())
.context("failed to write tree")?;
let merged_branch_tree = repo
.find_tree(merged_branch_tree_oid)
.find_tree(merged_branch_tree_oid.into())
.context("failed to find tree")?;
let ok_with_force_push = project_repository.project().ok_with_force_push;
@ -391,7 +391,7 @@ pub fn apply_branch(
// get tree from merge_tree_oid
let merge_tree = repo
.find_tree(merged_branch_tree_oid)
.find_tree(merged_branch_tree_oid.into())
.context("failed to find tree")?;
// commit the merge tree oid
@ -534,8 +534,8 @@ pub fn unapply_ownership(
let tree_oid = write_tree(project_repository, &integration_commit_id, status.1)?;
let branch_tree = repo.find_tree(tree_oid)?;
let mut result = repo.merge_trees(&base_tree, &final_tree, &branch_tree)?;
let final_tree_oid = result.write_tree_to(repo)?;
repo.find_tree(final_tree_oid)
let final_tree_oid = result.write_tree_to(project_repository.repo())?;
repo.find_tree(final_tree_oid.into())
.context("failed to find tree")
},
)?;
@ -677,8 +677,8 @@ pub fn unapply_branch(
let tree_oid = write_tree(project_repository, &branch.head, status.1)?;
let branch_tree = repo.find_tree(tree_oid)?;
let mut result = repo.merge_trees(&base_tree, &final_tree, &branch_tree)?;
let final_tree_oid = result.write_tree_to(repo)?;
repo.find_tree(final_tree_oid)
let final_tree_oid = result.write_tree_to(project_repository.repo())?;
repo.find_tree(final_tree_oid.into())
.context("failed to find tree")
},
)?;
@ -1301,8 +1301,8 @@ pub fn integrate_with_merge(
return Err(anyhow!("merge problem")).context(Marker::ProjectConflict);
}
let merge_tree_oid = merge_index.write_tree_to(repo)?;
let merge_tree = repo.find_tree(merge_tree_oid)?;
let merge_tree_oid = merge_index.write_tree_to(project_repository.repo())?;
let merge_tree = repo.find_tree(merge_tree_oid.into())?;
let head_commit = repo.find_commit(branch.head)?;
project_repository.commit(
@ -2466,12 +2466,12 @@ fn is_commit_integrated(
}
let merge_tree_oid = merge_index
.write_tree_to(&project_repository.git_repository)
.write_tree_to(project_repository.repo())
.context("failed to write tree")?;
// if the merge_tree is the same as the new_target_tree and there are no files (uncommitted changes)
// then the vbranch is fully merged
Ok(merge_tree_oid == upstream_tree.id().into())
Ok(merge_tree_oid == upstream_tree.id())
}
pub fn is_remote_branch_mergeable(
@ -3238,12 +3238,12 @@ fn cherry_rebase_group(
}
let merge_tree_oid = cherrypick_index
.write_tree_to(&project_repository.git_repository)
.write_tree_to(project_repository.repo())
.context("failed to write merge tree")?;
let merge_tree = project_repository
.git_repository
.find_tree(merge_tree_oid)
.find_tree(merge_tree_oid.into())
.context("failed to find merge tree")?;
let change_id = to_rebase.change_id();
@ -3399,11 +3399,11 @@ pub fn cherry_pick(
None
} else {
let merge_tree_oid = cherrypick_index
.write_tree_to(&project_repository.git_repository)
.write_tree_to(project_repository.repo())
.context("failed to write merge tree")?;
let merge_tree = project_repository
.git_repository
.find_tree(merge_tree_oid)
.find_tree(merge_tree_oid.into())
.context("failed to find merge tree")?;
let branch_head_commit = project_repository

View File

@ -720,7 +720,9 @@ fn commit_id_can_be_generated_or_specified() -> Result<()> {
&signature,
&signature,
"some commit",
&repository.find_tree(oid).expect("failed to find tree"),
&repository
.find_tree(oid.into())
.expect("failed to find tree"),
&[&repository
.find_commit(
repository

View File

@ -175,7 +175,9 @@ pub fn test_repository() -> (gitbutler_core::git::Repository, TempDir) {
&signature,
&signature,
"Initial commit",
&repository.find_tree(oid).expect("failed to find tree"),
&repository
.find_tree(oid.into())
.expect("failed to find tree"),
&[],
None,
)
@ -198,7 +200,9 @@ pub fn commit_all(repository: &gitbutler_core::git::Repository) -> gitbutler_cor
&signature,
&signature,
"some commit",
&repository.find_tree(oid).expect("failed to find tree"),
&repository
.find_tree(oid.into())
.expect("failed to find tree"),
&[&repository
.find_commit(
repository

View File

@ -42,7 +42,7 @@ impl Default for TestProject {
&signature,
"Initial commit",
&local_repository
.find_tree(oid)
.find_tree(oid.into())
.expect("failed to find tree"),
&[],
None,
@ -236,8 +236,9 @@ impl TestProject {
&branch.get().peel_to_tree().unwrap(),
)
.unwrap();
let oid = merge_index.write_tree_to(&self.remote_repository).unwrap();
self.remote_repository.find_tree(oid).unwrap()
let repo: &git2::Repository = (&self.remote_repository).into();
let oid = merge_index.write_tree_to(repo).unwrap();
self.remote_repository.find_tree(oid.into()).unwrap()
};
self.remote_repository
@ -313,7 +314,7 @@ impl TestProject {
message,
&self
.local_repository
.find_tree(oid)
.find_tree(oid.into())
.expect("failed to find tree"),
&[&self
.local_repository