Skip to content

Commit 28eb136

Browse files
committed
push: respect branch.*.merge when push default is upstream
asyncgit: config add push default strategy config
1 parent 7d60d53 commit 28eb136

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

asyncgit/src/sync/config.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,54 @@ pub fn untracked_files_config_repo(
6262
Ok(ShowUntrackedFilesConfig::All)
6363
}
6464

65+
// see https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault
66+
/// represent `push.default` git config
67+
pub enum PushDefaultStrategyConfig {
68+
Nothing,
69+
Current,
70+
Upstream,
71+
Simple,
72+
Matching,
73+
}
74+
75+
impl Default for PushDefaultStrategyConfig {
76+
fn default() -> Self {
77+
Self::Simple
78+
}
79+
}
80+
81+
impl<'a> TryFrom<&'a str> for PushDefaultStrategyConfig {
82+
type Error = crate::Error;
83+
fn try_from(
84+
value: &'a str,
85+
) -> std::result::Result<Self, Self::Error> {
86+
match value {
87+
"nothing" => Ok(Self::Nothing),
88+
"current" => Ok(Self::Current),
89+
"upstream" => Ok(Self::Upstream),
90+
"simple" => Ok(Self::Simple),
91+
"matching" => Ok(Self::Matching),
92+
_ => Err(crate::Error::Generic(format!(
93+
"{value} is incorrect push.default"
94+
))),
95+
}
96+
}
97+
}
98+
99+
pub fn push_default_strategy_config_repo(
100+
repo: &Repository,
101+
) -> Result<PushDefaultStrategyConfig> {
102+
(get_config_string_repo(repo, "push.default")?).map_or_else(
103+
|| Ok(PushDefaultStrategyConfig::default()),
104+
|entry_str| {
105+
Ok(PushDefaultStrategyConfig::try_from(
106+
entry_str.as_str(),
107+
)
108+
.unwrap_or_default())
109+
},
110+
)
111+
}
112+
65113
///
66114
pub fn untracked_files_config(
67115
repo_path: &RepoPath,

asyncgit/src/sync/remotes/push.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ use crate::{
33
progress::ProgressPercent,
44
sync::{
55
branch::branch_set_upstream_after_push,
6+
config::{
7+
push_default_strategy_config_repo,
8+
PushDefaultStrategyConfig,
9+
},
610
cred::BasicAuthCredential,
11+
get_branch_upstream_merge,
712
remotes::{proxy_auto, Callbacks},
813
repository::repo,
914
CommitId, RepoPath,
@@ -145,6 +150,9 @@ pub fn push_raw(
145150
let repo = repo(repo_path)?;
146151
let mut remote = repo.find_remote(remote)?;
147152

153+
let push_default_strategy =
154+
push_default_strategy_config_repo(&repo)?;
155+
148156
let mut options = PushOptions::new();
149157
options.proxy_options(proxy_auto());
150158

@@ -163,9 +171,23 @@ pub fn push_raw(
163171
PushType::Tag => "tags",
164172
};
165173

166-
let branch_name =
174+
let mut push_ref =
167175
format!("{branch_modifier}refs/{ref_type}/{branch}");
168-
remote.push(&[branch_name.as_str()], Some(&mut options))?;
176+
177+
if !delete
178+
&& matches!(
179+
push_default_strategy,
180+
PushDefaultStrategyConfig::Upstream
181+
) {
182+
if let Ok(Some(branch_upstream_merge)) =
183+
get_branch_upstream_merge(repo_path, branch)
184+
{
185+
push_ref.push_str(&format!(":{branch_upstream_merge}"));
186+
}
187+
}
188+
189+
log::debug!("push to: {push_ref}");
190+
remote.push(&[push_ref], Some(&mut options))?;
169191

170192
if let Some((reference, msg)) =
171193
callbacks.get_stats()?.push_rejected_msg

0 commit comments

Comments
 (0)