Skip to content

Commit ee5b9d9

Browse files
author
Stephan Dilly
authored
Fix exit on fetching a branch that has no upstream/remote (#638)
* do not assume remote/upstream of a branch anymore
1 parent 6594a8d commit ee5b9d9

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Fixed
11+
- fetch crashed when no upstream of branch is set ([#637](https://github.com/extrawurst/gitui/issues/637))
12+
1013
## [0.14.0] - 2020-04-11
1114

1215
### Added

asyncgit/src/fetch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
error::{Error, Result},
33
sync::{
44
cred::BasicAuthCredential,
5-
remotes::{fetch_origin, push::ProgressNotification},
5+
remotes::{fetch, push::ProgressNotification},
66
},
77
AsyncNotification, RemoteProgress, CWD,
88
};
@@ -91,7 +91,7 @@ impl AsyncFetch {
9191
arc_progress,
9292
);
9393

94-
let res = fetch_origin(
94+
let res = fetch(
9595
CWD,
9696
&params.branch,
9797
params.basic_credential,

asyncgit/src/sync/branch/merge_commit.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mod test {
9595
use super::*;
9696
use crate::sync::{
9797
branch_compare_upstream,
98-
remotes::{fetch_origin, push::push},
98+
remotes::{fetch, push::push},
9999
tests::{
100100
debug_cmd_print, get_commit_ids, repo_clone,
101101
repo_init_bare, write_commit_file,
@@ -146,8 +146,7 @@ mod test {
146146
.is_err());
147147

148148
//lets fetch from origin
149-
let bytes =
150-
fetch_origin(clone2_dir, "master", None, None).unwrap();
149+
let bytes = fetch(clone2_dir, "master", None, None).unwrap();
151150
assert!(bytes > 0);
152151

153152
//we should be one commit behind
@@ -218,7 +217,7 @@ mod test {
218217

219218
write_commit_file(&clone2, "test.bin", "foobar", "commit2");
220219

221-
let bytes = fetch_origin(
220+
let bytes = fetch(
222221
clone2_dir.path().to_str().unwrap(),
223222
"master",
224223
None,

asyncgit/src/sync/branch/merge_ff.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn branch_merge_upstream_fastforward(
4949
pub mod test {
5050
use super::*;
5151
use crate::sync::{
52-
remotes::{fetch_origin, push::push},
52+
remotes::{fetch, push::push},
5353
tests::{
5454
debug_cmd_print, get_commit_ids, repo_clone,
5555
repo_init_bare, write_commit_file,
@@ -106,7 +106,7 @@ pub mod test {
106106

107107
// clone1 again
108108

109-
let bytes = fetch_origin(
109+
let bytes = fetch(
110110
clone1_dir.path().to_str().unwrap(),
111111
"master",
112112
None,
@@ -115,7 +115,7 @@ pub mod test {
115115
.unwrap();
116116
assert!(bytes > 0);
117117

118-
let bytes = fetch_origin(
118+
let bytes = fetch(
119119
clone1_dir.path().to_str().unwrap(),
120120
"master",
121121
None,

asyncgit/src/sync/branch/merge_rebase.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ mod test {
5757
use super::*;
5858
use crate::sync::{
5959
branch_compare_upstream, get_commits_info,
60-
remotes::{fetch_origin, push::push},
60+
remotes::{fetch, push::push},
6161
tests::{
6262
debug_cmd_print, get_commit_ids, repo_clone,
6363
repo_init_bare, write_commit_file,
@@ -133,8 +133,7 @@ mod test {
133133
assert_eq!(clone1.head_detached().unwrap(), false);
134134

135135
//lets fetch from origin
136-
let bytes =
137-
fetch_origin(clone1_dir, "master", None, None).unwrap();
136+
let bytes = fetch(clone1_dir, "master", None, None).unwrap();
138137
assert!(bytes > 0);
139138

140139
//we should be one commit behind
@@ -204,7 +203,7 @@ mod test {
204203

205204
//lets fetch from origin
206205

207-
fetch_origin(clone1_dir, "master", None, None).unwrap();
206+
fetch(clone1_dir, "master", None, None).unwrap();
208207

209208
merge_upstream_rebase(clone1_dir, "master").unwrap();
210209

@@ -266,8 +265,7 @@ mod test {
266265
let _commit3 =
267266
write_commit_file(&clone1, "test2.txt", "foo", "commit3");
268267

269-
let bytes =
270-
fetch_origin(clone1_dir, "master", None, None).unwrap();
268+
let bytes = fetch(clone1_dir, "master", None, None).unwrap();
271269
assert!(bytes > 0);
272270

273271
assert_eq!(

asyncgit/src/sync/remotes/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use crate::{
1111
},
1212
};
1313
use crossbeam_channel::Sender;
14-
use git2::{FetchOptions, Repository};
14+
use git2::{BranchType, FetchOptions, Repository};
1515
use push::remote_callbacks;
1616
use scopetime::scope_time;
17+
use utils::bytes2string;
1718

1819
/// origin
1920
pub const DEFAULT_REMOTE_NAME: &str = "origin";
@@ -71,8 +72,8 @@ pub(crate) fn get_default_remote_in_repo(
7172
Err(Error::NoDefaultRemoteFound)
7273
}
7374

74-
///
75-
pub(crate) fn fetch_origin(
75+
/// fetches from upstream/remote for `branch`
76+
pub(crate) fn fetch(
7677
repo_path: &str,
7778
branch: &str,
7879
basic_credential: Option<BasicAuthCredential>,
@@ -81,8 +82,13 @@ pub(crate) fn fetch_origin(
8182
scope_time!("fetch_origin");
8283

8384
let repo = utils::repo(repo_path)?;
84-
let mut remote =
85-
repo.find_remote(&get_default_remote_in_repo(&repo)?)?;
85+
let branch_ref = repo
86+
.find_branch(branch, BranchType::Local)?
87+
.into_reference();
88+
let branch_ref = bytes2string(branch_ref.name_bytes())?;
89+
let remote_name = repo.branch_upstream_remote(&branch_ref)?;
90+
let remote_name = bytes2string(&*remote_name)?;
91+
let mut remote = repo.find_remote(&remote_name)?;
8692

8793
let mut options = FetchOptions::new();
8894
options.remote_callbacks(remote_callbacks(
@@ -117,7 +123,7 @@ mod tests {
117123

118124
assert_eq!(remotes, vec![String::from("origin")]);
119125

120-
fetch_origin(repo_path, "master", None, None).unwrap();
126+
fetch(repo_path, "master", None, None).unwrap();
121127
}
122128

123129
#[test]

asyncgit/src/sync/remotes/tags.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ mod tests {
154154
use super::*;
155155
use crate::sync::{
156156
self,
157-
remotes::{fetch_origin, push::push},
157+
remotes::{fetch, push::push},
158158
tests::{repo_clone, repo_init_bare},
159159
};
160160
use sync::tests::write_commit_file;
@@ -195,8 +195,7 @@ mod tests {
195195
assert_eq!(sync::get_tags(clone2_dir).unwrap().len(), 0);
196196

197197
//lets fetch from origin
198-
let bytes =
199-
fetch_origin(clone2_dir, "master", None, None).unwrap();
198+
let bytes = fetch(clone2_dir, "master", None, None).unwrap();
200199
assert!(bytes > 0);
201200

202201
sync::merge_upstream_commit(clone2_dir, "master").unwrap();

0 commit comments

Comments
 (0)