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 | |
| 7 | Includes support for svn, git-svn and git. |
| 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 | |
| 164 | class RawCheckout(CheckoutBase): |
| 165 | """Used to apply a patch locally without any intent to commit it. |
| 166 | |
| 167 | To be used by the try server. |
| 168 | """ |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 169 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 170 | """Stubbed out.""" |
| 171 | pass |
| 172 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 173 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 174 | """Ignores svn properties.""" |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 175 | post_processors = post_processors or self.post_processors or [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 176 | for p in patches: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 177 | stdout = [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 178 | try: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 179 | filepath = os.path.join(self.project_path, p.filename) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 180 | if p.is_delete: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 181 | os.remove(filepath) |
phajdan.jr@chromium.org | d9eb69e | 2014-06-05 20:33:37 +0000 | [diff] [blame] | 182 | assert(not os.path.exists(filepath)) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 183 | stdout.append('Deleted.') |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 184 | else: |
| 185 | dirname = os.path.dirname(p.filename) |
| 186 | full_dir = os.path.join(self.project_path, dirname) |
| 187 | if dirname and not os.path.isdir(full_dir): |
| 188 | os.makedirs(full_dir) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 189 | stdout.append('Created missing directory %s.' % dirname) |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 190 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 191 | if p.is_binary: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 192 | content = p.get() |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 193 | with open(filepath, 'wb') as f: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 194 | f.write(content) |
| 195 | stdout.append('Added binary file %d bytes.' % len(content)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 196 | else: |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 197 | if p.source_filename: |
| 198 | if not p.is_new: |
| 199 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 200 | p, |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 201 | 'File has a source filename specified but is not new') |
| 202 | # Copy the file first. |
| 203 | if os.path.isfile(filepath): |
| 204 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 205 | p, 'File exist but was about to be overwriten') |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 206 | shutil.copy2( |
| 207 | os.path.join(self.project_path, p.source_filename), filepath) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 208 | stdout.append('Copied %s -> %s' % (p.source_filename, p.filename)) |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 209 | if p.diff_hunks: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 210 | cmd = ['patch', '-u', '--binary', '-p%s' % p.patchlevel] |
| 211 | if verbose: |
| 212 | cmd.append('--verbose') |
groby@chromium.org | 2327994 | 2013-07-12 19:32:33 +0000 | [diff] [blame] | 213 | env = os.environ.copy() |
| 214 | env['TMPDIR'] = tempfile.mkdtemp(prefix='crpatch') |
| 215 | try: |
| 216 | stdout.append( |
| 217 | subprocess2.check_output( |
| 218 | cmd, |
| 219 | stdin=p.get(False), |
| 220 | stderr=subprocess2.STDOUT, |
| 221 | cwd=self.project_path, |
| 222 | timeout=GLOBAL_TIMEOUT, |
| 223 | env=env)) |
| 224 | finally: |
| 225 | shutil.rmtree(env['TMPDIR']) |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 226 | elif p.is_new and not os.path.exists(filepath): |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 227 | # There is only a header. Just create the file. |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 228 | open(filepath, 'w').close() |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 229 | stdout.append('Created an empty file.') |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 230 | for post in post_processors: |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 231 | post(self, p) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 232 | if verbose: |
| 233 | print p.filename |
| 234 | print align_stdout(stdout) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 235 | except OSError, e: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 236 | raise PatchApplicationFailed(p, '%s%s' % (align_stdout(stdout), e)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 237 | except subprocess.CalledProcessError, e: |
| 238 | raise PatchApplicationFailed( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 239 | p, |
| 240 | 'While running %s;\n%s%s' % ( |
| 241 | ' '.join(e.cmd), |
| 242 | align_stdout(stdout), |
| 243 | align_stdout([getattr(e, 'stdout', '')]))) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 244 | |
| 245 | def commit(self, commit_message, user): |
| 246 | """Stubbed out.""" |
| 247 | raise NotImplementedError('RawCheckout can\'t commit') |
| 248 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 249 | def revisions(self, _rev1, _rev2): |
| 250 | return None |
| 251 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 252 | |
| 253 | class SvnConfig(object): |
| 254 | """Parses a svn configuration file.""" |
| 255 | def __init__(self, svn_config_dir=None): |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 256 | super(SvnConfig, self).__init__() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 257 | self.svn_config_dir = svn_config_dir |
| 258 | self.default = not bool(self.svn_config_dir) |
| 259 | if not self.svn_config_dir: |
| 260 | if sys.platform == 'win32': |
| 261 | self.svn_config_dir = os.path.join(os.environ['APPDATA'], 'Subversion') |
| 262 | else: |
mmoss@chromium.org | c349971 | 2015-11-25 01:04:01 +0000 | [diff] [blame] | 263 | self.svn_config_dir = os.path.expanduser( |
| 264 | os.path.join('~', '.subversion')) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 265 | svn_config_file = os.path.join(self.svn_config_dir, 'config') |
vapier | 9f34371 | 2016-06-22 07:13:20 -0700 | [diff] [blame^] | 266 | parser = configparser.SafeConfigParser() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 267 | if os.path.isfile(svn_config_file): |
| 268 | parser.read(svn_config_file) |
| 269 | else: |
| 270 | parser.add_section('auto-props') |
| 271 | self.auto_props = dict(parser.items('auto-props')) |
| 272 | |
| 273 | |
| 274 | class SvnMixIn(object): |
| 275 | """MixIn class to add svn commands common to both svn and git-svn clients.""" |
| 276 | # These members need to be set by the subclass. |
| 277 | commit_user = None |
| 278 | commit_pwd = None |
| 279 | svn_url = None |
| 280 | project_path = None |
| 281 | # Override at class level when necessary. If used, --non-interactive is |
| 282 | # implied. |
| 283 | svn_config = SvnConfig() |
| 284 | # Set to True when non-interactivity is necessary but a custom subversion |
| 285 | # configuration directory is not necessary. |
| 286 | non_interactive = False |
| 287 | |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 288 | def _add_svn_flags(self, args, non_interactive, credentials=True): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 289 | args = ['svn'] + args |
| 290 | if not self.svn_config.default: |
| 291 | args.extend(['--config-dir', self.svn_config.svn_config_dir]) |
| 292 | if not self.svn_config.default or self.non_interactive or non_interactive: |
| 293 | args.append('--non-interactive') |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 294 | if credentials: |
| 295 | if self.commit_user: |
| 296 | args.extend(['--username', self.commit_user]) |
| 297 | if self.commit_pwd: |
| 298 | args.extend(['--password', self.commit_pwd]) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 299 | return args |
| 300 | |
| 301 | def _check_call_svn(self, args, **kwargs): |
| 302 | """Runs svn and throws an exception if the command failed.""" |
| 303 | kwargs.setdefault('cwd', self.project_path) |
| 304 | kwargs.setdefault('stdout', self.VOID) |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 305 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 0bcd1d3 | 2011-04-26 15:55:49 +0000 | [diff] [blame] | 306 | return subprocess2.check_call_out( |
maruel@chromium.org | 44b21b9 | 2012-11-08 19:37:08 +0000 | [diff] [blame] | 307 | self._add_svn_flags(args, False), **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 308 | |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 309 | def _check_output_svn(self, args, credentials=True, **kwargs): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 310 | """Runs svn and throws an exception if the command failed. |
| 311 | |
| 312 | Returns the output. |
| 313 | """ |
| 314 | kwargs.setdefault('cwd', self.project_path) |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 315 | return subprocess2.check_output( |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 316 | self._add_svn_flags(args, True, credentials), |
| 317 | stderr=subprocess2.STDOUT, |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 318 | timeout=GLOBAL_TIMEOUT, |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 319 | **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 320 | |
| 321 | @staticmethod |
| 322 | def _parse_svn_info(output, key): |
| 323 | """Returns value for key from svn info output. |
| 324 | |
| 325 | Case insensitive. |
| 326 | """ |
| 327 | values = {} |
| 328 | key = key.lower() |
| 329 | for line in output.splitlines(False): |
| 330 | if not line: |
| 331 | continue |
| 332 | k, v = line.split(':', 1) |
| 333 | k = k.strip().lower() |
| 334 | v = v.strip() |
| 335 | assert not k in values |
| 336 | values[k] = v |
| 337 | return values.get(key, None) |
| 338 | |
| 339 | |
| 340 | class SvnCheckout(CheckoutBase, SvnMixIn): |
| 341 | """Manages a subversion checkout.""" |
maruel@chromium.org | 6ed8b50 | 2011-06-12 01:05:35 +0000 | [diff] [blame] | 342 | def __init__(self, root_dir, project_name, commit_user, commit_pwd, svn_url, |
| 343 | post_processors=None): |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 344 | CheckoutBase.__init__(self, root_dir, project_name, post_processors) |
| 345 | SvnMixIn.__init__(self) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 346 | self.commit_user = commit_user |
| 347 | self.commit_pwd = commit_pwd |
| 348 | self.svn_url = svn_url |
| 349 | assert bool(self.commit_user) >= bool(self.commit_pwd) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 350 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 351 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 352 | # Will checkout if the directory is not present. |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 353 | assert self.svn_url |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 354 | if not os.path.isdir(self.project_path): |
| 355 | logging.info('Checking out %s in %s' % |
| 356 | (self.project_name, self.project_path)) |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 357 | return self._revert(revision) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 358 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 359 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 360 | post_processors = post_processors or self.post_processors or [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 361 | for p in patches: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 362 | stdout = [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 363 | try: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 364 | filepath = os.path.join(self.project_path, p.filename) |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 365 | # It is important to use credentials=False otherwise credentials could |
| 366 | # leak in the error message. Credentials are not necessary here for the |
| 367 | # following commands anyway. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 368 | if p.is_delete: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 369 | stdout.append(self._check_output_svn( |
| 370 | ['delete', p.filename, '--force'], credentials=False)) |
phajdan.jr@chromium.org | d9eb69e | 2014-06-05 20:33:37 +0000 | [diff] [blame] | 371 | assert(not os.path.exists(filepath)) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 372 | stdout.append('Deleted.') |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 373 | else: |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 374 | # svn add while creating directories otherwise svn add on the |
| 375 | # contained files will silently fail. |
| 376 | # First, find the root directory that exists. |
| 377 | dirname = os.path.dirname(p.filename) |
| 378 | dirs_to_create = [] |
| 379 | while (dirname and |
| 380 | not os.path.isdir(os.path.join(self.project_path, dirname))): |
| 381 | dirs_to_create.append(dirname) |
| 382 | dirname = os.path.dirname(dirname) |
| 383 | for dir_to_create in reversed(dirs_to_create): |
| 384 | os.mkdir(os.path.join(self.project_path, dir_to_create)) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 385 | stdout.append( |
| 386 | self._check_output_svn( |
| 387 | ['add', dir_to_create, '--force'], credentials=False)) |
| 388 | stdout.append('Created missing directory %s.' % dir_to_create) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 389 | |
| 390 | if p.is_binary: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 391 | content = p.get() |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 392 | with open(filepath, 'wb') as f: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 393 | f.write(content) |
| 394 | stdout.append('Added binary file %d bytes.' % len(content)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 395 | else: |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 396 | if p.source_filename: |
| 397 | if not p.is_new: |
| 398 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 399 | p, |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 400 | 'File has a source filename specified but is not new') |
| 401 | # Copy the file first. |
| 402 | if os.path.isfile(filepath): |
| 403 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 404 | p, 'File exist but was about to be overwriten') |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 405 | stdout.append( |
| 406 | self._check_output_svn( |
| 407 | ['copy', p.source_filename, p.filename])) |
| 408 | stdout.append('Copied %s -> %s' % (p.source_filename, p.filename)) |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 409 | if p.diff_hunks: |
maruel@chromium.org | ec4a918 | 2012-09-28 20:39:45 +0000 | [diff] [blame] | 410 | cmd = [ |
| 411 | 'patch', |
| 412 | '-p%s' % p.patchlevel, |
| 413 | '--forward', |
| 414 | '--force', |
| 415 | '--no-backup-if-mismatch', |
| 416 | ] |
groby@chromium.org | 2327994 | 2013-07-12 19:32:33 +0000 | [diff] [blame] | 417 | env = os.environ.copy() |
| 418 | env['TMPDIR'] = tempfile.mkdtemp(prefix='crpatch') |
| 419 | try: |
| 420 | stdout.append( |
| 421 | subprocess2.check_output( |
| 422 | cmd, |
| 423 | stdin=p.get(False), |
| 424 | cwd=self.project_path, |
| 425 | timeout=GLOBAL_TIMEOUT, |
| 426 | env=env)) |
| 427 | finally: |
| 428 | shutil.rmtree(env['TMPDIR']) |
| 429 | |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 430 | elif p.is_new and not os.path.exists(filepath): |
| 431 | # There is only a header. Just create the file if it doesn't |
| 432 | # exist. |
| 433 | open(filepath, 'w').close() |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 434 | stdout.append('Created an empty file.') |
maruel@chromium.org | 3da8317 | 2012-05-07 16:17:20 +0000 | [diff] [blame] | 435 | if p.is_new and not p.source_filename: |
| 436 | # Do not run it if p.source_filename is defined, since svn copy was |
| 437 | # using above. |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 438 | stdout.append( |
| 439 | self._check_output_svn( |
| 440 | ['add', p.filename, '--force'], credentials=False)) |
maruel@chromium.org | d7ca616 | 2012-08-29 17:22:22 +0000 | [diff] [blame] | 441 | for name, value in p.svn_properties: |
| 442 | if value is None: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 443 | stdout.append( |
| 444 | self._check_output_svn( |
| 445 | ['propdel', '--quiet', name, p.filename], |
| 446 | credentials=False)) |
| 447 | stdout.append('Property %s deleted.' % name) |
maruel@chromium.org | d7ca616 | 2012-08-29 17:22:22 +0000 | [diff] [blame] | 448 | else: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 449 | stdout.append( |
| 450 | self._check_output_svn( |
| 451 | ['propset', name, value, p.filename], credentials=False)) |
| 452 | stdout.append('Property %s=%s' % (name, value)) |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 453 | for prop, values in self.svn_config.auto_props.iteritems(): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 454 | if fnmatch.fnmatch(p.filename, prop): |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 455 | for value in values.split(';'): |
| 456 | if '=' not in value: |
maruel@chromium.org | e1a0376 | 2012-09-24 15:28:52 +0000 | [diff] [blame] | 457 | params = [value, '.'] |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 458 | else: |
| 459 | params = value.split('=', 1) |
maruel@chromium.org | e1a0376 | 2012-09-24 15:28:52 +0000 | [diff] [blame] | 460 | if params[1] == '*': |
| 461 | # Works around crbug.com/150960 on Windows. |
| 462 | params[1] = '.' |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 463 | stdout.append( |
| 464 | self._check_output_svn( |
| 465 | ['propset'] + params + [p.filename], credentials=False)) |
| 466 | stdout.append('Property (auto) %s' % '='.join(params)) |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 467 | for post in post_processors: |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 468 | post(self, p) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 469 | if verbose: |
| 470 | print p.filename |
| 471 | print align_stdout(stdout) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 472 | except OSError, e: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 473 | raise PatchApplicationFailed(p, '%s%s' % (align_stdout(stdout), e)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 474 | except subprocess.CalledProcessError, e: |
| 475 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 476 | p, |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 477 | 'While running %s;\n%s%s' % ( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 478 | ' '.join(e.cmd), |
| 479 | align_stdout(stdout), |
| 480 | align_stdout([getattr(e, 'stdout', '')]))) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 481 | |
| 482 | def commit(self, commit_message, user): |
| 483 | logging.info('Committing patch for %s' % user) |
| 484 | assert self.commit_user |
maruel@chromium.org | 1bf5097 | 2011-05-05 19:57:21 +0000 | [diff] [blame] | 485 | assert isinstance(commit_message, unicode) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 486 | handle, commit_filename = tempfile.mkstemp(text=True) |
| 487 | try: |
maruel@chromium.org | 1bf5097 | 2011-05-05 19:57:21 +0000 | [diff] [blame] | 488 | # Shouldn't assume default encoding is UTF-8. But really, if you are using |
| 489 | # anything else, you are living in another world. |
| 490 | os.write(handle, commit_message.encode('utf-8')) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 491 | os.close(handle) |
| 492 | # When committing, svn won't update the Revision metadata of the checkout, |
| 493 | # so if svn commit returns "Committed revision 3.", svn info will still |
| 494 | # return "Revision: 2". Since running svn update right after svn commit |
| 495 | # creates a race condition with other committers, this code _must_ parse |
| 496 | # the output of svn commit and use a regexp to grab the revision number. |
| 497 | # Note that "Committed revision N." is localized but subprocess2 forces |
| 498 | # LANGUAGE=en. |
| 499 | args = ['commit', '--file', commit_filename] |
| 500 | # realauthor is parsed by a server-side hook. |
| 501 | if user and user != self.commit_user: |
| 502 | args.extend(['--with-revprop', 'realauthor=%s' % user]) |
| 503 | out = self._check_output_svn(args) |
| 504 | finally: |
| 505 | os.remove(commit_filename) |
| 506 | lines = filter(None, out.splitlines()) |
| 507 | match = re.match(r'^Committed revision (\d+).$', lines[-1]) |
| 508 | if not match: |
| 509 | raise PatchApplicationFailed( |
| 510 | None, |
| 511 | 'Couldn\'t make sense out of svn commit message:\n' + out) |
| 512 | return int(match.group(1)) |
| 513 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 514 | def _revert(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 515 | """Reverts local modifications or checks out if the directory is not |
| 516 | present. Use depot_tools's functionality to do this. |
| 517 | """ |
| 518 | flags = ['--ignore-externals'] |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 519 | if revision: |
| 520 | flags.extend(['--revision', str(revision)]) |
maruel@chromium.org | 6e904b4 | 2012-12-19 14:21:14 +0000 | [diff] [blame] | 521 | if os.path.isdir(self.project_path): |
| 522 | # This may remove any part (or all) of the checkout. |
| 523 | scm.SVN.Revert(self.project_path, no_ignore=True) |
| 524 | |
| 525 | if os.path.isdir(self.project_path): |
| 526 | # Revive files that were deleted in scm.SVN.Revert(). |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 527 | self._check_call_svn(['update', '--force'] + flags, |
| 528 | timeout=FETCH_TIMEOUT) |
maruel@chromium.org | 6e904b4 | 2012-12-19 14:21:14 +0000 | [diff] [blame] | 529 | else: |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 530 | logging.info( |
| 531 | 'Directory %s is not present, checking it out.' % self.project_path) |
| 532 | self._check_call_svn( |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 533 | ['checkout', self.svn_url, self.project_path] + flags, cwd=None, |
| 534 | timeout=FETCH_TIMEOUT) |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 535 | return self._get_revision() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 536 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 537 | def _get_revision(self): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 538 | out = self._check_output_svn(['info', '.']) |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 539 | revision = int(self._parse_svn_info(out, 'revision')) |
| 540 | if revision != self._last_seen_revision: |
| 541 | logging.info('Updated to revision %d' % revision) |
| 542 | self._last_seen_revision = revision |
| 543 | return revision |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 544 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 545 | def revisions(self, rev1, rev2): |
| 546 | """Returns the number of actual commits, not just the difference between |
| 547 | numbers. |
| 548 | """ |
| 549 | rev2 = rev2 or 'HEAD' |
| 550 | # Revision range is inclusive and ordering doesn't matter, they'll appear in |
| 551 | # the order specified. |
| 552 | try: |
| 553 | out = self._check_output_svn( |
| 554 | ['log', '-q', self.svn_url, '-r', '%s:%s' % (rev1, rev2)]) |
| 555 | except subprocess.CalledProcessError: |
| 556 | return None |
| 557 | # Ignore the '----' lines. |
| 558 | return len([l for l in out.splitlines() if l.startswith('r')]) - 1 |
| 559 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 560 | |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 561 | class GitCheckout(CheckoutBase): |
| 562 | """Manages a git checkout.""" |
| 563 | def __init__(self, root_dir, project_name, remote_branch, git_url, |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 564 | commit_user, post_processors=None): |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 565 | super(GitCheckout, self).__init__(root_dir, project_name, post_processors) |
| 566 | self.git_url = git_url |
| 567 | self.commit_user = commit_user |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 568 | self.remote_branch = remote_branch |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 569 | # The working branch where patches will be applied. It will track the |
| 570 | # remote branch. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 571 | self.working_branch = 'working_branch' |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 572 | # There is no reason to not hardcode origin. |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 573 | self.remote = 'origin' |
| 574 | # There is no reason to not hardcode master. |
| 575 | self.master_branch = 'master' |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 576 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 577 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 578 | """Resets the git repository in a clean state. |
| 579 | |
| 580 | Checks it out if not present and deletes the working branch. |
| 581 | """ |
agable@chromium.org | 7dc1144 | 2014-03-12 22:37:32 +0000 | [diff] [blame] | 582 | assert self.remote_branch |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 583 | assert self.git_url |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 584 | |
| 585 | if not os.path.isdir(self.project_path): |
| 586 | # Clone the repo if the directory is not present. |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 587 | logging.info( |
| 588 | 'Checking out %s in %s', self.project_name, self.project_path) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 589 | self._check_call_git( |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 590 | ['clone', self.git_url, '-b', self.remote_branch, self.project_path], |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 591 | cwd=None, timeout=FETCH_TIMEOUT) |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 592 | else: |
| 593 | # Throw away all uncommitted changes in the existing checkout. |
| 594 | self._check_call_git(['checkout', self.remote_branch]) |
| 595 | self._check_call_git( |
| 596 | ['reset', '--hard', '--quiet', |
| 597 | '%s/%s' % (self.remote, self.remote_branch)]) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 598 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 599 | if revision: |
| 600 | try: |
| 601 | # Look if the commit hash already exist. If so, we can skip a |
| 602 | # 'git fetch' call. |
halton.huo@intel.com | 323ec37 | 2014-06-17 01:50:37 +0000 | [diff] [blame] | 603 | revision = self._check_output_git(['rev-parse', revision]).rstrip() |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 604 | except subprocess.CalledProcessError: |
| 605 | self._check_call_git( |
| 606 | ['fetch', self.remote, self.remote_branch, '--quiet']) |
halton.huo@intel.com | 323ec37 | 2014-06-17 01:50:37 +0000 | [diff] [blame] | 607 | revision = self._check_output_git(['rev-parse', revision]).rstrip() |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 608 | self._check_call_git(['checkout', '--force', '--quiet', revision]) |
| 609 | else: |
| 610 | branches, active = self._branches() |
| 611 | if active != self.master_branch: |
| 612 | self._check_call_git( |
| 613 | ['checkout', '--force', '--quiet', self.master_branch]) |
| 614 | self._sync_remote_branch() |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 615 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 616 | if self.working_branch in branches: |
| 617 | self._call_git(['branch', '-D', self.working_branch]) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 618 | return self._get_head_commit_hash() |
| 619 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 620 | def _sync_remote_branch(self): |
| 621 | """Syncs the remote branch.""" |
| 622 | # 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] | 623 | # 'git pull origin master' because from the manpage for git-pull: |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 624 | # A parameter <ref> without a colon is equivalent to <ref>: when |
| 625 | # pulling/fetching, so it merges <ref> into the current branch without |
| 626 | # storing the remote branch anywhere locally. |
| 627 | remote_tracked_path = 'refs/remotes/%s/%s' % ( |
| 628 | self.remote, self.remote_branch) |
| 629 | self._check_call_git( |
| 630 | ['pull', self.remote, |
| 631 | '%s:%s' % (self.remote_branch, remote_tracked_path), |
| 632 | '--quiet']) |
| 633 | |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 634 | def _get_head_commit_hash(self): |
rmistry@google.com | 11145db | 2013-10-03 12:43:40 +0000 | [diff] [blame] | 635 | """Gets the current revision (in unicode) from the local branch.""" |
| 636 | return unicode(self._check_output_git(['rev-parse', 'HEAD']).strip()) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 637 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 638 | def apply_patch(self, patches, post_processors=None, verbose=False): |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 639 | """Applies a patch on 'working_branch' and switches to it. |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 640 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 641 | The changes remain staged on the current branch. |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 642 | |
| 643 | Ignores svn properties and raise an exception on unexpected ones. |
| 644 | """ |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 645 | post_processors = post_processors or self.post_processors or [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 646 | # It this throws, the checkout is corrupted. Maybe worth deleting it and |
| 647 | # trying again? |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 648 | if self.remote_branch: |
| 649 | self._check_call_git( |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 650 | ['checkout', '-b', self.working_branch, '-t', self.remote_branch, |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 651 | '--quiet']) |
| 652 | |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 653 | for index, p in enumerate(patches): |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 654 | stdout = [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 655 | try: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 656 | filepath = os.path.join(self.project_path, p.filename) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 657 | if p.is_delete: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 658 | if (not os.path.exists(filepath) and |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 659 | 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] | 660 | # The file was already deleted if a prior patch with file rename |
| 661 | # was already processed because 'git apply' did it for us. |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 662 | pass |
| 663 | else: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 664 | stdout.append(self._check_output_git(['rm', p.filename])) |
phajdan.jr@chromium.org | d9eb69e | 2014-06-05 20:33:37 +0000 | [diff] [blame] | 665 | assert(not os.path.exists(filepath)) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 666 | stdout.append('Deleted.') |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 667 | else: |
| 668 | dirname = os.path.dirname(p.filename) |
| 669 | full_dir = os.path.join(self.project_path, dirname) |
| 670 | if dirname and not os.path.isdir(full_dir): |
| 671 | os.makedirs(full_dir) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 672 | stdout.append('Created missing directory %s.' % dirname) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 673 | if p.is_binary: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 674 | content = p.get() |
| 675 | with open(filepath, 'wb') as f: |
| 676 | f.write(content) |
| 677 | stdout.append('Added binary file %d bytes' % len(content)) |
| 678 | cmd = ['add', p.filename] |
| 679 | if verbose: |
| 680 | cmd.append('--verbose') |
| 681 | stdout.append(self._check_output_git(cmd)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 682 | else: |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 683 | # No need to do anything special with p.is_new or if not |
| 684 | # p.diff_hunks. git apply manages all that already. |
primiano@chromium.org | 49dfcde | 2014-09-23 08:14:39 +0000 | [diff] [blame] | 685 | cmd = ['apply', '--index', '-3', '-p%s' % p.patchlevel] |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 686 | if verbose: |
| 687 | cmd.append('--verbose') |
| 688 | stdout.append(self._check_output_git(cmd, stdin=p.get(True))) |
hinoka@google.com | 64d819b | 2014-05-06 19:59:11 +0000 | [diff] [blame] | 689 | for key, value in p.svn_properties: |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 690 | # Ignore some known auto-props flags through .subversion/config, |
| 691 | # bails out on the other ones. |
| 692 | # TODO(maruel): Read ~/.subversion/config and detect the rules that |
| 693 | # applies here to figure out if the property will be correctly |
| 694 | # handled. |
hinoka@google.com | 64d819b | 2014-05-06 19:59:11 +0000 | [diff] [blame] | 695 | stdout.append('Property %s=%s' % (key, value)) |
| 696 | if not key in ( |
maruel@chromium.org | 9799a07 | 2012-01-11 00:26:25 +0000 | [diff] [blame] | 697 | 'svn:eol-style', 'svn:executable', 'svn:mime-type'): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 698 | raise patch.UnsupportedPatchFormat( |
| 699 | p.filename, |
| 700 | 'Cannot apply svn property %s to file %s.' % ( |
hinoka@google.com | 64d819b | 2014-05-06 19:59:11 +0000 | [diff] [blame] | 701 | key, p.filename)) |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 702 | for post in post_processors: |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 703 | post(self, p) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 704 | if verbose: |
| 705 | print p.filename |
| 706 | print align_stdout(stdout) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 707 | except OSError, e: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 708 | raise PatchApplicationFailed(p, '%s%s' % (align_stdout(stdout), e)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 709 | except subprocess.CalledProcessError, e: |
| 710 | raise PatchApplicationFailed( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 711 | p, |
| 712 | 'While running %s;\n%s%s' % ( |
| 713 | ' '.join(e.cmd), |
| 714 | align_stdout(stdout), |
| 715 | align_stdout([getattr(e, 'stdout', '')]))) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 716 | found_files = self._check_output_git( |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 717 | ['diff', '--ignore-submodules', |
| 718 | '--name-only', '--staged']).splitlines(False) |
hinoka@chromium.org | dc6a1d0 | 2014-05-10 04:42:48 +0000 | [diff] [blame] | 719 | if sorted(patches.filenames) != sorted(found_files): |
| 720 | extra_files = sorted(set(found_files) - set(patches.filenames)) |
| 721 | unpatched_files = sorted(set(patches.filenames) - set(found_files)) |
| 722 | if extra_files: |
| 723 | print 'Found extra files: %r' % (extra_files,) |
| 724 | if unpatched_files: |
| 725 | print 'Found unpatched files: %r' % (unpatched_files,) |
| 726 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 727 | |
| 728 | def commit(self, commit_message, user): |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 729 | """Commits, updates the commit message and pushes.""" |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 730 | # TODO(hinoka): CQ no longer uses this, I think its deprecated. |
| 731 | # Delete this. |
rmistry@google.com | bb050f6 | 2013-10-03 16:53:54 +0000 | [diff] [blame] | 732 | assert self.commit_user |
maruel@chromium.org | 1bf5097 | 2011-05-05 19:57:21 +0000 | [diff] [blame] | 733 | assert isinstance(commit_message, unicode) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 734 | current_branch = self._check_output_git( |
| 735 | ['rev-parse', '--abbrev-ref', 'HEAD']).strip() |
| 736 | assert current_branch == self.working_branch |
hinoka@google.com | dabbea2 | 2014-04-21 23:58:11 +0000 | [diff] [blame] | 737 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 738 | commit_cmd = ['commit', '-m', commit_message] |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 739 | if user and user != self.commit_user: |
| 740 | # We do not have the first or last name of the user, grab the username |
| 741 | # from the email and call it the original author's name. |
| 742 | # TODO(rmistry): Do not need the below if user is already in |
| 743 | # "Name <email>" format. |
| 744 | name = user.split('@')[0] |
| 745 | commit_cmd.extend(['--author', '%s <%s>' % (name, user)]) |
| 746 | self._check_call_git(commit_cmd) |
| 747 | |
| 748 | # Push to the remote repository. |
| 749 | self._check_call_git( |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 750 | ['push', 'origin', '%s:%s' % (self.working_branch, self.remote_branch), |
agable@chromium.org | 3926228 | 2014-03-19 21:07:38 +0000 | [diff] [blame] | 751 | '--quiet']) |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 752 | # Get the revision after the push. |
| 753 | revision = self._get_head_commit_hash() |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 754 | # Switch back to the remote_branch and sync it. |
| 755 | self._check_call_git(['checkout', self.remote_branch]) |
| 756 | self._sync_remote_branch() |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 757 | # Delete the working branch since we are done with it. |
| 758 | self._check_call_git(['branch', '-D', self.working_branch]) |
| 759 | |
| 760 | return revision |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 761 | |
| 762 | def _check_call_git(self, args, **kwargs): |
| 763 | kwargs.setdefault('cwd', self.project_path) |
| 764 | kwargs.setdefault('stdout', self.VOID) |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 765 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 44b21b9 | 2012-11-08 19:37:08 +0000 | [diff] [blame] | 766 | return subprocess2.check_call_out(['git'] + args, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 767 | |
| 768 | def _call_git(self, args, **kwargs): |
| 769 | """Like check_call but doesn't throw on failure.""" |
| 770 | kwargs.setdefault('cwd', self.project_path) |
| 771 | kwargs.setdefault('stdout', self.VOID) |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 772 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 44b21b9 | 2012-11-08 19:37:08 +0000 | [diff] [blame] | 773 | return subprocess2.call(['git'] + args, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 774 | |
| 775 | def _check_output_git(self, args, **kwargs): |
| 776 | kwargs.setdefault('cwd', self.project_path) |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 777 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 778 | return subprocess2.check_output( |
maruel@chromium.org | 44b21b9 | 2012-11-08 19:37:08 +0000 | [diff] [blame] | 779 | ['git'] + args, stderr=subprocess2.STDOUT, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 780 | |
| 781 | def _branches(self): |
| 782 | """Returns the list of branches and the active one.""" |
| 783 | out = self._check_output_git(['branch']).splitlines(False) |
| 784 | branches = [l[2:] for l in out] |
| 785 | active = None |
| 786 | for l in out: |
| 787 | if l.startswith('*'): |
| 788 | active = l[2:] |
| 789 | break |
| 790 | return branches, active |
| 791 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 792 | def revisions(self, rev1, rev2): |
| 793 | """Returns the number of actual commits between both hash.""" |
| 794 | self._fetch_remote() |
| 795 | |
agable@chromium.org | 7e8c19d | 2014-03-19 16:47:37 +0000 | [diff] [blame] | 796 | rev2 = rev2 or '%s/%s' % (self.remote, self.remote_branch) |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 797 | # Revision range is ]rev1, rev2] and ordering matters. |
| 798 | try: |
| 799 | out = self._check_output_git( |
| 800 | ['log', '--format="%H"' , '%s..%s' % (rev1, rev2)]) |
| 801 | except subprocess.CalledProcessError: |
| 802 | return None |
| 803 | return len(out.splitlines()) |
| 804 | |
| 805 | def _fetch_remote(self): |
| 806 | """Fetches the remote without rebasing.""" |
rmistry@google.com | 3b5efdf | 2013-09-05 11:59:40 +0000 | [diff] [blame] | 807 | # 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] | 808 | self._check_output_git(['fetch', self.remote, self.remote_branch], |
csharp@chromium.org | 9af0a11 | 2013-03-20 20:21:35 +0000 | [diff] [blame] | 809 | timeout=FETCH_TIMEOUT) |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 810 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 811 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 812 | class ReadOnlyCheckout(object): |
| 813 | """Converts a checkout into a read-only one.""" |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 814 | def __init__(self, checkout, post_processors=None): |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 815 | super(ReadOnlyCheckout, self).__init__() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 816 | self.checkout = checkout |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 817 | self.post_processors = (post_processors or []) + ( |
| 818 | self.checkout.post_processors or []) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 819 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 820 | def prepare(self, revision): |
| 821 | return self.checkout.prepare(revision) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 822 | |
| 823 | def get_settings(self, key): |
| 824 | return self.checkout.get_settings(key) |
| 825 | |
hinoka@chromium.org | c4396a1 | 2014-05-10 02:19:27 +0000 | [diff] [blame] | 826 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 827 | return self.checkout.apply_patch( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 828 | patches, post_processors or self.post_processors, verbose) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 829 | |
| 830 | def commit(self, message, user): # pylint: disable=R0201 |
| 831 | logging.info('Would have committed for %s with message: %s' % ( |
| 832 | user, message)) |
| 833 | return 'FAKE' |
| 834 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 835 | def revisions(self, rev1, rev2): |
| 836 | return self.checkout.revisions(rev1, rev2) |
| 837 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 838 | @property |
| 839 | def project_name(self): |
| 840 | return self.checkout.project_name |
| 841 | |
| 842 | @property |
| 843 | def project_path(self): |
| 844 | return self.checkout.project_path |