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 ConfigParser |
| 11 | import fnmatch |
| 12 | import logging |
| 13 | import os |
| 14 | import re |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 15 | import shutil |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 16 | import subprocess |
| 17 | import sys |
| 18 | import tempfile |
| 19 | |
| 20 | import patch |
| 21 | import scm |
| 22 | import subprocess2 |
| 23 | |
| 24 | |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 25 | # Default timeout of 15 minutes. |
| 26 | GLOBAL_TIMEOUT = 15*60 |
| 27 | # Use a larger timeout for checkout since it can be a genuinely slower |
| 28 | # operation. |
| 29 | FETCH_TIMEOUT = 30*60 |
| 30 | |
| 31 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 32 | def get_code_review_setting(path, key, |
| 33 | codereview_settings_file='codereview.settings'): |
| 34 | """Parses codereview.settings and return the value for the key if present. |
| 35 | |
| 36 | Don't cache the values in case the file is changed.""" |
| 37 | # TODO(maruel): Do not duplicate code. |
| 38 | settings = {} |
| 39 | try: |
| 40 | settings_file = open(os.path.join(path, codereview_settings_file), 'r') |
| 41 | try: |
| 42 | for line in settings_file.readlines(): |
| 43 | if not line or line.startswith('#'): |
| 44 | continue |
| 45 | if not ':' in line: |
| 46 | # Invalid file. |
| 47 | return None |
| 48 | k, v = line.split(':', 1) |
| 49 | settings[k.strip()] = v.strip() |
| 50 | finally: |
| 51 | settings_file.close() |
maruel@chromium.org | 004fb71 | 2011-06-21 20:02:16 +0000 | [diff] [blame] | 52 | except IOError: |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 53 | return None |
| 54 | return settings.get(key, None) |
| 55 | |
| 56 | |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 57 | def align_stdout(stdout): |
| 58 | """Returns the aligned output of multiple stdouts.""" |
| 59 | output = '' |
| 60 | for item in stdout: |
| 61 | item = item.strip() |
| 62 | if not item: |
| 63 | continue |
| 64 | output += ''.join(' %s\n' % line for line in item.splitlines()) |
| 65 | return output |
| 66 | |
| 67 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 68 | class PatchApplicationFailed(Exception): |
| 69 | """Patch failed to be applied.""" |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 70 | def __init__(self, p, status): |
| 71 | super(PatchApplicationFailed, self).__init__(p, status) |
| 72 | self.patch = p |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 73 | self.status = status |
| 74 | |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 75 | @property |
| 76 | def filename(self): |
| 77 | if self.patch: |
| 78 | return self.patch.filename |
| 79 | |
| 80 | def __str__(self): |
| 81 | out = [] |
| 82 | if self.filename: |
| 83 | out.append('Failed to apply patch for %s:' % self.filename) |
| 84 | if self.status: |
| 85 | out.append(self.status) |
maruel@chromium.org | cb5667a | 2012-10-23 19:42:10 +0000 | [diff] [blame] | 86 | if self.patch: |
| 87 | out.append('Patch: %s' % self.patch.dump()) |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 88 | return '\n'.join(out) |
| 89 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 90 | |
| 91 | class CheckoutBase(object): |
| 92 | # Set to None to have verbose output. |
| 93 | VOID = subprocess2.VOID |
| 94 | |
maruel@chromium.org | 6ed8b50 | 2011-06-12 01:05:35 +0000 | [diff] [blame] | 95 | def __init__(self, root_dir, project_name, post_processors): |
| 96 | """ |
| 97 | Args: |
| 98 | post_processor: list of lambda(checkout, patches) to call on each of the |
| 99 | modified files. |
| 100 | """ |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 101 | super(CheckoutBase, self).__init__() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 102 | self.root_dir = root_dir |
| 103 | self.project_name = project_name |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 104 | if self.project_name is None: |
| 105 | self.project_path = self.root_dir |
| 106 | else: |
| 107 | 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] | 108 | # Only used for logging purposes. |
| 109 | self._last_seen_revision = None |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 110 | self.post_processors = post_processors |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 111 | assert self.root_dir |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 112 | assert self.project_path |
maruel@chromium.org | 0aca0f9 | 2012-10-01 16:39:45 +0000 | [diff] [blame] | 113 | assert os.path.isabs(self.project_path) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 114 | |
| 115 | def get_settings(self, key): |
| 116 | return get_code_review_setting(self.project_path, key) |
| 117 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 118 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 119 | """Checks out a clean copy of the tree and removes any local modification. |
| 120 | |
| 121 | This function shouldn't throw unless the remote repository is inaccessible, |
| 122 | there is no free disk space or hard issues like that. |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 123 | |
| 124 | Args: |
| 125 | revision: The revision it should sync to, SCM specific. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 126 | """ |
| 127 | raise NotImplementedError() |
| 128 | |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 129 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 130 | """Applies a patch and returns the list of modified files. |
| 131 | |
| 132 | This function should throw patch.UnsupportedPatchFormat or |
| 133 | PatchApplicationFailed when relevant. |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 134 | |
| 135 | Args: |
| 136 | patches: patch.PatchSet object. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 137 | """ |
| 138 | raise NotImplementedError() |
| 139 | |
| 140 | def commit(self, commit_message, user): |
| 141 | """Commits the patch upstream, while impersonating 'user'.""" |
| 142 | raise NotImplementedError() |
| 143 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 144 | def revisions(self, rev1, rev2): |
| 145 | """Returns the count of revisions from rev1 to rev2, e.g. len(]rev1, rev2]). |
| 146 | |
| 147 | If rev2 is None, it means 'HEAD'. |
| 148 | |
| 149 | Returns None if there is no link between the two. |
| 150 | """ |
| 151 | raise NotImplementedError() |
| 152 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 153 | |
| 154 | class RawCheckout(CheckoutBase): |
| 155 | """Used to apply a patch locally without any intent to commit it. |
| 156 | |
| 157 | To be used by the try server. |
| 158 | """ |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 159 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 160 | """Stubbed out.""" |
| 161 | pass |
| 162 | |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 163 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 164 | """Ignores svn properties.""" |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 165 | post_processors = post_processors or self.post_processors or [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 166 | for p in patches: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 167 | stdout = [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 168 | try: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 169 | filepath = os.path.join(self.project_path, p.filename) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 170 | if p.is_delete: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 171 | os.remove(filepath) |
| 172 | stdout.append('Deleted.') |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 173 | else: |
| 174 | dirname = os.path.dirname(p.filename) |
| 175 | full_dir = os.path.join(self.project_path, dirname) |
| 176 | if dirname and not os.path.isdir(full_dir): |
| 177 | os.makedirs(full_dir) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 178 | stdout.append('Created missing directory %s.' % dirname) |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 179 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 180 | if p.is_binary: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 181 | content = p.get() |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 182 | with open(filepath, 'wb') as f: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 183 | f.write(content) |
| 184 | stdout.append('Added binary file %d bytes.' % len(content)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 185 | else: |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 186 | if p.source_filename: |
| 187 | if not p.is_new: |
| 188 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 189 | p, |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 190 | 'File has a source filename specified but is not new') |
| 191 | # Copy the file first. |
| 192 | if os.path.isfile(filepath): |
| 193 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 194 | p, 'File exist but was about to be overwriten') |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 195 | shutil.copy2( |
| 196 | os.path.join(self.project_path, p.source_filename), filepath) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 197 | stdout.append('Copied %s -> %s' % (p.source_filename, p.filename)) |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 198 | if p.diff_hunks: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 199 | cmd = ['patch', '-u', '--binary', '-p%s' % p.patchlevel] |
| 200 | if verbose: |
| 201 | cmd.append('--verbose') |
| 202 | stdout.append( |
| 203 | subprocess2.check_output( |
| 204 | cmd, |
| 205 | stdin=p.get(False), |
| 206 | stderr=subprocess2.STDOUT, |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 207 | cwd=self.project_path, |
| 208 | timeout=GLOBAL_TIMEOUT)) |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 209 | elif p.is_new and not os.path.exists(filepath): |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 210 | # There is only a header. Just create the file. |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 211 | open(filepath, 'w').close() |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 212 | stdout.append('Created an empty file.') |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 213 | for post in post_processors: |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 214 | post(self, p) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 215 | if verbose: |
| 216 | print p.filename |
| 217 | print align_stdout(stdout) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 218 | except OSError, e: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 219 | raise PatchApplicationFailed(p, '%s%s' % (align_stdout(stdout), e)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 220 | except subprocess.CalledProcessError, e: |
| 221 | raise PatchApplicationFailed( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 222 | p, |
| 223 | 'While running %s;\n%s%s' % ( |
| 224 | ' '.join(e.cmd), |
| 225 | align_stdout(stdout), |
| 226 | align_stdout([getattr(e, 'stdout', '')]))) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 227 | |
| 228 | def commit(self, commit_message, user): |
| 229 | """Stubbed out.""" |
| 230 | raise NotImplementedError('RawCheckout can\'t commit') |
| 231 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 232 | def revisions(self, _rev1, _rev2): |
| 233 | return None |
| 234 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 235 | |
| 236 | class SvnConfig(object): |
| 237 | """Parses a svn configuration file.""" |
| 238 | def __init__(self, svn_config_dir=None): |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 239 | super(SvnConfig, self).__init__() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 240 | self.svn_config_dir = svn_config_dir |
| 241 | self.default = not bool(self.svn_config_dir) |
| 242 | if not self.svn_config_dir: |
| 243 | if sys.platform == 'win32': |
| 244 | self.svn_config_dir = os.path.join(os.environ['APPDATA'], 'Subversion') |
| 245 | else: |
| 246 | self.svn_config_dir = os.path.join(os.environ['HOME'], '.subversion') |
| 247 | svn_config_file = os.path.join(self.svn_config_dir, 'config') |
| 248 | parser = ConfigParser.SafeConfigParser() |
| 249 | if os.path.isfile(svn_config_file): |
| 250 | parser.read(svn_config_file) |
| 251 | else: |
| 252 | parser.add_section('auto-props') |
| 253 | self.auto_props = dict(parser.items('auto-props')) |
| 254 | |
| 255 | |
| 256 | class SvnMixIn(object): |
| 257 | """MixIn class to add svn commands common to both svn and git-svn clients.""" |
| 258 | # These members need to be set by the subclass. |
| 259 | commit_user = None |
| 260 | commit_pwd = None |
| 261 | svn_url = None |
| 262 | project_path = None |
| 263 | # Override at class level when necessary. If used, --non-interactive is |
| 264 | # implied. |
| 265 | svn_config = SvnConfig() |
| 266 | # Set to True when non-interactivity is necessary but a custom subversion |
| 267 | # configuration directory is not necessary. |
| 268 | non_interactive = False |
| 269 | |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 270 | def _add_svn_flags(self, args, non_interactive, credentials=True): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 271 | args = ['svn'] + args |
| 272 | if not self.svn_config.default: |
| 273 | args.extend(['--config-dir', self.svn_config.svn_config_dir]) |
| 274 | if not self.svn_config.default or self.non_interactive or non_interactive: |
| 275 | args.append('--non-interactive') |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 276 | if credentials: |
| 277 | if self.commit_user: |
| 278 | args.extend(['--username', self.commit_user]) |
| 279 | if self.commit_pwd: |
| 280 | args.extend(['--password', self.commit_pwd]) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 281 | return args |
| 282 | |
| 283 | def _check_call_svn(self, args, **kwargs): |
| 284 | """Runs svn and throws an exception if the command failed.""" |
| 285 | kwargs.setdefault('cwd', self.project_path) |
| 286 | kwargs.setdefault('stdout', self.VOID) |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 287 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
maruel@chromium.org | 0bcd1d3 | 2011-04-26 15:55:49 +0000 | [diff] [blame] | 288 | return subprocess2.check_call_out( |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 289 | self._add_svn_flags(args, False), |
| 290 | **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 291 | |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 292 | def _check_output_svn(self, args, credentials=True, **kwargs): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 293 | """Runs svn and throws an exception if the command failed. |
| 294 | |
| 295 | Returns the output. |
| 296 | """ |
| 297 | kwargs.setdefault('cwd', self.project_path) |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 298 | return subprocess2.check_output( |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 299 | self._add_svn_flags(args, True, credentials), |
| 300 | stderr=subprocess2.STDOUT, |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 301 | timeout=GLOBAL_TIMEOUT, |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 302 | **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 303 | |
| 304 | @staticmethod |
| 305 | def _parse_svn_info(output, key): |
| 306 | """Returns value for key from svn info output. |
| 307 | |
| 308 | Case insensitive. |
| 309 | """ |
| 310 | values = {} |
| 311 | key = key.lower() |
| 312 | for line in output.splitlines(False): |
| 313 | if not line: |
| 314 | continue |
| 315 | k, v = line.split(':', 1) |
| 316 | k = k.strip().lower() |
| 317 | v = v.strip() |
| 318 | assert not k in values |
| 319 | values[k] = v |
| 320 | return values.get(key, None) |
| 321 | |
| 322 | |
| 323 | class SvnCheckout(CheckoutBase, SvnMixIn): |
| 324 | """Manages a subversion checkout.""" |
maruel@chromium.org | 6ed8b50 | 2011-06-12 01:05:35 +0000 | [diff] [blame] | 325 | def __init__(self, root_dir, project_name, commit_user, commit_pwd, svn_url, |
| 326 | post_processors=None): |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 327 | CheckoutBase.__init__(self, root_dir, project_name, post_processors) |
| 328 | SvnMixIn.__init__(self) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 329 | self.commit_user = commit_user |
| 330 | self.commit_pwd = commit_pwd |
| 331 | self.svn_url = svn_url |
| 332 | assert bool(self.commit_user) >= bool(self.commit_pwd) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 333 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 334 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 335 | # Will checkout if the directory is not present. |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 336 | assert self.svn_url |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 337 | if not os.path.isdir(self.project_path): |
| 338 | logging.info('Checking out %s in %s' % |
| 339 | (self.project_name, self.project_path)) |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 340 | return self._revert(revision) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 341 | |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 342 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 343 | post_processors = post_processors or self.post_processors or [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 344 | for p in patches: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 345 | stdout = [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 346 | try: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 347 | filepath = os.path.join(self.project_path, p.filename) |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 348 | # It is important to use credentials=False otherwise credentials could |
| 349 | # leak in the error message. Credentials are not necessary here for the |
| 350 | # following commands anyway. |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 351 | if p.is_delete: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 352 | stdout.append(self._check_output_svn( |
| 353 | ['delete', p.filename, '--force'], credentials=False)) |
| 354 | stdout.append('Deleted.') |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 355 | else: |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 356 | # svn add while creating directories otherwise svn add on the |
| 357 | # contained files will silently fail. |
| 358 | # First, find the root directory that exists. |
| 359 | dirname = os.path.dirname(p.filename) |
| 360 | dirs_to_create = [] |
| 361 | while (dirname and |
| 362 | not os.path.isdir(os.path.join(self.project_path, dirname))): |
| 363 | dirs_to_create.append(dirname) |
| 364 | dirname = os.path.dirname(dirname) |
| 365 | for dir_to_create in reversed(dirs_to_create): |
| 366 | os.mkdir(os.path.join(self.project_path, dir_to_create)) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 367 | stdout.append( |
| 368 | self._check_output_svn( |
| 369 | ['add', dir_to_create, '--force'], credentials=False)) |
| 370 | stdout.append('Created missing directory %s.' % dir_to_create) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 371 | |
| 372 | if p.is_binary: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 373 | content = p.get() |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 374 | with open(filepath, 'wb') as f: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 375 | f.write(content) |
| 376 | stdout.append('Added binary file %d bytes.' % len(content)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 377 | else: |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 378 | if p.source_filename: |
| 379 | if not p.is_new: |
| 380 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 381 | p, |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 382 | 'File has a source filename specified but is not new') |
| 383 | # Copy the file first. |
| 384 | if os.path.isfile(filepath): |
| 385 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 386 | p, 'File exist but was about to be overwriten') |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 387 | stdout.append( |
| 388 | self._check_output_svn( |
| 389 | ['copy', p.source_filename, p.filename])) |
| 390 | stdout.append('Copied %s -> %s' % (p.source_filename, p.filename)) |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 391 | if p.diff_hunks: |
maruel@chromium.org | ec4a918 | 2012-09-28 20:39:45 +0000 | [diff] [blame] | 392 | cmd = [ |
| 393 | 'patch', |
| 394 | '-p%s' % p.patchlevel, |
| 395 | '--forward', |
| 396 | '--force', |
| 397 | '--no-backup-if-mismatch', |
| 398 | ] |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 399 | stdout.append( |
| 400 | subprocess2.check_output( |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 401 | cmd, |
| 402 | stdin=p.get(False), |
| 403 | cwd=self.project_path, |
| 404 | timeout=GLOBAL_TIMEOUT)) |
maruel@chromium.org | 4869bcf | 2011-06-04 01:14:32 +0000 | [diff] [blame] | 405 | elif p.is_new and not os.path.exists(filepath): |
| 406 | # There is only a header. Just create the file if it doesn't |
| 407 | # exist. |
| 408 | open(filepath, 'w').close() |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 409 | stdout.append('Created an empty file.') |
maruel@chromium.org | 3da8317 | 2012-05-07 16:17:20 +0000 | [diff] [blame] | 410 | if p.is_new and not p.source_filename: |
| 411 | # Do not run it if p.source_filename is defined, since svn copy was |
| 412 | # using above. |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 413 | stdout.append( |
| 414 | self._check_output_svn( |
| 415 | ['add', p.filename, '--force'], credentials=False)) |
maruel@chromium.org | d7ca616 | 2012-08-29 17:22:22 +0000 | [diff] [blame] | 416 | for name, value in p.svn_properties: |
| 417 | if value is None: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 418 | stdout.append( |
| 419 | self._check_output_svn( |
| 420 | ['propdel', '--quiet', name, p.filename], |
| 421 | credentials=False)) |
| 422 | stdout.append('Property %s deleted.' % name) |
maruel@chromium.org | d7ca616 | 2012-08-29 17:22:22 +0000 | [diff] [blame] | 423 | else: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 424 | stdout.append( |
| 425 | self._check_output_svn( |
| 426 | ['propset', name, value, p.filename], credentials=False)) |
| 427 | stdout.append('Property %s=%s' % (name, value)) |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 428 | for prop, values in self.svn_config.auto_props.iteritems(): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 429 | if fnmatch.fnmatch(p.filename, prop): |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 430 | for value in values.split(';'): |
| 431 | if '=' not in value: |
maruel@chromium.org | e1a0376 | 2012-09-24 15:28:52 +0000 | [diff] [blame] | 432 | params = [value, '.'] |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 433 | else: |
| 434 | params = value.split('=', 1) |
maruel@chromium.org | e1a0376 | 2012-09-24 15:28:52 +0000 | [diff] [blame] | 435 | if params[1] == '*': |
| 436 | # Works around crbug.com/150960 on Windows. |
| 437 | params[1] = '.' |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 438 | stdout.append( |
| 439 | self._check_output_svn( |
| 440 | ['propset'] + params + [p.filename], credentials=False)) |
| 441 | stdout.append('Property (auto) %s' % '='.join(params)) |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 442 | for post in post_processors: |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 443 | post(self, p) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 444 | if verbose: |
| 445 | print p.filename |
| 446 | print align_stdout(stdout) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 447 | except OSError, e: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 448 | raise PatchApplicationFailed(p, '%s%s' % (align_stdout(stdout), e)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 449 | except subprocess.CalledProcessError, e: |
| 450 | raise PatchApplicationFailed( |
maruel@chromium.org | 34f6855 | 2012-05-09 19:18:36 +0000 | [diff] [blame] | 451 | p, |
maruel@chromium.org | 9842a0c | 2011-05-30 20:41:54 +0000 | [diff] [blame] | 452 | 'While running %s;\n%s%s' % ( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 453 | ' '.join(e.cmd), |
| 454 | align_stdout(stdout), |
| 455 | align_stdout([getattr(e, 'stdout', '')]))) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 456 | |
| 457 | def commit(self, commit_message, user): |
| 458 | logging.info('Committing patch for %s' % user) |
| 459 | assert self.commit_user |
maruel@chromium.org | 1bf5097 | 2011-05-05 19:57:21 +0000 | [diff] [blame] | 460 | assert isinstance(commit_message, unicode) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 461 | handle, commit_filename = tempfile.mkstemp(text=True) |
| 462 | try: |
maruel@chromium.org | 1bf5097 | 2011-05-05 19:57:21 +0000 | [diff] [blame] | 463 | # Shouldn't assume default encoding is UTF-8. But really, if you are using |
| 464 | # anything else, you are living in another world. |
| 465 | os.write(handle, commit_message.encode('utf-8')) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 466 | os.close(handle) |
| 467 | # When committing, svn won't update the Revision metadata of the checkout, |
| 468 | # so if svn commit returns "Committed revision 3.", svn info will still |
| 469 | # return "Revision: 2". Since running svn update right after svn commit |
| 470 | # creates a race condition with other committers, this code _must_ parse |
| 471 | # the output of svn commit and use a regexp to grab the revision number. |
| 472 | # Note that "Committed revision N." is localized but subprocess2 forces |
| 473 | # LANGUAGE=en. |
| 474 | args = ['commit', '--file', commit_filename] |
| 475 | # realauthor is parsed by a server-side hook. |
| 476 | if user and user != self.commit_user: |
| 477 | args.extend(['--with-revprop', 'realauthor=%s' % user]) |
| 478 | out = self._check_output_svn(args) |
| 479 | finally: |
| 480 | os.remove(commit_filename) |
| 481 | lines = filter(None, out.splitlines()) |
| 482 | match = re.match(r'^Committed revision (\d+).$', lines[-1]) |
| 483 | if not match: |
| 484 | raise PatchApplicationFailed( |
| 485 | None, |
| 486 | 'Couldn\'t make sense out of svn commit message:\n' + out) |
| 487 | return int(match.group(1)) |
| 488 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 489 | def _revert(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 490 | """Reverts local modifications or checks out if the directory is not |
| 491 | present. Use depot_tools's functionality to do this. |
| 492 | """ |
| 493 | flags = ['--ignore-externals'] |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 494 | if revision: |
| 495 | flags.extend(['--revision', str(revision)]) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 496 | if not os.path.isdir(self.project_path): |
| 497 | logging.info( |
| 498 | 'Directory %s is not present, checking it out.' % self.project_path) |
| 499 | self._check_call_svn( |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 500 | ['checkout', self.svn_url, self.project_path] + flags, |
| 501 | cwd=None, |
| 502 | timeout=FETCH_TIMEOUT) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 503 | else: |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 504 | # TODO(maruel): This command will shell out without a timeout. |
maruel@chromium.org | ea15cb7 | 2012-05-04 14:16:31 +0000 | [diff] [blame] | 505 | scm.SVN.Revert(self.project_path, no_ignore=True) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 506 | # Revive files that were deleted in scm.SVN.Revert(). |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 507 | self._check_call_svn(['update', '--force'] + flags, timeout=FETCH_TIMEOUT) |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 508 | return self._get_revision() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 509 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 510 | def _get_revision(self): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 511 | out = self._check_output_svn(['info', '.']) |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 512 | revision = int(self._parse_svn_info(out, 'revision')) |
| 513 | if revision != self._last_seen_revision: |
| 514 | logging.info('Updated to revision %d' % revision) |
| 515 | self._last_seen_revision = revision |
| 516 | return revision |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 517 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 518 | def revisions(self, rev1, rev2): |
| 519 | """Returns the number of actual commits, not just the difference between |
| 520 | numbers. |
| 521 | """ |
| 522 | rev2 = rev2 or 'HEAD' |
| 523 | # Revision range is inclusive and ordering doesn't matter, they'll appear in |
| 524 | # the order specified. |
| 525 | try: |
| 526 | out = self._check_output_svn( |
| 527 | ['log', '-q', self.svn_url, '-r', '%s:%s' % (rev1, rev2)]) |
| 528 | except subprocess.CalledProcessError: |
| 529 | return None |
| 530 | # Ignore the '----' lines. |
| 531 | return len([l for l in out.splitlines() if l.startswith('r')]) - 1 |
| 532 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 533 | |
| 534 | class GitCheckoutBase(CheckoutBase): |
| 535 | """Base class for git checkout. Not to be used as-is.""" |
maruel@chromium.org | 6ed8b50 | 2011-06-12 01:05:35 +0000 | [diff] [blame] | 536 | def __init__(self, root_dir, project_name, remote_branch, |
| 537 | post_processors=None): |
| 538 | super(GitCheckoutBase, self).__init__( |
| 539 | root_dir, project_name, post_processors) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 540 | # There is no reason to not hardcode it. |
| 541 | self.remote = 'origin' |
| 542 | self.remote_branch = remote_branch |
| 543 | self.working_branch = 'working_branch' |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 544 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 545 | def prepare(self, revision): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 546 | """Resets the git repository in a clean state. |
| 547 | |
| 548 | Checks it out if not present and deletes the working branch. |
| 549 | """ |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 550 | assert self.remote_branch |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 551 | assert os.path.isdir(self.project_path) |
| 552 | self._check_call_git(['reset', '--hard', '--quiet']) |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 553 | if revision: |
| 554 | try: |
| 555 | revision = self._check_output_git(['rev-parse', revision]) |
| 556 | except subprocess.CalledProcessError: |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 557 | self._fetch_remote() |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 558 | revision = self._check_output_git(['rev-parse', revision]) |
| 559 | self._check_call_git(['checkout', '--force', '--quiet', revision]) |
| 560 | else: |
| 561 | branches, active = self._branches() |
| 562 | if active != 'master': |
| 563 | self._check_call_git(['checkout', '--force', '--quiet', 'master']) |
| 564 | self._check_call_git(['pull', self.remote, self.remote_branch, '--quiet']) |
| 565 | if self.working_branch in branches: |
| 566 | self._call_git(['branch', '-D', self.working_branch]) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 567 | |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 568 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 569 | """Applies a patch on 'working_branch' and switch to it. |
| 570 | |
| 571 | Also commits the changes on the local branch. |
| 572 | |
| 573 | Ignores svn properties and raise an exception on unexpected ones. |
| 574 | """ |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 575 | post_processors = post_processors or self.post_processors or [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 576 | # It this throws, the checkout is corrupted. Maybe worth deleting it and |
| 577 | # trying again? |
maruel@chromium.org | 3cdb7f3 | 2011-05-05 16:37:24 +0000 | [diff] [blame] | 578 | if self.remote_branch: |
| 579 | self._check_call_git( |
| 580 | ['checkout', '-b', self.working_branch, |
| 581 | '%s/%s' % (self.remote, self.remote_branch), '--quiet']) |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 582 | for index, p in enumerate(patches): |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 583 | stdout = [] |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 584 | try: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 585 | filepath = os.path.join(self.project_path, p.filename) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 586 | if p.is_delete: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 587 | if (not os.path.exists(filepath) and |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 588 | 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] | 589 | # The file was already deleted if a prior patch with file rename |
| 590 | # was already processed because 'git apply' did it for us. |
maruel@chromium.org | 5e97563 | 2011-09-29 18:07:06 +0000 | [diff] [blame] | 591 | pass |
| 592 | else: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 593 | stdout.append(self._check_output_git(['rm', p.filename])) |
| 594 | stdout.append('Deleted.') |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 595 | else: |
| 596 | dirname = os.path.dirname(p.filename) |
| 597 | full_dir = os.path.join(self.project_path, dirname) |
| 598 | if dirname and not os.path.isdir(full_dir): |
| 599 | os.makedirs(full_dir) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 600 | stdout.append('Created missing directory %s.' % dirname) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 601 | if p.is_binary: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 602 | content = p.get() |
| 603 | with open(filepath, 'wb') as f: |
| 604 | f.write(content) |
| 605 | stdout.append('Added binary file %d bytes' % len(content)) |
| 606 | cmd = ['add', p.filename] |
| 607 | if verbose: |
| 608 | cmd.append('--verbose') |
| 609 | stdout.append(self._check_output_git(cmd)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 610 | else: |
maruel@chromium.org | 58fe662 | 2011-06-03 20:59:27 +0000 | [diff] [blame] | 611 | # No need to do anything special with p.is_new or if not |
| 612 | # p.diff_hunks. git apply manages all that already. |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 613 | cmd = ['apply', '--index', '-p%s' % p.patchlevel] |
| 614 | if verbose: |
| 615 | cmd.append('--verbose') |
| 616 | stdout.append(self._check_output_git(cmd, stdin=p.get(True))) |
| 617 | for name, value in p.svn_properties: |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 618 | # Ignore some known auto-props flags through .subversion/config, |
| 619 | # bails out on the other ones. |
| 620 | # TODO(maruel): Read ~/.subversion/config and detect the rules that |
| 621 | # applies here to figure out if the property will be correctly |
| 622 | # handled. |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 623 | stdout.append('Property %s=%s' % (name, value)) |
maruel@chromium.org | d7ca616 | 2012-08-29 17:22:22 +0000 | [diff] [blame] | 624 | if not name in ( |
maruel@chromium.org | 9799a07 | 2012-01-11 00:26:25 +0000 | [diff] [blame] | 625 | 'svn:eol-style', 'svn:executable', 'svn:mime-type'): |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 626 | raise patch.UnsupportedPatchFormat( |
| 627 | p.filename, |
| 628 | 'Cannot apply svn property %s to file %s.' % ( |
maruel@chromium.org | d7ca616 | 2012-08-29 17:22:22 +0000 | [diff] [blame] | 629 | name, p.filename)) |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 630 | for post in post_processors: |
maruel@chromium.org | 8a1396c | 2011-04-22 00:14:24 +0000 | [diff] [blame] | 631 | post(self, p) |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 632 | if verbose: |
| 633 | print p.filename |
| 634 | print align_stdout(stdout) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 635 | except OSError, e: |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 636 | raise PatchApplicationFailed(p, '%s%s' % (align_stdout(stdout), e)) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 637 | except subprocess.CalledProcessError, e: |
| 638 | raise PatchApplicationFailed( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 639 | p, |
| 640 | 'While running %s;\n%s%s' % ( |
| 641 | ' '.join(e.cmd), |
| 642 | align_stdout(stdout), |
| 643 | align_stdout([getattr(e, 'stdout', '')]))) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 644 | # Once all the patches are processed and added to the index, commit the |
| 645 | # index. |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 646 | cmd = ['commit', '-m', 'Committed patch'] |
| 647 | if verbose: |
| 648 | cmd.append('--verbose') |
| 649 | self._check_call_git(cmd) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 650 | # TODO(maruel): Weirdly enough they don't match, need to investigate. |
| 651 | #found_files = self._check_output_git( |
| 652 | # ['diff', 'master', '--name-only']).splitlines(False) |
| 653 | #assert sorted(patches.filenames) == sorted(found_files), ( |
| 654 | # sorted(out), sorted(found_files)) |
| 655 | |
| 656 | def commit(self, commit_message, user): |
| 657 | """Updates the commit message. |
| 658 | |
| 659 | Subclass needs to dcommit or push. |
| 660 | """ |
maruel@chromium.org | 1bf5097 | 2011-05-05 19:57:21 +0000 | [diff] [blame] | 661 | assert isinstance(commit_message, unicode) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 662 | self._check_call_git(['commit', '--amend', '-m', commit_message]) |
| 663 | return self._check_output_git(['rev-parse', 'HEAD']).strip() |
| 664 | |
| 665 | def _check_call_git(self, args, **kwargs): |
| 666 | kwargs.setdefault('cwd', self.project_path) |
| 667 | kwargs.setdefault('stdout', self.VOID) |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 668 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
| 669 | return subprocess2.check_call_out( |
| 670 | ['git'] + args, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 671 | |
| 672 | def _call_git(self, args, **kwargs): |
| 673 | """Like check_call but doesn't throw on failure.""" |
| 674 | kwargs.setdefault('cwd', self.project_path) |
| 675 | kwargs.setdefault('stdout', self.VOID) |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 676 | return subprocess2.call(['git'] + args, timeout=GLOBAL_TIMEOUT, **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 677 | |
| 678 | def _check_output_git(self, args, **kwargs): |
| 679 | kwargs.setdefault('cwd', self.project_path) |
maruel@chromium.org | 87e6d33 | 2011-09-09 19:01:28 +0000 | [diff] [blame] | 680 | return subprocess2.check_output( |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 681 | ['git'] + args, |
| 682 | stderr=subprocess2.STDOUT, |
| 683 | timeout=GLOBAL_TIMEOUT, |
| 684 | **kwargs) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 685 | |
| 686 | def _branches(self): |
| 687 | """Returns the list of branches and the active one.""" |
| 688 | out = self._check_output_git(['branch']).splitlines(False) |
| 689 | branches = [l[2:] for l in out] |
| 690 | active = None |
| 691 | for l in out: |
| 692 | if l.startswith('*'): |
| 693 | active = l[2:] |
| 694 | break |
| 695 | return branches, active |
| 696 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 697 | def revisions(self, rev1, rev2): |
| 698 | """Returns the number of actual commits between both hash.""" |
| 699 | self._fetch_remote() |
| 700 | |
| 701 | rev2 = rev2 or '%s/%s' % (self.remote, self.remote_branch) |
| 702 | # Revision range is ]rev1, rev2] and ordering matters. |
| 703 | try: |
| 704 | out = self._check_output_git( |
| 705 | ['log', '--format="%H"' , '%s..%s' % (rev1, rev2)]) |
| 706 | except subprocess.CalledProcessError: |
| 707 | return None |
| 708 | return len(out.splitlines()) |
| 709 | |
| 710 | def _fetch_remote(self): |
| 711 | """Fetches the remote without rebasing.""" |
| 712 | raise NotImplementedError() |
| 713 | |
| 714 | |
| 715 | class GitCheckout(GitCheckoutBase): |
| 716 | """Git checkout implementation.""" |
| 717 | def _fetch_remote(self): |
| 718 | # git fetch is always verbose even with -q -q so redirect its output. |
maruel@chromium.org | 8327354 | 2012-11-08 19:10:50 +0000 | [diff] [blame^] | 719 | self._check_call_git( |
| 720 | ['fetch', self.remote, self.remote_branch, '--quiet'], |
| 721 | timeout=FETCH_TIMEOUT) |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 722 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 723 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 724 | class ReadOnlyCheckout(object): |
| 725 | """Converts a checkout into a read-only one.""" |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 726 | def __init__(self, checkout, post_processors=None): |
maruel@chromium.org | a5129fb | 2011-06-20 18:36:25 +0000 | [diff] [blame] | 727 | super(ReadOnlyCheckout, self).__init__() |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 728 | self.checkout = checkout |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 729 | self.post_processors = (post_processors or []) + ( |
| 730 | self.checkout.post_processors or []) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 731 | |
maruel@chromium.org | 5191977 | 2011-06-12 01:27:42 +0000 | [diff] [blame] | 732 | def prepare(self, revision): |
| 733 | return self.checkout.prepare(revision) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 734 | |
| 735 | def get_settings(self, key): |
| 736 | return self.checkout.get_settings(key) |
| 737 | |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 738 | def apply_patch(self, patches, post_processors=None, verbose=False): |
maruel@chromium.org | b1d1a78 | 2011-09-29 14:13:55 +0000 | [diff] [blame] | 739 | return self.checkout.apply_patch( |
maruel@chromium.org | 4dd9f72 | 2012-10-01 16:23:03 +0000 | [diff] [blame] | 740 | patches, post_processors or self.post_processors, verbose) |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 741 | |
| 742 | def commit(self, message, user): # pylint: disable=R0201 |
| 743 | logging.info('Would have committed for %s with message: %s' % ( |
| 744 | user, message)) |
| 745 | return 'FAKE' |
| 746 | |
maruel@chromium.org | bc32ad1 | 2012-07-26 13:22:47 +0000 | [diff] [blame] | 747 | def revisions(self, rev1, rev2): |
| 748 | return self.checkout.revisions(rev1, rev2) |
| 749 | |
maruel@chromium.org | dfaecd2 | 2011-04-21 00:33:31 +0000 | [diff] [blame] | 750 | @property |
| 751 | def project_name(self): |
| 752 | return self.checkout.project_name |
| 753 | |
| 754 | @property |
| 755 | def project_path(self): |
| 756 | return self.checkout.project_path |