Skip to content

Commit

Permalink
cli: move code that reloads repo to working-copy operation
Browse files Browse the repository at this point in the history
Reloading repo also means the working-copy commit can change, so the caller
needs to do more things than repo.reload_at().
  • Loading branch information
yuja committed May 21, 2023
1 parent 38a7e7f commit 3e59563
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,9 @@ impl WorkspaceCommandHelper {
let mut locked_wc = self.workspace.working_copy_mut().start_mutation();
let old_op_id = locked_wc.old_operation_id().clone();
let wc_commit = repo.store().get_commit(&wc_commit_id)?;
let repo = match check_stale_working_copy(&locked_wc, &wc_commit, repo.clone()) {
Ok(repo) => repo,
let repo = match check_stale_working_copy(&locked_wc, &wc_commit, &repo) {
Ok(None) => repo,
Ok(Some(wc_operation)) => repo.reload_at(&wc_operation),
Err(StaleWorkingCopyError::WorkingCopyStale) => {
locked_wc.discard();
return Err(user_error_with_hint(
Expand Down Expand Up @@ -1430,12 +1431,13 @@ pub enum StaleWorkingCopyError {
pub fn check_stale_working_copy(
locked_wc: &LockedWorkingCopy,
wc_commit: &Commit,
repo: Arc<ReadonlyRepo>,
) -> Result<Arc<ReadonlyRepo>, StaleWorkingCopyError> {
repo: &ReadonlyRepo,
) -> Result<Option<Operation>, StaleWorkingCopyError> {
// Check if the working copy's tree matches the repo's view
let wc_tree_id = locked_wc.old_tree_id().clone();
if *wc_commit.tree_id() == wc_tree_id {
Ok(repo)
// The working copy isn't stale, and no need to reload the repo.
Ok(None)
} else {
let wc_operation_data = repo
.op_store()
Expand All @@ -1455,9 +1457,9 @@ pub fn check_stale_working_copy(
);
if let Some(ancestor_op) = maybe_ancestor_op {
if ancestor_op.id() == repo_operation.id() {
// The working copy was updated since we loaded the repo. We reload the repo
// at the working copy's operation.
Ok(repo.reload_at(&wc_operation))
// The working copy was updated since we loaded the repo. The repo must be
// reloaded at the working copy's operation.
Ok(Some(wc_operation))
} else if ancestor_op.id() == wc_operation.id() {
// The working copy was not updated when some repo operation committed,
// meaning that it's stale compared to the repo view.
Expand Down
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3436,7 +3436,7 @@ fn cmd_workspace_update_stale(
let repo = workspace_command.repo().clone();
let (mut locked_wc, desired_wc_commit) =
workspace_command.unsafe_start_working_copy_mutation()?;
match check_stale_working_copy(&locked_wc, &desired_wc_commit, repo.clone()) {
match check_stale_working_copy(&locked_wc, &desired_wc_commit, &repo) {
Ok(_) => {
locked_wc.discard();
ui.write("Nothing to do (the working copy is not stale).\n")?;
Expand Down

0 comments on commit 3e59563

Please sign in to comment.