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 |
| 11 import re | 11 import re |
| 12 import sys | 12 import sys |
| 13 import tempfile | |
| 13 import threading | 14 import threading |
| 14 import time | 15 import time |
| 15 | 16 |
| 16 import gclient_utils | 17 import gclient_utils |
| 17 import scm | 18 import scm |
| 18 import subprocess2 | 19 import subprocess2 |
| 19 | 20 |
| 20 | 21 |
| 21 THIS_FILE_PATH = os.path.abspath(__file__) | 22 THIS_FILE_PATH = os.path.abspath(__file__) |
| 22 | 23 |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 if revision.startswith('refs/'): | 344 if revision.startswith('refs/'): |
| 344 rev_type = "branch" | 345 rev_type = "branch" |
| 345 elif revision.startswith('origin/'): | 346 elif revision.startswith('origin/'): |
| 346 # For compatability with old naming, translate 'origin' to 'refs/heads' | 347 # For compatability with old naming, translate 'origin' to 'refs/heads' |
| 347 revision = revision.replace('origin/', 'refs/heads/') | 348 revision = revision.replace('origin/', 'refs/heads/') |
| 348 rev_type = "branch" | 349 rev_type = "branch" |
| 349 else: | 350 else: |
| 350 # hash is also a tag, only make a distinction at checkout | 351 # hash is also a tag, only make a distinction at checkout |
| 351 rev_type = "hash" | 352 rev_type = "hash" |
| 352 | 353 |
| 353 if not os.path.exists(self.checkout_path) or ( | 354 if (not os.path.exists(self.checkout_path) or |
| 354 os.path.isdir(self.checkout_path) and | 355 (os.path.isdir(self.checkout_path) and |
| 355 not os.listdir(self.checkout_path)): | 356 not os.path.exists(os.path.join(self.checkout_path, '.git')))): |
| 356 gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path)) | |
| 357 self._Clone(revision, url, options) | 357 self._Clone(revision, url, options) |
| 358 self.UpdateSubmoduleConfig() | 358 self.UpdateSubmoduleConfig() |
| 359 if file_list is not None: | 359 if file_list is not None: |
| 360 files = self._Capture(['ls-files']).splitlines() | 360 files = self._Capture(['ls-files']).splitlines() |
| 361 file_list.extend([os.path.join(self.checkout_path, f) for f in files]) | 361 file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 362 if not verbose: | 362 if not verbose: |
| 363 # Make the output a little prettier. It's nice to have some whitespace | 363 # Make the output a little prettier. It's nice to have some whitespace |
| 364 # between projects when cloning. | 364 # between projects when cloning. |
| 365 print('') | 365 print('') |
| 366 return | 366 return |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 self._FetchAndReset(revision, file_list, options) | 401 self._FetchAndReset(revision, file_list, options) |
| 402 return_early = True | 402 return_early = True |
| 403 | 403 |
| 404 # Need to do this in the normal path as well as in the post-remote-switch | 404 # Need to do this in the normal path as well as in the post-remote-switch |
| 405 # path. | 405 # path. |
| 406 self._PossiblySwitchCache(url, options) | 406 self._PossiblySwitchCache(url, options) |
| 407 | 407 |
| 408 if return_early: | 408 if return_early: |
| 409 return | 409 return |
| 410 | 410 |
| 411 if not self._IsValidGitRepo(): | |
| 412 # .git directory is hosed for some reason, set it back up. | |
| 413 print('_____ %s/.git is corrupted, rebuilding' % self.relpath) | |
| 414 self._Run(['init'], options) | |
| 415 self._Run(['remote', 'set-url', 'origin', url], options) | |
| 416 | |
| 417 if not self._HasHead(): | |
| 418 # Previous checkout was aborted before branches could be created in repo, | |
| 419 # so we need to reconstruct them here. | |
| 420 self._Run(['-c', 'core.deltaBaseCacheLimit=2g', 'pull', 'origin', | |
| 421 'master'], options) | |
| 422 self._FetchAndReset(revision, file_list, options) | |
| 423 | |
| 424 cur_branch = self._GetCurrentBranch() | 411 cur_branch = self._GetCurrentBranch() |
| 425 | 412 |
| 426 # Cases: | 413 # Cases: |
| 427 # 0) HEAD is detached. Probably from our initial clone. | 414 # 0) HEAD is detached. Probably from our initial clone. |
| 428 # - make sure HEAD is contained by a named ref, then update. | 415 # - make sure HEAD is contained by a named ref, then update. |
| 429 # Cases 1-4. HEAD is a branch. | 416 # Cases 1-4. HEAD is a branch. |
| 430 # 1) current branch is not tracking a remote branch (could be git-svn) | 417 # 1) current branch is not tracking a remote branch (could be git-svn) |
| 431 # - try to rebase onto the new hash or branch | 418 # - try to rebase onto the new hash or branch |
| 432 # 2) current branch is tracking a remote branch with local committed | 419 # 2) current branch is tracking a remote branch with local committed |
| 433 # changes, but the DEPS file switched to point to a hash | 420 # changes, but the DEPS file switched to point to a hash |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 852 revision is a branch head. If it is a tag or a specific commit, then we | 839 revision is a branch head. If it is a tag or a specific commit, then we |
| 853 leave HEAD detached as it makes future updates simpler -- in this case the | 840 leave HEAD detached as it makes future updates simpler -- in this case the |
| 854 user should first create a new branch or switch to an existing branch before | 841 user should first create a new branch or switch to an existing branch before |
| 855 making changes in the repo.""" | 842 making changes in the repo.""" |
| 856 if not options.verbose: | 843 if not options.verbose: |
| 857 # git clone doesn't seem to insert a newline properly before printing | 844 # git clone doesn't seem to insert a newline properly before printing |
| 858 # to stdout | 845 # to stdout |
| 859 print('') | 846 print('') |
| 860 template_path = os.path.join( | 847 template_path = os.path.join( |
| 861 os.path.dirname(THIS_FILE_PATH), 'git-templates') | 848 os.path.dirname(THIS_FILE_PATH), 'git-templates') |
| 862 clone_cmd = ['-c', 'core.deltaBaseCacheLimit=2g', 'clone', '--progress', | 849 clone_cmd = ['-c', 'core.deltaBaseCacheLimit=2g', 'clone', '--no-checkout', |
| 863 '--template=%s' % template_path] | 850 '--progress', '--template=%s' % template_path] |
| 864 if self.cache_dir: | 851 if self.cache_dir: |
| 865 clone_cmd.append('--shared') | 852 clone_cmd.append('--shared') |
| 866 if revision.startswith('refs/heads/'): | |
| 867 clone_cmd.extend(['-b', revision.replace('refs/heads/', '')]) | |
| 868 detach_head = False | |
| 869 else: | |
| 870 detach_head = True | |
| 871 if options.verbose: | 853 if options.verbose: |
| 872 clone_cmd.append('--verbose') | 854 clone_cmd.append('--verbose') |
| 873 clone_cmd.extend([url, self.checkout_path]) | 855 clone_cmd.append(url) |
| 874 | |
| 875 # If the parent directory does not exist, Git clone on Windows will not | 856 # If the parent directory does not exist, Git clone on Windows will not |
| 876 # create it, so we need to do it manually. | 857 # create it, so we need to do it manually. |
| 877 parent_dir = os.path.dirname(self.checkout_path) | 858 parent_dir = os.path.dirname(self.checkout_path) |
| 878 if not os.path.exists(parent_dir): | 859 gclient_utils.safe_makedirs(parent_dir) |
| 879 gclient_utils.safe_makedirs(parent_dir) | 860 tmp_dir = tempfile.mkdtemp( |
| 880 | 861 prefix='_gclient_%s_' % os.path.basename(self.checkout_path), |
| 881 for _ in range(3): | 862 dir=parent_dir) |
| 882 try: | 863 try: |
| 883 self._Run(clone_cmd, options, cwd=self._root_dir, git_filter=True) | 864 clone_cmd.append(tmp_dir) |
| 884 break | 865 for _ in range(3): |
| 885 except subprocess2.CalledProcessError, e: | 866 try: |
| 886 # Too bad we don't have access to the actual output yet. | 867 self._Run(clone_cmd, options, cwd=self._root_dir, git_filter=True) |
| 887 # We should check for "transfer closed with NNN bytes remaining to | 868 break |
| 888 # read". In the meantime, just make sure .git exists. | 869 except subprocess2.CalledProcessError, e: |
| 889 if (e.returncode == 128 and | 870 # Too bad we don't have access to the actual output yet. |
| 890 os.path.exists(os.path.join(self.checkout_path, '.git'))): | 871 # We should check for "transfer closed with NNN bytes remaining to |
| 891 print(str(e)) | 872 # read". In the meantime, just make sure .git exists. |
| 892 print('Retrying...') | 873 if (e.returncode == 128 and |
| 893 continue | 874 os.path.exists(os.path.join(tmp_dir, '.git'))): |
| 894 raise e | 875 print(str(e)) |
| 895 | 876 print('Retrying...') |
|
iannucci
2013/07/20 00:45:37
I'm meaning we should rmtree(tmp_dir) before retry
Isaac (away)
2013/07/20 00:57:54
Done.
| |
| 896 # Update the "branch-heads" remote-tracking branches, since we might need it | 877 continue |
| 897 # to checkout a specific revision below. | 878 raise e |
| 898 self._UpdateBranchHeads(options, fetch=True) | 879 gclient_utils.safe_makedirs(self.checkout_path) |
| 899 | 880 os.rename(os.path.join(tmp_dir, '.git'), |
| 900 if detach_head: | 881 os.path.join(self.checkout_path, '.git')) |
| 882 finally: | |
| 883 if os.listdir(tmp_dir): | |
| 884 print('\n_____ removing non-empty tmp dir %s' % tmp_dir) | |
| 885 gclient_utils.rmtree(tmp_dir) | |
| 886 if revision.startswith('refs/heads/'): | |
| 887 self._Run( | |
| 888 ['checkout', '--quiet', revision.replace('refs/heads/', '')], options) | |
| 889 else: | |
| 901 # Squelch git's very verbose detached HEAD warning and use our own | 890 # Squelch git's very verbose detached HEAD warning and use our own |
| 902 self._Capture(['checkout', '--quiet', '%s' % revision]) | 891 self._Run(['checkout', '--quiet', revision], options) |
| 903 print( | 892 print( |
| 904 ('Checked out %s to a detached HEAD. Before making any commits\n' | 893 ('Checked out %s to a detached HEAD. Before making any commits\n' |
| 905 'in this repo, you should use \'git checkout <branch>\' to switch to\n' | 894 'in this repo, you should use \'git checkout <branch>\' to switch to\n' |
| 906 'an existing branch or use \'git checkout origin -b <branch>\' to\n' | 895 'an existing branch or use \'git checkout origin -b <branch>\' to\n' |
| 907 'create a new branch for your work.') % revision) | 896 'create a new branch for your work.') % revision) |
| 908 | 897 |
| 909 def _AttemptRebase(self, upstream, files, options, newbase=None, | 898 def _AttemptRebase(self, upstream, files, options, newbase=None, |
| 910 branch=None, printed_path=False): | 899 branch=None, printed_path=False): |
| 911 """Attempt to rebase onto either upstream or, if specified, newbase.""" | 900 """Attempt to rebase onto either upstream or, if specified, newbase.""" |
| 912 if files is not None: | 901 if files is not None: |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 970 "manually.\ncd %s && git " % | 959 "manually.\ncd %s && git " % |
| 971 self.checkout_path | 960 self.checkout_path |
| 972 + "%s" % ' '.join(rebase_cmd)) | 961 + "%s" % ' '.join(rebase_cmd)) |
| 973 | 962 |
| 974 print(rebase_output.strip()) | 963 print(rebase_output.strip()) |
| 975 if not options.verbose: | 964 if not options.verbose: |
| 976 # Make the output a little prettier. It's nice to have some | 965 # Make the output a little prettier. It's nice to have some |
| 977 # whitespace between projects when syncing. | 966 # whitespace between projects when syncing. |
| 978 print('') | 967 print('') |
| 979 | 968 |
| 980 def _IsValidGitRepo(self): | |
| 981 """Returns if the directory is a valid git repository. | |
| 982 | |
| 983 Checks if git status works. | |
| 984 """ | |
| 985 try: | |
| 986 self._Capture(['status']) | |
| 987 return True | |
| 988 except subprocess2.CalledProcessError: | |
| 989 return False | |
| 990 | |
| 991 def _HasHead(self): | 969 def _HasHead(self): |
| 992 """Returns True if any commit is checked out. | 970 """Returns True if any commit is checked out. |
| 993 | 971 |
| 994 This is done by checking if rev-parse HEAD works in the current repository. | 972 This is done by checking if rev-parse HEAD works in the current repository. |
| 995 """ | 973 """ |
| 996 try: | 974 try: |
| 997 self._GetCurrentBranch() | 975 self._GetCurrentBranch() |
| 998 return True | 976 return True |
| 999 except subprocess2.CalledProcessError: | 977 except subprocess2.CalledProcessError: |
| 1000 return False | 978 return False |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1506 new_command.append('--force') | 1484 new_command.append('--force') |
| 1507 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1485 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1508 new_command.extend(('--accept', 'theirs-conflict')) | 1486 new_command.extend(('--accept', 'theirs-conflict')) |
| 1509 elif options.manually_grab_svn_rev: | 1487 elif options.manually_grab_svn_rev: |
| 1510 new_command.append('--force') | 1488 new_command.append('--force') |
| 1511 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1489 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1512 new_command.extend(('--accept', 'postpone')) | 1490 new_command.extend(('--accept', 'postpone')) |
| 1513 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: | 1491 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1514 new_command.extend(('--accept', 'postpone')) | 1492 new_command.extend(('--accept', 'postpone')) |
| 1515 return new_command | 1493 return new_command |
| OLD | NEW |