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`.
This commit is contained in:
Martin von Zweigbergk 2021-11-25 22:02:17 -08:00
parent 6fcd3b3af6
commit 1fc19dbbaf
2 changed files with 16 additions and 12 deletions

View File

@ -702,10 +702,18 @@ pub struct WorkingCopy {
}
impl WorkingCopy {
pub fn init(store: Arc<Store>, 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<Store>,
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),
}
}

View File

@ -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 {