| OLD | NEW |
| 1 # coding=utf8 | 1 # coding=utf8 |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 """Manages a project checkout. | 5 """Manages a project checkout. |
| 6 | 6 |
| 7 Includes support for svn, git-svn and git. | 7 Includes support for svn, git-svn and git. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 from __future__ import with_statement | 10 from __future__ import with_statement |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 self.status = status | 55 self.status = status |
| 56 | 56 |
| 57 | 57 |
| 58 class CheckoutBase(object): | 58 class CheckoutBase(object): |
| 59 # Set to None to have verbose output. | 59 # Set to None to have verbose output. |
| 60 VOID = subprocess2.VOID | 60 VOID = subprocess2.VOID |
| 61 | 61 |
| 62 def __init__(self, root_dir, project_name): | 62 def __init__(self, root_dir, project_name): |
| 63 self.root_dir = root_dir | 63 self.root_dir = root_dir |
| 64 self.project_name = project_name | 64 self.project_name = project_name |
| 65 self.project_path = os.path.join(self.root_dir, self.project_name) | 65 if self.project_name is None: |
| 66 self.project_path = self.root_dir |
| 67 else: |
| 68 self.project_path = os.path.join(self.root_dir, self.project_name) |
| 66 # Only used for logging purposes. | 69 # Only used for logging purposes. |
| 67 self._last_seen_revision = None | 70 self._last_seen_revision = None |
| 68 assert self.root_dir | 71 assert self.root_dir |
| 69 assert self.project_name | |
| 70 assert self.project_path | 72 assert self.project_path |
| 71 | 73 |
| 72 def get_settings(self, key): | 74 def get_settings(self, key): |
| 73 return get_code_review_setting(self.project_path, key) | 75 return get_code_review_setting(self.project_path, key) |
| 74 | 76 |
| 75 def prepare(self): | 77 def prepare(self): |
| 76 """Checks out a clean copy of the tree and removes any local modification. | 78 """Checks out a clean copy of the tree and removes any local modification. |
| 77 | 79 |
| 78 This function shouldn't throw unless the remote repository is inaccessible, | 80 This function shouldn't throw unless the remote repository is inaccessible, |
| 79 there is no free disk space or hard issues like that. | 81 there is no free disk space or hard issues like that. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 | 224 |
| 223 | 225 |
| 224 class SvnCheckout(CheckoutBase, SvnMixIn): | 226 class SvnCheckout(CheckoutBase, SvnMixIn): |
| 225 """Manages a subversion checkout.""" | 227 """Manages a subversion checkout.""" |
| 226 def __init__(self, root_dir, project_name, commit_user, commit_pwd, svn_url): | 228 def __init__(self, root_dir, project_name, commit_user, commit_pwd, svn_url): |
| 227 super(SvnCheckout, self).__init__(root_dir, project_name) | 229 super(SvnCheckout, self).__init__(root_dir, project_name) |
| 228 self.commit_user = commit_user | 230 self.commit_user = commit_user |
| 229 self.commit_pwd = commit_pwd | 231 self.commit_pwd = commit_pwd |
| 230 self.svn_url = svn_url | 232 self.svn_url = svn_url |
| 231 assert bool(self.commit_user) >= bool(self.commit_pwd) | 233 assert bool(self.commit_user) >= bool(self.commit_pwd) |
| 232 assert self.svn_url | |
| 233 | 234 |
| 234 def prepare(self): | 235 def prepare(self): |
| 235 # Will checkout if the directory is not present. | 236 # Will checkout if the directory is not present. |
| 237 assert self.svn_url |
| 236 if not os.path.isdir(self.project_path): | 238 if not os.path.isdir(self.project_path): |
| 237 logging.info('Checking out %s in %s' % | 239 logging.info('Checking out %s in %s' % |
| 238 (self.project_name, self.project_path)) | 240 (self.project_name, self.project_path)) |
| 239 revision = self._revert() | 241 revision = self._revert() |
| 240 if revision != self._last_seen_revision: | 242 if revision != self._last_seen_revision: |
| 241 logging.info('Updated at revision %d' % revision) | 243 logging.info('Updated at revision %d' % revision) |
| 242 self._last_seen_revision = revision | 244 self._last_seen_revision = revision |
| 243 return revision | 245 return revision |
| 244 | 246 |
| 245 def apply_patch(self, patches, post_processor=None): | 247 def apply_patch(self, patches, post_processor=None): |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 | 341 |
| 340 | 342 |
| 341 class GitCheckoutBase(CheckoutBase): | 343 class GitCheckoutBase(CheckoutBase): |
| 342 """Base class for git checkout. Not to be used as-is.""" | 344 """Base class for git checkout. Not to be used as-is.""" |
| 343 def __init__(self, root_dir, project_name, remote_branch): | 345 def __init__(self, root_dir, project_name, remote_branch): |
| 344 super(GitCheckoutBase, self).__init__(root_dir, project_name) | 346 super(GitCheckoutBase, self).__init__(root_dir, project_name) |
| 345 # There is no reason to not hardcode it. | 347 # There is no reason to not hardcode it. |
| 346 self.remote = 'origin' | 348 self.remote = 'origin' |
| 347 self.remote_branch = remote_branch | 349 self.remote_branch = remote_branch |
| 348 self.working_branch = 'working_branch' | 350 self.working_branch = 'working_branch' |
| 349 assert self.remote_branch | |
| 350 | 351 |
| 351 def prepare(self): | 352 def prepare(self): |
| 352 """Resets the git repository in a clean state. | 353 """Resets the git repository in a clean state. |
| 353 | 354 |
| 354 Checks it out if not present and deletes the working branch. | 355 Checks it out if not present and deletes the working branch. |
| 355 """ | 356 """ |
| 357 assert self.remote_branch |
| 356 assert os.path.isdir(self.project_path) | 358 assert os.path.isdir(self.project_path) |
| 357 self._check_call_git(['reset', '--hard', '--quiet']) | 359 self._check_call_git(['reset', '--hard', '--quiet']) |
| 358 branches, active = self._branches() | 360 branches, active = self._branches() |
| 359 if active != 'master': | 361 if active != 'master': |
| 360 self._check_call_git(['checkout', 'master', '--force', '--quiet']) | 362 self._check_call_git(['checkout', 'master', '--force', '--quiet']) |
| 361 self._check_call_git(['pull', self.remote, self.remote_branch, '--quiet']) | 363 self._check_call_git(['pull', self.remote, self.remote_branch, '--quiet']) |
| 362 if self.working_branch in branches: | 364 if self.working_branch in branches: |
| 363 self._call_git(['branch', '-D', self.working_branch]) | 365 self._call_git(['branch', '-D', self.working_branch]) |
| 364 | 366 |
| 365 def apply_patch(self, patches, post_processor=None): | 367 def apply_patch(self, patches, post_processor=None): |
| 366 """Applies a patch on 'working_branch' and switch to it. | 368 """Applies a patch on 'working_branch' and switch to it. |
| 367 | 369 |
| 368 Also commits the changes on the local branch. | 370 Also commits the changes on the local branch. |
| 369 | 371 |
| 370 Ignores svn properties and raise an exception on unexpected ones. | 372 Ignores svn properties and raise an exception on unexpected ones. |
| 371 """ | 373 """ |
| 372 post_processor = post_processor or [] | 374 post_processor = post_processor or [] |
| 373 # It this throws, the checkout is corrupted. Maybe worth deleting it and | 375 # It this throws, the checkout is corrupted. Maybe worth deleting it and |
| 374 # trying again? | 376 # trying again? |
| 375 self._check_call_git( | 377 if self.remote_branch: |
| 376 ['checkout', '-b', self.working_branch, | 378 self._check_call_git( |
| 377 '%s/%s' % (self.remote, self.remote_branch), '--quiet']) | 379 ['checkout', '-b', self.working_branch, |
| 380 '%s/%s' % (self.remote, self.remote_branch), '--quiet']) |
| 378 for p in patches: | 381 for p in patches: |
| 379 try: | 382 try: |
| 380 stdout = '' | 383 stdout = '' |
| 381 if p.is_delete: | 384 if p.is_delete: |
| 382 stdout += self._check_output_git(['rm', p.filename]) | 385 stdout += self._check_output_git(['rm', p.filename]) |
| 383 else: | 386 else: |
| 384 dirname = os.path.dirname(p.filename) | 387 dirname = os.path.dirname(p.filename) |
| 385 full_dir = os.path.join(self.project_path, dirname) | 388 full_dir = os.path.join(self.project_path, dirname) |
| 386 if dirname and not os.path.isdir(full_dir): | 389 if dirname and not os.path.isdir(full_dir): |
| 387 os.makedirs(full_dir) | 390 os.makedirs(full_dir) |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 user, message)) | 641 user, message)) |
| 639 return 'FAKE' | 642 return 'FAKE' |
| 640 | 643 |
| 641 @property | 644 @property |
| 642 def project_name(self): | 645 def project_name(self): |
| 643 return self.checkout.project_name | 646 return self.checkout.project_name |
| 644 | 647 |
| 645 @property | 648 @property |
| 646 def project_path(self): | 649 def project_path(self): |
| 647 return self.checkout.project_path | 650 return self.checkout.project_path |
| OLD | NEW |