blob: c0ac42ec11cd50c8329061789e954c329909e169 [file] [log] [blame]
[email protected]c050a5b2014-03-26 06:18:501#!/usr/bin/env python
2# 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
39 for branch, merge in branch_config_map('merge').iteritems():
40 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
[email protected]013731e2015-02-26 18:28:4347 return 0
[email protected]c050a5b2014-03-26 06:18:5048
49
50if __name__ == '__main__': # pragma: no cover
[email protected]013731e2015-02-26 18:28:4351 try:
52 sys.exit(main(sys.argv[1:]))
53 except KeyboardInterrupt:
54 sys.stderr.write('interrupted\n')
55 sys.exit(1)