You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
depot_tools/git_new_branch.py

77 lines
2.8 KiB
Python

#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Create new branch tracking origin/master by default.
"""
import argparse
import sys
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 main(args):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=__doc__,
)
parser.add_argument('branch_name')
g = parser.add_mutually_exclusive_group()
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(),
help='upstream branch (or tag) to track.')
g.add_argument('--inject-current', '--inject_current',
action='store_true',
help='new branch adopts current branch\'s upstream,' +
' and new branch becomes current branch\'s upstream.')
g.add_argument('--lkgr', action='store_const', const='lkgr', dest='upstream',
help='set basis ref for new branch to lkgr.')
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?
Revert "Fix semantics of git new-branch --upstream" This reverts commit ba83229a731721ff888374b0ba6eab8ec80fb652. Reason for revert: After mail discussion we came to the conclusion that the old behavior makes more sense. Original change's description: > Fix semantics of git new-branch --upstream > > Currently, the "--upstream A" option for new-branch behaves totally > different than "--upstream_current". While "--upstream A" checks out > branch A and then creates a new branch which tracks A, > "--upstream_current" creates a new branch for the current HEAD and sets > the upstream to the previously checked out branch. > > As the documentation does not mention that any of the options changes > the currently-checked-out commit (HEAD), this CL changes the semantics > of "git new-branch --upstream A B" to be identical to "git checkout -b B > && git branch --set-upstream-to A". > > It also slightly extends the documentation to mention that in any case > the new branch is based on HEAD. > > R=​iannucci@chromium.org > > Change-Id: Ic335d2caf27cb6afca1b8bc5a008424c0e880fca > Reviewed-on: https://chromium-review.googlesource.com/c/1350748 > Reviewed-by: Robbie Iannucci <iannucci@chromium.org> > Commit-Queue: Clemens Hammacher <clemensh@chromium.org> > Auto-Submit: Clemens Hammacher <clemensh@chromium.org> TBR=iannucci@chromium.org,tandrii@chromium.org,clemensh@chromium.org # Not skipping CQ checks because original CL landed > 1 day ago. Change-Id: I7463935af172f0801c7da94d2de106a02fc4c42e Reviewed-on: https://chromium-review.googlesource.com/c/1362972 Reviewed-by: Sergiy Belozorov <sergiyb@chromium.org> Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
6 years ago
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)
sys.stderr.write(cpe.stderr)
return 1
sys.stderr.write('Switched to branch %s.\n' % opts.branch_name)
return 0
if __name__ == '__main__': # pragma: no cover
try:
sys.exit(main(sys.argv[1:]))
except KeyboardInterrupt:
sys.stderr.write('interrupted\n')
sys.exit(1)