switch error handling lib from failure to anyhow

This commit is contained in:
Alexandru Macovei 2020-10-08 09:53:57 +03:00
parent e96f00e302
commit 92e92e449b
5 changed files with 39 additions and 132 deletions

105
Cargo.lock generated
View file

@ -9,6 +9,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "anyhow"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1fd36ffbb1fb7c834eac128ea8d0e310c5aeb635548f9d58861e1308d46e71c"
[[package]]
name = "arrayref"
version = "0.3.6"
@ -43,28 +49,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]]
name = "backtrace"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18b50f5258d1a9ad8396d2d345827875de4261b158124d4c819d9b351454fae5"
dependencies = [
"backtrace-sys",
"cfg-if",
"libc",
"rustc-demangle",
]
[[package]]
name = "backtrace-sys"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b3a000b9c543553af61bc01cbfc403b04b5caa9e421033866f2e98061eb3e61"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "base64"
version = "0.11.0"
@ -179,28 +163,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "failure"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "getrandom"
version = "0.1.6"
@ -215,8 +177,8 @@ dependencies = [
name = "git-absorb"
version = "0.6.4"
dependencies = [
"anyhow",
"clap",
"failure",
"git2",
"memchr",
"slog",
@ -361,24 +323,6 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
[[package]]
name = "proc-macro2"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
@ -458,12 +402,6 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "rustc-demangle"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7f4dccf6f4891ebcc0c39f9b6eb1a83b9bf5d747cb439ec6fba4f3b977038af"
[[package]]
name = "slog"
version = "2.5.2"
@ -513,29 +451,6 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "0.15.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4d960b829a55e56db167e861ddb43602c003c7be0bee1d345021703fac2fb7c"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "synstructure"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]]
name = "take_mut"
version = "0.2.2"
@ -619,12 +534,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
[[package]]
name = "url"
version = "2.1.1"

View file

@ -29,8 +29,8 @@ clap = "~2.33"
slog = "~2.5"
slog-term = "~2.6"
slog-async = "~2.5"
failure = "~0.1"
memchr = "~2.3"
anyhow = "~1.0"
[dev-dependencies]
tempfile = "~3.1"

View file

@ -1,5 +1,6 @@
#[macro_use]
extern crate slog;
use anyhow::{anyhow, Result};
mod commute;
mod owned;
@ -15,7 +16,7 @@ pub struct Config<'a> {
pub logger: &'a slog::Logger,
}
pub fn run(config: &Config) -> Result<(), failure::Error> {
pub fn run(config: &Config) -> Result<()> {
let repo = git2::Repository::open_from_env()?;
debug!(config.logger, "repository found"; "path" => repo.path().to_str());
@ -282,7 +283,7 @@ fn apply_hunk_to_tree<'repo>(
base: &git2::Tree,
hunk: &owned::Hunk,
path: &[u8],
) -> Result<git2::Tree<'repo>, failure::Error> {
) -> Result<git2::Tree<'repo>> {
let mut treebuilder = repo.treebuilder(Some(base))?;
// recurse into nested tree if applicable
@ -293,7 +294,7 @@ fn apply_hunk_to_tree<'repo>(
let (subtree, submode) = {
let entry = treebuilder
.get(first)?
.ok_or_else(|| failure::err_msg("couldn't find tree entry in tree for path"))?;
.ok_or_else(|| anyhow!("couldn't find tree entry in tree for path"))?;
(repo.find_tree(entry.id())?, entry.filemode())
};
// TODO: loop instead of recursing to avoid potential stack overflow
@ -306,7 +307,7 @@ fn apply_hunk_to_tree<'repo>(
let (blob, mode) = {
let entry = treebuilder
.get(path)?
.ok_or_else(|| failure::err_msg("couldn't find blob entry in tree for path"))?;
.ok_or_else(|| anyhow!("couldn't find blob entry in tree for path"))?;
(repo.find_blob(entry.id())?, entry.filemode())
};

View file

@ -1,3 +1,5 @@
use anyhow::{anyhow, Result};
use std::collections::hash_map::HashMap;
use std::rc::Rc;
@ -14,7 +16,7 @@ impl ::std::ops::Deref for Diff {
}
}
impl Diff {
pub fn new(diff: &git2::Diff) -> Result<Self, failure::Error> {
pub fn new(diff: &git2::Diff) -> Result<Self> {
let mut ret = Diff {
patches: Vec::new(),
by_old: HashMap::new(),
@ -24,15 +26,15 @@ impl Diff {
for (delta_idx, _delta) in diff.deltas().enumerate() {
let patch = Patch::new(
&mut git2::Patch::from_diff(diff, delta_idx)?
.ok_or_else(|| failure::err_msg("got empty delta"))?,
.ok_or_else(|| anyhow!("got empty delta"))?,
)?;
if ret.by_old.contains_key(&patch.old_path) {
// TODO: would this case be hit if the diff was put through copy detection?
return Err(failure::err_msg("old path already occupied"));
return Err(anyhow!("old path already occupied"));
}
ret.by_old.insert(patch.old_path.clone(), ret.patches.len());
if ret.by_new.contains_key(&patch.new_path) {
return Err(failure::err_msg("new path already occupied"));
return Err(anyhow!("new path already occupied"));
}
ret.by_new.insert(patch.new_path.clone(), ret.patches.len());
ret.patches.push(patch);
@ -60,7 +62,7 @@ pub struct Hunk {
pub removed: Block,
}
impl Hunk {
pub fn new(patch: &mut git2::Patch, idx: usize) -> Result<Self, failure::Error> {
pub fn new(patch: &mut git2::Patch, idx: usize) -> Result<Self> {
let (added_start, removed_start, mut added_lines, mut removed_lines) = {
let (hunk, _size) = patch.hunk(idx)?;
(
@ -78,62 +80,55 @@ impl Hunk {
match line.origin() {
'+' => {
if line.num_lines() > 1 {
return Err(failure::err_msg("wrong number of lines in hunk"));
return Err(anyhow!("wrong number of lines in hunk"));
}
if line
.new_lineno()
.ok_or_else(|| failure::err_msg("added line did not have lineno"))?
.ok_or_else(|| anyhow!("added line did not have lineno"))?
as usize
!= added_start + added_lines.len()
{
return Err(failure::err_msg("added line did not reach expected lineno"));
return Err(anyhow!("added line did not reach expected lineno"));
}
added_lines.push(Vec::from(line.content()))
}
'-' => {
if line.num_lines() > 1 {
return Err(failure::err_msg("wrong number of lines in hunk"));
return Err(anyhow!("wrong number of lines in hunk"));
}
if line
.old_lineno()
.ok_or_else(|| failure::err_msg("removed line did not have lineno"))?
.ok_or_else(|| anyhow!("removed line did not have lineno"))?
as usize
!= removed_start + removed_lines.len()
{
return Err(failure::err_msg(
"removed line did not reach expected lineno",
));
return Err(anyhow!("removed line did not reach expected lineno",));
}
removed_lines.push(Vec::from(line.content()))
}
'>' => {
if !removed_trailing_newline {
return Err(failure::err_msg("removed nneof was already detected"));
return Err(anyhow!("removed nneof was already detected"));
};
removed_trailing_newline = false
}
'<' => {
if !added_trailing_newline {
return Err(failure::err_msg("added nneof was already detected"));
return Err(anyhow!("added nneof was already detected"));
};
added_trailing_newline = false
}
_ => {
return Err(failure::err_msg(format!(
"unknown line type {:?}",
line.origin()
)))
}
_ => return Err(anyhow!("unknown line type {:?}", line.origin())),
};
}
{
let (hunk, _size) = patch.hunk(idx)?;
if added_lines.len() != hunk.new_lines() as usize {
return Err(failure::err_msg("hunk added block size mismatch"));
return Err(anyhow!("hunk added block size mismatch"));
}
if removed_lines.len() != hunk.old_lines() as usize {
return Err(failure::err_msg("hunk removed block size mismatch"));
return Err(anyhow!("hunk removed block size mismatch"));
}
}
@ -221,27 +216,27 @@ pub struct Patch {
pub hunks: Vec<Hunk>,
}
impl Patch {
pub fn new(patch: &mut git2::Patch) -> Result<Self, failure::Error> {
pub fn new(patch: &mut git2::Patch) -> Result<Self> {
let mut ret = Patch {
old_path: patch
.delta()
.old_file()
.path_bytes()
.map(Vec::from)
.ok_or_else(|| failure::err_msg("delta with empty old path"))?,
.ok_or_else(|| anyhow!("delta with empty old path"))?,
old_id: patch.delta().old_file().id(),
new_path: patch
.delta()
.new_file()
.path_bytes()
.map(Vec::from)
.ok_or_else(|| failure::err_msg("delta with empty new path"))?,
.ok_or_else(|| anyhow!("delta with empty new path"))?,
new_id: patch.delta().new_file().id(),
status: patch.delta().status(),
hunks: Vec::with_capacity(patch.num_hunks()),
};
if patch.delta().nfiles() < 1 || patch.delta().nfiles() > 2 {
return Err(failure::err_msg("delta with multiple files"));
return Err(anyhow!("delta with multiple files"));
}
for idx in 0..patch.num_hunks() {

View file

@ -1,3 +1,5 @@
use anyhow::{anyhow, Result};
use std::collections::HashMap;
pub const MAX_STACK_CONFIG_NAME: &str = "absorb.maxStack";
@ -18,12 +20,12 @@ pub fn working_stack<'repo>(
user_provided_base: Option<&str>,
force: bool,
logger: &slog::Logger,
) -> Result<Vec<git2::Commit<'repo>>, failure::Error> {
) -> Result<Vec<git2::Commit<'repo>>> {
let head = repo.head()?;
debug!(logger, "head found"; "head" => head.name());
if !head.is_branch() {
return Err(failure::err_msg("HEAD is not a branch"));
return Err(anyhow!("HEAD is not a branch"));
}
let mut revwalk = repo.revwalk()?;