Skip to content

Commit

Permalink
cli: add support for push options
Browse files Browse the repository at this point in the history
  • Loading branch information
shikanime committed Aug 22, 2024
1 parent bb018a5 commit b96d804
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cli/src/commands/git/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ pub struct GitPushArgs {
/// Only display what will change on the remote
#[arg(long)]
dry_run: bool,
/// Git push options
#[arg(long, short)]
options: Vec<String>,
}

fn make_branch_term(branch_names: &[impl fmt::Display]) -> String {
Expand Down Expand Up @@ -322,7 +325,14 @@ pub fn cmd_git_push(
_ = writer.write(ui, progress_message);
};
with_remote_git_callbacks(ui, Some(&mut sideband_progress_callback), |cb| {
git::push_branches(tx.mut_repo(), &git_repo, &remote, &targets, cb)
git::push_branches(
tx.mut_repo(),
&git_repo,
&remote,
&targets,
&args.options,
cb,
)
})
.map_err(|err| match err {
GitPushError::InternalGitError(err) => map_git_error(err),
Expand Down
1 change: 1 addition & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ Before the command actually moves, creates, or deletes a remote branch, it makes
* `-r`, `--revisions <REVISIONS>` — Push branches pointing to these commits (can be repeated)
* `-c`, `--change <CHANGE>` — Push this commit by creating a branch based on its change ID (can be repeated)
* `--dry-run` — Only display what will change on the remote
* `-o`, `--options <OPTIONS>` — Git push options
Expand Down
19 changes: 18 additions & 1 deletion lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ pub fn push_branches(
git_repo: &git2::Repository,
remote_name: &str,
targets: &GitBranchPushTargets,
remote_push_options: &[String],
callbacks: RemoteCallbacks<'_>,
) -> Result<(), GitPushError> {
let ref_updates = targets
Expand All @@ -1354,7 +1355,14 @@ pub fn push_branches(
new_target: update.new_target.clone(),
})
.collect_vec();
push_updates(mut_repo, git_repo, remote_name, &ref_updates, callbacks)?;
push_updates(
mut_repo,
git_repo,
remote_name,
&ref_updates,
remote_push_options,
callbacks,
)?;

// TODO: add support for partially pushed refs? we could update the view
// excluding rejected refs, but the transaction would be aborted anyway
Expand All @@ -1378,6 +1386,7 @@ pub fn push_updates(
git_repo: &git2::Repository,
remote_name: &str,
updates: &[GitRefUpdate],
remote_push_options: &[String],
callbacks: RemoteCallbacks<'_>,
) -> Result<(), GitPushError> {
let mut qualified_remote_refs_expected_locations = HashMap::new();
Expand Down Expand Up @@ -1407,6 +1416,7 @@ pub fn push_updates(
remote_name,
&qualified_remote_refs_expected_locations,
&refspecs,
remote_push_options,
callbacks,
)
}
Expand All @@ -1417,6 +1427,7 @@ fn push_refs(
remote_name: &str,
qualified_remote_refs_expected_locations: &HashMap<&str, Option<&CommitId>>,
refspecs: &[String],
remote_push_options: &[String],
callbacks: RemoteCallbacks<'_>,
) -> Result<(), GitPushError> {
if remote_name == REMOTE_NAME_FOR_LOCAL_GIT_REPO {
Expand All @@ -1436,6 +1447,12 @@ fn push_refs(
let mut failed_push_negotiations = vec![];
let push_result = {
let mut push_options = git2::PushOptions::new();
push_options.remote_push_options(
&remote_push_options
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>(),
);
let mut proxy_options = git2::ProxyOptions::new();
proxy_options.auto();
push_options.proxy_options(proxy_options);
Expand Down
10 changes: 10 additions & 0 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,7 @@ fn test_push_branches_success() {
&clone_repo,
"origin",
&targets,
&[],
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -2687,6 +2688,7 @@ fn test_push_branches_deletion() {
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
&[],
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -2744,6 +2746,7 @@ fn test_push_branches_mixed_deletion_and_addition() {
&clone_repo,
"origin",
&targets,
&[],
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -2803,6 +2806,7 @@ fn test_push_branches_not_fast_forward() {
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
&[],
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -2848,6 +2852,7 @@ fn test_push_updates_unexpectedly_moved_sideways_on_remote() {
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
&[],
git::RemoteCallbacks::default(),
)
};
Expand Down Expand Up @@ -2915,6 +2920,7 @@ fn test_push_updates_unexpectedly_moved_forward_on_remote() {
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
&[],
git::RemoteCallbacks::default(),
)
};
Expand Down Expand Up @@ -2973,6 +2979,7 @@ fn test_push_updates_unexpectedly_exists_on_remote() {
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
&[],
git::RemoteCallbacks::default(),
)
};
Expand Down Expand Up @@ -3004,6 +3011,7 @@ fn test_push_updates_success() {
expected_current_target: Some(setup.main_commit.id().clone()),
new_target: Some(setup.child_of_main_commit.id().clone()),
}],
&[],
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
Expand Down Expand Up @@ -3041,6 +3049,7 @@ fn test_push_updates_no_such_remote() {
expected_current_target: Some(setup.main_commit.id().clone()),
new_target: Some(setup.child_of_main_commit.id().clone()),
}],
&[],
git::RemoteCallbacks::default(),
);
assert!(matches!(result, Err(GitPushError::NoSuchRemote(_))));
Expand All @@ -3060,6 +3069,7 @@ fn test_push_updates_invalid_remote() {
expected_current_target: Some(setup.main_commit.id().clone()),
new_target: Some(setup.child_of_main_commit.id().clone()),
}],
&[],
git::RemoteCallbacks::default(),
);
assert!(matches!(result, Err(GitPushError::NoSuchRemote(_))));
Expand Down

0 comments on commit b96d804

Please sign in to comment.