blob: 8a07535f73944d045a6ce24ae54ccd8b98ea9c73 [file] [log] [blame]
Edward Lesmes98eda3f2019-08-12 21:09:53 +00001#!/usr/bin/env python
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00002# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Rename the current branch while maintaining correct dependencies."""
7
8import argparse
9import sys
10
11import subprocess2
12
13from git_common import current_branch, run, set_branch_config, branch_config
14from git_common import branch_config_map
15
16def main(args):
17 current = current_branch()
18 if current == 'HEAD':
19 current = None
20 old_name_help = 'The old branch to rename.'
21 if current:
22 old_name_help += ' (default %(default)r)'
23
24 parser = argparse.ArgumentParser()
25 parser.add_argument('old_name', nargs=('?' if current else 1),
26 help=old_name_help, default=current)
27 parser.add_argument('new_name', help='The new branch name.')
28
29 opts = parser.parse_args(args)
30
31 # when nargs=1, we get a list :(
32 if isinstance(opts.old_name, list):
33 opts.old_name = opts.old_name[0]
34
35 try:
36 run('branch', '-m', opts.old_name, opts.new_name)
37
38 # update the downstreams
Marc-Antoine Ruel8e57b4b2019-10-11 01:01:36 +000039 for branch, merge in branch_config_map('merge').items():
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000040 if merge == 'refs/heads/' + opts.old_name:
41 # Only care about local branches
42 if branch_config(branch, 'remote') == '.':
43 set_branch_config(branch, 'merge', 'refs/heads/' + opts.new_name)
44 except subprocess2.CalledProcessError as cpe:
45 sys.stderr.write(cpe.stderr)
46 return 1
sbc@chromium.org013731e2015-02-26 18:28:43 +000047 return 0
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000048
49
50if __name__ == '__main__': # pragma: no cover
sbc@chromium.org013731e2015-02-26 18:28:43 +000051 try:
52 sys.exit(main(sys.argv[1:]))
53 except KeyboardInterrupt:
54 sys.stderr.write('interrupted\n')
55 sys.exit(1)