blob: 9c2a53780a9f288376c16c3ecefffa6f0ca948c8 [file] [log] [blame]
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00001#!/usr/bin/env python
2"""
3Checks out a downstream branch from the currently checked out branch. If there
4is more than one downstream branch, then this script will prompt you to select
5which branch.
6"""
7import sys
8
9from git_common import current_branch, branches, upstream, run, hash_one
10
11
12def main(argv):
13 assert len(argv) == 1, "No arguments expected"
14 upfn = upstream
15 cur = current_branch()
16 if cur == 'HEAD':
17 upfn = lambda b: hash_one(upstream(b))
18 cur = hash_one(cur)
19 downstreams = [b for b in branches() if upfn(b) == cur]
20 if not downstreams:
21 return "No downstream branches"
22 elif len(downstreams) == 1:
23 run('checkout', downstreams[0])
24 else:
25 high = len(downstreams) - 1
26 print
27 while True:
28 print "Please select a downstream branch"
29 for i, b in enumerate(downstreams):
30 print " %d. %s" % (i, b)
31 r = raw_input("Selection (0-%d)[0]: " % high).strip() or '0'
32 if not r.isdigit() or (0 > int(r) > high):
33 print "Invalid choice."
34 else:
35 run('checkout', downstreams[int(r)])
36 break
37
38
39if __name__ == '__main__':
40 sys.exit(main(sys.argv))