diff --git a/git_cl.py b/git_cl.py index de18bcb86..8ecafb393 100755 --- a/git_cl.py +++ b/git_cl.py @@ -4524,7 +4524,7 @@ def CMDpatch(parser, args): if options.force: RunGit(['branch', '-D', options.newbranch], stderr=subprocess2.PIPE, error_ok=True) - git_new_branch.main(options.newbranch) + git_new_branch.create_new_branch(options.newbranch) cl = Changelist( codereview_host=target_issue_arg.hostname, issue=target_issue_arg.issue) diff --git a/git_new_branch.py b/git_new_branch.py index 856b6eab9..f2ca024f8 100755 --- a/git_new_branch.py +++ b/git_new_branch.py @@ -9,11 +9,44 @@ Create new branch tracking origin/master by default. import argparse import sys +import git_common import subprocess2 -from git_common import run, root, set_config, get_or_create_merge_base, tags -from git_common import hash_one, upstream, set_branch_config, current_branch +def create_new_branch( + branch_name, upstream_current=False, upstream=None, inject_current=False): + upstream = upstream or git_common.root() + try: + if inject_current: + below = git_common.current_branch() + if below is None: + raise Exception('no current branch') + above = git_common.upstream(below) + if above is None: + raise Exception('branch %s has no upstream' % (below)) + git_common.run('checkout', '--track', above, '-b', branch_name) + git_common.run('branch', '--set-upstream-to', branch_name, below) + elif upstream_current: + git_common.run('checkout', '--track', '-b', branch_name) + else: + if upstream in git_common.tags(): + # TODO(iannucci): ensure that basis_ref is an ancestor of HEAD? + git_common.run( + 'checkout', '--no-track', '-b', branch_name, + git_common.hash_one(upstream)) + git_common.set_config('branch.%s.remote' % branch_name, '.') + git_common.set_config('branch.%s.merge' % branch_name, upstream) + else: + # TODO(iannucci): Detect unclean workdir then stash+pop if we need to + # teleport to a conflicting portion of history? + git_common.run('checkout', '--track', upstream, '-b', branch_name) + git_common.get_or_create_merge_base(branch_name) + except subprocess2.CalledProcessError as cpe: + sys.stdout.write(cpe.stdout.decode('utf-8', 'replace')) + sys.stderr.write(cpe.stderr.decode('utf-8', 'replace')) + return 1 + sys.stderr.write('Switched to branch %s.\n' % branch_name) + return 0 def main(args): parser = argparse.ArgumentParser( @@ -25,7 +58,7 @@ def main(args): g.add_argument('--upstream-current', '--upstream_current', action='store_true', help='set upstream branch to current branch.') - g.add_argument('--upstream', metavar='REF', default=root(), + g.add_argument('--upstream', metavar='REF', help='upstream branch (or tag) to track.') g.add_argument('--inject-current', '--inject_current', action='store_true', @@ -36,36 +69,9 @@ def main(args): opts = parser.parse_args(args) - try: - if opts.inject_current: - below = current_branch() - if below is None: - raise Exception('no current branch') - above = upstream(below) - if above is None: - raise Exception('branch %s has no upstream' % (below)) - run('checkout', '--track', above, '-b', opts.branch_name) - run('branch', '--set-upstream-to', opts.branch_name, below) - elif opts.upstream_current: - run('checkout', '--track', '-b', opts.branch_name) - else: - if opts.upstream in tags(): - # TODO(iannucci): ensure that basis_ref is an ancestor of HEAD? - run('checkout', '--no-track', '-b', opts.branch_name, - hash_one(opts.upstream)) - set_config('branch.%s.remote' % opts.branch_name, '.') - set_config('branch.%s.merge' % opts.branch_name, opts.upstream) - else: - # TODO(iannucci): Detect unclean workdir then stash+pop if we need to - # teleport to a conflicting portion of history? - run('checkout', '--track', opts.upstream, '-b', opts.branch_name) - get_or_create_merge_base(opts.branch_name) - except subprocess2.CalledProcessError as cpe: - sys.stdout.write(cpe.stdout.decode('utf-8', 'replace')) - sys.stderr.write(cpe.stderr.decode('utf-8', 'replace')) - return 1 - sys.stderr.write('Switched to branch %s.\n' % opts.branch_name) - return 0 + return create_new_branch( + opts.branch_name, opts.upstream_current, opts.upstream, + opts.inject_current) if __name__ == '__main__': # pragma: no cover diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index a34b59a58..4c1818207 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -659,7 +659,8 @@ class TestGitCl(unittest.TestCase): mock.patch('scm.GIT.GetBranchRef', self.mockGit.GetBranchRef).start() mock.patch('scm.GIT.GetConfig', self.mockGit.GetConfig).start() mock.patch('scm.GIT.SetConfig', self.mockGit.SetConfig).start() - mock.patch('git_new_branch.main', self.mockGit.NewBranch).start() + mock.patch( + 'git_new_branch.create_new_branch', self.mockGit.NewBranch).start() mock.patch( 'scm.GIT.FetchUpstreamTuple', return_value=('origin', 'refs/heads/master')).start()