Skip to content

Commit

Permalink
jj git push: Display message from remote after pushing
Browse files Browse the repository at this point in the history
Closes #3236.
  • Loading branch information
bnjmnt4n committed Mar 7, 2024
1 parent bf76080 commit 3e38e6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
7 changes: 6 additions & 1 deletion cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ fn cmd_git_push(
branch_updates,
force_pushed_branches,
};
with_remote_git_callbacks(ui, |cb| {
let remote_message = with_remote_git_callbacks(ui, |cb| {
git::push_branches(tx.mut_repo(), &git_repo, &remote, &targets, cb)
})
.map_err(|err| match err {
Expand All @@ -1101,6 +1101,11 @@ fn cmd_git_push(
),
_ => user_error(err),
})?;
if let Some(remote_message) = remote_message {
for line in remote_message.lines() {
writeln!(ui.stderr(), "remote: {line}")?;
}
}
tx.finish(ui, tx_description)?;
Ok(())
}
Expand Down
22 changes: 16 additions & 6 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ pub fn push_branches(
remote_name: &str,
targets: &GitBranchPushTargets,
callbacks: RemoteCallbacks<'_>,
) -> Result<(), GitPushError> {
) -> Result<Option<String>, GitPushError> {
let ref_updates = targets
.branch_updates
.iter()
Expand All @@ -1233,7 +1233,7 @@ pub fn push_branches(
new_target: update.new_target.clone(),
})
.collect_vec();
push_updates(git_repo, remote_name, &ref_updates, callbacks)?;
let remote_message = push_updates(git_repo, remote_name, &ref_updates, 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 @@ -1248,7 +1248,7 @@ pub fn push_branches(
mut_repo.set_remote_branch(branch_name, remote_name, new_remote_ref);
}

Ok(())
Ok(remote_message)
}

/// Pushes the specified Git refs without updating the repo view.
Expand All @@ -1257,7 +1257,7 @@ pub fn push_updates(
remote_name: &str,
updates: &[GitRefUpdate],
callbacks: RemoteCallbacks<'_>,
) -> Result<(), GitPushError> {
) -> Result<Option<String>, GitPushError> {
let mut temp_refs = vec![];
let mut qualified_remote_refs = vec![];
let mut refspecs = vec![];
Expand Down Expand Up @@ -1310,7 +1310,7 @@ fn push_refs(
qualified_remote_refs: &[&str],
refspecs: &[String],
callbacks: RemoteCallbacks<'_>,
) -> Result<(), GitPushError> {
) -> Result<Option<String>, GitPushError> {
if remote_name == REMOTE_NAME_FOR_LOCAL_GIT_REPO {
return Err(GitPushError::RemoteReservedForLocalGitRepo);
}
Expand All @@ -1322,11 +1322,21 @@ fn push_refs(
}
})?;
let mut remaining_remote_refs: HashSet<_> = qualified_remote_refs.iter().copied().collect();
let mut remote_message: Option<String> = None;
let mut push_options = git2::PushOptions::new();
let mut proxy_options = git2::ProxyOptions::new();
proxy_options.auto();
push_options.proxy_options(proxy_options);
let mut callbacks = callbacks.into_git();
callbacks.sideband_progress(|progress| {
let message = String::from_utf8_lossy(progress);
if let Some(remote_message) = &mut remote_message {
remote_message.push_str(&message);
} else {
remote_message = Some(message.to_string());
}
true
});
callbacks.push_update_reference(|refname, status| {
// The status is Some if the ref update was rejected
if status.is_none() {
Expand All @@ -1345,7 +1355,7 @@ fn push_refs(
})?;
drop(push_options);
if remaining_remote_refs.is_empty() {
Ok(())
Ok(remote_message)
} else {
Err(GitPushError::RefUpdateRejected(
remaining_remote_refs
Expand Down
10 changes: 5 additions & 5 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@ fn test_push_branches_success() {
&targets,
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
assert_eq!(result, Ok(None));

// Check that the ref got updated in the source repo
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
Expand Down Expand Up @@ -2453,7 +2453,7 @@ fn test_push_branches_deletion() {
&targets,
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
assert_eq!(result, Ok(None));

// Check that the ref got deleted in the source repo
assert!(source_repo.find_reference("refs/heads/main").is_err());
Expand Down Expand Up @@ -2511,7 +2511,7 @@ fn test_push_branches_mixed_deletion_and_addition() {
&targets,
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
assert_eq!(result, Ok(None));

// Check that the topic ref got updated in the source repo
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
Expand Down Expand Up @@ -2606,7 +2606,7 @@ fn test_push_branches_not_fast_forward_with_force() {
&targets,
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
assert_eq!(result, Ok(None));

// Check that the ref got updated in the source repo
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
Expand All @@ -2633,7 +2633,7 @@ fn test_push_updates_success() {
}],
git::RemoteCallbacks::default(),
);
assert_eq!(result, Ok(()));
assert_eq!(result, Ok(None));

// Check that the ref got updated in the source repo
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
Expand Down

0 comments on commit 3e38e6c

Please sign in to comment.