diff --git a/dvc/command/checkout.py b/dvc/command/checkout.py index f163b934e..c251a04d5 100644 --- a/dvc/command/checkout.py +++ b/dvc/command/checkout.py @@ -1,6 +1,8 @@ import os from dvc.command.common.base import CmdBase +from dvc.command.common.cache_dir import CacheDir +from dvc.config import ConfigI from dvc.logger import Logger from dvc.system import System @@ -35,6 +37,23 @@ class CmdCheckout(CmdBase): Logger.info('Checkout \'{}\''.format(item.data.relative)) def run(self): + self.remove_not_tracked_hardlinks() items = self.settings.path_factory.all_existing_data_items() self.checkout(items) return 0 + + def remove_not_tracked_hardlinks(self): + untracked_files = self.git.all_untracked_files() + + cache_dir = os.path.join(self.git.git_dir_abs, ConfigI.CACHE_DIR) + cached_files = CacheDir(cache_dir).find_caches(untracked_files) + + for file in cached_files: + Logger.info(u'Remove \'{}\''.format(file)) + os.remove(file) + + dir = os.path.dirname(file) + if not os.listdir(dir): + Logger.info(u'Remove empty directory \'{}\''.format(dir)) + os.removedirs(dir) + pass diff --git a/dvc/git_wrapper.py b/dvc/git_wrapper.py index f9eb9d45c..1aee053c2 100644 --- a/dvc/git_wrapper.py +++ b/dvc/git_wrapper.py @@ -439,3 +439,11 @@ class GitWrapper(GitWrapperI): def get_file_content(fname, branch): cmd = 'git show {}:{}'.format(branch, fname) return Executor.exec_cmd_only_success(cmd.split()) + + @staticmethod + def all_untracked_files(): + cmd = 'git ls-files --others --exclude-standard' + res = Executor.exec_cmd_only_success(cmd.split()) + if not res.strip(): + return [] + return res.split('\n')