git_nav_downstream.py 1.9 KB

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