diff --git a/dvc/cli.py b/dvc/cli.py index df02b5ea5..9db4f3841 100644 --- a/dvc/cli.py +++ b/dvc/cli.py @@ -25,21 +25,34 @@ from dvc import VERSION def parse_args(argv=None): # Common args parent_parser = argparse.ArgumentParser(add_help=False) - parent_parser.add_argument('-q', + parent_parser.add_argument( + '-q', '--quiet', action='store_true', default=False, help='Be quiet.') - parent_parser.add_argument('-v', + parent_parser.add_argument( + '-v', '--verbose', action='store_true', default=False, help='Be verbose.') - parent_parser.add_argument('-G', + parent_parser.add_argument( + '-G', '--no-git-actions', action='store_true', default=False, help='Skip all git actions including reproducibility check and commits.') + parent_parser.add_argument( + '-b', + '--branch', + metavar='BRANCH', + help='Execute a command in the branch.') + parent_parser.add_argument( + '-n', + '--new-branch', + metavar='BRANCH', + help='Create a new branch and execute a command in the branch.') # Main parser desc = 'Data Version Control' diff --git a/dvc/command/base.py b/dvc/command/base.py index b28d13206..b545b675f 100644 --- a/dvc/command/base.py +++ b/dvc/command/base.py @@ -1,9 +1,5 @@ -import argparse import fasteners -from multiprocessing import cpu_count - -from dvc.config import ConfigError from dvc.exceptions import DvcException from dvc.logger import Logger @@ -36,6 +32,30 @@ class DvcLock(object): self.lock.release() +class ForeignBranch(object): + def __init__(self, branch, new_branch, git): + self.branch = branch + self.new_branch = new_branch + self.git = git + + if branch and new_branch: + raise DvcLockerError("Commands conflict: --branch and --new-branch cannot be used at the same command") + + self.perform_action = branch or new_branch + + def __enter__(self): + if self.perform_action: + if self.branch: + self.git.checkout(self.branch) + else: + self.git.checkout_new(self.new_branch) + return self + + def __exit__(self, type, value, traceback): + if self.perform_action: + self.git.checkout() + + class CmdBase(object): def __init__(self, settings): self._settings = settings @@ -99,5 +119,10 @@ class CmdBase(object): def not_committed_changes_warning(): Logger.warn('changes were not committed to git') + def run_cmd(self): + with ForeignBranch(self.parsed_args.branch, self.parsed_args.new_branch, self.git): + return self.run() + + # Abstract method that has to be implemented by any inheritance class def run(self): pass diff --git a/dvc/command/remove.py b/dvc/command/remove.py index 49460d322..00ec8b058 100644 --- a/dvc/command/remove.py +++ b/dvc/command/remove.py @@ -11,10 +11,9 @@ class CmdRemove(Traverse): def run(self): if not self.parsed_args.target: Logger.error('Nothing specified, nothing removed.') - return 0 + return 1 - super(CmdRemove, self).run() - pass + return super(CmdRemove, self).run() def process_file(self, target): Logger.debug(u'[Cmd-Remove] Remove file {}.'.format(target)) diff --git a/dvc/git_wrapper.py b/dvc/git_wrapper.py index aca0162b6..f07fffba1 100644 --- a/dvc/git_wrapper.py +++ b/dvc/git_wrapper.py @@ -317,9 +317,15 @@ class GitWrapper(GitWrapperI): Executor.exec_cmd_only_success(cmd.split()) @staticmethod - def checkout(commit): - Logger.debug('[dvc-git] Checkout {}'.format(commit)) - cmd = 'git checkout {}'.format(commit) + def checkout(commit_or_branch): + Logger.debug('[dvc-git] Checkout {}'.format(commit_or_branch)) + cmd = 'git checkout {}'.format(commit_or_branch) + Executor.exec_cmd(cmd.split()) + + @staticmethod + def checkout_new(branch): + Logger.debug('[dvc-git] Checkout new branch {}'.format(branch)) + cmd = 'git checkout -b{}'.format(branch) Executor.exec_cmd(cmd.split()) @staticmethod diff --git a/dvc/main.py b/dvc/main.py index 22f9af212..276d60cd8 100644 --- a/dvc/main.py +++ b/dvc/main.py @@ -18,7 +18,7 @@ def main(): return 255 try: - ret = instance.run() + ret = instance.run_cmd() except Exception as e: exc_info = settings.parsed_args.verbose Logger.error("{} error: {}".format(instance.__class__.__name__, e), exc_info=exc_info)