Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
iannucci@chromium.org | a112f03 | 2014-03-13 07:47:50 +0000 | [diff] [blame] | 2 | # 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.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 5 | """ |
| 6 | Checks out a downstream branch from the currently checked out branch. If there |
| 7 | is more than one downstream branch, then this script will prompt you to select |
| 8 | which branch. |
| 9 | """ |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 10 | |
iannucci@chromium.org | 2198002 | 2014-04-11 04:51:49 +0000 | [diff] [blame] | 11 | import argparse |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 12 | import sys |
| 13 | |
Edward Lesmes | ae3586b | 2020-03-23 21:21:14 +0000 | [diff] [blame] | 14 | import gclient_utils |
asanka@chromium.org | 39e72a4 | 2014-08-28 00:25:22 +0000 | [diff] [blame] | 15 | from git_common import current_branch, branches, upstream, run, hash_one |
Edward Lemur | 8a2e331 | 2018-07-12 21:15:09 +0000 | [diff] [blame] | 16 | import metrics |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 17 | |
| 18 | |
Edward Lemur | 8a2e331 | 2018-07-12 21:15:09 +0000 | [diff] [blame] | 19 | @metrics.collector.collect_metrics('git nav-downstream') |
iannucci@chromium.org | 2198002 | 2014-04-11 04:51:49 +0000 | [diff] [blame] | 20 | def main(args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 21 | 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.org | 2198002 | 2014-04-11 04:51:49 +0000 | [diff] [blame] | 26 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 27 | upfn = upstream |
| 28 | cur = current_branch() |
| 29 | if cur == 'HEAD': |
Aravind Vasudevan | c5f0cbb | 2022-01-24 23:56:57 +0000 | [diff] [blame] | 30 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 31 | 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.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 69 | with metrics.collector.print_notice_and_exit(): |
| 70 | sys.exit(main(sys.argv[1:])) |