| OLD | NEW |
| 1 #!/usr/bin/env python | 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 |
| 2 """ | 6 """ |
| 3 Checks out a downstream branch from the currently checked out branch. If there | 7 Checks out a downstream branch from the currently checked out branch. If there |
| 4 is more than one downstream branch, then this script will prompt you to select | 8 is more than one downstream branch, then this script will prompt you to select |
| 5 which branch. | 9 which branch. |
| 6 """ | 10 """ |
| 7 import sys | 11 import sys |
| 8 | 12 |
| 9 from git_common import current_branch, branches, upstream, run, hash_one | 13 from git_common import current_branch, branches, upstream, run, hash_one |
| 10 | 14 |
| 11 | 15 |
| 12 def main(argv): | 16 def main(argv): |
| 13 assert len(argv) == 1, "No arguments expected" | 17 assert len(argv) == 1, "No arguments expected" |
| 14 upfn = upstream | 18 upfn = upstream |
| 15 cur = current_branch() | 19 cur = current_branch() |
| 16 if cur == 'HEAD': | 20 if cur == 'HEAD': |
| 17 upfn = lambda b: hash_one(upstream(b)) | 21 def _upfn(b): |
| 22 parent = upstream(b) |
| 23 if parent: |
| 24 return hash_one(parent) |
| 25 upfn = _upfn |
| 18 cur = hash_one(cur) | 26 cur = hash_one(cur) |
| 19 downstreams = [b for b in branches() if upfn(b) == cur] | 27 downstreams = [b for b in branches() if upfn(b) == cur] |
| 20 if not downstreams: | 28 if not downstreams: |
| 21 return "No downstream branches" | 29 return "No downstream branches" |
| 22 elif len(downstreams) == 1: | 30 elif len(downstreams) == 1: |
| 23 run('checkout', downstreams[0]) | 31 run('checkout', downstreams[0]) |
| 24 else: | 32 else: |
| 25 high = len(downstreams) - 1 | 33 high = len(downstreams) - 1 |
| 26 print | 34 print |
| 27 while True: | 35 while True: |
| 28 print "Please select a downstream branch" | 36 print "Please select a downstream branch" |
| 29 for i, b in enumerate(downstreams): | 37 for i, b in enumerate(downstreams): |
| 30 print " %d. %s" % (i, b) | 38 print " %d. %s" % (i, b) |
| 31 r = raw_input("Selection (0-%d)[0]: " % high).strip() or '0' | 39 r = raw_input("Selection (0-%d)[0]: " % high).strip() or '0' |
| 32 if not r.isdigit() or (0 > int(r) > high): | 40 if not r.isdigit() or (0 > int(r) > high): |
| 33 print "Invalid choice." | 41 print "Invalid choice." |
| 34 else: | 42 else: |
| 35 run('checkout', downstreams[int(r)]) | 43 run('checkout', downstreams[int(r)]) |
| 36 break | 44 break |
| 37 | 45 |
| 38 | 46 |
| 39 if __name__ == '__main__': | 47 if __name__ == '__main__': |
| 40 sys.exit(main(sys.argv)) | 48 sys.exit(main(sys.argv)) |
| OLD | NEW |