blob: e394343ba4ba3bd75f3648c2e50f171c5530d74d [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"""
11import sys
12
13from git_common import current_branch, branches, upstream, run, hash_one
14
15
16def main(argv):
17 assert len(argv) == 1, "No arguments expected"
18 upfn = upstream
19 cur = current_branch()
20 if cur == 'HEAD':
iannucci@chromium.orga112f032014-03-13 07:47:50 +000021 def _upfn(b):
22 parent = upstream(b)
23 if parent:
24 return hash_one(parent)
25 upfn = _upfn
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000026 cur = hash_one(cur)
27 downstreams = [b for b in branches() if upfn(b) == cur]
28 if not downstreams:
29 return "No downstream branches"
30 elif len(downstreams) == 1:
31 run('checkout', downstreams[0])
32 else:
33 high = len(downstreams) - 1
34 print
35 while True:
36 print "Please select a downstream branch"
37 for i, b in enumerate(downstreams):
38 print " %d. %s" % (i, b)
39 r = raw_input("Selection (0-%d)[0]: " % high).strip() or '0'
40 if not r.isdigit() or (0 > int(r) > high):
41 print "Invalid choice."
42 else:
43 run('checkout', downstreams[int(r)])
44 break
45
46
47if __name__ == '__main__':
48 sys.exit(main(sys.argv))