Merge pull request #1242 from gitbutlerapp/refactor-build-wd-tree-function

refactor: simplify code for adding index entry and building working d…
This commit is contained in:
Nikita Galaiko 2023-09-26 10:35:00 +02:00 committed by GitHub
commit a26f8f1a16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View File

@ -627,13 +627,12 @@ fn build_wd_tree(
.find_reference("refs/heads/current")
{
Result::Ok(reference) => {
let index = &mut git2::Index::new()?;
// build the working directory tree from the current commit
// and the session files
let tree = reference.peel_to_tree()?;
let wd_tree_entry = tree.get_name("wd").unwrap();
let wd_tree = gb_repository.git_repository.find_tree(wd_tree_entry.id())?;
index.read_tree((&wd_tree).into())?;
let mut index = git::Index::try_from(wd_tree)?;
let session_wd_reader = reader::DirReader::open(gb_repository.session_wd_path());
let session_wd_files = session_wd_reader
@ -664,15 +663,9 @@ fn build_wd_tree(
};
index
.add(&git2::IndexEntry {
ctime: git2::IndexTime::new(
ctime.seconds().try_into()?,
ctime.nanoseconds(),
),
mtime: git2::IndexTime::new(
mtime.seconds().try_into()?,
mtime.nanoseconds(),
),
.add(&git::IndexEntry {
ctime,
mtime,
dev: metadata.dev().try_into()?,
ino: metadata.ino().try_into()?,
mode: 33188,
@ -682,18 +675,15 @@ fn build_wd_tree(
flags: 10, // normal flags for normal file (for the curious: https://git-scm.com/docs/index-format)
flags_extended: 0, // no extended flags
path: file_path.clone().into(),
id: gb_repository
.git_repository
.blob(file_content.as_bytes())?
.into(),
id: gb_repository.git_repository.blob(file_content.as_bytes())?,
})
.with_context(|| format!("failed to add index entry for {}", file_path))?;
}
let wd_tree_oid = index
.write_tree_to((&gb_repository.git_repository).into())
.write_tree_to(&gb_repository.git_repository)
.context("failed to write wd tree")?;
Ok(wd_tree_oid.into())
Ok(wd_tree_oid)
}
Err(git::Error::NotFound(_)) => build_wd_tree_from_repo(gb_repository, project_repository)
.context("failed to build wd index"),

View File

@ -2,12 +2,22 @@ use std::path;
use filetime::FileTime;
use super::{Oid, Repository, Result, Tree};
use super::{Error, Oid, Repository, Result, Tree};
pub struct Index {
index: git2::Index,
}
impl TryFrom<Tree<'_>> for Index {
type Error = Error;
fn try_from(value: Tree) -> Result<Self> {
let mut empty_index = Self::new()?;
empty_index.read_tree(&value)?;
Ok(empty_index)
}
}
impl<'a> From<&'a mut Index> for &'a mut git2::Index {
fn from(index: &'a mut Index) -> Self {
&mut index.index