From 1fc19dbbafcb3bc503f28b0deee95c4016e20c49 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 25 Nov 2021 22:02:17 -0800 Subject: [PATCH] working_copy: take initial commit in init() function The working copy object knows the currently checked out commit ID. It is set to `None` when the object is initialized. It is also set to `None` when an existing working copy is loaded. In that case, it's used only to facilitate lazy loading. However, that means that `WorkingCopy::current_commit_id()` fails if the working copy has been initalized but no checkout has been specified. I've never run into that case, but it's ugly that it can happen. This patch fixes it by having `WorkingCopy::init()` take a `CommitId`. --- lib/src/working_copy.rs | 18 +++++++++++++----- lib/src/workspace.rs | 10 +++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index c75e8c897..e7922e9d2 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -702,10 +702,18 @@ pub struct WorkingCopy { } impl WorkingCopy { - pub fn init(store: Arc, working_copy_path: PathBuf, state_path: PathBuf) -> WorkingCopy { - // Leave the commit_id empty so a subsequent call to check out the root revision - // will have an effect. - let proto = crate::protos::working_copy::Checkout::new(); + /// Initializes a new working copy at `working_copy_path`. The working copy's state will be + /// stored in the `state_path` directory. The working copy will be recorded as being already + /// checked out at commit pointed to by `commit_id`; this function doesn't update the working + /// copy file to that commit. + pub fn init( + store: Arc, + working_copy_path: PathBuf, + state_path: PathBuf, + commit_id: CommitId, + ) -> WorkingCopy { + let mut proto = crate::protos::working_copy::Checkout::new(); + proto.commit_id = commit_id.to_bytes(); let mut file = OpenOptions::new() .create_new(true) .write(true) @@ -716,7 +724,7 @@ impl WorkingCopy { store, working_copy_path, state_path, - commit_id: RefCell::new(None), + commit_id: RefCell::new(Some(commit_id)), tree_state: RefCell::new(None), } } diff --git a/lib/src/workspace.rs b/lib/src/workspace.rs index 11e4be5b6..89a420bc1 100644 --- a/lib/src/workspace.rs +++ b/lib/src/workspace.rs @@ -58,16 +58,12 @@ fn init_working_copy( workspace_root: &Path, jj_dir: &Path, ) -> WorkingCopy { - let mut working_copy = WorkingCopy::init( + WorkingCopy::init( repo.store().clone(), workspace_root.to_path_buf(), jj_dir.join("working_copy"), - ); - let checkout_commit = repo.store().get_commit(repo.view().checkout()).unwrap(); - working_copy - .check_out(checkout_commit) - .expect("failed to check out root commit"); - working_copy + repo.view().checkout().clone(), + ) } impl Workspace {