Skip to content

Commit a2ca58a

Browse files
author
Stephan Dilly
authored
use branches upstream remote if it is already tracked (#598)
closes #597
1 parent e9b296a commit a2ca58a

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
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+
- push branch to its tracking remote ([#597](https://github.com/extrawurst/gitui/issues/597))
12+
1013
## [0.13.0] - 2020-03-15 - Happy Birthday GitUI 🥳
1114

1215
Thanks for your interest and support over this year! Read more about the 1 year anniversary reflections of this project on my [blog](https://blog.extrawurst.org/general/programming/rust/2021/03/15/gitui-a-year-in-opensource.html).

asyncgit/src/sync/branch/mod.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ pub(crate) fn branch_set_upstream(
128128
Ok(())
129129
}
130130

131+
/// returns remote of the upstream tracking branch for `branch`
132+
pub fn get_branch_remote(
133+
repo_path: &str,
134+
branch: &str,
135+
) -> Result<Option<String>> {
136+
let repo = utils::repo(repo_path)?;
137+
let branch = repo.find_branch(branch, BranchType::Local)?;
138+
let reference = bytes2string(branch.get().name_bytes())?;
139+
let remote_name = repo.branch_upstream_remote(&reference).ok();
140+
if let Some(remote_name) = remote_name {
141+
Ok(Some(bytes2string(remote_name.as_ref())?))
142+
} else {
143+
Ok(None)
144+
}
145+
}
146+
131147
/// returns whether the pull merge strategy is set to rebase
132148
pub fn config_is_pull_rebase(repo_path: &str) -> Result<bool> {
133149
let repo = utils::repo(repo_path)?;
@@ -411,6 +427,41 @@ mod tests_branches {
411427
assert_eq!(branches.len(), 3);
412428
assert_eq!(branches[1].remote.as_ref().unwrap(), "r1");
413429
assert_eq!(branches[2].remote.as_ref().unwrap(), "r2");
430+
431+
assert_eq!(
432+
get_branch_remote(repo_path, "r1branch")
433+
.unwrap()
434+
.unwrap(),
435+
String::from("r1")
436+
);
437+
438+
assert_eq!(
439+
get_branch_remote(repo_path, "r2branch")
440+
.unwrap()
441+
.unwrap(),
442+
String::from("r2")
443+
);
444+
}
445+
446+
#[test]
447+
fn test_branch_remote_no_upstream() {
448+
let (_r, repo) = repo_init().unwrap();
449+
let root = repo.path().parent().unwrap();
450+
let repo_path = root.as_os_str().to_str().unwrap();
451+
452+
assert_eq!(
453+
get_branch_remote(repo_path, "master").unwrap(),
454+
None
455+
);
456+
}
457+
458+
#[test]
459+
fn test_branch_remote_no_branch() {
460+
let (_r, repo) = repo_init().unwrap();
461+
let root = repo.path().parent().unwrap();
462+
let repo_path = root.as_os_str().to_str().unwrap();
463+
464+
assert!(get_branch_remote(repo_path, "foo").is_err());
414465
}
415466
}
416467

asyncgit/src/sync/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub mod utils;
2626

2727
pub use branch::{
2828
branch_compare_upstream, checkout_branch, config_is_pull_rebase,
29-
create_branch, delete_branch, get_branches_info,
30-
merge_commit::merge_upstream_commit,
29+
create_branch, delete_branch, get_branch_remote,
30+
get_branches_info, merge_commit::merge_upstream_commit,
3131
merge_ff::branch_merge_upstream_fastforward,
3232
merge_rebase::merge_upstream_rebase, rename::rename_branch,
3333
BranchCompare, BranchInfo,

src/components/push.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use asyncgit::{
1515
extract_username_password, need_username_password,
1616
BasicAuthCredential,
1717
},
18-
get_default_remote,
18+
get_branch_remote, get_default_remote,
1919
},
2020
AsyncNotification, AsyncPush, PushRequest, RemoteProgress,
2121
RemoteProgressState, CWD,
@@ -78,6 +78,7 @@ impl PushComponent {
7878
self.branch = branch;
7979
self.force = force;
8080
self.show()?;
81+
8182
if need_username_password()? {
8283
let cred =
8384
extract_username_password().unwrap_or_else(|_| {
@@ -99,10 +100,26 @@ impl PushComponent {
99100
cred: Option<BasicAuthCredential>,
100101
force: bool,
101102
) -> Result<()> {
103+
let remote = if let Some(remote) =
104+
get_branch_remote(CWD, &self.branch)?
105+
{
106+
log::info!("push: branch '{}' has upstream for remote '{}' - using that",self.branch,remote);
107+
remote
108+
} else {
109+
log::info!("push: branch '{}' has no upstream - looking up default remote",self.branch);
110+
let remote = get_default_remote(CWD)?;
111+
log::info!(
112+
"push: branch '{}' to remote '{}'",
113+
self.branch,
114+
remote
115+
);
116+
remote
117+
};
118+
102119
self.pending = true;
103120
self.progress = None;
104121
self.git_push.request(PushRequest {
105-
remote: get_default_remote(CWD)?,
122+
remote,
106123
branch: self.branch.clone(),
107124
force,
108125
basic_credential: cred,

0 commit comments

Comments
 (0)