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