diff --git a/CHANGELOG.md b/CHANGELOG.md index f7e52b041d..ec73961715 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed +- fetch crashed when no upstream of branch is set ([#637](https://github.com/extrawurst/gitui/issues/637)) + ## [0.14.0] - 2020-04-11 ### Added diff --git a/asyncgit/src/fetch.rs b/asyncgit/src/fetch.rs index 448ffc7d6a..cd2552914b 100644 --- a/asyncgit/src/fetch.rs +++ b/asyncgit/src/fetch.rs @@ -2,7 +2,7 @@ use crate::{ error::{Error, Result}, sync::{ cred::BasicAuthCredential, - remotes::{fetch_origin, push::ProgressNotification}, + remotes::{fetch, push::ProgressNotification}, }, AsyncNotification, RemoteProgress, CWD, }; @@ -91,7 +91,7 @@ impl AsyncFetch { arc_progress, ); - let res = fetch_origin( + let res = fetch( CWD, ¶ms.branch, params.basic_credential, diff --git a/asyncgit/src/sync/branch/merge_commit.rs b/asyncgit/src/sync/branch/merge_commit.rs index 139ea7b7ea..186c731a80 100644 --- a/asyncgit/src/sync/branch/merge_commit.rs +++ b/asyncgit/src/sync/branch/merge_commit.rs @@ -95,7 +95,7 @@ mod test { use super::*; use crate::sync::{ branch_compare_upstream, - remotes::{fetch_origin, push::push}, + remotes::{fetch, push::push}, tests::{ debug_cmd_print, get_commit_ids, repo_clone, repo_init_bare, write_commit_file, @@ -146,8 +146,7 @@ mod test { .is_err()); //lets fetch from origin - let bytes = - fetch_origin(clone2_dir, "master", None, None).unwrap(); + let bytes = fetch(clone2_dir, "master", None, None).unwrap(); assert!(bytes > 0); //we should be one commit behind @@ -218,7 +217,7 @@ mod test { write_commit_file(&clone2, "test.bin", "foobar", "commit2"); - let bytes = fetch_origin( + let bytes = fetch( clone2_dir.path().to_str().unwrap(), "master", None, diff --git a/asyncgit/src/sync/branch/merge_ff.rs b/asyncgit/src/sync/branch/merge_ff.rs index eb2973ea42..f5265a70cd 100644 --- a/asyncgit/src/sync/branch/merge_ff.rs +++ b/asyncgit/src/sync/branch/merge_ff.rs @@ -49,7 +49,7 @@ pub fn branch_merge_upstream_fastforward( pub mod test { use super::*; use crate::sync::{ - remotes::{fetch_origin, push::push}, + remotes::{fetch, push::push}, tests::{ debug_cmd_print, get_commit_ids, repo_clone, repo_init_bare, write_commit_file, @@ -106,7 +106,7 @@ pub mod test { // clone1 again - let bytes = fetch_origin( + let bytes = fetch( clone1_dir.path().to_str().unwrap(), "master", None, @@ -115,7 +115,7 @@ pub mod test { .unwrap(); assert!(bytes > 0); - let bytes = fetch_origin( + let bytes = fetch( clone1_dir.path().to_str().unwrap(), "master", None, diff --git a/asyncgit/src/sync/branch/merge_rebase.rs b/asyncgit/src/sync/branch/merge_rebase.rs index 2d2ad86b07..c04f12aeaa 100644 --- a/asyncgit/src/sync/branch/merge_rebase.rs +++ b/asyncgit/src/sync/branch/merge_rebase.rs @@ -57,7 +57,7 @@ mod test { use super::*; use crate::sync::{ branch_compare_upstream, get_commits_info, - remotes::{fetch_origin, push::push}, + remotes::{fetch, push::push}, tests::{ debug_cmd_print, get_commit_ids, repo_clone, repo_init_bare, write_commit_file, @@ -133,8 +133,7 @@ mod test { assert_eq!(clone1.head_detached().unwrap(), false); //lets fetch from origin - let bytes = - fetch_origin(clone1_dir, "master", None, None).unwrap(); + let bytes = fetch(clone1_dir, "master", None, None).unwrap(); assert!(bytes > 0); //we should be one commit behind @@ -204,7 +203,7 @@ mod test { //lets fetch from origin - fetch_origin(clone1_dir, "master", None, None).unwrap(); + fetch(clone1_dir, "master", None, None).unwrap(); merge_upstream_rebase(clone1_dir, "master").unwrap(); @@ -266,8 +265,7 @@ mod test { let _commit3 = write_commit_file(&clone1, "test2.txt", "foo", "commit3"); - let bytes = - fetch_origin(clone1_dir, "master", None, None).unwrap(); + let bytes = fetch(clone1_dir, "master", None, None).unwrap(); assert!(bytes > 0); assert_eq!( diff --git a/asyncgit/src/sync/remotes/mod.rs b/asyncgit/src/sync/remotes/mod.rs index 36ad736612..c7cd68e4eb 100644 --- a/asyncgit/src/sync/remotes/mod.rs +++ b/asyncgit/src/sync/remotes/mod.rs @@ -11,9 +11,10 @@ use crate::{ }, }; use crossbeam_channel::Sender; -use git2::{FetchOptions, Repository}; +use git2::{BranchType, FetchOptions, Repository}; use push::remote_callbacks; use scopetime::scope_time; +use utils::bytes2string; /// origin pub const DEFAULT_REMOTE_NAME: &str = "origin"; @@ -71,8 +72,8 @@ pub(crate) fn get_default_remote_in_repo( Err(Error::NoDefaultRemoteFound) } -/// -pub(crate) fn fetch_origin( +/// fetches from upstream/remote for `branch` +pub(crate) fn fetch( repo_path: &str, branch: &str, basic_credential: Option, @@ -81,8 +82,13 @@ pub(crate) fn fetch_origin( scope_time!("fetch_origin"); let repo = utils::repo(repo_path)?; - let mut remote = - repo.find_remote(&get_default_remote_in_repo(&repo)?)?; + let branch_ref = repo + .find_branch(branch, BranchType::Local)? + .into_reference(); + let branch_ref = bytes2string(branch_ref.name_bytes())?; + let remote_name = repo.branch_upstream_remote(&branch_ref)?; + let remote_name = bytes2string(&*remote_name)?; + let mut remote = repo.find_remote(&remote_name)?; let mut options = FetchOptions::new(); options.remote_callbacks(remote_callbacks( @@ -117,7 +123,7 @@ mod tests { assert_eq!(remotes, vec![String::from("origin")]); - fetch_origin(repo_path, "master", None, None).unwrap(); + fetch(repo_path, "master", None, None).unwrap(); } #[test] diff --git a/asyncgit/src/sync/remotes/tags.rs b/asyncgit/src/sync/remotes/tags.rs index bfb983a184..155bf04fab 100644 --- a/asyncgit/src/sync/remotes/tags.rs +++ b/asyncgit/src/sync/remotes/tags.rs @@ -154,7 +154,7 @@ mod tests { use super::*; use crate::sync::{ self, - remotes::{fetch_origin, push::push}, + remotes::{fetch, push::push}, tests::{repo_clone, repo_init_bare}, }; use sync::tests::write_commit_file; @@ -195,8 +195,7 @@ mod tests { assert_eq!(sync::get_tags(clone2_dir).unwrap().len(), 0); //lets fetch from origin - let bytes = - fetch_origin(clone2_dir, "master", None, None).unwrap(); + let bytes = fetch(clone2_dir, "master", None, None).unwrap(); assert!(bytes > 0); sync::merge_upstream_commit(clone2_dir, "master").unwrap();