| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 import logging | 5 import logging |
| 6 import os | 6 import os |
| 7 from urlparse import urlparse | 7 from urlparse import urlparse |
| 8 import subprocess | 8 import subprocess |
| 9 import threading | 9 import threading |
| 10 | 10 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 75 |
| 76 parsed_url = urlparse(repo_url) | 76 parsed_url = urlparse(repo_url) |
| 77 self._host = parsed_url.netloc | 77 self._host = parsed_url.netloc |
| 78 # Remove the / in the front of path. | 78 # Remove the / in the front of path. |
| 79 self._repo_path = parsed_url.path[1:] | 79 self._repo_path = parsed_url.path[1:] |
| 80 | 80 |
| 81 self._CloneOrUpdateRepoIfNeeded() | 81 self._CloneOrUpdateRepoIfNeeded() |
| 82 | 82 |
| 83 def _CloneOrUpdateRepoIfNeeded(self): | 83 def _CloneOrUpdateRepoIfNeeded(self): |
| 84 """Clones repo, or update it if it didn't got updated before.""" | 84 """Clones repo, or update it if it didn't got updated before.""" |
| 85 if self.repo_url in LocalGitRepository._updated_repos: | 85 with LocalGitRepository.lock: |
| 86 return | 86 if self.repo_url in LocalGitRepository._updated_repos: |
| 87 return |
| 87 | 88 |
| 88 with LocalGitRepository.lock: | |
| 89 # Clone the repo if needed. | 89 # Clone the repo if needed. |
| 90 if not os.path.exists(self.real_repo_path): | 90 if not os.path.exists(self.real_repo_path): |
| 91 try: | 91 try: |
| 92 subprocess.check_call(['git', 'clone', | 92 subprocess.check_call(['git', 'clone', |
| 93 self.repo_url, self.real_repo_path]) | 93 self.repo_url, self.real_repo_path]) |
| 94 except subprocess.CalledProcessError as e: # pragma: no cover. | 94 except subprocess.CalledProcessError as e: # pragma: no cover. |
| 95 logging.error('Exception while cloning %s: %s', self.repo_url, e) | 95 raise Exception( |
| 96 return | 96 'Exception while cloning %s: %s' % (self.repo_url, e)) |
| 97 # Update repo if it's already cloned. | 97 # Update repo if it's already cloned. |
| 98 else: | 98 else: |
| 99 try: | 99 try: |
| 100 # Disable verbose of cd and git pull. | 100 # Disable verbose of cd and git pull. |
| 101 with open(os.devnull, 'w') as null_handle: | 101 with open(os.devnull, 'w') as null_handle: |
| 102 subprocess.check_call( | 102 subprocess.check_call( |
| 103 'cd %s && git pull' % self.real_repo_path, | 103 'cd %s && git pull' % self.real_repo_path, |
| 104 stdout=null_handle, stderr=null_handle, shell=True) | 104 stdout=null_handle, stderr=null_handle, shell=True) |
| 105 except subprocess.CalledProcessError as e: # pragma: no cover. | 105 except subprocess.CalledProcessError as e: # pragma: no cover. |
| 106 logging.error('Exception while updating %s: %s', self.repo_path, e) | 106 raise Exception( |
| 107 return | 107 'Exception while updating %s: %s' % (self.repo_path, e)) |
| 108 | 108 |
| 109 LocalGitRepository._updated_repos.add(self.repo_url) | 109 LocalGitRepository._updated_repos.add(self.repo_url) |
| 110 | 110 |
| 111 def _GetFinalCommand(self, command, utc=False): | 111 def _GetFinalCommand(self, command, utc=False): |
| 112 # Change local time to utc time. | 112 # Change local time to utc time. |
| 113 if utc: | 113 if utc: |
| 114 command = 'TZ=UTC %s --date=format-local:"%s"' % ( | 114 command = 'TZ=UTC %s --date=format-local:"%s"' % ( |
| 115 command, local_git_parsers.DATETIME_FORMAT) | 115 command, local_git_parsers.DATETIME_FORMAT) |
| 116 return 'cd %s && %s' % (self.real_repo_path, command) | 116 return 'cd %s && %s' % (self.real_repo_path, command) |
| 117 | 117 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 136 """Returns the diff of the given revision.""" | 136 """Returns the diff of the given revision.""" |
| 137 command = ('git log --format="" --max-count=1 %s' % | 137 command = ('git log --format="" --max-count=1 %s' % |
| 138 ConvertRemoteCommitToLocal(revision)) | 138 ConvertRemoteCommitToLocal(revision)) |
| 139 if path: | 139 if path: |
| 140 command += ' -p %s' % path | 140 command += ' -p %s' % path |
| 141 output = script_util.GetCommandOutput(self._GetFinalCommand(command)) | 141 output = script_util.GetCommandOutput(self._GetFinalCommand(command)) |
| 142 return self.diff_parser(output) | 142 return self.diff_parser(output) |
| 143 | 143 |
| 144 def GetBlame(self, path, revision): | 144 def GetBlame(self, path, revision): |
| 145 """Returns blame of the file at ``path`` of the given revision.""" | 145 """Returns blame of the file at ``path`` of the given revision.""" |
| 146 command = 'git blame --incremental %s %s' % ( | 146 command = 'git blame --incremental %s -- %s' % ( |
| 147 path, ConvertRemoteCommitToLocal(revision)) | 147 ConvertRemoteCommitToLocal(revision), path) |
| 148 output = script_util.GetCommandOutput(self._GetFinalCommand(command)) | 148 output = script_util.GetCommandOutput(self._GetFinalCommand(command)) |
| 149 return self.blame_parser(output, path, revision) | 149 return self.blame_parser(output, path, revision) |
| 150 | 150 |
| 151 def GetSource(self, path, revision): | 151 def GetSource(self, path, revision): |
| 152 """Returns source code of the file at ``path`` of the given revision.""" | 152 """Returns source code of the file at ``path`` of the given revision.""" |
| 153 # Check whether the requested file exist or not. | 153 # Check whether the requested file exist or not. |
| 154 command = 'git show %s:%s' % (ConvertRemoteCommitToLocal(revision), path) | 154 command = 'git show %s:%s' % (ConvertRemoteCommitToLocal(revision), path) |
| 155 output = script_util.GetCommandOutput(self._GetFinalCommand(command)) | 155 output = script_util.GetCommandOutput(self._GetFinalCommand(command)) |
| 156 return output | 156 return output |
| OLD | NEW |