diff --git a/dvc/command/checkout.py b/dvc/command/checkout.py index cf558307b..1eda94896 100644 --- a/dvc/command/checkout.py +++ b/dvc/command/checkout.py @@ -1,7 +1,38 @@ +import os + from dvc.command.common.base import CmdBase +from dvc.logger import Logger +from dvc.system import System class CmdCheckout(CmdBase): def run(self): + self.remove_untracked_hardlinks() self.project.checkout() return 0 + + def remove_untracked_hardlinks(self): + untracked_cache_files = self.untracked_hardlinks_files() + + for file in untracked_cache_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 + + def untracked_hardlinks_files(self): + untracked_files = set(self.project.scm.untracked_files()) + cached_files = [] + for cache_file in self.project.cache.all(): + to_remove = set() + for file in untracked_files: + if System.samefile(cache_file, file): + cached_files.append(file) + to_remove.add(file) + untracked_files = untracked_files - to_remove + + return cached_files diff --git a/dvc/scm.py b/dvc/scm.py index 55321665a..b5c18d8dd 100644 --- a/dvc/scm.py +++ b/dvc/scm.py @@ -38,6 +38,9 @@ class Base(object): def brancher(self, branch, new_branch): return BranchChanger(self, branch, new_branch) + def untracked_files(self): + pass + class Git(Base): GITIGNORE = '.gitignore' @@ -79,6 +82,9 @@ class Git(Base): def branch(self, branch): self.repo.git.branch(branch) + def untracked_files(self): + return self.repo.untracked_files + def SCM(root_dir): if Git.is_repo(root_dir):