[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 1 | #!/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 | |
| 8 | import argparse |
| 9 | import sys |
| 10 | |
| 11 | import subprocess2 |
| 12 | |
| 13 | from git_common import current_branch, run, set_branch_config, branch_config |
| 14 | from git_common import branch_config_map |
| 15 | |
| 16 | def 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] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 47 | return 0 |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 48 | |
| 49 | |
| 50 | if __name__ == '__main__': # pragma: no cover |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 51 | try: |
| 52 | sys.exit(main(sys.argv[1:])) |
| 53 | except KeyboardInterrupt: |
| 54 | sys.stderr.write('interrupted\n') |
| 55 | sys.exit(1) |