wrap index

This commit is contained in:
Nikita Galaiko 2023-08-30 15:27:30 +02:00 committed by GitButler
parent 1adb0e4edb
commit 55fee3056c
5 changed files with 127 additions and 21 deletions

View File

@ -753,7 +753,7 @@ fn build_wd_tree_from_repo(
gb_repository: &Repository,
project_repository: &project_repository::Repository,
) -> Result<git2::Oid> {
let mut index = git2::Index::new()?;
let mut index = git::Index::new()?;
// create a new in-memory git2 index and open the working one so we can cheat if none of the metadata of an entry has changed
let repo_index = &mut project_repository
@ -843,8 +843,8 @@ fn build_wd_tree_from_repo(
// and also looks for large files and puts in a placeholder hash in the LFS format
// TODO: actually upload the file to LFS
fn add_wd_path(
index: &mut git2::Index,
repo_index: &mut git2::Index,
index: &mut git::Index,
repo_index: &mut git::Index,
dir: &std::path::Path,
rel_file_path: &std::path::Path,
gb_repository: &Repository,

View File

@ -61,3 +61,19 @@ impl<'repo> Commit<'repo> {
self.commit.committer()
}
}
pub struct AnnotatedCommit<'repo> {
annotated_commit: git2::AnnotatedCommit<'repo>,
}
impl<'repo> From<git2::AnnotatedCommit<'repo>> for AnnotatedCommit<'repo> {
fn from(annotated_commit: git2::AnnotatedCommit<'repo>) -> Self {
Self { annotated_commit }
}
}
impl<'repo> From<&'repo AnnotatedCommit<'repo>> for &'repo git2::AnnotatedCommit<'repo> {
fn from(val: &'repo AnnotatedCommit<'repo>) -> Self {
&val.annotated_commit
}
}

View File

@ -0,0 +1,80 @@
use std::path;
use super::{Repository, Result, Tree};
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)
}
pub fn conflicts(&self) -> Result<git2::IndexConflicts> {
self.index.conflicts()
}
pub fn read_tree(&mut self, tree: &Tree) -> Result<()> {
self.index.read_tree(tree.into())
}
pub fn write_tree_to(&mut self, repo: &Repository) -> Result<git2::Oid> {
self.index.write_tree_to(repo.into())
}
pub fn has_conflicts(&self) -> bool {
self.index.has_conflicts()
}
pub fn write_tree(&mut self) -> Result<git2::Oid> {
self.index.write_tree()
}
pub fn add(&mut self, entry: &git2::IndexEntry) -> Result<()> {
self.index.add(entry)
}
pub fn write(&mut self) -> Result<()> {
self.index.write()
}
pub fn add_path(&mut self, path: &path::Path) -> Result<()> {
self.index.add_path(path)
}
pub fn remove_path(&mut self, path: &path::Path) -> Result<()> {
self.index.remove_path(path)
}
pub fn get_path(&self, path: &path::Path, stage: i32) -> Option<git2::IndexEntry> {
self.index.get_path(path, stage)
}
}

View File

@ -20,3 +20,6 @@ pub use tree::*;
mod remote;
pub use remote::*;
mod index;
pub use index::*;

View File

@ -1,6 +1,6 @@
use std::path;
use super::{Branch, Commit, Reference, Remote, Result, Tree};
use super::{AnnotatedCommit, Branch, Commit, Index, Reference, Remote, Result, Tree};
// wrapper around git2::Repository to get control over how it's used.
pub struct Repository(git2::Repository);
@ -33,18 +33,23 @@ impl Repository {
self.0.odb().and_then(|odb| odb.add_disk_alternate(path))
}
pub fn find_annotated_commit(&self, id: git2::Oid) -> Result<git2::AnnotatedCommit<'_>> {
self.0.find_annotated_commit(id)
pub fn find_annotated_commit(&self, id: git2::Oid) -> Result<AnnotatedCommit<'_>> {
self.0.find_annotated_commit(id).map(AnnotatedCommit::from)
}
pub fn rebase(
&self,
branch: Option<&git2::AnnotatedCommit<'_>>,
upstream: Option<&git2::AnnotatedCommit<'_>>,
onto: Option<&git2::AnnotatedCommit<'_>>,
branch: Option<&AnnotatedCommit<'_>>,
upstream: Option<&AnnotatedCommit<'_>>,
onto: Option<&AnnotatedCommit<'_>>,
opts: Option<&mut git2::RebaseOptions<'_>>,
) -> Result<git2::Rebase<'_>> {
self.0.rebase(branch, upstream, onto, opts)
self.0.rebase(
branch.map(|commit| commit.into()),
upstream.map(|commit| commit.into()),
onto.map(|commit| commit.into()),
opts,
)
}
pub fn merge_base(&self, one: git2::Oid, two: git2::Oid) -> Result<git2::Oid> {
@ -56,13 +61,15 @@ impl Repository {
ancestor_tree: &Tree<'_>,
our_tree: &Tree<'_>,
their_tree: &Tree<'_>,
) -> Result<git2::Index> {
self.0.merge_trees(
ancestor_tree.into(),
our_tree.into(),
their_tree.into(),
None,
)
) -> Result<Index> {
self.0
.merge_trees(
ancestor_tree.into(),
our_tree.into(),
their_tree.into(),
None,
)
.map(Index::from)
}
pub fn diff_tree_to_tree(
@ -132,8 +139,8 @@ impl Repository {
self.0.branches(filter)
}
pub fn index(&self) -> Result<git2::Index> {
self.0.index()
pub fn index(&self) -> Result<Index> {
self.0.index().map(Index::from)
}
pub fn blob_path(&self, path: &path::Path) -> Result<git2::Oid> {
@ -230,10 +237,10 @@ impl Repository {
pub fn checkout_index(
&self,
index: Option<&mut git2::Index>,
index: Option<&mut Index>,
opts: Option<&mut git2::build::CheckoutBuilder<'_>>,
) -> Result<()> {
self.0.checkout_index(index, opts)
self.0.checkout_index(index.map(|i| i.into()), opts)
}
pub fn checkout_tree(