From 423d620b89417c83dc8e75a039cdaea9ce8ec53c Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Wed, 14 Nov 2018 10:53:02 -0600 Subject: [PATCH 1/2] checkout: Clean working directory changes of tracked dir Fix #1326 --- dvc/remote/local.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dvc/remote/local.py b/dvc/remote/local.py index 317c47dbf..8816b1307 100644 --- a/dvc/remote/local.py +++ b/dvc/remote/local.py @@ -337,11 +337,30 @@ class RemoteLOCAL(RemoteBase): if bar: progress.update_target(dir_relpath, processed, dir_size) + self._discard_working_directory_changes(path, dir_info) + self.state.update_link(path) if bar: progress.finish_target(dir_relpath) + def _discard_working_directory_changes(self, path, dir_info): + working_dir_files = set( + os.path.join(root, file) + for root, _, files in os.walk(path) + for file in files + ) + + cached_files = set( + os.path.join(path, file['relpath']) + for file in dir_info + ) + + delta = working_dir_files - cached_files + + for file in delta: + remove(file) + def _move(self, inp, outp): # moving in two stages to make last the move atomic in # case inp and outp are in different filesystems From b4588671a064a93221d9e9977a8fed89e4014962 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Wed, 14 Nov 2018 11:03:53 -0600 Subject: [PATCH 2/2] Add a test for it --- tests/test_checkout.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_checkout.py b/tests/test_checkout.py index 4e46fcde0..efd4c7e28 100644 --- a/tests/test_checkout.py +++ b/tests/test_checkout.py @@ -71,7 +71,7 @@ class TestCheckoutCorruptedCacheFile(TestRepro): class TestCheckoutCorruptedCacheDir(TestDvc): def test(self): time.sleep(1) - + # NOTE: using 'copy' so that cache and link don't have same inode ret = main(['config', 'cache.type', 'copy']) self.assertEqual(ret, 0) @@ -159,6 +159,20 @@ class TestRemoveFilesWhenCheckout(CheckoutBase): self.assertFalse(os.path.exists(fname)) +class TestCheckoutCleanWorkingDir(CheckoutBase): + def test(self): + stages = self.dvc.add(self.DATA_DIR) + stage = stages[0] + + working_dir_change = os.path.join(self.DATA_DIR, 'not_cached.txt') + with open(working_dir_change, 'w') as f: + f.write('not_cached') + + ret = main(['checkout', stage.relpath]) + self.assertEqual(ret, 0) + self.assertFalse(os.path.exists(working_dir_change)) + + class TestCheckoutSelectiveRemove(CheckoutBase): def test(self): # Use copy to test for changes in the inodes