Skip to content

Commit

Permalink
Clean up some unwraps / error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonAPenn committed Aug 20, 2024
1 parent 970fe73 commit 7309319
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ pub mod transformer {
}
#[derive(Debug)]
pub enum Error {
/// An error was returned from `libgit2`.
GitError(git2::Error),

/// One of the transformers encountered an error.
TransformerError(String),
}

Expand All @@ -110,36 +113,33 @@ fn build_worktree_slice<'repo>(
repo: &'repo Repository,
formatted: &'repo Tree,
ancestor: &'repo Tree,
) -> Tree<'repo> {
) -> Result<Tree<'repo>, git2::Error> {
let mut builder = TreeUpdateBuilder::new();
let repo_path = repo.workdir().unwrap();
formatted
.walk(TreeWalkMode::PreOrder, |path, entry| {
let relative_file_path = Path::new(path).join(entry.name().unwrap());
let absolute_file_path = repo_path.join(&relative_file_path);
if absolute_file_path.is_file() {
let oid = repo
.odb()
.unwrap()
.write(
git2::ObjectType::Blob,
&std::fs::read(absolute_file_path).unwrap(),
)
.unwrap();
builder.upsert(
relative_file_path.to_str().unwrap(),
oid,
git2::FileMode::Blob,
);
}
TreeWalkResult::Ok
})
.unwrap();
formatted.walk(TreeWalkMode::PreOrder, |path, entry| {
let relative_file_path = Path::new(path).join(entry.name().unwrap());
let absolute_file_path = repo_path.join(&relative_file_path);
if absolute_file_path.is_file() {
let oid = repo
.odb()
.unwrap()
.write(
git2::ObjectType::Blob,
&std::fs::read(absolute_file_path).unwrap(),
)
.unwrap();
builder.upsert(
relative_file_path.to_str().unwrap(),
oid,
git2::FileMode::Blob,
);
}
TreeWalkResult::Ok
})?;
repo.find_tree(builder.create_updated(repo, ancestor).unwrap())
.unwrap()
}

pub fn pre_commit(configuration: &Configuration, path: &str) -> Result<(), git2::Error> {
pub fn pre_commit(configuration: &Configuration, path: &str) -> Result<(), Error> {
let repository = Repository::discover(path)?;
let mut index = repository.index()?;
let index_tree = repository.find_tree(index.write_tree()?)?;
Expand Down Expand Up @@ -174,8 +174,7 @@ pub fn pre_commit(configuration: &Configuration, path: &str) -> Result<(), git2:
&repository,
&repository.find_blob(entry.new_file().id())?,
&transformers,
)
.unwrap();
)?;
transformed_tree_builder.upsert(
entry.new_file().path_bytes().unwrap(),
oid,
Expand All @@ -189,20 +188,18 @@ pub fn pre_commit(configuration: &Configuration, path: &str) -> Result<(), git2:
index.read_tree(&transformed_tree)?;
index.write()?;

let mini_worktree = build_worktree_slice(&repository, &transformed_tree, &index_tree);
let mini_worktree = build_worktree_slice(&repository, &transformed_tree, &index_tree)?;

let mut merged_index = repository
.merge_trees(
&index_tree,
&mini_worktree,
&transformed_tree,
Some(
MergeOptions::new()
.file_favor(git2::FileFavor::Ours)
.fail_on_conflict(false),
),
)
.unwrap();
let mut merged_index = repository.merge_trees(
&index_tree,
&mini_worktree,
&transformed_tree,
Some(
MergeOptions::new()
.file_favor(git2::FileFavor::Ours)
.fail_on_conflict(false),
),
)?;
/*
* Update the worktree with files from the transformed index. In the case of
* any conflicts, the worktree version will be preserved.
Expand Down

0 comments on commit 7309319

Please sign in to comment.