Skip to content

[WIP] Replace unwrap calls in asyncgit with error handling - closes #53 #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1076c26
adding a custom error type.
MCord May 14, 2020
7158c3d
hooks.rs updated.
MCord May 14, 2020
f96b86b
removed unwrap from commits_infos.rs
MCord May 14, 2020
3e4d1e9
removed unwrap from diff.rs
MCord May 14, 2020
edcade6
fixed broken linux tests.
MCord May 14, 2020
befee58
removed an unused Error variant.
MCord May 14, 2020
0e6a5b3
changed Error variants from struct to tuples.
MCord May 14, 2020
abdd53e
temperary commit to be modified.
MCord May 14, 2020
ada3955
removed unwrap from spawned code.
MCord May 14, 2020
c326c9b
fixed broken linux tests.
MCord May 15, 2020
da48385
fixed a clippy warning.
MCord May 15, 2020
7cb91c8
use Returns every where.
MCord May 15, 2020
1e5bf3e
renamed returns to result.
MCord May 15, 2020
be70a2c
current_tick should not return a Result.
MCord May 15, 2020
6aa7b74
changed an error message.
MCord May 15, 2020
9b5082b
changed a method signature.
MCord May 15, 2020
6402ea7
changed a method signature.
MCord May 15, 2020
cf07352
changed a method signature.
MCord May 15, 2020
66ae8dc
changed a method signature.
MCord May 15, 2020
e302159
added missing error messages.
MCord May 15, 2020
df601f3
use lossy utf8 conversion for standard io text.
MCord May 15, 2020
71187c0
us is_ok for results in conditions instead of let binding.
MCord May 15, 2020
f91be92
don't use result in test code.
MCord May 15, 2020
e9b5d4c
don't use result in test code.
MCord May 15, 2020
d4e3a4f
don't use result in test code.
MCord May 15, 2020
cdcbf89
handle hook failure case.
MCord May 15, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/release
.DS_Store
/.idea/
56 changes: 56 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion asyncgit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ crossbeam-channel = "0.4"
log = "0.4"
is_executable = "0.1"
scopetime = { path = "../scopetime", version = "0.1" }
tempfile = "3.1"
tempfile = "3.1"
thiserror = "1.0"
99 changes: 61 additions & 38 deletions asyncgit/src/diff.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error::Result;
use crate::{hash, sync, AsyncNotification, FileDiff, CWD};
use crossbeam_channel::Sender;
use log::trace;
Expand Down Expand Up @@ -42,21 +43,22 @@ impl AsyncDiff {
}

///
pub fn last(&mut self) -> Option<(DiffParams, FileDiff)> {
let last = self.last.lock().unwrap();
if let Some(res) = last.clone() {
Some((res.params, res.result))
} else {
None
}
pub fn last(&mut self) -> Result<Option<(DiffParams, FileDiff)>> {
let last = self.last.lock()?;

Ok(match last.clone() {
Some(res) => Some((res.params, res.result)),
None => None,
})
}

///
pub fn refresh(&mut self) {
if let Some(param) = self.get_last_param() {
self.clear_current();
self.request(param);
pub fn refresh(&mut self) -> Result<()> {
if let Ok(Some(param)) = self.get_last_param() {
self.clear_current()?;
self.request(param)?;
}
Ok(())
}

///
Expand All @@ -68,16 +70,16 @@ impl AsyncDiff {
pub fn request(
&mut self,
params: DiffParams,
) -> Option<FileDiff> {
) -> Result<Option<FileDiff>> {
trace!("request");

let hash = hash(&params);

{
let mut current = self.current.lock().unwrap();
let mut current = self.current.lock()?;

if current.0 == hash {
return current.1.clone();
return Ok(current.1.clone());
}

current.0 = hash;
Expand All @@ -91,25 +93,13 @@ impl AsyncDiff {
rayon_core::spawn(move || {
arc_pending.fetch_add(1, Ordering::Relaxed);

let res =
sync::diff::get_diff(CWD, params.0.clone(), params.1);
let mut notify = false;
{
let mut current = arc_current.lock().unwrap();
if current.0 == hash {
current.1 = Some(res.clone());
notify = true;
}
}

{
let mut last = arc_last.lock().unwrap();
*last = Some(LastResult {
result: res,
hash,
params,
});
}
let notify = AsyncDiff::get_diff_helper(
params,
arc_last,
arc_current,
hash,
)
.expect("error getting diff");

arc_pending.fetch_sub(1, Ordering::Relaxed);

Expand All @@ -120,16 +110,49 @@ impl AsyncDiff {
}
});

None
Ok(None)
}

fn get_diff_helper(
params: DiffParams,
arc_last: Arc<
Mutex<Option<LastResult<DiffParams, FileDiff>>>,
>,
arc_current: Arc<Mutex<Request<u64, FileDiff>>>,
hash: u64,
) -> Result<bool> {
let res =
sync::diff::get_diff(CWD, params.0.clone(), params.1)?;

let mut notify = false;
{
let mut current = arc_current.lock()?;
if current.0 == hash {
current.1 = Some(res.clone());
notify = true;
}
}

{
let mut last = arc_last.lock()?;
*last = Some(LastResult {
result: res,
hash,
params,
});
}

Ok(notify)
}

fn get_last_param(&self) -> Option<DiffParams> {
self.last.lock().unwrap().clone().map(|e| e.params)
fn get_last_param(&self) -> Result<Option<DiffParams>> {
Ok(self.last.lock()?.clone().map(|e| e.params))
}

fn clear_current(&mut self) {
let mut current = self.current.lock().unwrap();
fn clear_current(&mut self) -> Result<()> {
let mut current = self.current.lock()?;
current.0 = 0;
current.1 = None;
Ok(())
}
}
21 changes: 21 additions & 0 deletions asyncgit/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("`{0}`")]
Generic(String),

#[error("io error:{0}")]
Io(#[from] std::io::Error),

#[error("git error:{0}")]
Git(#[from] git2::Error),
}

pub type Result<T> = std::result::Result<T, Error>;

impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(error: std::sync::PoisonError<T>) -> Self {
Error::Generic(format!("poison error: {}", error))
}
}
1 change: 1 addition & 0 deletions asyncgit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![deny(clippy::all)]

mod diff;
mod error;
mod revlog;
mod status;
pub mod sync;
Expand Down
Loading