Add branch and new-branch options

This commit is contained in:
Dmitry
2017-11-19 02:41:59 -08:00
parent f624110686
commit 847a5e2716
5 changed files with 57 additions and 14 deletions
+16 -3
View File
@@ -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'
+29 -4
View File
@@ -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
+2 -3
View File
@@ -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))
+9 -3
View File
@@ -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
+1 -1
View File
@@ -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)