blob: 4d8b823cb7e2c4cf1e50025ac3b92fd68ec6c16c [file] [log] [blame]
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00001#!/usr/bin/env python
iannucci@chromium.orga112f032014-03-13 07:47: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
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00006"""
7Checks out a downstream branch from the currently checked out branch. If there
8is more than one downstream branch, then this script will prompt you to select
9which branch.
10"""
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000011
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000012import sys
13
14from git_common import current_branch, branches, upstream, run, hash_one
15
16
17def main(argv):
18 assert len(argv) == 1, "No arguments expected"
19 upfn = upstream
20 cur = current_branch()
21 if cur == 'HEAD':
iannucci@chromium.orga112f032014-03-13 07:47:50 +000022 def _upfn(b):
23 parent = upstream(b)
24 if parent:
25 return hash_one(parent)
26 upfn = _upfn
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000027 cur = hash_one(cur)
28 downstreams = [b for b in branches() if upfn(b) == cur]
29 if not downstreams:
30 return "No downstream branches"
31 elif len(downstreams) == 1:
32 run('checkout', downstreams[0])
33 else:
34 high = len(downstreams) - 1
35 print
36 while True:
37 print "Please select a downstream branch"
38 for i, b in enumerate(downstreams):
39 print " %d. %s" % (i, b)
40 r = raw_input("Selection (0-%d)[0]: " % high).strip() or '0'
41 if not r.isdigit() or (0 > int(r) > high):
42 print "Invalid choice."
43 else:
44 run('checkout', downstreams[int(r)])
45 break
46
47
48if __name__ == '__main__':
49 sys.exit(main(sys.argv))