[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 1 | #!/usr/bin/env python |
[email protected] | a112f03 | 2014-03-13 07:47:50 | [diff] [blame] | 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 | |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 6 | """ |
| 7 | Checks out a downstream branch from the currently checked out branch. If there |
| 8 | is more than one downstream branch, then this script will prompt you to select |
| 9 | which branch. |
| 10 | """ |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 11 | |
[email protected] | 2198002 | 2014-04-11 04:51:49 | [diff] [blame] | 12 | import argparse |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 13 | import sys |
| 14 | |
[email protected] | 39e72a4 | 2014-08-28 00:25:22 | [diff] [blame] | 15 | from git_common import current_branch, branches, upstream, run, hash_one |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 16 | |
| 17 | |
[email protected] | 2198002 | 2014-04-11 04:51:49 | [diff] [blame] | 18 | def main(args): |
| 19 | parser = argparse.ArgumentParser() |
| 20 | parser.add_argument('--pick', |
| 21 | help=( |
| 22 | 'The number to pick if this command would ' |
| 23 | 'prompt')) |
| 24 | opts = parser.parse_args(args) |
| 25 | |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 26 | upfn = upstream |
| 27 | cur = current_branch() |
| 28 | if cur == 'HEAD': |
[email protected] | a112f03 | 2014-03-13 07:47:50 | [diff] [blame] | 29 | def _upfn(b): |
| 30 | parent = upstream(b) |
| 31 | if parent: |
| 32 | return hash_one(parent) |
| 33 | upfn = _upfn |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 34 | cur = hash_one(cur) |
| 35 | downstreams = [b for b in branches() if upfn(b) == cur] |
| 36 | if not downstreams: |
| 37 | return "No downstream branches" |
| 38 | elif len(downstreams) == 1: |
[email protected] | 39e72a4 | 2014-08-28 00:25:22 | [diff] [blame] | 39 | run('checkout', downstreams[0], stdout=sys.stdout, stderr=sys.stderr) |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 40 | else: |
| 41 | high = len(downstreams) - 1 |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 42 | while True: |
| 43 | print "Please select a downstream branch" |
| 44 | for i, b in enumerate(downstreams): |
| 45 | print " %d. %s" % (i, b) |
[email protected] | 2198002 | 2014-04-11 04:51:49 | [diff] [blame] | 46 | prompt = "Selection (0-%d)[0]: " % high |
| 47 | r = opts.pick |
| 48 | if r: |
| 49 | print prompt + r |
| 50 | else: |
| 51 | r = raw_input(prompt).strip() or '0' |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 52 | if not r.isdigit() or (0 > int(r) > high): |
| 53 | print "Invalid choice." |
| 54 | else: |
[email protected] | 39e72a4 | 2014-08-28 00:25:22 | [diff] [blame] | 55 | run('checkout', downstreams[int(r)], stdout=sys.stdout, |
| 56 | stderr=sys.stderr) |
[email protected] | 8bc9b5c | 2014-03-12 01:36:18 | [diff] [blame] | 57 | break |
| 58 | |
| 59 | |
| 60 | if __name__ == '__main__': |
[email protected] | 04404bc | 2014-05-11 00:49:45 | [diff] [blame] | 61 | try: |
| 62 | sys.exit(main(sys.argv[1:])) |
| 63 | except KeyboardInterrupt: |
| 64 | pass |