blob: 217a5a5a4bfbed8868ec1c4e43085c7fbea7e83a [file] [log] [blame]
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +00001# coding=utf8
maruel@chromium.org9799a072012-01-11 00:26:25 +00002# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +00003# 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
7Includes support for svn, git-svn and git.
8"""
9
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000010import ConfigParser
11import fnmatch
12import logging
13import os
14import re
maruel@chromium.org5e975632011-09-29 18:07:06 +000015import shutil
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000016import subprocess
17import sys
18import tempfile
19
20import patch
21import scm
22import subprocess2
23
24
25def get_code_review_setting(path, key,
26 codereview_settings_file='codereview.settings'):
27 """Parses codereview.settings and return the value for the key if present.
28
29 Don't cache the values in case the file is changed."""
30 # TODO(maruel): Do not duplicate code.
31 settings = {}
32 try:
33 settings_file = open(os.path.join(path, codereview_settings_file), 'r')
34 try:
35 for line in settings_file.readlines():
36 if not line or line.startswith('#'):
37 continue
38 if not ':' in line:
39 # Invalid file.
40 return None
41 k, v = line.split(':', 1)
42 settings[k.strip()] = v.strip()
43 finally:
44 settings_file.close()
maruel@chromium.org004fb712011-06-21 20:02:16 +000045 except IOError:
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000046 return None
47 return settings.get(key, None)
48
49
50class PatchApplicationFailed(Exception):
51 """Patch failed to be applied."""
maruel@chromium.org34f68552012-05-09 19:18:36 +000052 def __init__(self, p, status):
53 super(PatchApplicationFailed, self).__init__(p, status)
54 self.patch = p
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000055 self.status = status
56
maruel@chromium.org34f68552012-05-09 19:18:36 +000057 @property
58 def filename(self):
59 if self.patch:
60 return self.patch.filename
61
62 def __str__(self):
63 out = []
64 if self.filename:
65 out.append('Failed to apply patch for %s:' % self.filename)
66 if self.status:
67 out.append(self.status)
68 return '\n'.join(out)
69
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000070
71class CheckoutBase(object):
72 # Set to None to have verbose output.
73 VOID = subprocess2.VOID
74
maruel@chromium.org6ed8b502011-06-12 01:05:35 +000075 def __init__(self, root_dir, project_name, post_processors):
76 """
77 Args:
78 post_processor: list of lambda(checkout, patches) to call on each of the
79 modified files.
80 """
maruel@chromium.orga5129fb2011-06-20 18:36:25 +000081 super(CheckoutBase, self).__init__()
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000082 self.root_dir = root_dir
83 self.project_name = project_name
maruel@chromium.org3cdb7f32011-05-05 16:37:24 +000084 if self.project_name is None:
85 self.project_path = self.root_dir
86 else:
87 self.project_path = os.path.join(self.root_dir, self.project_name)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000088 # Only used for logging purposes.
89 self._last_seen_revision = None
maruel@chromium.orga5129fb2011-06-20 18:36:25 +000090 self.post_processors = post_processors
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000091 assert self.root_dir
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000092 assert self.project_path
93
94 def get_settings(self, key):
95 return get_code_review_setting(self.project_path, key)
96
maruel@chromium.org51919772011-06-12 01:27:42 +000097 def prepare(self, revision):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +000098 """Checks out a clean copy of the tree and removes any local modification.
99
100 This function shouldn't throw unless the remote repository is inaccessible,
101 there is no free disk space or hard issues like that.
maruel@chromium.org51919772011-06-12 01:27:42 +0000102
103 Args:
104 revision: The revision it should sync to, SCM specific.
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000105 """
106 raise NotImplementedError()
107
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000108 def apply_patch(self, patches, post_processors=None):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000109 """Applies a patch and returns the list of modified files.
110
111 This function should throw patch.UnsupportedPatchFormat or
112 PatchApplicationFailed when relevant.
maruel@chromium.org8a1396c2011-04-22 00:14:24 +0000113
114 Args:
115 patches: patch.PatchSet object.
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000116 """
117 raise NotImplementedError()
118
119 def commit(self, commit_message, user):
120 """Commits the patch upstream, while impersonating 'user'."""
121 raise NotImplementedError()
122
maruel@chromium.orgbc32ad12012-07-26 13:22:47 +0000123 def revisions(self, rev1, rev2):
124 """Returns the count of revisions from rev1 to rev2, e.g. len(]rev1, rev2]).
125
126 If rev2 is None, it means 'HEAD'.
127
128 Returns None if there is no link between the two.
129 """
130 raise NotImplementedError()
131
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000132
133class RawCheckout(CheckoutBase):
134 """Used to apply a patch locally without any intent to commit it.
135
136 To be used by the try server.
137 """
maruel@chromium.org51919772011-06-12 01:27:42 +0000138 def prepare(self, revision):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000139 """Stubbed out."""
140 pass
141
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000142 def apply_patch(self, patches, post_processors=None):
maruel@chromium.org8a1396c2011-04-22 00:14:24 +0000143 """Ignores svn properties."""
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000144 post_processors = post_processors or self.post_processors or []
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000145 for p in patches:
maruel@chromium.orgde800ff2012-09-12 19:25:24 +0000146 logging.debug('Applying %s' % p.filename)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000147 try:
148 stdout = ''
149 filename = os.path.join(self.project_path, p.filename)
150 if p.is_delete:
151 os.remove(filename)
152 else:
153 dirname = os.path.dirname(p.filename)
154 full_dir = os.path.join(self.project_path, dirname)
155 if dirname and not os.path.isdir(full_dir):
156 os.makedirs(full_dir)
maruel@chromium.org4869bcf2011-06-04 01:14:32 +0000157
158 filepath = os.path.join(self.project_path, p.filename)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000159 if p.is_binary:
maruel@chromium.org4869bcf2011-06-04 01:14:32 +0000160 with open(filepath, 'wb') as f:
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000161 f.write(p.get())
162 else:
maruel@chromium.org5e975632011-09-29 18:07:06 +0000163 if p.source_filename:
164 if not p.is_new:
165 raise PatchApplicationFailed(
maruel@chromium.org34f68552012-05-09 19:18:36 +0000166 p,
maruel@chromium.org5e975632011-09-29 18:07:06 +0000167 'File has a source filename specified but is not new')
168 # Copy the file first.
169 if os.path.isfile(filepath):
170 raise PatchApplicationFailed(
maruel@chromium.org34f68552012-05-09 19:18:36 +0000171 p, 'File exist but was about to be overwriten')
maruel@chromium.org5e975632011-09-29 18:07:06 +0000172 shutil.copy2(
173 os.path.join(self.project_path, p.source_filename), filepath)
maruel@chromium.org58fe6622011-06-03 20:59:27 +0000174 if p.diff_hunks:
175 stdout = subprocess2.check_output(
maruel@chromium.org5e975632011-09-29 18:07:06 +0000176 ['patch', '-u', '--binary', '-p%s' % p.patchlevel],
177 stdin=p.get(False),
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000178 stderr=subprocess2.STDOUT,
maruel@chromium.org58fe6622011-06-03 20:59:27 +0000179 cwd=self.project_path)
maruel@chromium.org4869bcf2011-06-04 01:14:32 +0000180 elif p.is_new and not os.path.exists(filepath):
maruel@chromium.org58fe6622011-06-03 20:59:27 +0000181 # There is only a header. Just create the file.
maruel@chromium.org4869bcf2011-06-04 01:14:32 +0000182 open(filepath, 'w').close()
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000183 for post in post_processors:
maruel@chromium.org8a1396c2011-04-22 00:14:24 +0000184 post(self, p)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000185 except OSError, e:
maruel@chromium.org34f68552012-05-09 19:18:36 +0000186 raise PatchApplicationFailed(p, '%s%s' % (stdout, e))
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000187 except subprocess.CalledProcessError, e:
188 raise PatchApplicationFailed(
maruel@chromium.org34f68552012-05-09 19:18:36 +0000189 p, '%s%s' % (stdout, getattr(e, 'stdout', None)))
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000190
191 def commit(self, commit_message, user):
192 """Stubbed out."""
193 raise NotImplementedError('RawCheckout can\'t commit')
194
maruel@chromium.orgbc32ad12012-07-26 13:22:47 +0000195 def revisions(self, _rev1, _rev2):
196 return None
197
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000198
199class SvnConfig(object):
200 """Parses a svn configuration file."""
201 def __init__(self, svn_config_dir=None):
maruel@chromium.orga5129fb2011-06-20 18:36:25 +0000202 super(SvnConfig, self).__init__()
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000203 self.svn_config_dir = svn_config_dir
204 self.default = not bool(self.svn_config_dir)
205 if not self.svn_config_dir:
206 if sys.platform == 'win32':
207 self.svn_config_dir = os.path.join(os.environ['APPDATA'], 'Subversion')
208 else:
209 self.svn_config_dir = os.path.join(os.environ['HOME'], '.subversion')
210 svn_config_file = os.path.join(self.svn_config_dir, 'config')
211 parser = ConfigParser.SafeConfigParser()
212 if os.path.isfile(svn_config_file):
213 parser.read(svn_config_file)
214 else:
215 parser.add_section('auto-props')
216 self.auto_props = dict(parser.items('auto-props'))
217
218
219class SvnMixIn(object):
220 """MixIn class to add svn commands common to both svn and git-svn clients."""
221 # These members need to be set by the subclass.
222 commit_user = None
223 commit_pwd = None
224 svn_url = None
225 project_path = None
226 # Override at class level when necessary. If used, --non-interactive is
227 # implied.
228 svn_config = SvnConfig()
229 # Set to True when non-interactivity is necessary but a custom subversion
230 # configuration directory is not necessary.
231 non_interactive = False
232
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000233 def _add_svn_flags(self, args, non_interactive, credentials=True):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000234 args = ['svn'] + args
235 if not self.svn_config.default:
236 args.extend(['--config-dir', self.svn_config.svn_config_dir])
237 if not self.svn_config.default or self.non_interactive or non_interactive:
238 args.append('--non-interactive')
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000239 if credentials:
240 if self.commit_user:
241 args.extend(['--username', self.commit_user])
242 if self.commit_pwd:
243 args.extend(['--password', self.commit_pwd])
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000244 return args
245
246 def _check_call_svn(self, args, **kwargs):
247 """Runs svn and throws an exception if the command failed."""
248 kwargs.setdefault('cwd', self.project_path)
249 kwargs.setdefault('stdout', self.VOID)
maruel@chromium.org0bcd1d32011-04-26 15:55:49 +0000250 return subprocess2.check_call_out(
251 self._add_svn_flags(args, False), **kwargs)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000252
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000253 def _check_output_svn(self, args, credentials=True, **kwargs):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000254 """Runs svn and throws an exception if the command failed.
255
256 Returns the output.
257 """
258 kwargs.setdefault('cwd', self.project_path)
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000259 return subprocess2.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000260 self._add_svn_flags(args, True, credentials),
261 stderr=subprocess2.STDOUT,
262 **kwargs)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000263
264 @staticmethod
265 def _parse_svn_info(output, key):
266 """Returns value for key from svn info output.
267
268 Case insensitive.
269 """
270 values = {}
271 key = key.lower()
272 for line in output.splitlines(False):
273 if not line:
274 continue
275 k, v = line.split(':', 1)
276 k = k.strip().lower()
277 v = v.strip()
278 assert not k in values
279 values[k] = v
280 return values.get(key, None)
281
282
283class SvnCheckout(CheckoutBase, SvnMixIn):
284 """Manages a subversion checkout."""
maruel@chromium.org6ed8b502011-06-12 01:05:35 +0000285 def __init__(self, root_dir, project_name, commit_user, commit_pwd, svn_url,
286 post_processors=None):
maruel@chromium.orga5129fb2011-06-20 18:36:25 +0000287 CheckoutBase.__init__(self, root_dir, project_name, post_processors)
288 SvnMixIn.__init__(self)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000289 self.commit_user = commit_user
290 self.commit_pwd = commit_pwd
291 self.svn_url = svn_url
292 assert bool(self.commit_user) >= bool(self.commit_pwd)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000293
maruel@chromium.org51919772011-06-12 01:27:42 +0000294 def prepare(self, revision):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000295 # Will checkout if the directory is not present.
maruel@chromium.org3cdb7f32011-05-05 16:37:24 +0000296 assert self.svn_url
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000297 if not os.path.isdir(self.project_path):
298 logging.info('Checking out %s in %s' %
299 (self.project_name, self.project_path))
maruel@chromium.org51919772011-06-12 01:27:42 +0000300 return self._revert(revision)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000301
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000302 def apply_patch(self, patches, post_processors=None):
303 post_processors = post_processors or self.post_processors or []
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000304 for p in patches:
maruel@chromium.orgde800ff2012-09-12 19:25:24 +0000305 logging.debug('Applying %s' % p.filename)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000306 try:
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000307 # It is important to use credentials=False otherwise credentials could
308 # leak in the error message. Credentials are not necessary here for the
309 # following commands anyway.
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000310 stdout = ''
311 if p.is_delete:
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000312 stdout += self._check_output_svn(
313 ['delete', p.filename, '--force'], credentials=False)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000314 else:
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000315 # svn add while creating directories otherwise svn add on the
316 # contained files will silently fail.
317 # First, find the root directory that exists.
318 dirname = os.path.dirname(p.filename)
319 dirs_to_create = []
320 while (dirname and
321 not os.path.isdir(os.path.join(self.project_path, dirname))):
322 dirs_to_create.append(dirname)
323 dirname = os.path.dirname(dirname)
324 for dir_to_create in reversed(dirs_to_create):
325 os.mkdir(os.path.join(self.project_path, dir_to_create))
326 stdout += self._check_output_svn(
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000327 ['add', dir_to_create, '--force'], credentials=False)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000328
maruel@chromium.org4869bcf2011-06-04 01:14:32 +0000329 filepath = os.path.join(self.project_path, p.filename)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000330 if p.is_binary:
maruel@chromium.org4869bcf2011-06-04 01:14:32 +0000331 with open(filepath, 'wb') as f:
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000332 f.write(p.get())
333 else:
maruel@chromium.org5e975632011-09-29 18:07:06 +0000334 if p.source_filename:
335 if not p.is_new:
336 raise PatchApplicationFailed(
maruel@chromium.org34f68552012-05-09 19:18:36 +0000337 p,
maruel@chromium.org5e975632011-09-29 18:07:06 +0000338 'File has a source filename specified but is not new')
339 # Copy the file first.
340 if os.path.isfile(filepath):
341 raise PatchApplicationFailed(
maruel@chromium.org34f68552012-05-09 19:18:36 +0000342 p, 'File exist but was about to be overwriten')
maruel@chromium.org3da83172012-05-07 16:17:20 +0000343 self._check_output_svn(
ilevy@chromium.orgb0f852f2012-09-15 01:37:21 +0000344 ['copy', p.source_filename, p.filename])
maruel@chromium.org58fe6622011-06-03 20:59:27 +0000345 if p.diff_hunks:
346 cmd = ['patch', '-p%s' % p.patchlevel, '--forward', '--force']
347 stdout += subprocess2.check_output(
maruel@chromium.org5e975632011-09-29 18:07:06 +0000348 cmd, stdin=p.get(False), cwd=self.project_path)
maruel@chromium.org4869bcf2011-06-04 01:14:32 +0000349 elif p.is_new and not os.path.exists(filepath):
350 # There is only a header. Just create the file if it doesn't
351 # exist.
352 open(filepath, 'w').close()
maruel@chromium.org3da83172012-05-07 16:17:20 +0000353 if p.is_new and not p.source_filename:
354 # Do not run it if p.source_filename is defined, since svn copy was
355 # using above.
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000356 stdout += self._check_output_svn(
357 ['add', p.filename, '--force'], credentials=False)
maruel@chromium.orgd7ca6162012-08-29 17:22:22 +0000358 for name, value in p.svn_properties:
359 if value is None:
360 stdout += self._check_output_svn(
361 ['propdel', '--quiet', name, p.filename], credentials=False)
362 else:
363 stdout += self._check_output_svn(
364 ['propset', name, value, p.filename], credentials=False)
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000365 for prop, values in self.svn_config.auto_props.iteritems():
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000366 if fnmatch.fnmatch(p.filename, prop):
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000367 for value in values.split(';'):
368 if '=' not in value:
maruel@chromium.orge1a03762012-09-24 15:28:52 +0000369 params = [value, '.']
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000370 else:
371 params = value.split('=', 1)
maruel@chromium.orge1a03762012-09-24 15:28:52 +0000372 if params[1] == '*':
373 # Works around crbug.com/150960 on Windows.
374 params[1] = '.'
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000375 stdout += self._check_output_svn(
376 ['propset'] + params + [p.filename], credentials=False)
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000377 for post in post_processors:
maruel@chromium.org8a1396c2011-04-22 00:14:24 +0000378 post(self, p)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000379 except OSError, e:
maruel@chromium.org34f68552012-05-09 19:18:36 +0000380 raise PatchApplicationFailed(p, '%s%s' % (stdout, e))
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000381 except subprocess.CalledProcessError, e:
382 raise PatchApplicationFailed(
maruel@chromium.org34f68552012-05-09 19:18:36 +0000383 p,
maruel@chromium.org9842a0c2011-05-30 20:41:54 +0000384 'While running %s;\n%s%s' % (
385 ' '.join(e.cmd), stdout, getattr(e, 'stdout', '')))
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000386
387 def commit(self, commit_message, user):
388 logging.info('Committing patch for %s' % user)
389 assert self.commit_user
maruel@chromium.org1bf50972011-05-05 19:57:21 +0000390 assert isinstance(commit_message, unicode)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000391 handle, commit_filename = tempfile.mkstemp(text=True)
392 try:
maruel@chromium.org1bf50972011-05-05 19:57:21 +0000393 # Shouldn't assume default encoding is UTF-8. But really, if you are using
394 # anything else, you are living in another world.
395 os.write(handle, commit_message.encode('utf-8'))
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000396 os.close(handle)
397 # When committing, svn won't update the Revision metadata of the checkout,
398 # so if svn commit returns "Committed revision 3.", svn info will still
399 # return "Revision: 2". Since running svn update right after svn commit
400 # creates a race condition with other committers, this code _must_ parse
401 # the output of svn commit and use a regexp to grab the revision number.
402 # Note that "Committed revision N." is localized but subprocess2 forces
403 # LANGUAGE=en.
404 args = ['commit', '--file', commit_filename]
405 # realauthor is parsed by a server-side hook.
406 if user and user != self.commit_user:
407 args.extend(['--with-revprop', 'realauthor=%s' % user])
408 out = self._check_output_svn(args)
409 finally:
410 os.remove(commit_filename)
411 lines = filter(None, out.splitlines())
412 match = re.match(r'^Committed revision (\d+).$', lines[-1])
413 if not match:
414 raise PatchApplicationFailed(
415 None,
416 'Couldn\'t make sense out of svn commit message:\n' + out)
417 return int(match.group(1))
418
maruel@chromium.org51919772011-06-12 01:27:42 +0000419 def _revert(self, revision):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000420 """Reverts local modifications or checks out if the directory is not
421 present. Use depot_tools's functionality to do this.
422 """
423 flags = ['--ignore-externals']
maruel@chromium.org51919772011-06-12 01:27:42 +0000424 if revision:
425 flags.extend(['--revision', str(revision)])
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000426 if not os.path.isdir(self.project_path):
427 logging.info(
428 'Directory %s is not present, checking it out.' % self.project_path)
429 self._check_call_svn(
430 ['checkout', self.svn_url, self.project_path] + flags, cwd=None)
431 else:
maruel@chromium.orgea15cb72012-05-04 14:16:31 +0000432 scm.SVN.Revert(self.project_path, no_ignore=True)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000433 # Revive files that were deleted in scm.SVN.Revert().
434 self._check_call_svn(['update', '--force'] + flags)
maruel@chromium.org51919772011-06-12 01:27:42 +0000435 return self._get_revision()
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000436
maruel@chromium.org51919772011-06-12 01:27:42 +0000437 def _get_revision(self):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000438 out = self._check_output_svn(['info', '.'])
maruel@chromium.org51919772011-06-12 01:27:42 +0000439 revision = int(self._parse_svn_info(out, 'revision'))
440 if revision != self._last_seen_revision:
441 logging.info('Updated to revision %d' % revision)
442 self._last_seen_revision = revision
443 return revision
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000444
maruel@chromium.orgbc32ad12012-07-26 13:22:47 +0000445 def revisions(self, rev1, rev2):
446 """Returns the number of actual commits, not just the difference between
447 numbers.
448 """
449 rev2 = rev2 or 'HEAD'
450 # Revision range is inclusive and ordering doesn't matter, they'll appear in
451 # the order specified.
452 try:
453 out = self._check_output_svn(
454 ['log', '-q', self.svn_url, '-r', '%s:%s' % (rev1, rev2)])
455 except subprocess.CalledProcessError:
456 return None
457 # Ignore the '----' lines.
458 return len([l for l in out.splitlines() if l.startswith('r')]) - 1
459
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000460
461class GitCheckoutBase(CheckoutBase):
462 """Base class for git checkout. Not to be used as-is."""
maruel@chromium.org6ed8b502011-06-12 01:05:35 +0000463 def __init__(self, root_dir, project_name, remote_branch,
464 post_processors=None):
465 super(GitCheckoutBase, self).__init__(
466 root_dir, project_name, post_processors)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000467 # There is no reason to not hardcode it.
468 self.remote = 'origin'
469 self.remote_branch = remote_branch
470 self.working_branch = 'working_branch'
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000471
maruel@chromium.org51919772011-06-12 01:27:42 +0000472 def prepare(self, revision):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000473 """Resets the git repository in a clean state.
474
475 Checks it out if not present and deletes the working branch.
476 """
maruel@chromium.org3cdb7f32011-05-05 16:37:24 +0000477 assert self.remote_branch
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000478 assert os.path.isdir(self.project_path)
479 self._check_call_git(['reset', '--hard', '--quiet'])
maruel@chromium.org51919772011-06-12 01:27:42 +0000480 if revision:
481 try:
482 revision = self._check_output_git(['rev-parse', revision])
483 except subprocess.CalledProcessError:
484 self._check_call_git(
485 ['fetch', self.remote, self.remote_branch, '--quiet'])
486 revision = self._check_output_git(['rev-parse', revision])
487 self._check_call_git(['checkout', '--force', '--quiet', revision])
488 else:
489 branches, active = self._branches()
490 if active != 'master':
491 self._check_call_git(['checkout', '--force', '--quiet', 'master'])
492 self._check_call_git(['pull', self.remote, self.remote_branch, '--quiet'])
493 if self.working_branch in branches:
494 self._call_git(['branch', '-D', self.working_branch])
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000495
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000496 def apply_patch(self, patches, post_processors=None):
maruel@chromium.org8a1396c2011-04-22 00:14:24 +0000497 """Applies a patch on 'working_branch' and switch to it.
498
499 Also commits the changes on the local branch.
500
501 Ignores svn properties and raise an exception on unexpected ones.
502 """
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000503 post_processors = post_processors or self.post_processors or []
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000504 # It this throws, the checkout is corrupted. Maybe worth deleting it and
505 # trying again?
maruel@chromium.org3cdb7f32011-05-05 16:37:24 +0000506 if self.remote_branch:
507 self._check_call_git(
508 ['checkout', '-b', self.working_branch,
509 '%s/%s' % (self.remote, self.remote_branch), '--quiet'])
maruel@chromium.org5e975632011-09-29 18:07:06 +0000510 for index, p in enumerate(patches):
maruel@chromium.orgde800ff2012-09-12 19:25:24 +0000511 logging.debug('Applying %s' % p.filename)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000512 try:
513 stdout = ''
514 if p.is_delete:
maruel@chromium.org5e975632011-09-29 18:07:06 +0000515 if (not os.path.exists(p.filename) and
516 any(p1.source_filename == p.filename for p1 in patches[0:index])):
517 # The file could already be deleted if a prior patch with file
518 # rename was already processed. To be sure, look at all the previous
519 # patches to see if they were a file rename.
520 pass
521 else:
522 stdout += self._check_output_git(['rm', p.filename])
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000523 else:
524 dirname = os.path.dirname(p.filename)
525 full_dir = os.path.join(self.project_path, dirname)
526 if dirname and not os.path.isdir(full_dir):
527 os.makedirs(full_dir)
528 if p.is_binary:
529 with open(os.path.join(self.project_path, p.filename), 'wb') as f:
530 f.write(p.get())
531 stdout += self._check_output_git(['add', p.filename])
532 else:
maruel@chromium.org58fe6622011-06-03 20:59:27 +0000533 # No need to do anything special with p.is_new or if not
534 # p.diff_hunks. git apply manages all that already.
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000535 stdout += self._check_output_git(
maruel@chromium.org5e975632011-09-29 18:07:06 +0000536 ['apply', '--index', '-p%s' % p.patchlevel], stdin=p.get(True))
maruel@chromium.orgd7ca6162012-08-29 17:22:22 +0000537 for name, _ in p.svn_properties:
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000538 # Ignore some known auto-props flags through .subversion/config,
539 # bails out on the other ones.
540 # TODO(maruel): Read ~/.subversion/config and detect the rules that
541 # applies here to figure out if the property will be correctly
542 # handled.
maruel@chromium.orgd7ca6162012-08-29 17:22:22 +0000543 if not name in (
maruel@chromium.org9799a072012-01-11 00:26:25 +0000544 'svn:eol-style', 'svn:executable', 'svn:mime-type'):
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000545 raise patch.UnsupportedPatchFormat(
546 p.filename,
547 'Cannot apply svn property %s to file %s.' % (
maruel@chromium.orgd7ca6162012-08-29 17:22:22 +0000548 name, p.filename))
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000549 for post in post_processors:
maruel@chromium.org8a1396c2011-04-22 00:14:24 +0000550 post(self, p)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000551 except OSError, e:
maruel@chromium.org34f68552012-05-09 19:18:36 +0000552 raise PatchApplicationFailed(p, '%s%s' % (stdout, e))
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000553 except subprocess.CalledProcessError, e:
554 raise PatchApplicationFailed(
maruel@chromium.org34f68552012-05-09 19:18:36 +0000555 p, '%s%s' % (stdout, getattr(e, 'stdout', None)))
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000556 # Once all the patches are processed and added to the index, commit the
557 # index.
558 self._check_call_git(['commit', '-m', 'Committed patch'])
559 # TODO(maruel): Weirdly enough they don't match, need to investigate.
560 #found_files = self._check_output_git(
561 # ['diff', 'master', '--name-only']).splitlines(False)
562 #assert sorted(patches.filenames) == sorted(found_files), (
563 # sorted(out), sorted(found_files))
564
565 def commit(self, commit_message, user):
566 """Updates the commit message.
567
568 Subclass needs to dcommit or push.
569 """
maruel@chromium.org1bf50972011-05-05 19:57:21 +0000570 assert isinstance(commit_message, unicode)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000571 self._check_call_git(['commit', '--amend', '-m', commit_message])
572 return self._check_output_git(['rev-parse', 'HEAD']).strip()
573
574 def _check_call_git(self, args, **kwargs):
575 kwargs.setdefault('cwd', self.project_path)
576 kwargs.setdefault('stdout', self.VOID)
maruel@chromium.org0bcd1d32011-04-26 15:55:49 +0000577 return subprocess2.check_call_out(['git'] + args, **kwargs)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000578
579 def _call_git(self, args, **kwargs):
580 """Like check_call but doesn't throw on failure."""
581 kwargs.setdefault('cwd', self.project_path)
582 kwargs.setdefault('stdout', self.VOID)
583 return subprocess2.call(['git'] + args, **kwargs)
584
585 def _check_output_git(self, args, **kwargs):
586 kwargs.setdefault('cwd', self.project_path)
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000587 return subprocess2.check_output(
588 ['git'] + args, stderr=subprocess2.STDOUT, **kwargs)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000589
590 def _branches(self):
591 """Returns the list of branches and the active one."""
592 out = self._check_output_git(['branch']).splitlines(False)
593 branches = [l[2:] for l in out]
594 active = None
595 for l in out:
596 if l.startswith('*'):
597 active = l[2:]
598 break
599 return branches, active
600
maruel@chromium.orgbc32ad12012-07-26 13:22:47 +0000601 def revisions(self, rev1, rev2):
602 """Returns the number of actual commits between both hash."""
603 self._fetch_remote()
604
605 rev2 = rev2 or '%s/%s' % (self.remote, self.remote_branch)
606 # Revision range is ]rev1, rev2] and ordering matters.
607 try:
608 out = self._check_output_git(
609 ['log', '--format="%H"' , '%s..%s' % (rev1, rev2)])
610 except subprocess.CalledProcessError:
611 return None
612 return len(out.splitlines())
613
614 def _fetch_remote(self):
615 """Fetches the remote without rebasing."""
616 raise NotImplementedError()
617
618
619class GitCheckout(GitCheckoutBase):
620 """Git checkout implementation."""
621 def _fetch_remote(self):
622 # git fetch is always verbose even with -q -q so redirect its output.
623 self._check_output_git(['fetch', self.remote, self.remote_branch])
624
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000625
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000626class ReadOnlyCheckout(object):
627 """Converts a checkout into a read-only one."""
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000628 def __init__(self, checkout, post_processors=None):
maruel@chromium.orga5129fb2011-06-20 18:36:25 +0000629 super(ReadOnlyCheckout, self).__init__()
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000630 self.checkout = checkout
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000631 self.post_processors = (post_processors or []) + (
632 self.checkout.post_processors or [])
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000633
maruel@chromium.org51919772011-06-12 01:27:42 +0000634 def prepare(self, revision):
635 return self.checkout.prepare(revision)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000636
637 def get_settings(self, key):
638 return self.checkout.get_settings(key)
639
maruel@chromium.orgb1d1a782011-09-29 14:13:55 +0000640 def apply_patch(self, patches, post_processors=None):
641 return self.checkout.apply_patch(
642 patches, post_processors or self.post_processors)
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000643
644 def commit(self, message, user): # pylint: disable=R0201
645 logging.info('Would have committed for %s with message: %s' % (
646 user, message))
647 return 'FAKE'
648
maruel@chromium.orgbc32ad12012-07-26 13:22:47 +0000649 def revisions(self, rev1, rev2):
650 return self.checkout.revisions(rev1, rev2)
651
maruel@chromium.orgdfaecd22011-04-21 00:33:31 +0000652 @property
653 def project_name(self):
654 return self.checkout.project_name
655
656 @property
657 def project_path(self):
658 return self.checkout.project_path