use branches upstream remote if it is already tracked (#598)

closes #597
This commit is contained in:
Stephan Dilly 2021-03-20 18:47:53 +01:00 committed by GitHub
parent e9b296ac62
commit a2ca58a3f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 4 deletions

View file

@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Fixed
- push branch to its tracking remote ([#597](https://github.com/extrawurst/gitui/issues/597))
## [0.13.0] - 2020-03-15 - Happy Birthday GitUI 🥳
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).

View file

@ -128,6 +128,22 @@ pub(crate) fn branch_set_upstream(
Ok(())
}
/// returns remote of the upstream tracking branch for `branch`
pub fn get_branch_remote(
repo_path: &str,
branch: &str,
) -> Result<Option<String>> {
let repo = utils::repo(repo_path)?;
let branch = repo.find_branch(branch, BranchType::Local)?;
let reference = bytes2string(branch.get().name_bytes())?;
let remote_name = repo.branch_upstream_remote(&reference).ok();
if let Some(remote_name) = remote_name {
Ok(Some(bytes2string(remote_name.as_ref())?))
} else {
Ok(None)
}
}
/// returns whether the pull merge strategy is set to rebase
pub fn config_is_pull_rebase(repo_path: &str) -> Result<bool> {
let repo = utils::repo(repo_path)?;
@ -411,6 +427,41 @@ mod tests_branches {
assert_eq!(branches.len(), 3);
assert_eq!(branches[1].remote.as_ref().unwrap(), "r1");
assert_eq!(branches[2].remote.as_ref().unwrap(), "r2");
assert_eq!(
get_branch_remote(repo_path, "r1branch")
.unwrap()
.unwrap(),
String::from("r1")
);
assert_eq!(
get_branch_remote(repo_path, "r2branch")
.unwrap()
.unwrap(),
String::from("r2")
);
}
#[test]
fn test_branch_remote_no_upstream() {
let (_r, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
assert_eq!(
get_branch_remote(repo_path, "master").unwrap(),
None
);
}
#[test]
fn test_branch_remote_no_branch() {
let (_r, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();
assert!(get_branch_remote(repo_path, "foo").is_err());
}
}

View file

@ -26,8 +26,8 @@ pub mod utils;
pub use branch::{
branch_compare_upstream, checkout_branch, config_is_pull_rebase,
create_branch, delete_branch, get_branches_info,
merge_commit::merge_upstream_commit,
create_branch, delete_branch, get_branch_remote,
get_branches_info, merge_commit::merge_upstream_commit,
merge_ff::branch_merge_upstream_fastforward,
merge_rebase::merge_upstream_rebase, rename::rename_branch,
BranchCompare, BranchInfo,

View file

@ -15,7 +15,7 @@ use asyncgit::{
extract_username_password, need_username_password,
BasicAuthCredential,
},
get_default_remote,
get_branch_remote, get_default_remote,
},
AsyncNotification, AsyncPush, PushRequest, RemoteProgress,
RemoteProgressState, CWD,
@ -78,6 +78,7 @@ impl PushComponent {
self.branch = branch;
self.force = force;
self.show()?;
if need_username_password()? {
let cred =
extract_username_password().unwrap_or_else(|_| {
@ -99,10 +100,26 @@ impl PushComponent {
cred: Option<BasicAuthCredential>,
force: bool,
) -> Result<()> {
let remote = if let Some(remote) =
get_branch_remote(CWD, &self.branch)?
{
log::info!("push: branch '{}' has upstream for remote '{}' - using that",self.branch,remote);
remote
} else {
log::info!("push: branch '{}' has no upstream - looking up default remote",self.branch);
let remote = get_default_remote(CWD)?;
log::info!(
"push: branch '{}' to remote '{}'",
self.branch,
remote
);
remote
};
self.pending = true;
self.progress = None;
self.git_push.request(PushRequest {
remote: get_default_remote(CWD)?,
remote,
branch: self.branch.clone(),
force,
basic_credential: cred,