Skip to content

Commit 38f2716

Browse files
committed
fix branch destination ref logic
Branch case now uses the same destination ref logic as the push path: it honors `push.default=upstream` (when not deleting) via `branch_push_destination_ref`, so the `<remote-ref>` field matches the refspec Git would use.
1 parent 914231b commit 38f2716

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

asyncgit/src/sync/hooks.rs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ use crate::{
55
proxy_auto, tags::tags_missing_remote, Callbacks,
66
},
77
};
8+
use crate::{
9+
sync::branch::get_branch_upstream_merge,
10+
sync::config::{
11+
push_default_strategy_config_repo, PushDefaultStrategyConfig,
12+
},
13+
};
814
use git2::{BranchType, Direction, Oid};
915
pub use git2_hooks::{PrePushRef, PrepareCommitMsgSource};
1016
use scopetime::scope_time;
@@ -65,6 +71,29 @@ pub fn advertised_remote_refs(
6571
Ok(map)
6672
}
6773

74+
fn branch_push_destination_ref(
75+
repo_path: &RepoPath,
76+
branch: &str,
77+
delete: bool,
78+
) -> Result<String> {
79+
let repo = repo(repo_path)?;
80+
let push_default_strategy =
81+
push_default_strategy_config_repo(&repo)?;
82+
83+
if !delete
84+
&& push_default_strategy
85+
== PushDefaultStrategyConfig::Upstream
86+
{
87+
if let Ok(Some(branch_upstream_merge)) =
88+
get_branch_upstream_merge(repo_path, branch)
89+
{
90+
return Ok(branch_upstream_merge);
91+
}
92+
}
93+
94+
Ok(format!("refs/heads/{branch}"))
95+
}
96+
6897
/// see `git2_hooks::hooks_commit_msg`
6998
pub fn hooks_commit_msg(
7099
repo_path: &RepoPath,
@@ -123,17 +152,18 @@ pub fn hooks_pre_push(
123152
let repo = repo(repo_path)?;
124153
let advertised = advertised_remote_refs(repo_path, remote, url)?;
125154
let updates = match push {
126-
PrePushTarget::Branch {
127-
branch,
128-
remote_branch,
129-
delete,
130-
} => vec![pre_push_branch_update(
131-
repo_path,
132-
branch,
133-
*remote_branch,
134-
*delete,
135-
&advertised,
136-
)?],
155+
PrePushTarget::Branch { branch, delete } => {
156+
let remote_ref = branch_push_destination_ref(
157+
repo_path, branch, *delete,
158+
)?;
159+
vec![pre_push_branch_update(
160+
repo_path,
161+
branch,
162+
&remote_ref,
163+
*delete,
164+
&advertised,
165+
)?]
166+
}
137167
PrePushTarget::Tags => {
138168
// If remote is None, use url per git spec
139169
let remote = remote.unwrap_or(url);
@@ -152,7 +182,7 @@ pub fn hooks_pre_push(
152182
fn pre_push_branch_update(
153183
repo_path: &RepoPath,
154184
branch_name: &str,
155-
remote_branch_name: Option<&str>,
185+
remote_ref: &str,
156186
delete: bool,
157187
advertised: &HashMap<String, Oid>,
158188
) -> Result<PrePushRef> {
@@ -167,10 +197,7 @@ fn pre_push_branch_update(
167197
})
168198
.flatten();
169199

170-
let remote_branch = remote_branch_name.unwrap_or(branch_name);
171-
let remote_ref = format!("refs/heads/{remote_branch}");
172-
173-
let remote_oid = advertised.get(&remote_ref).copied();
200+
let remote_oid = advertised.get(remote_ref).copied();
174201

175202
Ok(PrePushRef::new(
176203
local_ref, local_oid, remote_ref, remote_oid,
@@ -213,8 +240,6 @@ pub enum PrePushTarget<'a> {
213240
Branch {
214241
/// Local branch name being pushed.
215242
branch: &'a str,
216-
/// Optional remote branch name if different from local.
217-
remote_branch: Option<&'a str>,
218243
/// Whether this is a delete push.
219244
delete: bool,
220245
},

src/popups/push.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ impl PushPopup {
169169
&remote_url,
170170
&asyncgit::sync::PrePushTarget::Branch {
171171
branch: &self.branch,
172-
remote_branch: None,
173172
delete: self.modifier.delete(),
174173
},
175174
)? {

0 commit comments

Comments
 (0)