Skip to content

Commit

Permalink
workspace: refactor for_stale_working_copy
Browse files Browse the repository at this point in the history
Move this function from cli_util.rs, since workspace.rs is the only
caller. This function will be enlarged in a subsequent commit.
  • Loading branch information
jonathantanmy committed Feb 3, 2024
1 parent 976b801 commit 7abf168
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
16 changes: 0 additions & 16 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,22 +691,6 @@ impl CommandHelper {
) -> Result<WorkspaceCommandHelper, CommandError> {
WorkspaceCommandHelper::new(ui, self, workspace, repo)
}

/// Loads workspace that will diverge from the last working-copy operation.
pub fn for_stale_working_copy(
&self,
ui: &mut Ui,
) -> Result<WorkspaceCommandHelper, CommandError> {
let workspace = self.load_workspace()?;
let op_store = workspace.repo_loader().op_store();
let op_id = workspace.working_copy().operation_id();
let op_data = op_store
.read_operation(op_id)
.map_err(|e| CommandError::InternalError(format!("Failed to read operation: {e}")))?;
let operation = Operation::new(op_store.clone(), op_id.clone(), op_data);
let repo = workspace.repo_loader().load_at(&operation)?;
self.for_loaded_repo(ui, workspace, repo)
}
}

/// A ReadonlyRepo along with user-config-dependent derived data. The derived
Expand Down
19 changes: 18 additions & 1 deletion cli/src/commands/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use itertools::Itertools;
use jj_lib::file_util;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::WorkspaceId;
use jj_lib::operation::Operation;
use jj_lib::repo::Repo;
use jj_lib::rewrite::merge_commit_trees;
use jj_lib::workspace::Workspace;
Expand Down Expand Up @@ -279,6 +280,22 @@ fn cmd_workspace_root(
Ok(())
}

/// Loads workspace that will diverge from the last working-copy operation.
fn for_stale_working_copy(
command: &CommandHelper,
ui: &mut Ui,
) -> Result<WorkspaceCommandHelper, CommandError> {
let workspace = command.load_workspace()?;
let op_store = workspace.repo_loader().op_store();
let op_id = workspace.working_copy().operation_id();
let op_data = op_store
.read_operation(op_id)
.map_err(|e| CommandError::InternalError(format!("Failed to read operation: {e}")))?;
let operation = Operation::new(op_store.clone(), op_id.clone(), op_data);
let repo = workspace.repo_loader().load_at(&operation)?;
command.for_loaded_repo(ui, workspace, repo)
}

#[instrument(skip_all)]
fn cmd_workspace_update_stale(
ui: &mut Ui,
Expand All @@ -290,7 +307,7 @@ fn cmd_workspace_update_stale(
// merged repo wouldn't change because the old one wins, but it's probably
// fine if we picked the new wc_commit_id.
let known_wc_commit = {
let mut workspace_command = command.for_stale_working_copy(ui)?;
let mut workspace_command = for_stale_working_copy(command, ui)?;
workspace_command.maybe_snapshot(ui)?;
let wc_commit_id = workspace_command.get_wc_commit_id().unwrap();
workspace_command.repo().store().get_commit(wc_commit_id)?
Expand Down

0 comments on commit 7abf168

Please sign in to comment.