- move `gix` repository extension to where it belongs more naturally.
- make sure that `get_wd_tree()` is named more closely to what it really
  does, and use it in more places.
This commit is contained in:
Sebastian Thiel 2024-08-28 21:56:55 +02:00
parent 751e091f6f
commit a0e236110a
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B
7 changed files with 31 additions and 51 deletions

View File

@ -5,11 +5,11 @@ use core::fmt;
use gitbutler_branch::{
Branch as GitButlerBranch, BranchId, BranchIdentity, ReferenceExtGix, Target,
};
use gitbutler_command_context::{CommandContext, GixRepositoryExt};
use gitbutler_command_context::CommandContext;
use gitbutler_diff::DiffByPathMap;
use gitbutler_project::access::WorktreeReadPermission;
use gitbutler_reference::normalize_branch_name;
use gitbutler_repo::RepositoryExt;
use gitbutler_repo::{GixRepositoryExt, RepositoryExt};
use gitbutler_serde::BStringForFrontend;
use gix::object::tree::diff::Action;
use gix::prelude::ObjectIdExt;

View File

@ -454,7 +454,7 @@ impl BranchManager<'_> {
vb_state.set_branch(branch.clone())?;
}
let wd_tree = self.ctx.repository().get_wd_tree()?;
let wd_tree = self.ctx.repository().create_wd_tree()?;
let branch_tree = repo
.find_tree(branch.tree)

View File

@ -578,7 +578,7 @@ pub fn integrate_upstream_commits(ctx: &CommandContext, branch_id: BranchId) ->
let new_head_tree = repo.find_commit(new_head)?.tree()?;
let head_commit = repo.find_commit(new_head)?;
let wd_tree = ctx.repository().get_wd_tree()?;
let wd_tree = ctx.repository().create_wd_tree()?;
let integration_tree = repo.find_commit(get_workspace_head(ctx)?)?.tree()?;
let mut merge_index = repo.merge_trees(&integration_tree, &new_head_tree, &wd_tree, None)?;
@ -616,7 +616,7 @@ pub(crate) fn integrate_with_merge(
upstream_commit: &git2::Commit,
merge_base: git2::Oid,
) -> Result<git2::Oid> {
let wd_tree = ctx.repository().get_wd_tree()?;
let wd_tree = ctx.repository().create_wd_tree()?;
let repo = ctx.repository();
let remote_tree = upstream_commit.tree().context("failed to get tree")?;
let upstream_branch = branch.upstream.as_ref().context("upstream not found")?;
@ -1171,7 +1171,7 @@ pub fn is_remote_branch_mergeable(
let base_tree = find_base_tree(ctx.repository(), &branch_commit, &target_commit)?;
let wd_tree = ctx.repository().get_wd_tree()?;
let wd_tree = ctx.repository().create_wd_tree()?;
let branch_tree = branch_commit.tree().context("failed to find branch tree")?;
let mergeable = !ctx

View File

@ -98,18 +98,3 @@ impl CommandContext {
)?)
}
}
// TODO(ST): put this into `gix`, the logic seems good, add unit-test for number generation.
pub trait GixRepositoryExt: Sized {
/// Configure the repository for diff operations between trees.
/// This means it needs an object cache relative to the amount of files in the repository.
fn for_tree_diffing(self) -> Result<Self>;
}
impl GixRepositoryExt for gix::Repository {
fn for_tree_diffing(mut self) -> anyhow::Result<Self> {
let bytes = self.compute_object_cache_size_for_tree_diffs(&***self.index_or_empty()?);
self.object_cache_size_if_unset(bytes);
Ok(self)
}
}

View File

@ -34,17 +34,7 @@ fn save_uncommited_files(ctx: &CommandContext) -> Result<()> {
let repository = ctx.repository();
// Create a tree of all uncommited files
let mut index = repository.index().context("Failed to get index")?;
index
.add_all(["*"], git2::IndexAddOption::DEFAULT, None)
.context("Failed to add all to index")?;
index.write().context("Failed to write index")?;
let tree_oid = index
.write_tree()
.context("Failed to create tree from index")?;
let tree = repository
.find_tree(tree_oid)
.context("Failed to find tree")?;
let tree = repository.create_wd_tree()?;
// Commit tree and reference it
let author_signature =
@ -134,10 +124,7 @@ fn checkout_edit_branch(ctx: &CommandContext, commit: &git2::Commit) -> Result<(
),
)?;
let mut index = repository.index()?;
index.add_all(["*"], git2::IndexAddOption::DEFAULT, None)?;
let tree = index.write_tree()?;
let tree = repository.find_tree(tree)?;
let tree = repository.create_wd_tree()?;
let author_signature = signature(SignaturePurpose::Author)?;
let committer_signature = signature(SignaturePurpose::Committer)?;
@ -265,17 +252,7 @@ pub(crate) fn save_and_return_to_workspace(
};
// Recommit commit
let mut index = repository.index().context("Failed to get index")?;
index
.add_all(["*"].iter(), git2::IndexAddOption::DEFAULT, None)
.context("Failed to add all to index")?;
index.write().context("Failed to write index")?;
let tree_oid = index
.write_tree()
.context("Failed to create tree from index")?;
let tree = repository
.find_tree(tree_oid)
.context("Failed to find tree")?;
let tree = repository.create_wd_tree()?;
let commit_headers = commit
.gitbutler_headers()
.map(|commit_headers| CommitHeadersV2 {

View File

@ -7,7 +7,7 @@ mod commands;
pub use commands::RepoCommands;
mod repository_ext;
pub use repository_ext::RepositoryExt;
pub use repository_ext::{GixRepositoryExt, RepositoryExt};
pub mod credentials;

View File

@ -41,7 +41,7 @@ pub trait RepositoryExt {
fn checkout_tree_builder<'a>(&'a self, tree: &'a git2::Tree<'a>) -> CheckoutTreeBuidler;
fn find_branch_by_refname(&self, name: &Refname) -> Result<Option<git2::Branch>>;
/// Based on the index, add all data similar to `git add .` and create a tree from it, which is returned.
fn get_wd_tree(&self) -> Result<Tree>;
fn create_wd_tree(&self) -> Result<Tree>;
/// Returns the `gitbutler/integration` branch if the head currently points to it, or fail otherwise.
/// Use it before any modification to the repository, or extra defensively each time the
@ -136,10 +136,13 @@ impl RepositoryExt for git2::Repository {
}
}
/// Note that this will add all untracked files in the worktree to the index,
/// and write a tree from it.
/// The index won't be stored though.
#[instrument(level = tracing::Level::DEBUG, skip(self), err(Debug))]
fn get_wd_tree(&self) -> Result<Tree> {
fn create_wd_tree(&self) -> Result<Tree> {
let mut index = self.index()?;
index.add_all(["*"], git2::IndexAddOption::CHECK_PATHSPEC, None)?;
index.add_all(["*"], git2::IndexAddOption::DEFAULT, None)?;
let oid = index.write_tree()?;
self.find_tree(oid).map(Into::into).map_err(Into::into)
}
@ -448,3 +451,18 @@ impl CheckoutIndexBuilder<'_> {
.map_err(Into::into)
}
}
// TODO(ST): put this into `gix`, the logic seems good, add unit-test for number generation.
pub trait GixRepositoryExt: Sized {
/// Configure the repository for diff operations between trees.
/// This means it needs an object cache relative to the amount of files in the repository.
fn for_tree_diffing(self) -> Result<Self>;
}
impl GixRepositoryExt for gix::Repository {
fn for_tree_diffing(mut self) -> anyhow::Result<Self> {
let bytes = self.compute_object_cache_size_for_tree_diffs(&***self.index_or_empty()?);
self.object_cache_size_if_unset(bytes);
Ok(self)
}
}