Skip to content
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

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

Merged
merged 4 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions asyncgit/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::{Error, Result},
sync::{
cred::BasicAuthCredential,
remotes::{fetch_origin, push::ProgressNotification},
remotes::{fetch, push::ProgressNotification},
},
AsyncNotification, RemoteProgress, CWD,
};
Expand Down Expand Up @@ -91,7 +91,7 @@ impl AsyncFetch {
arc_progress,
);

let res = fetch_origin(
let res = fetch(
CWD,
&params.branch,
params.basic_credential,
Expand Down
7 changes: 3 additions & 4 deletions asyncgit/src/sync/branch/merge_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions asyncgit/src/sync/branch/merge_ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -106,7 +106,7 @@ pub mod test {

// clone1 again

let bytes = fetch_origin(
let bytes = fetch(
clone1_dir.path().to_str().unwrap(),
"master",
None,
Expand All @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions asyncgit/src/sync/branch/merge_rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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!(
Expand Down
18 changes: 12 additions & 6 deletions asyncgit/src/sync/remotes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<BasicAuthCredential>,
Expand All @@ -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(
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions asyncgit/src/sync/remotes/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down