blob: 1eeeb6ab025ee9a38db36d496a302d0f80915862 [file] [log] [blame]
Edward Lesmes98eda3f2019-08-12 21:09:53 +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
Raul Tambre80ee78e2019-05-06 22:41:05 +000012from __future__ import print_function
13
iannucci@chromium.org21980022014-04-11 04:51:49 +000014import argparse
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000015import sys
16
Edward Lesmesae3586b2020-03-23 21:21:14 +000017import gclient_utils
asanka@chromium.org39e72a42014-08-28 00:25:22 +000018from git_common import current_branch, branches, upstream, run, hash_one
Edward Lemur8a2e3312018-07-12 21:15:09 +000019import metrics
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000020
21
Edward Lemur8a2e3312018-07-12 21:15:09 +000022@metrics.collector.collect_metrics('git nav-downstream')
iannucci@chromium.org21980022014-04-11 04:51:49 +000023def main(args):
24 parser = argparse.ArgumentParser()
25 parser.add_argument('--pick',
26 help=(
27 'The number to pick if this command would '
28 'prompt'))
29 opts = parser.parse_args(args)
30
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000031 upfn = upstream
32 cur = current_branch()
33 if cur == 'HEAD':
iannucci@chromium.orga112f032014-03-13 07:47:50 +000034 def _upfn(b):
35 parent = upstream(b)
36 if parent:
37 return hash_one(parent)
38 upfn = _upfn
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000039 cur = hash_one(cur)
40 downstreams = [b for b in branches() if upfn(b) == cur]
41 if not downstreams:
Raul Tambre80ee78e2019-05-06 22:41:05 +000042 print("No downstream branches")
sbc@chromium.org013731e2015-02-26 18:28:43 +000043 return 1
Josip Sokcevic42c5bbb2022-01-24 21:42:28 +000044 elif len(downstreams) == 1:
asanka@chromium.org39e72a42014-08-28 00:25:22 +000045 run('checkout', downstreams[0], stdout=sys.stdout, stderr=sys.stderr)
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000046 else:
47 high = len(downstreams) - 1
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000048 while True:
Raul Tambre80ee78e2019-05-06 22:41:05 +000049 print("Please select a downstream branch")
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000050 for i, b in enumerate(downstreams):
Raul Tambre80ee78e2019-05-06 22:41:05 +000051 print(" %d. %s" % (i, b))
iannucci@chromium.org21980022014-04-11 04:51:49 +000052 prompt = "Selection (0-%d)[0]: " % high
53 r = opts.pick
54 if r:
Raul Tambre80ee78e2019-05-06 22:41:05 +000055 print(prompt + r)
iannucci@chromium.org21980022014-04-11 04:51:49 +000056 else:
Edward Lesmesae3586b2020-03-23 21:21:14 +000057 r = gclient_utils.AskForData(prompt).strip() or '0'
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000058 if not r.isdigit() or (0 > int(r) > high):
Raul Tambre80ee78e2019-05-06 22:41:05 +000059 print("Invalid choice.")
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000060 else:
asanka@chromium.org39e72a42014-08-28 00:25:22 +000061 run('checkout', downstreams[int(r)], stdout=sys.stdout,
62 stderr=sys.stderr)
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000063 break
sbc@chromium.org013731e2015-02-26 18:28:43 +000064 return 0
iannucci@chromium.org8bc9b5c2014-03-12 01:36:18 +000065
66
67if __name__ == '__main__':
Edward Lemur6f812e12018-07-31 22:45:57 +000068 with metrics.collector.print_notice_and_exit():
iannucci@chromium.org04404bc2014-05-11 00:49:45 +000069 sys.exit(main(sys.argv[1:]))