Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Gclient-specific SCM-specific operations.""" | 5 """Gclient-specific SCM-specific operations.""" |
| 6 | 6 |
| 7 import collections | 7 import collections |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import posixpath | 10 import posixpath |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 ['git', 'config', 'remote.%s.gclient-auto-fix-url' % self.remote], | 345 ['git', 'config', 'remote.%s.gclient-auto-fix-url' % self.remote], |
| 346 cwd=self.checkout_path).strip() != 'False'): | 346 cwd=self.checkout_path).strip() != 'False'): |
| 347 print('_____ switching %s to a new upstream' % self.relpath) | 347 print('_____ switching %s to a new upstream' % self.relpath) |
| 348 # Make sure it's clean | 348 # Make sure it's clean |
| 349 self._CheckClean(rev_str) | 349 self._CheckClean(rev_str) |
| 350 # Switch over to the new upstream | 350 # Switch over to the new upstream |
| 351 self._Run(['remote', 'set-url', self.remote, url], options) | 351 self._Run(['remote', 'set-url', self.remote, url], options) |
| 352 self._FetchAndReset(revision, file_list, options) | 352 self._FetchAndReset(revision, file_list, options) |
| 353 return_early = True | 353 return_early = True |
| 354 | 354 |
| 355 # Need to do this in the normal path as well as in the post-remote-switch | |
| 356 # path. | |
| 357 self._PossiblySwitchCache(url, options) | |
| 358 | |
| 359 if return_early: | 355 if return_early: |
| 360 return self._Capture(['rev-parse', '--verify', 'HEAD']) | 356 return self._Capture(['rev-parse', '--verify', 'HEAD']) |
| 361 | 357 |
| 362 cur_branch = self._GetCurrentBranch() | 358 cur_branch = self._GetCurrentBranch() |
| 363 | 359 |
| 364 # Cases: | 360 # Cases: |
| 365 # 0) HEAD is detached. Probably from our initial clone. | 361 # 0) HEAD is detached. Probably from our initial clone. |
| 366 # - make sure HEAD is contained by a named ref, then update. | 362 # - make sure HEAD is contained by a named ref, then update. |
| 367 # Cases 1-4. HEAD is a branch. | 363 # Cases 1-4. HEAD is a branch. |
| 368 # 1) current branch is not tracking a remote branch (could be git-svn) | 364 # 1) current branch is not tracking a remote branch (could be git-svn) |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 674 '#Initial_checkout' ) % rev) | 670 '#Initial_checkout' ) % rev) |
| 675 | 671 |
| 676 return sha1 | 672 return sha1 |
| 677 | 673 |
| 678 def FullUrlForRelativeUrl(self, url): | 674 def FullUrlForRelativeUrl(self, url): |
| 679 # Strip from last '/' | 675 # Strip from last '/' |
| 680 # Equivalent to unix basename | 676 # Equivalent to unix basename |
| 681 base_url = self.url | 677 base_url = self.url |
| 682 return base_url[:base_url.rfind('/')] + url | 678 return base_url[:base_url.rfind('/')] + url |
| 683 | 679 |
| 684 @staticmethod | |
| 685 def _NormalizeGitURL(url): | |
| 686 '''Takes a git url, strips the scheme, and ensures it ends with '.git'.''' | |
| 687 idx = url.find('://') | |
| 688 if idx != -1: | |
| 689 url = url[idx+3:] | |
| 690 if not url.endswith('.git'): | |
| 691 url += '.git' | |
| 692 return url | |
| 693 | |
| 694 def _PossiblySwitchCache(self, url, options): | |
| 695 """Handles switching a repo from with-cache to direct, or vice versa. | |
| 696 | |
| 697 When we go from direct to with-cache, the remote url changes from the | |
| 698 'real' url to the local file url (in cache_dir). Therefore, this function | |
| 699 assumes that |url| points to the correctly-switched-over local file url, if | |
| 700 we're in cache_mode. | |
| 701 | |
| 702 When we go from with-cache to direct, assume that the normal url-switching | |
| 703 code already flipped the remote over, and we just need to repack and break | |
| 704 the dependency to the cache. | |
| 705 """ | |
| 706 | |
| 707 altfile = os.path.join( | |
| 708 self.checkout_path, '.git', 'objects', 'info', 'alternates') | |
| 709 if self.cache_dir: | |
| 710 if not os.path.exists(altfile): | |
| 711 try: | |
| 712 with open(altfile, 'w') as f: | |
| 713 f.write(os.path.join(url, 'objects')) | |
| 714 # pylint: disable=C0301 | |
| 715 # This dance is necessary according to emperical evidence, also at: | |
| 716 # http://lists-archives.com/git/713652-retrospectively-add-alternates- to-a-repository.html | |
| 717 self._Run(['repack', '-ad'], options) | |
| 718 self._Run(['repack', '-adl'], options) | |
| 719 except Exception: | |
| 720 # If something goes wrong, try to remove the altfile so we'll go down | |
| 721 # this path again next time. | |
| 722 try: | |
| 723 os.remove(altfile) | |
| 724 except OSError as e: | |
| 725 print >> sys.stderr, "FAILED: os.remove('%s') -> %s" % (altfile, e) | |
| 726 raise | |
| 727 else: | |
| 728 if os.path.exists(altfile): | |
| 729 self._Run(['repack', '-a'], options) | |
| 730 os.remove(altfile) | |
| 731 | |
| 732 def _CreateOrUpdateCache(self, url, options): | 680 def _CreateOrUpdateCache(self, url, options): |
| 733 """Make a new git mirror or update existing mirror for |url|, and return the | 681 """Make a new git mirror or update existing mirror for |url|, and return the |
| 734 mirror URI to clone from. | 682 mirror URI to clone from. |
| 735 | 683 |
| 736 If no cache-dir is specified, just return |url| unchanged. | 684 If no cache-dir is specified, just return |url| unchanged. |
| 737 """ | 685 """ |
| 738 if not self.cache_dir: | 686 if not self.cache_dir: |
| 739 return url | 687 return url |
| 740 | |
| 741 # Replace - with -- to avoid ambiguity. / with - to flatten folder structure | |
| 742 folder = os.path.join( | |
| 743 self.cache_dir, | |
| 744 self._NormalizeGitURL(url).replace('-', '--').replace('/', '-')) | |
| 745 altfile = os.path.join(folder, 'objects', 'info', 'alternates') | |
| 746 | |
| 747 # If we're bringing an old cache up to date or cloning a new cache, and the | |
| 748 # existing repo is currently a direct clone, use its objects to help out | |
| 749 # the fetch here. | |
| 750 checkout_objects = os.path.join(self.checkout_path, '.git', 'objects') | |
| 751 checkout_altfile = os.path.join(checkout_objects, 'info', 'alternates') | |
| 752 use_reference = ( | |
| 753 os.path.exists(checkout_objects) and | |
| 754 not os.path.exists(checkout_altfile)) | |
| 755 | |
| 756 v = ['-v'] if options.verbose else [] | 688 v = ['-v'] if options.verbose else [] |
| 757 filter_fn = lambda l: '[up to date]' not in l | 689 self._Run(['cache', 'populate'] + v + |
| 758 with self.cache_locks[folder]: | 690 ['--shallow', '--cache-dir', self.cache_dir, url], |
| 759 gclient_utils.safe_makedirs(self.cache_dir) | 691 options, cwd=self._root_dir, retry=True) |
| 760 if not os.path.exists(os.path.join(folder, 'config')): | 692 git_bin = 'git.bat' if sys.platform.startswith('win') else 'git' |
| 761 gclient_utils.rmtree(folder) | 693 return subprocess2.check_output([git_bin, 'cache', 'exists', '--cache-dir', |
|
agable
2014/02/26 22:43:12
could also make _Run able to return output?
Ryan Tseng
2014/02/27 23:31:40
Done.
| |
| 762 cmd = ['clone'] + v + ['-c', 'core.deltaBaseCacheLimit=2g', | 694 self.cache_dir, url]).strip() |
| 763 '--progress', '--bare'] | |
| 764 | |
| 765 if use_reference: | |
| 766 cmd += ['--reference', os.path.abspath(self.checkout_path)] | |
| 767 | |
| 768 self._Run(cmd + [url, folder], | |
| 769 options, filter_fn=filter_fn, cwd=self.cache_dir, retry=True) | |
| 770 else: | |
| 771 # For now, assert that host/path/to/repo.git is identical. We may want | |
| 772 # to relax this restriction in the future to allow for smarter cache | |
| 773 # repo update schemes (such as pulling the same repo, but from a | |
| 774 # different host). | |
| 775 existing_url = self._Capture(['config', 'remote.%s.url' % self.remote], | |
| 776 cwd=folder) | |
| 777 assert self._NormalizeGitURL(existing_url) == self._NormalizeGitURL(url) | |
| 778 | |
| 779 if use_reference: | |
| 780 with open(altfile, 'w') as f: | |
| 781 f.write(os.path.abspath(checkout_objects)) | |
| 782 | |
| 783 # Would normally use `git remote update`, but it doesn't support | |
| 784 # --progress, so use fetch instead. | |
| 785 self._Run(['fetch'] + v + ['--multiple', '--progress', '--all'], | |
| 786 options, filter_fn=filter_fn, cwd=folder, retry=True) | |
| 787 | |
| 788 # If the clone has an object dependency on the existing repo, break it | |
| 789 # with repack and remove the linkage. | |
| 790 if os.path.exists(altfile): | |
| 791 self._Run(['repack', '-a'], options, cwd=folder) | |
| 792 os.remove(altfile) | |
| 793 return folder | |
| 794 | 695 |
| 795 def _Clone(self, revision, url, options): | 696 def _Clone(self, revision, url, options): |
| 796 """Clone a git repository from the given URL. | 697 """Clone a git repository from the given URL. |
| 797 | 698 |
| 798 Once we've cloned the repo, we checkout a working branch if the specified | 699 Once we've cloned the repo, we checkout a working branch if the specified |
| 799 revision is a branch head. If it is a tag or a specific commit, then we | 700 revision is a branch head. If it is a tag or a specific commit, then we |
| 800 leave HEAD detached as it makes future updates simpler -- in this case the | 701 leave HEAD detached as it makes future updates simpler -- in this case the |
| 801 user should first create a new branch or switch to an existing branch before | 702 user should first create a new branch or switch to an existing branch before |
| 802 making changes in the repo.""" | 703 making changes in the repo.""" |
| 803 if not options.verbose: | 704 if not options.verbose: |
| (...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1506 new_command.append('--force') | 1407 new_command.append('--force') |
| 1507 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1408 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1508 new_command.extend(('--accept', 'theirs-conflict')) | 1409 new_command.extend(('--accept', 'theirs-conflict')) |
| 1509 elif options.manually_grab_svn_rev: | 1410 elif options.manually_grab_svn_rev: |
| 1510 new_command.append('--force') | 1411 new_command.append('--force') |
| 1511 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1412 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1512 new_command.extend(('--accept', 'postpone')) | 1413 new_command.extend(('--accept', 'postpone')) |
| 1513 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1414 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1514 new_command.extend(('--accept', 'postpone')) | 1415 new_command.extend(('--accept', 'postpone')) |
| 1515 return new_command | 1416 return new_command |
| OLD | NEW |