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 import os |
15 | 16 |
16 from fnmatch import fnmatch | 17 from fnmatch import fnmatch |
17 from pprint import pformat | 18 from pprint import pformat |
18 | 19 |
19 import git_common as git | 20 import git_common as git |
20 | 21 |
21 | 22 |
22 STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' | 23 STARTING_BRANCH_KEY = 'depot-tools.rebase-update.starting-branch' |
| 24 STARTING_WORKDIR_KEY = 'depot-tools.rebase-update.starting-workdir' |
23 | 25 |
24 | 26 |
25 def find_return_branch(): | 27 def find_return_branch_workdir(): |
26 """Finds the branch which we should return to after rebase-update completes. | 28 """Finds the branch and working directory which we should return to after |
| 29 rebase-update completes. |
27 | 30 |
28 This value may persist across multiple invocations of rebase-update, if | 31 These values may persist across multiple invocations of rebase-update, if |
29 rebase-update runs into a conflict mid-way. | 32 rebase-update runs into a conflict mid-way. |
30 """ | 33 """ |
31 return_branch = git.config(STARTING_BRANCH_KEY) | 34 return_branch = git.config(STARTING_BRANCH_KEY) |
| 35 workdir = git.config(STARTING_WORKDIR_KEY) |
32 if not return_branch: | 36 if not return_branch: |
| 37 workdir = os.getcwd() |
| 38 git.set_config(STARTING_WORKDIR_KEY, workdir) |
33 return_branch = git.current_branch() | 39 return_branch = git.current_branch() |
34 if return_branch != 'HEAD': | 40 if return_branch != 'HEAD': |
35 git.set_config(STARTING_BRANCH_KEY, return_branch) | 41 git.set_config(STARTING_BRANCH_KEY, return_branch) |
36 | 42 |
37 return return_branch | 43 return return_branch, workdir |
38 | 44 |
39 | 45 |
40 def fetch_remotes(branch_tree): | 46 def fetch_remotes(branch_tree): |
41 """Fetches all remotes which are needed to update |branch_tree|.""" | 47 """Fetches all remotes which are needed to update |branch_tree|.""" |
42 fetch_tags = False | 48 fetch_tags = False |
43 remotes = set() | 49 remotes = set() |
44 tag_set = git.tags() | 50 tag_set = git.tags() |
45 fetchspec_map = {} | 51 fetchspec_map = {} |
46 all_fetchspec_configs = git.run( | 52 all_fetchspec_configs = git.run( |
47 'config', '--get-regexp', r'^remote\..*\.fetch').strip() | 53 'config', '--get-regexp', r'^remote\..*\.fetch').strip() |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 213 |
208 if git.in_rebase(): | 214 if git.in_rebase(): |
209 # TODO(iannucci): Be able to resume rebase with flags like --continue, | 215 # TODO(iannucci): Be able to resume rebase with flags like --continue, |
210 # etc. | 216 # etc. |
211 print ( | 217 print ( |
212 'Rebase in progress. Please complete the rebase before running ' | 218 'Rebase in progress. Please complete the rebase before running ' |
213 '`git rebase-update`.' | 219 '`git rebase-update`.' |
214 ) | 220 ) |
215 return 1 | 221 return 1 |
216 | 222 |
217 return_branch = find_return_branch() | 223 return_branch, return_workdir = find_return_branch_workdir() |
| 224 os.chdir(git.run('rev-parse', '--show-toplevel')) |
218 | 225 |
219 if git.current_branch() == 'HEAD': | 226 if git.current_branch() == 'HEAD': |
220 if git.run('status', '--porcelain'): | 227 if git.run('status', '--porcelain'): |
221 print 'Cannot rebase-update with detached head + uncommitted changes.' | 228 print 'Cannot rebase-update with detached head + uncommitted changes.' |
222 return 1 | 229 return 1 |
223 else: | 230 else: |
224 git.freeze() # just in case there are any local changes. | 231 git.freeze() # just in case there are any local changes. |
225 | 232 |
226 skipped, branch_tree = git.get_branch_tree() | 233 skipped, branch_tree = git.get_branch_tree() |
227 for branch in skipped: | 234 for branch in skipped: |
(...skipping 29 matching lines...) Expand all Loading... |
257 git.run('checkout', return_branch) | 264 git.run('checkout', return_branch) |
258 git.thaw() | 265 git.thaw() |
259 else: | 266 else: |
260 root_branch = git.root() | 267 root_branch = git.root() |
261 if return_branch != 'HEAD': | 268 if return_branch != 'HEAD': |
262 print ( | 269 print ( |
263 "%r was merged with its parent, checking out %r instead." | 270 "%r was merged with its parent, checking out %r instead." |
264 % (return_branch, root_branch) | 271 % (return_branch, root_branch) |
265 ) | 272 ) |
266 git.run('checkout', root_branch) | 273 git.run('checkout', root_branch) |
| 274 os.chdir(return_workdir) |
267 git.set_config(STARTING_BRANCH_KEY, '') | 275 git.set_config(STARTING_BRANCH_KEY, '') |
| 276 git.set_config(STARTING_WORKDIR_KEY, '') |
268 | 277 |
269 return retcode | 278 return retcode |
270 | 279 |
271 | 280 |
272 if __name__ == '__main__': # pragma: no cover | 281 if __name__ == '__main__': # pragma: no cover |
273 try: | 282 try: |
274 sys.exit(main()) | 283 sys.exit(main()) |
275 except KeyboardInterrupt: | 284 except KeyboardInterrupt: |
276 sys.stderr.write('interrupted\n') | 285 sys.stderr.write('interrupted\n') |
277 sys.exit(1) | 286 sys.exit(1) |
OLD | NEW |