mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
dvc checkout: remove files from previous branch
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user