From afb07110b23fc85789dccdca46a9d37a0f4419fa Mon Sep 17 00:00:00 2001 From: dploch Date: Fri, 8 Nov 2024 14:29:24 -0500 Subject: [PATCH] cli_util: move update-stale checkout operation into cli_util This centralizes the logic so the cli helpers can use it. --- cli/src/cli_util.rs | 38 +++++++++++++++++++++ cli/src/commands/workspace/update_stale.rs | 39 +++++----------------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 6f1ed3943..e66d6cd86 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -1535,6 +1535,22 @@ to the current parents may contain changes from multiple commits. self.commit_summary_template().format(commit, formatter) } + pub fn write_stale_commit_stats( + &self, + ui: &Ui, + commit: &Commit, + stats: CheckoutStats, + ) -> std::io::Result<()> { + if let Some(mut formatter) = ui.status_formatter() { + write!(formatter, "Working copy now at: ")?; + formatter.with_label("working_copy", |fmt| self.write_commit_summary(fmt, commit))?; + writeln!(formatter)?; + } + print_checkout_stats(ui, stats, commit)?; + + Ok(()) + } + pub fn check_rewritable<'a>( &self, commits: impl IntoIterator, @@ -2245,6 +2261,28 @@ pub fn start_repo_transaction( tx } +pub fn update_stale_working_copy( + mut locked_ws: LockedWorkspace, + op_id: OperationId, + stale_commit: &Commit, + new_commit: &Commit, +) -> Result { + // The same check as start_working_copy_mutation(), but with the stale + // working-copy commit. + if stale_commit.tree_id() != locked_ws.locked_wc().old_tree_id() { + return Err(user_error("Concurrent working copy operation. Try again.")); + } + let stats = locked_ws.locked_wc().check_out(new_commit).map_err(|err| { + internal_error_with_message( + format!("Failed to check out commit {}", new_commit.id().hex()), + err, + ) + })?; + locked_ws.finish(op_id)?; + + Ok(stats) +} + #[instrument(skip_all)] pub fn print_conflicted_paths( conflicts: &[(RepoPathBuf, MergedTreeValue)], diff --git a/cli/src/commands/workspace/update_stale.rs b/cli/src/commands/workspace/update_stale.rs index a0d67173c..5f851f4fb 100644 --- a/cli/src/commands/workspace/update_stale.rs +++ b/cli/src/commands/workspace/update_stale.rs @@ -12,17 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use jj_lib::object_id::ObjectId; use jj_lib::op_store::OpStoreError; use jj_lib::repo::Repo; use jj_lib::working_copy::WorkingCopyFreshness; use tracing::instrument; -use crate::cli_util::print_checkout_stats; +use crate::cli_util::update_stale_working_copy; use crate::cli_util::CommandHelper; use crate::cli_util::WorkspaceCommandHelper; -use crate::command_error::internal_error_with_message; -use crate::command_error::user_error; use crate::command_error::CommandError; use crate::ui::Ui; @@ -70,32 +67,14 @@ pub fn cmd_workspace_update_stale( )?; } WorkingCopyFreshness::WorkingCopyStale | WorkingCopyFreshness::SiblingOperation => { - // The same check as start_working_copy_mutation(), but with the stale - // working-copy commit. - if known_wc_commit.tree_id() != locked_ws.locked_wc().old_tree_id() { - return Err(user_error("Concurrent working copy operation. Try again.")); - } - let stats = locked_ws - .locked_wc() - .check_out(&desired_wc_commit) - .map_err(|err| { - internal_error_with_message( - format!( - "Failed to check out commit {}", - desired_wc_commit.id().hex() - ), - err, - ) - })?; - locked_ws.finish(repo.op_id().clone())?; - if let Some(mut formatter) = ui.status_formatter() { - write!(formatter, "Working copy now at: ")?; - formatter.with_label("working_copy", |fmt| { - workspace_command.write_commit_summary(fmt, &desired_wc_commit) - })?; - writeln!(formatter)?; - } - print_checkout_stats(ui, stats, &desired_wc_commit)?; + let stats = update_stale_working_copy( + locked_ws, + repo.op_id().clone(), + &known_wc_commit, + &desired_wc_commit, + )?; + + workspace_command.write_stale_commit_stats(ui, &desired_wc_commit, stats)?; } } Ok(())