blob: 7834d8a77bdf5127e3cf8965386f2bbe34a9bc95 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
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.
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +00005"""
6Checks out a downstream branch from the currently checked out branch. If there
7is more than one downstream branch, then this script will prompt you to select
8which branch.
9"""
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000010
iannucci@chromium.org21980022014-04-11 04:51:49 +000011import argparse
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000012import sys
13
Edward Lesmesae3586b2020-03-23 21:21:14 +000014import gclient_utils
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):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000021 parser = argparse.ArgumentParser()
22 parser.add_argument('--pick',
23 help=('The number to pick if this command would '
24 'prompt'))
25 opts = parser.parse_args(args)
iannucci@chromium.org21980022014-04-11 04:51:49 +000026
Mike Frysinger124bb8e2023-09-06 05:48:55 +000027 upfn = upstream
28 cur = current_branch()
29 if cur == 'HEAD':
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +000030
Mike Frysinger124bb8e2023-09-06 05:48:55 +000031 def _upfn(b):
32 parent = upstream(b)
33 if parent:
34 return hash_one(parent)
35
36 upfn = _upfn
37 cur = hash_one(cur)
38 downstreams = [b for b in branches() if upfn(b) == cur]
39 if not downstreams:
40 print("No downstream branches")
41 return 1
42
43 if len(downstreams) == 1:
44 run('checkout', downstreams[0], stdout=sys.stdout, stderr=sys.stderr)
45 else:
46 high = len(downstreams) - 1
47 while True:
48 print("Please select a downstream branch")
49 for i, b in enumerate(downstreams):
50 print(" %d. %s" % (i, b))
51 prompt = "Selection (0-%d)[0]: " % high
52 r = opts.pick
53 if r:
54 print(prompt + r)
55 else:
56 r = gclient_utils.AskForData(prompt).strip() or '0'
57 if not r.isdigit() or (0 > int(r) > high):
58 print("Invalid choice.")
59 else:
60 run('checkout',
61 downstreams[int(r)],
62 stdout=sys.stdout,
63 stderr=sys.stderr)
64 break
65 return 0
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000066
67
68if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000069 with metrics.collector.print_notice_and_exit():
70 sys.exit(main(sys.argv[1:]))