git_nav_downstream.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  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.
  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. """
  10. from __future__ import print_function
  11. import argparse
  12. import sys
  13. import gclient_utils
  14. from git_common import current_branch, branches, upstream, run, hash_one
  15. import metrics
  16. @metrics.collector.collect_metrics('git nav-downstream')
  17. def main(args):
  18. parser = argparse.ArgumentParser()
  19. parser.add_argument('--pick',
  20. help=(
  21. 'The number to pick if this command would '
  22. 'prompt'))
  23. opts = parser.parse_args(args)
  24. upfn = upstream
  25. cur = current_branch()
  26. if cur == 'HEAD':
  27. def _upfn(b):
  28. parent = upstream(b)
  29. if parent:
  30. return hash_one(parent)
  31. upfn = _upfn
  32. cur = hash_one(cur)
  33. downstreams = [b for b in branches() if upfn(b) == cur]
  34. if not downstreams:
  35. print("No downstream branches")
  36. return 1
  37. elif len(downstreams) == 1:
  38. run('checkout', downstreams[0], stdout=sys.stdout, stderr=sys.stderr)
  39. else:
  40. high = len(downstreams) - 1
  41. while True:
  42. print("Please select a downstream branch")
  43. for i, b in enumerate(downstreams):
  44. print(" %d. %s" % (i, b))
  45. prompt = "Selection (0-%d)[0]: " % high
  46. r = opts.pick
  47. if r:
  48. print(prompt + r)
  49. else:
  50. r = gclient_utils.AskForData(prompt).strip() or '0'
  51. if not r.isdigit() or (0 > int(r) > high):
  52. print("Invalid choice.")
  53. else:
  54. run('checkout', downstreams[int(r)], stdout=sys.stdout,
  55. stderr=sys.stderr)
  56. break
  57. return 0
  58. if __name__ == '__main__':
  59. with metrics.collector.print_notice_and_exit():
  60. sys.exit(main(sys.argv[1:]))