blob: 1b9cce5e44823fc4e733296a5b13716576d4993f [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.org21980022014-04-11 04:51:49 +000012import argparse
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000013import sys
14
asanka@chromium.org39e72a42014-08-28 00:25:22 +000015from git_common import current_branch, branches, upstream, run, hash_one
Edward Lemur8a2e3312018-07-12 21:15:09 +000016import metrics
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000017
18
Edward Lemur8a2e3312018-07-12 21:15:09 +000019@metrics.collector.collect_metrics('git nav-downstream')
iannucci@chromium.org21980022014-04-11 04:51:49 +000020def main(args):
21 parser = argparse.ArgumentParser()
22 parser.add_argument('--pick',
23 help=(
24 'The number to pick if this command would '
25 'prompt'))
26 opts = parser.parse_args(args)
27
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000028 upfn = upstream
29 cur = current_branch()
30 if cur == 'HEAD':
iannucci@chromium.orga112f032014-03-13 07:47:50 +000031 def _upfn(b):
32 parent = upstream(b)
33 if parent:
34 return hash_one(parent)
35 upfn = _upfn
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000036 cur = hash_one(cur)
37 downstreams = [b for b in branches() if upfn(b) == cur]
38 if not downstreams:
sbc@chromium.org013731e2015-02-26 18:28:43 +000039 print "No downstream branches"
40 return 1
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000041 elif len(downstreams) == 1:
asanka@chromium.org39e72a42014-08-28 00:25:22 +000042 run('checkout', downstreams[0], stdout=sys.stdout, stderr=sys.stderr)
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000043 else:
44 high = len(downstreams) - 1
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000045 while True:
46 print "Please select a downstream branch"
47 for i, b in enumerate(downstreams):
48 print " %d. %s" % (i, b)
iannucci@chromium.org21980022014-04-11 04:51:49 +000049 prompt = "Selection (0-%d)[0]: " % high
50 r = opts.pick
51 if r:
52 print prompt + r
53 else:
54 r = raw_input(prompt).strip() or '0'
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000055 if not r.isdigit() or (0 > int(r) > high):
56 print "Invalid choice."
57 else:
asanka@chromium.org39e72a42014-08-28 00:25:22 +000058 run('checkout', downstreams[int(r)], stdout=sys.stdout,
59 stderr=sys.stderr)
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000060 break
sbc@chromium.org013731e2015-02-26 18:28:43 +000061 return 0
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000062
63
64if __name__ == '__main__':
iannucci@chromium.org04404bc2014-05-11 00:49:45 +000065 try:
66 sys.exit(main(sys.argv[1:]))
67 except KeyboardInterrupt:
sbc@chromium.org013731e2015-02-26 18:28:43 +000068 sys.stderr.write('interrupted\n')
69 sys.exit(1)