cli_util: move update-stale checkout operation into cli_util

This centralizes the logic so the cli helpers can use it.
This commit is contained in:
dploch 2024-11-08 14:29:24 -05:00 committed by Daniel Ploch
parent 0a5bc2bbed
commit afb07110b2
2 changed files with 47 additions and 30 deletions

View file

@ -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<Item = &'a CommitId>,
@ -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<CheckoutStats, CommandError> {
// 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)],

View file

@ -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(())