Fix exit on fetching a branch that has no upstream/remote (#638)

* do not assume remote/upstream of a branch anymore
This commit is contained in:
Stephan Dilly 2021-04-12 16:47:08 +02:00 committed by GitHub
parent 6594a8d31c
commit ee5b9d9f4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 24 deletions

View file

@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased ## 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 ## [0.14.0] - 2020-04-11
### Added ### Added

View file

@ -2,7 +2,7 @@ use crate::{
error::{Error, Result}, error::{Error, Result},
sync::{ sync::{
cred::BasicAuthCredential, cred::BasicAuthCredential,
remotes::{fetch_origin, push::ProgressNotification}, remotes::{fetch, push::ProgressNotification},
}, },
AsyncNotification, RemoteProgress, CWD, AsyncNotification, RemoteProgress, CWD,
}; };
@ -91,7 +91,7 @@ impl AsyncFetch {
arc_progress, arc_progress,
); );
let res = fetch_origin( let res = fetch(
CWD, CWD,
&params.branch, &params.branch,
params.basic_credential, params.basic_credential,

View file

@ -95,7 +95,7 @@ mod test {
use super::*; use super::*;
use crate::sync::{ use crate::sync::{
branch_compare_upstream, branch_compare_upstream,
remotes::{fetch_origin, push::push}, remotes::{fetch, push::push},
tests::{ tests::{
debug_cmd_print, get_commit_ids, repo_clone, debug_cmd_print, get_commit_ids, repo_clone,
repo_init_bare, write_commit_file, repo_init_bare, write_commit_file,
@ -146,8 +146,7 @@ mod test {
.is_err()); .is_err());
//lets fetch from origin //lets fetch from origin
let bytes = let bytes = fetch(clone2_dir, "master", None, None).unwrap();
fetch_origin(clone2_dir, "master", None, None).unwrap();
assert!(bytes > 0); assert!(bytes > 0);
//we should be one commit behind //we should be one commit behind
@ -218,7 +217,7 @@ mod test {
write_commit_file(&clone2, "test.bin", "foobar", "commit2"); write_commit_file(&clone2, "test.bin", "foobar", "commit2");
let bytes = fetch_origin( let bytes = fetch(
clone2_dir.path().to_str().unwrap(), clone2_dir.path().to_str().unwrap(),
"master", "master",
None, None,

View file

@ -49,7 +49,7 @@ pub fn branch_merge_upstream_fastforward(
pub mod test { pub mod test {
use super::*; use super::*;
use crate::sync::{ use crate::sync::{
remotes::{fetch_origin, push::push}, remotes::{fetch, push::push},
tests::{ tests::{
debug_cmd_print, get_commit_ids, repo_clone, debug_cmd_print, get_commit_ids, repo_clone,
repo_init_bare, write_commit_file, repo_init_bare, write_commit_file,
@ -106,7 +106,7 @@ pub mod test {
// clone1 again // clone1 again
let bytes = fetch_origin( let bytes = fetch(
clone1_dir.path().to_str().unwrap(), clone1_dir.path().to_str().unwrap(),
"master", "master",
None, None,
@ -115,7 +115,7 @@ pub mod test {
.unwrap(); .unwrap();
assert!(bytes > 0); assert!(bytes > 0);
let bytes = fetch_origin( let bytes = fetch(
clone1_dir.path().to_str().unwrap(), clone1_dir.path().to_str().unwrap(),
"master", "master",
None, None,

View file

@ -57,7 +57,7 @@ mod test {
use super::*; use super::*;
use crate::sync::{ use crate::sync::{
branch_compare_upstream, get_commits_info, branch_compare_upstream, get_commits_info,
remotes::{fetch_origin, push::push}, remotes::{fetch, push::push},
tests::{ tests::{
debug_cmd_print, get_commit_ids, repo_clone, debug_cmd_print, get_commit_ids, repo_clone,
repo_init_bare, write_commit_file, repo_init_bare, write_commit_file,
@ -133,8 +133,7 @@ mod test {
assert_eq!(clone1.head_detached().unwrap(), false); assert_eq!(clone1.head_detached().unwrap(), false);
//lets fetch from origin //lets fetch from origin
let bytes = let bytes = fetch(clone1_dir, "master", None, None).unwrap();
fetch_origin(clone1_dir, "master", None, None).unwrap();
assert!(bytes > 0); assert!(bytes > 0);
//we should be one commit behind //we should be one commit behind
@ -204,7 +203,7 @@ mod test {
//lets fetch from origin //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(); merge_upstream_rebase(clone1_dir, "master").unwrap();
@ -266,8 +265,7 @@ mod test {
let _commit3 = let _commit3 =
write_commit_file(&clone1, "test2.txt", "foo", "commit3"); write_commit_file(&clone1, "test2.txt", "foo", "commit3");
let bytes = let bytes = fetch(clone1_dir, "master", None, None).unwrap();
fetch_origin(clone1_dir, "master", None, None).unwrap();
assert!(bytes > 0); assert!(bytes > 0);
assert_eq!( assert_eq!(

View file

@ -11,9 +11,10 @@ use crate::{
}, },
}; };
use crossbeam_channel::Sender; use crossbeam_channel::Sender;
use git2::{FetchOptions, Repository}; use git2::{BranchType, FetchOptions, Repository};
use push::remote_callbacks; use push::remote_callbacks;
use scopetime::scope_time; use scopetime::scope_time;
use utils::bytes2string;
/// origin /// origin
pub const DEFAULT_REMOTE_NAME: &str = "origin"; pub const DEFAULT_REMOTE_NAME: &str = "origin";
@ -71,8 +72,8 @@ pub(crate) fn get_default_remote_in_repo(
Err(Error::NoDefaultRemoteFound) Err(Error::NoDefaultRemoteFound)
} }
/// /// fetches from upstream/remote for `branch`
pub(crate) fn fetch_origin( pub(crate) fn fetch(
repo_path: &str, repo_path: &str,
branch: &str, branch: &str,
basic_credential: Option<BasicAuthCredential>, basic_credential: Option<BasicAuthCredential>,
@ -81,8 +82,13 @@ pub(crate) fn fetch_origin(
scope_time!("fetch_origin"); scope_time!("fetch_origin");
let repo = utils::repo(repo_path)?; let repo = utils::repo(repo_path)?;
let mut remote = let branch_ref = repo
repo.find_remote(&get_default_remote_in_repo(&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(); let mut options = FetchOptions::new();
options.remote_callbacks(remote_callbacks( options.remote_callbacks(remote_callbacks(
@ -117,7 +123,7 @@ mod tests {
assert_eq!(remotes, vec![String::from("origin")]); assert_eq!(remotes, vec![String::from("origin")]);
fetch_origin(repo_path, "master", None, None).unwrap(); fetch(repo_path, "master", None, None).unwrap();
} }
#[test] #[test]

View file

@ -154,7 +154,7 @@ mod tests {
use super::*; use super::*;
use crate::sync::{ use crate::sync::{
self, self,
remotes::{fetch_origin, push::push}, remotes::{fetch, push::push},
tests::{repo_clone, repo_init_bare}, tests::{repo_clone, repo_init_bare},
}; };
use sync::tests::write_commit_file; use sync::tests::write_commit_file;
@ -195,8 +195,7 @@ mod tests {
assert_eq!(sync::get_tags(clone2_dir).unwrap().len(), 0); assert_eq!(sync::get_tags(clone2_dir).unwrap().len(), 0);
//lets fetch from origin //lets fetch from origin
let bytes = let bytes = fetch(clone2_dir, "master", None, None).unwrap();
fetch_origin(clone2_dir, "master", None, None).unwrap();
assert!(bytes > 0); assert!(bytes > 0);
sync::merge_upstream_commit(clone2_dir, "master").unwrap(); sync::merge_upstream_commit(clone2_dir, "master").unwrap();