From 2434af55bde414582c7b7fcee18480850f090dc7 Mon Sep 17 00:00:00 2001 From: Eduardo Flores Date: Sun, 4 Oct 2020 13:05:20 -0700 Subject: [PATCH] Handled crash when git2 failed to obtain username from URL Fixed formatting errors --- asyncgit/src/sync/remotes.rs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/asyncgit/src/sync/remotes.rs b/asyncgit/src/sync/remotes.rs index 278b3ccc..c4e2d204 100644 --- a/asyncgit/src/sync/remotes.rs +++ b/asyncgit/src/sync/remotes.rs @@ -1,15 +1,13 @@ //! +use super::CommitId; use crate::{error::Result, sync::utils}; use crossbeam_channel::Sender; use git2::{ - Cred, FetchOptions, PackBuilderStage, PushOptions, - RemoteCallbacks, + Cred, Error as GitError, FetchOptions, PackBuilderStage, + PushOptions, RemoteCallbacks, }; use scopetime::scope_time; - -use super::CommitId; - /// #[derive(Debug, Clone)] pub enum ProgressNotification { @@ -71,7 +69,10 @@ pub fn fetch_origin(repo_path: &str, branch: &str) -> Result { let mut remote = repo.find_remote("origin")?; let mut options = FetchOptions::new(); - options.remote_callbacks(remote_callbacks(None)); + options.remote_callbacks(match remote_callbacks(None) { + Ok(callback) => callback, + Err(e) => return Err(e), + }); remote.fetch(&[branch], Some(&mut options), None)?; @@ -92,7 +93,12 @@ pub fn push( let mut options = PushOptions::new(); - options.remote_callbacks(remote_callbacks(Some(progress_sender))); + options.remote_callbacks( + match remote_callbacks(Some(progress_sender)) { + Ok(callbacks) => callbacks, + Err(e) => return Err(e), + }, + ); options.packbuilder_parallelism(0); remote.push(&[branch], Some(&mut options))?; @@ -102,7 +108,7 @@ pub fn push( fn remote_callbacks<'a>( sender: Option>, -) -> RemoteCallbacks<'a> { +) -> Result> { let mut callbacks = RemoteCallbacks::new(); let sender_clone = sender.clone(); callbacks.push_transfer_progress(move |current, total, bytes| { @@ -167,12 +173,15 @@ fn remote_callbacks<'a>( allowed_types ); - Cred::ssh_key_from_agent( - username_from_url.expect("username not found"), - ) + match username_from_url { + Some(username) => Cred::ssh_key_from_agent(username), + None => Err(GitError::from_str( + " Couldn't extract username from url.", + )), + } }); - callbacks + Ok(callbacks) } #[cfg(test)]