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

Push with refspec #2542

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions asyncgit/src/sync/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@ pub fn get_branch_remote(
}
}

/// Retrieve the upstream merge of a local `branch`,
/// configured in "branch.*.merge"
///
/// For details check git2 `branch_upstream_merge`
pub fn get_branch_upstream_merge(
repo_path: &RepoPath,
branch: &str,
) -> Result<Option<String>> {
let repo = repo(repo_path)?;
let branch = repo.find_branch(branch, BranchType::Local)?;
let reference = bytes2string(branch.get().name_bytes())?;
let remote_name = repo.branch_upstream_merge(&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: &RepoPath) -> Result<bool> {
let repo = repo(repo_path)?;
Expand Down Expand Up @@ -549,11 +568,21 @@ mod tests_branches {
);
}

fn branch_set_upstream(repo_path: &RepoPath, branch_ref: &str, upstream: Option<&str>) -> Result<()> {
let repo = repo(repo_path)?;
let branch_as_ref = repo.find_reference(branch_ref)?;
let mut branch = git2::Branch::wrap(branch_as_ref);
branch.set_upstream(upstream)?;

Ok(())
}

fn clone_branch_commit_push(target: &str, branch_name: &str) {
let (dir, repo) = repo_clone(target).unwrap();
let dir = dir.path().to_str().unwrap();

write_commit_file(&repo, "f1.txt", "foo", "c1");
branch_set_upstream(&dir.into(), "refs/heads/master", None).unwrap();
rename_branch(&dir.into(), "refs/heads/master", branch_name)
.unwrap();
push_branch(
Expand Down
2 changes: 1 addition & 1 deletion asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub use blame::{blame_file, BlameHunk, FileBlame};
pub use branch::{
branch_compare_upstream, checkout_branch, checkout_commit,
config_is_pull_rebase, create_branch, delete_branch,
get_branch_remote, get_branches_info,
get_branch_remote, get_branches_info, get_branch_upstream_merge,
merge_commit::merge_upstream_commit,
merge_ff::branch_merge_upstream_fastforward,
merge_rebase::merge_upstream_rebase, rename::rename_branch,
Expand Down
15 changes: 13 additions & 2 deletions asyncgit/src/sync/remotes/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
sync::{
branch::branch_set_upstream_after_push,
cred::BasicAuthCredential,
get_branch_upstream_merge,
remotes::{proxy_auto, Callbacks},
repository::repo,
CommitId, RepoPath,
Expand Down Expand Up @@ -163,9 +164,19 @@ pub fn push_raw(
PushType::Tag => "tags",
};

let branch_name =
let mut push_ref =
format!("{branch_modifier}refs/{ref_type}/{branch}");
remote.push(&[branch_name.as_str()], Some(&mut options))?;

if !delete {
if let Ok(Some(branch_upstream_merge)) =
get_branch_upstream_merge(repo_path, branch)
{
push_ref.push_str(&format!(":{branch_upstream_merge}"));
}
}

log::debug!("push to: {push_ref}");
remote.push(&[push_ref], Some(&mut options))?;

if let Some((reference, msg)) =
callbacks.get_stats()?.push_rejected_msg
Expand Down