From 53a115e67e50a1878586982eefb69958dfeda4a2 Mon Sep 17 00:00:00 2001 From: Asanka Herath Date: Thu, 12 Apr 2018 19:07:37 -0400 Subject: [PATCH] Add a '--inject-current' option to 'git new-branch' This option creates a new branch that sets itself as the upstream for the current branch and adopts the current branch's upstream as its own. What does it do? ---------------- Imagine we have the following set of branches (as `git map-branches -vv` would show it): origin/master 00001111 [ ahead 1 ] foo 00002222 [ ahead 1 ] baz * 00003333 [ ahead 1 ] 'baz' is the current branch. If one were to issue the following command: $ git new-branch --inject-current bar ... then the branch layout will now look like this: origin/master 00001111 [ ahead 1 ] foo 00002222 [ ahead 1 ] bar * 00002222 baz 00003333 [ ahead 1 ] Why would you need this? ------------------------ When working on a single change or a sequence of changes organized into dependent branches, this option lets you quickly create a new branch with the correct upstreams so that you can peel off smaller cleanups and potentially unrelated changes out of your main feature branch into separate branches. These can then be uploaded as dependent CLs. R=petermayo@chromium.org,iannucci@chromium.org Change-Id: Id912f8e5c17e267fc52d74bdfac7bbcf87a50908 Reviewed-on: https://chromium-review.googlesource.com/987529 Commit-Queue: Asanka Herath Reviewed-by: Robbie Iannucci --- git_new_branch.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/git_new_branch.py b/git_new_branch.py index ed1225dc40..d61a42d790 100755 --- a/git_new_branch.py +++ b/git_new_branch.py @@ -2,7 +2,6 @@ # 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. """ @@ -13,7 +12,7 @@ import sys import subprocess2 from git_common import run, root, set_config, get_or_create_merge_base, tags -from git_common import hash_one +from git_common import hash_one, upstream, set_branch_config, current_branch def main(args): @@ -28,13 +27,26 @@ def main(args): 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.upstream_current: + 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():