ok/jj
1
0
Fork 0
forked from mirrors/jj

working_copy: make some functions require mutable references

We use interior mutability for caching in `WorkingCopy`, but let's
still take mutable reference in the functions where the state change
is visible.
This commit is contained in:
Martin von Zweigbergk 2021-11-17 09:42:44 -08:00
parent 32018da423
commit 33b272f5fa
4 changed files with 19 additions and 19 deletions

View file

@ -802,13 +802,13 @@ impl WorkingCopy {
self.tree_state().as_ref().unwrap().file_states().clone()
}
fn save(&self) {
fn save(&mut self) {
let mut proto = crate::protos::working_copy::Checkout::new();
proto.commit_id = self.current_commit_id().0;
self.write_proto(proto);
}
pub fn check_out(&self, commit: Commit) -> Result<CheckoutStats, CheckoutError> {
pub fn check_out(&mut self, commit: Commit) -> Result<CheckoutStats, CheckoutError> {
assert!(commit.is_open());
let lock_path = self.state_path.join("working_copy.lock");
let _lock = FileLock::lock(lock_path);
@ -845,7 +845,7 @@ impl WorkingCopy {
Ok(stats)
}
pub fn write_tree(&self) -> LockedWorkingCopy {
pub fn write_tree(&mut self) -> LockedWorkingCopy {
let lock_path = self.state_path.join("working_copy.lock");
let lock = FileLock::lock(lock_path);
@ -861,7 +861,7 @@ impl WorkingCopy {
}
}
pub fn untrack(&self, matcher: &dyn Matcher) -> Result<LockedWorkingCopy, UntrackError> {
pub fn untrack(&mut self, matcher: &dyn Matcher) -> Result<LockedWorkingCopy, UntrackError> {
let lock_path = self.state_path.join("working_copy.lock");
let lock = FileLock::lock(lock_path);
@ -878,7 +878,7 @@ impl WorkingCopy {
// A working copy that's locked on disk. The tree state has already been
// updated.
pub struct LockedWorkingCopy<'a> {
wc: &'a WorkingCopy,
wc: &'a mut WorkingCopy,
#[allow(dead_code)]
lock: FileLock,
closed: bool,

View file

@ -38,7 +38,7 @@ fn test_root(use_git: bool) {
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
let owned_wc = repo.working_copy().clone();
let wc = owned_wc.lock().unwrap();
let mut wc = owned_wc.lock().unwrap();
let locked_wc = wc.write_tree();
let new_tree_id = locked_wc.new_tree_id();
locked_wc.discard();
@ -221,7 +221,7 @@ fn test_checkout_file_transitions(use_git: bool) {
tx.commit();
let owned_wc = repo.working_copy().clone();
let wc = owned_wc.lock().unwrap();
let mut wc = owned_wc.lock().unwrap();
wc.check_out(left_commit).unwrap();
wc.check_out(right_commit.clone()).unwrap();
@ -324,7 +324,7 @@ fn test_untrack() {
.write_to_repo(tx.mut_repo());
let repo = tx.commit();
let working_copy = repo.working_copy().clone();
let locked_working_copy = working_copy.lock().unwrap();
let mut locked_working_copy = working_copy.lock().unwrap();
locked_working_copy
.check_out(initial_commit.clone())
.unwrap();
@ -370,7 +370,7 @@ fn test_commit_racy_timestamps(use_git: bool) {
let file_path = repo.working_copy_path().join("file");
let mut previous_tree_id = repo.store().empty_tree_id().clone();
let owned_wc = repo.working_copy().clone();
let wc = owned_wc.lock().unwrap();
let mut wc = owned_wc.lock().unwrap();
for i in 0..100 {
{
let mut file = OpenOptions::new()
@ -413,7 +413,7 @@ fn test_gitignores(use_git: bool) {
testutils::write_working_copy_file(&repo, &subdir_modified_path, "1");
let owned_wc = repo.working_copy().clone();
let wc = owned_wc.lock().unwrap();
let mut wc = owned_wc.lock().unwrap();
let locked_wc = wc.write_tree();
let new_tree_id1 = locked_wc.new_tree_id();
locked_wc.discard();
@ -502,7 +502,7 @@ fn test_gitignores_checkout_overwrites_ignored(use_git: bool) {
assert_eq!(buf, b"contents");
// Check that the file is in the tree created by committing the working copy
let wc = repo.working_copy_locked();
let mut wc = repo.working_copy_locked();
let locked_wc = wc.write_tree();
let new_tree_id = locked_wc.new_tree_id();
locked_wc.discard();
@ -548,7 +548,7 @@ fn test_gitignores_ignored_directory_already_tracked(use_git: bool) {
// Check that the file is still in the tree created by committing the working
// copy (that it didn't get removed because the directory is ignored)
let wc = repo.working_copy_locked();
let mut wc = repo.working_copy_locked();
let locked_wc = wc.write_tree();
let new_tree_id = locked_wc.new_tree_id();
locked_wc.discard();
@ -569,7 +569,7 @@ fn test_dotgit_ignored(use_git: bool) {
let settings = testutils::user_settings();
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
let wc = repo.working_copy_locked();
let mut wc = repo.working_copy_locked();
// Test with a .git/ directory (with a file in, since we don't write empty
// trees)

View file

@ -44,7 +44,7 @@ fn test_concurrent_checkout(use_git: bool) {
tx1.commit();
// Check out commit1
let wc1 = repo1.working_copy_locked();
let mut wc1 = repo1.working_copy_locked();
wc1.check_out(commit1).unwrap();
// Check out commit2 from another process (simulated by another repo instance)
@ -112,7 +112,7 @@ fn test_checkout_parallel(use_git: bool) {
let handle = thread::spawn(move || {
let repo = ReadonlyRepo::load(&settings, working_copy_path).unwrap();
let owned_wc = repo.working_copy().clone();
let wc = owned_wc.lock().unwrap();
let mut wc = owned_wc.lock().unwrap();
let commit = repo.store().get_commit(&commit_id).unwrap();
let stats = wc.check_out(commit).unwrap();
assert_eq!(stats.updated_files, 0);

View file

@ -343,7 +343,7 @@ impl RepoCommandHelper {
fn maybe_commit_working_copy(&mut self, ui: &mut Ui) -> Result<(), CommandError> {
if self.may_update_working_copy {
let repo = self.repo.clone();
let wc = repo.working_copy_locked();
let mut wc = repo.working_copy_locked();
let locked_wc = wc.write_tree();
let old_commit = locked_wc.old_commit();
// Check if the current checkout has changed on disk after we read it. It's fine
@ -427,7 +427,7 @@ impl RepoCommandHelper {
}
}
self.repo = tx.commit();
update_working_copy(ui, &self.repo, &self.repo.working_copy_locked())
update_working_copy(ui, &self.repo, &mut self.repo.working_copy_locked())
}
}
@ -559,7 +559,7 @@ fn matcher_from_values(
fn update_working_copy(
ui: &mut Ui,
repo: &Arc<ReadonlyRepo>,
wc: &WorkingCopy,
wc: &mut WorkingCopy,
) -> Result<Option<CheckoutStats>, CommandError> {
let old_commit = wc.current_commit();
let new_commit = repo.store().get_commit(repo.view().checkout()).unwrap();
@ -1409,7 +1409,7 @@ fn cmd_untrack(
args.values_of("paths"),
)?;
let mut tx = repo_command.start_transaction("untrack paths");
let locked_working_copy = base_repo.working_copy_locked();
let mut locked_working_copy = base_repo.working_copy_locked();
let old_commit = locked_working_copy.current_commit();
let unfinished_write = locked_working_copy
.untrack(matcher.as_ref())