| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 Tool to update all branches to have the latest changes from their upstreams. | 7 Tool to update all branches to have the latest changes from their upstreams. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import argparse | 10 import argparse |
| 11 import collections | 11 import collections |
| 12 import logging | 12 import logging |
| 13 import sys | 13 import sys |
| 14 import textwrap | 14 import textwrap |
| 15 | 15 |
| 16 from fnmatch import fnmatch |
| 16 from pprint import pformat | 17 from pprint import pformat |
| 17 | 18 |
| 18 import git_common as git | 19 import git_common as git |
| 19 | 20 |
| 20 | 21 |
| 21 STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' | 22 STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' |
| 22 | 23 |
| 23 | 24 |
| 24 def find_return_branch(): | 25 def find_return_branch(): |
| 25 """Finds the branch which we should return to after rebase-update completes. | 26 """Finds the branch which we should return to after rebase-update completes. |
| 26 | 27 |
| 27 This value may persist across multiple invocations of rebase-update, if | 28 This value may persist across multiple invocations of rebase-update, if |
| 28 rebase-update runs into a conflict mid-way. | 29 rebase-update runs into a conflict mid-way. |
| 29 """ | 30 """ |
| 30 return_branch = git.config(STARTING_BRANCH_KEY) | 31 return_branch = git.config(STARTING_BRANCH_KEY) |
| 31 if not return_branch: | 32 if not return_branch: |
| 32 return_branch = git.current_branch() | 33 return_branch = git.current_branch() |
| 33 if return_branch != 'HEAD': | 34 if return_branch != 'HEAD': |
| 34 git.set_config(STARTING_BRANCH_KEY, return_branch) | 35 git.set_config(STARTING_BRANCH_KEY, return_branch) |
| 35 | 36 |
| 36 return return_branch | 37 return return_branch |
| 37 | 38 |
| 38 | 39 |
| 39 def fetch_remotes(branch_tree): | 40 def fetch_remotes(branch_tree): |
| 40 """Fetches all remotes which are needed to update |branch_tree|.""" | 41 """Fetches all remotes which are needed to update |branch_tree|.""" |
| 41 fetch_tags = False | 42 fetch_tags = False |
| 42 remotes = set() | 43 remotes = set() |
| 43 tag_set = git.tags() | 44 tag_set = git.tags() |
| 45 fetchspec_map = {} |
| 46 all_fetchspec_configs = git.run( |
| 47 'config', '--get-regexp', r'remote\..*\.fetch').strip() |
| 48 for fetchspec_config in all_fetchspec_configs.splitlines(): |
| 49 key, _, fetchspec = fetchspec_config.partition(' ') |
| 50 dest_spec = fetchspec.partition(':')[2] |
| 51 remote_name = key.split('.')[1] |
| 52 fetchspec_map[dest_spec] = remote_name |
| 44 for parent in branch_tree.itervalues(): | 53 for parent in branch_tree.itervalues(): |
| 45 if parent in tag_set: | 54 if parent in tag_set: |
| 46 fetch_tags = True | 55 fetch_tags = True |
| 47 else: | 56 else: |
| 48 full_ref = git.run('rev-parse', '--symbolic-full-name', parent) | 57 full_ref = git.run('rev-parse', '--symbolic-full-name', parent) |
| 49 if full_ref.startswith('refs/remotes'): | 58 for dest_spec, remote_name in fetchspec_map.iteritems(): |
| 50 parts = full_ref.split('/') | 59 if fnmatch(full_ref, dest_spec): |
| 51 remote_name = parts[2] | 60 remotes.add(remote_name) |
| 52 remotes.add(remote_name) | 61 break |
| 53 | 62 |
| 54 fetch_args = [] | 63 fetch_args = [] |
| 55 if fetch_tags: | 64 if fetch_tags: |
| 56 # Need to fetch all because we don't know what remote the tag comes from :( | 65 # Need to fetch all because we don't know what remote the tag comes from :( |
| 57 # TODO(iannucci): assert that the tags are in the remote fetch refspec | 66 # TODO(iannucci): assert that the tags are in the remote fetch refspec |
| 58 fetch_args = ['--all'] | 67 fetch_args = ['--all'] |
| 59 else: | 68 else: |
| 60 fetch_args.append('--multiple') | 69 fetch_args.append('--multiple') |
| 61 fetch_args.extend(remotes) | 70 fetch_args.extend(remotes) |
| 62 # TODO(iannucci): Should we fetch git-svn? | 71 # TODO(iannucci): Should we fetch git-svn? |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 % (return_branch, root_branch) | 252 % (return_branch, root_branch) |
| 244 ) | 253 ) |
| 245 git.run('checkout', root_branch) | 254 git.run('checkout', root_branch) |
| 246 git.set_config(STARTING_BRANCH_KEY, '') | 255 git.set_config(STARTING_BRANCH_KEY, '') |
| 247 | 256 |
| 248 return retcode | 257 return retcode |
| 249 | 258 |
| 250 | 259 |
| 251 if __name__ == '__main__': # pragma: no cover | 260 if __name__ == '__main__': # pragma: no cover |
| 252 sys.exit(main(sys.argv[1:])) | 261 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |