maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 1 | # coding=utf8 |
maruel@chromium.org | 9799a07 | 2012-01-11 00:26:25 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Manages a project checkout. |
| 6 | |
agable | c972d18 | 2016-10-24 16:59:13 -0700 | [diff] [blame^] | 7 | Includes support only for git. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 8 | """ |
| 9 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 10 | import fnmatch |
| 11 | import logging |
| 12 | import os |
| 13 | import re |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 14 | import shutil |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 15 | import subprocess |
| 16 | import sys |
| 17 | import tempfile |
| 18 | |
vapier | 9f34371 | 2016-06-22 07:13:20 -0700 | [diff] [blame] | 19 | # The configparser module was renamed in Python 3. |
| 20 | try: |
| 21 | import configparser |
| 22 | except ImportError: |
| 23 | import ConfigParser as configparser |
| 24 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 25 | import patch |
| 26 | import scm |
| 27 | import subprocess2 |
| 28 | |
| 29 | |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 30 | if sys.platform in ('cygwin', 'win32'): |
| 31 | # Disable timeouts on Windows since we can't have shells with timeouts. |
| 32 | GLOBAL_TIMEOUT = None |
| 33 | FETCH_TIMEOUT = None |
| 34 | else: |
| 35 | # Default timeout of 15 minutes. |
| 36 | GLOBAL_TIMEOUT = 15*60 |
| 37 | # Use a larger timeout for checkout since it can be a genuinely slower |
| 38 | # operation. |
| 39 | FETCH_TIMEOUT = 30*60 |
| 40 | |
| 41 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 42 | def get_code_review_setting(path, key, |
| 43 | codereview_settings_file='codereview.settings'): |
| 44 | """Parses codereview.settings and return the value for the key if present. |
| 45 | |
| 46 | Don't cache the values in case the file is changed.""" |
| 47 | # TODO(maruel): Do not duplicate code. |
| 48 | settings = {} |
| 49 | try: |
| 50 | settings_file = open(os.path.join(path, codereview_settings_file), 'r') |
| 51 | try: |
| 52 | for line in settings_file.readlines(): |
| 53 | if not line or line.startswith('#'): |
| 54 | continue |
| 55 | if not ':' in line: |
| 56 | # Invalid file. |
| 57 | return None |
| 58 | k, v = line.split(':', 1) |
| 59 | settings[k.strip()] = v.strip() |
| 60 | finally: |
| 61 | settings_file.close() |
maruel@chromium.org | 004fb71 | 2011-06-21 20:02:16 +0000 | [diff] [blame] | 62 | except IOError: |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 63 | return None |
| 64 | return settings.get(key, None) |
| 65 | |
| 66 | |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 67 | def align_stdout(stdout): |
| 68 | """Returns the aligned output of multiple stdouts.""" |
| 69 | output = '' |
| 70 | for item in stdout: |
| 71 | item = item.strip() |
| 72 | if not item: |
| 73 | continue |
| 74 | output += ''.join(' %s\n' % line for line in item.splitlines()) |
| 75 | return output |
| 76 | |
| 77 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 78 | class PatchApplicationFailed(Exception): |
| 79 | """Patch failed to be applied.""" |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 80 | def __init__(self, p, status): |
| 81 | super(PatchApplicationFailed, self).__init__(p, status) |
| 82 | self.patch = p |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 83 | self.status = status |
| 84 | |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 85 | @property |
| 86 | def filename(self): |
| 87 | if self.patch: |
| 88 | return self.patch.filename |
| 89 | |
| 90 | def __str__(self): |
| 91 | out = [] |
| 92 | if self.filename: |
| 93 | out.append('Failed to apply patch for %s:' % self.filename) |
| 94 | if self.status: |
| 95 | out.append(self.status) |
maruel@chromium.org | cb5667a | 2012-10-23 19:42:10 +0000 | [diff] [blame] | 96 | if self.patch: |
| 97 | out.append('Patch: %s' % self.patch.dump()) |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 98 | return '\n'.join(out) |
| 99 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 100 | |
| 101 | class CheckoutBase(object): |
| 102 | # Set to None to have verbose output. |
| 103 | VOID = subprocess2.VOID |
| 104 | |
maruel@chromium.org | 6ed8b50 | 2011-06-12 01:05:35 +0000 | [diff] [blame] | 105 | def __init__(self, root_dir, project_name, post_processors): |
| 106 | """ |
| 107 | Args: |
| 108 | post_processor: list of lambda(checkout, patches) to call on each of the |
| 109 | modified files. |
| 110 | """ |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 111 | super(CheckoutBase, self).__init__() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 112 | self.root_dir = root_dir |
| 113 | self.project_name = project_name |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 114 | if self.project_name is None: |
| 115 | self.project_path = self.root_dir |
| 116 | else: |
| 117 | self.project_path = os.path.join(self.root_dir, self.project_name) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 118 | # Only used for logging purposes. |
| 119 | self._last_seen_revision = None |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 120 | self.post_processors = post_processors |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 121 | assert self.root_dir |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 122 | assert self.project_path |
maruel@chromium.org | 0aca0f9 | 2012-10-01 16:39:45 +0000 | [diff] [blame] | 123 | assert os.path.isabs(self.project_path) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 124 | |
| 125 | def get_settings(self, key): |
| 126 | return get_code_review_setting(self.project_path, key) |
| 127 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 128 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 129 | """Checks out a clean copy of the tree and removes any local modification. |
| 130 | |
| 131 | This function shouldn't throw unless the remote repository is inaccessible, |
| 132 | there is no free disk space or hard issues like that. |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 133 | |
| 134 | Args: |
| 135 | revision: The revision it should sync to, SCM specific. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 136 | """ |
| 137 | raise NotImplementedError() |
| 138 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 139 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 140 | """Applies a patch and returns the list of modified files. |
| 141 | |
| 142 | This function should throw patch.UnsupportedPatchFormat or |
| 143 | PatchApplicationFailed when relevant. |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 144 | |
| 145 | Args: |
| 146 | patches: patch.PatchSet object. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 147 | """ |
| 148 | raise NotImplementedError() |
| 149 | |
| 150 | def commit(self, commit_message, user): |
| 151 | """Commits the patch upstream, while impersonating 'user'.""" |
| 152 | raise NotImplementedError() |
| 153 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 154 | def revisions(self, rev1, rev2): |
| 155 | """Returns the count of revisions from rev1 to rev2, e.g. len(]rev1, rev2]). |
| 156 | |
| 157 | If rev2 is None, it means 'HEAD'. |
| 158 | |
| 159 | Returns None if there is no link between the two. |
| 160 | """ |
| 161 | raise NotImplementedError() |
| 162 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 163 | |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 164 | class GitCheckout(CheckoutBase): |
| 165 | """Manages a git checkout.""" |
| 166 | def __init__(self, root_dir, project_name, remote_branch, git_url, |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 167 | commit_user, post_processors=None): |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 168 | super(GitCheckout, self).__init__(root_dir, project_name, post_processors) |
| 169 | self.git_url = git_url |
| 170 | self.commit_user = commit_user |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 171 | self.remote_branch = remote_branch |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 172 | # The working branch where patches will be applied. It will track the |
| 173 | # remote branch. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 174 | self.working_branch = 'working_branch' |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 175 | # There is no reason to not hardcode origin. |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 176 | self.remote = 'origin' |
| 177 | # There is no reason to not hardcode master. |
| 178 | self.master_branch = 'master' |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 179 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 180 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 181 | """Resets the git repository in a clean state. |
| 182 | |
| 183 | Checks it out if not present and deletes the working branch. |
| 184 | """ |
agable@chromium.org | 7dc1144 | 2014-03-12 22:37:32 +0000 | [diff] [blame] | 185 | assert self.remote_branch |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 186 | assert self.git_url |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 187 | |
| 188 | if not os.path.isdir(self.project_path): |
| 189 | # Clone the repo if the directory is not present. |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 190 | logging.info( |
| 191 | 'Checking out %s in %s', self.project_name, self.project_path) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 192 | self._check_call_git( |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 193 | ['clone', self.git_url, '-b', self.remote_branch, self.project_path], |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 194 | cwd=None, timeout=FETCH_TIMEOUT) |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 195 | else: |
| 196 | # Throw away all uncommitted changes in the existing checkout. |
| 197 | self._check_call_git(['checkout', self.remote_branch]) |
| 198 | self._check_call_git( |
| 199 | ['reset', '--hard', '--quiet', |
| 200 | '%s/%s' % (self.remote, self.remote_branch)]) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 201 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 202 | if revision: |
| 203 | try: |
| 204 | # Look if the commit hash already exist. If so, we can skip a |
| 205 | # 'git fetch' call. |
halton.huo@intel.com | 323ec37 | 2014-06-17 01:50:37 +0000 | [diff] [blame] | 206 | revision = self._check_output_git(['rev-parse', revision]).rstrip() |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 207 | except subprocess.CalledProcessError: |
| 208 | self._check_call_git( |
| 209 | ['fetch', self.remote, self.remote_branch, '--quiet']) |
halton.huo@intel.com | 323ec37 | 2014-06-17 01:50:37 +0000 | [diff] [blame] | 210 | revision = self._check_output_git(['rev-parse', revision]).rstrip() |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 211 | self._check_call_git(['checkout', '--force', '--quiet', revision]) |
| 212 | else: |
| 213 | branches, active = self._branches() |
| 214 | if active != self.master_branch: |
| 215 | self._check_call_git( |
| 216 | ['checkout', '--force', '--quiet', self.master_branch]) |
| 217 | self._sync_remote_branch() |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 218 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 219 | if self.working_branch in branches: |
| 220 | self._call_git(['branch', '-D', self.working_branch]) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 221 | return self._get_head_commit_hash() |
| 222 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 223 | def _sync_remote_branch(self): |
| 224 | """Syncs the remote branch.""" |
| 225 | # We do a 'git pull origin master:refs/remotes/origin/master' instead of |
hinoka@google.com | dabbea2 | 2014-04-21 23:58:11 +0000 | [diff] [blame] | 226 | # 'git pull origin master' because from the manpage for git-pull: |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 227 | # A parameter <ref> without a colon is equivalent to <ref>: when |
| 228 | # pulling/fetching, so it merges <ref> into the current branch without |
| 229 | # storing the remote branch anywhere locally. |
| 230 | remote_tracked_path = 'refs/remotes/%s/%s' % ( |
| 231 | self.remote, self.remote_branch) |
| 232 | self._check_call_git( |
| 233 | ['pull', self.remote, |
| 234 | '%s:%s' % (self.remote_branch, remote_tracked_path), |
| 235 | '--quiet']) |
| 236 | |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 237 | def _get_head_commit_hash(self): |
rmistry@google.com | 11145db | 2013-10-03 12:43:40 +0000 | [diff] [blame] | 238 | """Gets the current revision (in unicode) from the local branch.""" |
| 239 | return unicode(self._check_output_git(['rev-parse', 'HEAD']).strip()) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 240 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 241 | def apply_patch(self, patches, post_processors=None, verbose=False): |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 242 | """Applies a patch on 'working_branch' and switches to it. |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 243 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 244 | The changes remain staged on the current branch. |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 245 | """ |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 246 | post_processors = post_processors or self.post_processors or [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 247 | # It this throws, the checkout is corrupted. Maybe worth deleting it and |
| 248 | # trying again? |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 249 | if self.remote_branch: |
| 250 | self._check_call_git( |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 251 | ['checkout', '-b', self.working_branch, '-t', self.remote_branch, |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 252 | '--quiet']) |
| 253 | |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 254 | for index, p in enumerate(patches): |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 255 | stdout = [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 256 | try: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 257 | filepath = os.path.join(self.project_path, p.filename) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 258 | if p.is_delete: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 259 | if (not os.path.exists(filepath) and |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 260 | any(p1.source_filename == p.filename for p1 in patches[0:index])): |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 261 | # The file was already deleted if a prior patch with file rename |
| 262 | # was already processed because 'git apply' did it for us. |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 263 | pass |
| 264 | else: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 265 | stdout.append(self._check_output_git(['rm', p.filename])) |
phajdan.jr@chromium.org | d9eb69e | 2014-06-05 20:33:37 +0000 | [diff] [blame] | 266 | assert(not os.path.exists(filepath)) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 267 | stdout.append('Deleted.') |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 268 | else: |
| 269 | dirname = os.path.dirname(p.filename) |
| 270 | full_dir = os.path.join(self.project_path, dirname) |
| 271 | if dirname and not os.path.isdir(full_dir): |
| 272 | os.makedirs(full_dir) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 273 | stdout.append('Created missing directory %s.' % dirname) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 274 | if p.is_binary: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 275 | content = p.get() |
| 276 | with open(filepath, 'wb') as f: |
| 277 | f.write(content) |
| 278 | stdout.append('Added binary file %d bytes' % len(content)) |
| 279 | cmd = ['add', p.filename] |
| 280 | if verbose: |
| 281 | cmd.append('--verbose') |
| 282 | stdout.append(self._check_output_git(cmd)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 283 | else: |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 284 | # No need to do anything special with p.is_new or if not |
| 285 | # p.diff_hunks. git apply manages all that already. |
primiano@chromium.org | 49dfcde | 2014-09-23 08:14:39 +0000 | [diff] [blame] | 286 | cmd = ['apply', '--index', '-3', '-p%s' % p.patchlevel] |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 287 | if verbose: |
| 288 | cmd.append('--verbose') |
| 289 | stdout.append(self._check_output_git(cmd, stdin=p.get(True))) |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 290 | for post in post_processors: |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 291 | post(self, p) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 292 | if verbose: |
| 293 | print p.filename |
| 294 | print align_stdout(stdout) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 295 | except OSError, e: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 296 | raise PatchApplicationFailed(p, '%s%s' % (align_stdout(stdout), e)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 297 | except subprocess.CalledProcessError, e: |
| 298 | raise PatchApplicationFailed( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 299 | p, |
| 300 | 'While running %s;\n%s%s' % ( |
| 301 | ' '.join(e.cmd), |
| 302 | align_stdout(stdout), |
| 303 | align_stdout([getattr(e, 'stdout', '')]))) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 304 | found_files = self._check_output_git( |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 305 | ['diff', '--ignore-submodules', |
| 306 | '--name-only', '--staged']).splitlines(False) |
hinoka@chromium.org | dc6a1d0 | 2014-05-10 04:42:48 +0000 | [diff] [blame] | 307 | if sorted(patches.filenames) != sorted(found_files): |
| 308 | extra_files = sorted(set(found_files) - set(patches.filenames)) |
| 309 | unpatched_files = sorted(set(patches.filenames) - set(found_files)) |
| 310 | if extra_files: |
| 311 | print 'Found extra files: %r' % (extra_files,) |
| 312 | if unpatched_files: |
| 313 | print 'Found unpatched files: %r' % (unpatched_files,) |
| 314 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 315 | |
| 316 | def commit(self, commit_message, user): |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 317 | """Commits, updates the commit message and pushes.""" |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 318 | # TODO(hinoka): CQ no longer uses this, I think its deprecated. |
| 319 | # Delete this. |
rmistry@google.com | bb050f6 | 2013-10-03 16:53:54 +0000 | [diff] [blame] | 320 | assert self.commit_user |
maruel@chromium.org | 1bf5097 | 2011-05-05 19:57:21 +0000 | [diff] [blame] | 321 | assert isinstance(commit_message, unicode) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 322 | current_branch = self._check_output_git( |
| 323 | ['rev-parse', '--abbrev-ref', 'HEAD']).strip() |
| 324 | assert current_branch == self.working_branch |
hinoka@google.com | dabbea2 | 2014-04-21 23:58:11 +0000 | [diff] [blame] | 325 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 326 | commit_cmd = ['commit', '-m', commit_message] |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 327 | if user and user != self.commit_user: |
| 328 | # We do not have the first or last name of the user, grab the username |
| 329 | # from the email and call it the original author's name. |
| 330 | # TODO(rmistry): Do not need the below if user is already in |
| 331 | # "Name <email>" format. |
| 332 | name = user.split('@')[0] |
| 333 | commit_cmd.extend(['--author', '%s <%s>' % (name, user)]) |
| 334 | self._check_call_git(commit_cmd) |
| 335 | |
| 336 | # Push to the remote repository. |
| 337 | self._check_call_git( |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 338 | ['push', 'origin', '%s:%s' % (self.working_branch, self.remote_branch), |
agable@chromium.org | 3926228 | 2014-03-19 21:07:38 +0000 | [diff] [blame] | 339 | '--quiet']) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 340 | # Get the revision after the push. |
| 341 | revision = self._get_head_commit_hash() |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 342 | # Switch back to the remote_branch and sync it. |
| 343 | self._check_call_git(['checkout', self.remote_branch]) |
| 344 | self._sync_remote_branch() |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 345 | # Delete the working branch since we are done with it. |
| 346 | self._check_call_git(['branch', '-D', self.working_branch]) |
| 347 | |
| 348 | return revision |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 349 | |
| 350 | def _check_call_git(self, args, **kwargs): |
| 351 | kwargs.setdefault('cwd', self.project_path) |
| 352 | kwargs.setdefault('stdout', self.VOID) |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 353 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 44b21b9 | 2012-11-08 19:37:08 +0000 | [diff] [blame] | 354 | return subprocess2.check_call_out(['git'] + args, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 355 | |
| 356 | def _call_git(self, args, **kwargs): |
| 357 | """Like check_call but doesn't throw on failure.""" |
| 358 | kwargs.setdefault('cwd', self.project_path) |
| 359 | kwargs.setdefault('stdout', self.VOID) |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 360 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 44b21b9 | 2012-11-08 19:37:08 +0000 | [diff] [blame] | 361 | return subprocess2.call(['git'] + args, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 362 | |
| 363 | def _check_output_git(self, args, **kwargs): |
| 364 | kwargs.setdefault('cwd', self.project_path) |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 365 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 366 | return subprocess2.check_output( |
maruel@chromium.org | 44b21b9 | 2012-11-08 19:37:08 +0000 | [diff] [blame] | 367 | ['git'] + args, stderr=subprocess2.STDOUT, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 368 | |
| 369 | def _branches(self): |
| 370 | """Returns the list of branches and the active one.""" |
| 371 | out = self._check_output_git(['branch']).splitlines(False) |
| 372 | branches = [l[2:] for l in out] |
| 373 | active = None |
| 374 | for l in out: |
| 375 | if l.startswith('*'): |
| 376 | active = l[2:] |
| 377 | break |
| 378 | return branches, active |
| 379 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 380 | def revisions(self, rev1, rev2): |
| 381 | """Returns the number of actual commits between both hash.""" |
| 382 | self._fetch_remote() |
| 383 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 384 | rev2 = rev2 or '%s/%s' % (self.remote, self.remote_branch) |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 385 | # Revision range is ]rev1, rev2] and ordering matters. |
| 386 | try: |
| 387 | out = self._check_output_git( |
| 388 | ['log', '--format="%H"' , '%s..%s' % (rev1, rev2)]) |
| 389 | except subprocess.CalledProcessError: |
| 390 | return None |
| 391 | return len(out.splitlines()) |
| 392 | |
| 393 | def _fetch_remote(self): |
| 394 | """Fetches the remote without rebasing.""" |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 395 | # git fetch is always verbose even with -q, so redirect its output. |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 396 | self._check_output_git(['fetch', self.remote, self.remote_branch], |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 397 | timeout=FETCH_TIMEOUT) |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 398 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 399 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 400 | class ReadOnlyCheckout(object): |
| 401 | """Converts a checkout into a read-only one.""" |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 402 | def __init__(self, checkout, post_processors=None): |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 403 | super(ReadOnlyCheckout, self).__init__() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 404 | self.checkout = checkout |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 405 | self.post_processors = (post_processors or []) + ( |
| 406 | self.checkout.post_processors or []) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 407 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 408 | def prepare(self, revision): |
| 409 | return self.checkout.prepare(revision) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 410 | |
| 411 | def get_settings(self, key): |
| 412 | return self.checkout.get_settings(key) |
| 413 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 414 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 415 | return self.checkout.apply_patch( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 416 | patches, post_processors or self.post_processors, verbose) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 417 | |
| 418 | def commit(self, message, user): # pylint: disable=R0201 |
| 419 | logging.info('Would have committed for %s with message: %s' % ( |
| 420 | user, message)) |
| 421 | return 'FAKE' |
| 422 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 423 | def revisions(self, rev1, rev2): |
| 424 | return self.checkout.revisions(rev1, rev2) |
| 425 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 426 | @property |
| 427 | def project_name(self): |
| 428 | return self.checkout.project_name |
| 429 | |
| 430 | @property |
| 431 | def project_path(self): |
| 432 | return self.checkout.project_path |