blob: e2a0017ca6433c13759e2b3299005ad7c979ecae [file] [log] [blame]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +00001# Copyright (c) 2009 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00004
maruel@chromium.orgd5800f12009-11-12 20:03:43 +00005"""Gclient-specific SCM-specific operations."""
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00006
maruel@chromium.org754960e2009-09-21 12:31:05 +00007import logging
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00008import os
9import re
10import subprocess
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000011
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000012import scm
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000013import gclient_utils
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000014
15
16### SCM abstraction layer
17
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000018# Factory Method for SCM wrapper creation
19
20def CreateSCM(url=None, root_dir=None, relpath=None, scm_name='svn'):
21 # TODO(maruel): Deduce the SCM from the url.
22 scm_map = {
23 'svn' : SVNWrapper,
msb@chromium.orge28e4982009-09-25 20:51:45 +000024 'git' : GitWrapper,
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000025 }
msb@chromium.orge28e4982009-09-25 20:51:45 +000026
27 if url and (url.startswith('git:') or
28 url.startswith('ssh:') or
29 url.endswith('.git')):
30 scm_name = 'git'
31
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000032 if not scm_name in scm_map:
33 raise gclient_utils.Error('Unsupported scm %s' % scm_name)
34 return scm_map[scm_name](url, root_dir, relpath, scm_name)
35
36
37# SCMWrapper base class
38
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000039class SCMWrapper(object):
40 """Add necessary glue between all the supported SCM.
41
42 This is the abstraction layer to bind to different SCM. Since currently only
43 subversion is supported, a lot of subersionism remains. This can be sorted out
44 once another SCM is supported."""
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +000045 def __init__(self, url=None, root_dir=None, relpath=None,
46 scm_name='svn'):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000047 self.scm_name = scm_name
48 self.url = url
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +000049 self._root_dir = root_dir
50 if self._root_dir:
51 self._root_dir = self._root_dir.replace('/', os.sep)
52 self.relpath = relpath
53 if self.relpath:
54 self.relpath = self.relpath.replace('/', os.sep)
msb@chromium.orge28e4982009-09-25 20:51:45 +000055 if self.relpath and self._root_dir:
56 self.checkout_path = os.path.join(self._root_dir, self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000057
58 def FullUrlForRelativeUrl(self, url):
59 # Find the forth '/' and strip from there. A bit hackish.
60 return '/'.join(self.url.split('/')[:4]) + url
61
62 def RunCommand(self, command, options, args, file_list=None):
63 # file_list will have all files that are modified appended to it.
maruel@chromium.orgde754ac2009-09-17 18:04:50 +000064 if file_list is None:
65 file_list = []
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000066
msb@chromium.org0f282062009-11-06 20:14:02 +000067 commands = ['cleanup', 'export', 'update', 'revert', 'revinfo',
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000068 'status', 'diff', 'pack', 'runhooks']
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000069
70 if not command in commands:
71 raise gclient_utils.Error('Unknown command %s' % command)
72
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000073 if not command in dir(self):
74 raise gclient_utils.Error('Command %s not implemnted in %s wrapper' % (
75 command, self.scm_name))
76
77 return getattr(self, command)(options, args, file_list)
78
79
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000080class GitWrapper(SCMWrapper, scm.GIT):
msb@chromium.orge28e4982009-09-25 20:51:45 +000081 """Wrapper for Git"""
82
83 def cleanup(self, options, args, file_list):
84 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +000085 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000086 self._Run(['prune'], redirect_stdout=False)
87 self._Run(['fsck'], redirect_stdout=False)
88 self._Run(['gc'], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +000089
90 def diff(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +000091 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000092 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
93 self._Run(['diff', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +000094
95 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +000096 __pychecker__ = 'unusednames=file_list,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +000097 assert len(args) == 1
98 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
99 if not os.path.exists(export_path):
100 os.makedirs(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000101 self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path],
102 redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000103
104 def update(self, options, args, file_list):
105 """Runs git to update or transparently checkout the working copy.
106
107 All updated files will be appended to file_list.
108
109 Raises:
110 Error: if can't get URL for relative path.
111 """
112
113 if args:
114 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
115
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000116 url, revision = gclient_utils.SplitUrlRevision(self.url)
117 rev_str = ""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000118 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000119 # Override the revision number.
120 revision = str(options.revision)
121 if revision:
122 url = '%s@%s' % (url, revision)
123 rev_str = ' at %s' % revision
msb@chromium.orge28e4982009-09-25 20:51:45 +0000124
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000125 if options.verbose:
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000126 print("\n_____ %s%s" % (self.relpath, rev_str))
127
msb@chromium.orge28e4982009-09-25 20:51:45 +0000128 if not os.path.exists(self.checkout_path):
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000129 self._Run(['clone', url, self.checkout_path],
130 cwd=self._root_dir, redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000131 if revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000132 self._Run(['reset', '--hard', revision], redirect_stdout=False)
133 files = self._Run(['ls-files']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000134 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
135 return
136
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000137 self._Run(['remote', 'update'], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000138 new_base = 'origin'
139 if revision:
140 new_base = revision
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000141 files = self._Run(['diff', new_base, '--name-only']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000142 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000143 self._Run(['rebase', '-v', new_base], redirect_stdout=False)
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000144 print "Checked out revision %s." % self.revinfo(options, (), None)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000145
146 def revert(self, options, args, file_list):
147 """Reverts local modifications.
148
149 All reverted files will be appended to file_list.
150 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000151 __pychecker__ = 'unusednames=args'
msb@chromium.org260c6532009-10-28 03:22:35 +0000152 path = os.path.join(self._root_dir, self.relpath)
153 if not os.path.isdir(path):
154 # revert won't work if the directory doesn't exist. It needs to
155 # checkout instead.
156 print("\n_____ %s is missing, synching instead" % self.relpath)
157 # Don't reuse the args.
158 return self.update(options, [], file_list)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000159 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
160 files = self._Run(['diff', merge_base, '--name-only']).split()
161 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000162 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
163
msb@chromium.org0f282062009-11-06 20:14:02 +0000164 def revinfo(self, options, args, file_list):
165 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000166 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000167 return self._Run(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000168
msb@chromium.orge28e4982009-09-25 20:51:45 +0000169 def runhooks(self, options, args, file_list):
170 self.status(options, args, file_list)
171
172 def status(self, options, args, file_list):
173 """Display status information."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000174 __pychecker__ = 'unusednames=args,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000175 if not os.path.isdir(self.checkout_path):
176 print('\n________ couldn\'t run status in %s:\nThe directory '
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000177 'does not exist.' % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000178 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000179 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
180 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
181 files = self._Run(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000182 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
183
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000184 def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True):
185 # TODO(maruel): Merge with Capture?
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000186 stdout=None
187 if redirect_stdout:
188 stdout=subprocess.PIPE
msb@chromium.orge28e4982009-09-25 20:51:45 +0000189 if cwd == None:
190 cwd = self.checkout_path
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000191 cmd = [self.COMMAND]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000192 cmd.extend(args)
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000193 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000194 if checkrc and sp.returncode:
195 raise gclient_utils.Error('git command %s returned %d' %
196 (args[0], sp.returncode))
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000197 output = sp.communicate()[0]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000198 if output is not None:
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000199 return output.strip()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000200
201
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000202class SVNWrapper(SCMWrapper, scm.SVN):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000203 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000204
205 def cleanup(self, options, args, file_list):
206 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000207 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000208 command = ['cleanup']
209 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000210 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000211
212 def diff(self, options, args, file_list):
213 # NOTE: This function does not currently modify file_list.
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000214 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000215 command = ['diff']
216 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000217 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000218
219 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000220 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000221 assert len(args) == 1
222 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
223 try:
224 os.makedirs(export_path)
225 except OSError:
226 pass
227 assert os.path.exists(export_path)
228 command = ['export', '--force', '.']
229 command.append(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000230 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000231
232 def update(self, options, args, file_list):
233 """Runs SCM to update or transparently checkout the working copy.
234
235 All updated files will be appended to file_list.
236
237 Raises:
238 Error: if can't get URL for relative path.
239 """
240 # Only update if git is not controlling the directory.
241 checkout_path = os.path.join(self._root_dir, self.relpath)
242 git_path = os.path.join(self._root_dir, self.relpath, '.git')
243 if os.path.exists(git_path):
244 print("________ found .git directory; skipping %s" % self.relpath)
245 return
246
247 if args:
248 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
249
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000250 url, revision = gclient_utils.SplitUrlRevision(self.url)
251 base_url = url
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000252 forced_revision = False
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000253 rev_str = ""
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000254 if options.revision:
255 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000256 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000257 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000258 forced_revision = True
259 url = '%s@%s' % (url, revision)
msb@chromium.org770ff9e2009-09-23 17:18:18 +0000260 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000261
262 if not os.path.exists(checkout_path):
263 # We need to checkout.
264 command = ['checkout', url, checkout_path]
265 if revision:
266 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000267 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000268 return
269
270 # Get the existing scm url and the revision number of the current checkout.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000271 from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000272 if not from_info:
273 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
274 "directory is present. Delete the directory "
275 "and try again." %
276 checkout_path)
277
maruel@chromium.org7753d242009-10-07 17:40:24 +0000278 if options.manually_grab_svn_rev:
279 # Retrieve the current HEAD version because svn is slow at null updates.
280 if not revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000281 from_info_live = self.CaptureInfo(from_info['URL'], '.')
maruel@chromium.org7753d242009-10-07 17:40:24 +0000282 revision = str(from_info_live['Revision'])
283 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000284
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000285 if from_info['URL'] != base_url:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000286 to_info = self.CaptureInfo(url, '.')
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +0000287 if not to_info.get('Repository Root') or not to_info.get('UUID'):
288 # The url is invalid or the server is not accessible, it's safer to bail
289 # out right now.
290 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000291 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
292 and (from_info['UUID'] == to_info['UUID']))
293 if can_switch:
294 print("\n_____ relocating %s to a new checkout" % self.relpath)
295 # We have different roots, so check if we can switch --relocate.
296 # Subversion only permits this if the repository UUIDs match.
297 # Perform the switch --relocate, then rewrite the from_url
298 # to reflect where we "are now." (This is the same way that
299 # Subversion itself handles the metadata when switch --relocate
300 # is used.) This makes the checks below for whether we
301 # can update to a revision or have to switch to a different
302 # branch work as expected.
303 # TODO(maruel): TEST ME !
304 command = ["switch", "--relocate",
305 from_info['Repository Root'],
306 to_info['Repository Root'],
307 self.relpath]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000308 self.Run(command, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000309 from_info['URL'] = from_info['URL'].replace(
310 from_info['Repository Root'],
311 to_info['Repository Root'])
312 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000313 if self.CaptureStatus(checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000314 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
315 "don't match and there is local changes "
316 "in %s. Delete the directory and "
317 "try again." % (url, checkout_path))
318 # Ok delete it.
319 print("\n_____ switching %s to a new checkout" % self.relpath)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000320 gclient_utils.RemoveDirectory(checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000321 # We need to checkout.
322 command = ['checkout', url, checkout_path]
323 if revision:
324 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000325 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000326 return
327
328
329 # If the provided url has a revision number that matches the revision
330 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +0000331 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000332 if options.verbose or not forced_revision:
333 print("\n_____ %s%s" % (self.relpath, rev_str))
334 return
335
336 command = ["update", checkout_path]
337 if revision:
338 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000339 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000340
341 def revert(self, options, args, file_list):
342 """Reverts local modifications. Subversion specific.
343
344 All reverted files will be appended to file_list, even if Subversion
345 doesn't know about them.
346 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000347 __pychecker__ = 'unusednames=args'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000348 path = os.path.join(self._root_dir, self.relpath)
349 if not os.path.isdir(path):
350 # svn revert won't work if the directory doesn't exist. It needs to
351 # checkout instead.
352 print("\n_____ %s is missing, synching instead" % self.relpath)
353 # Don't reuse the args.
354 return self.update(options, [], file_list)
355
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000356 for file_status in self.CaptureStatus(path):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000357 file_path = os.path.join(path, file_status[1])
358 if file_status[0][0] == 'X':
maruel@chromium.org754960e2009-09-21 12:31:05 +0000359 # Ignore externals.
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000360 logging.info('Ignoring external %s' % file_path)
maruel@chromium.org754960e2009-09-21 12:31:05 +0000361 continue
362
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000363 if logging.getLogger().isEnabledFor(logging.INFO):
364 logging.info('%s%s' % (file[0], file[1]))
365 else:
366 print(file_path)
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000367 if file_status[0].isspace():
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000368 logging.error('No idea what is the status of %s.\n'
369 'You just found a bug in gclient, please ping '
370 'maruel@chromium.org ASAP!' % file_path)
371 # svn revert is really stupid. It fails on inconsistent line-endings,
372 # on switched directories, etc. So take no chance and delete everything!
373 try:
374 if not os.path.exists(file_path):
375 pass
376 elif os.path.isfile(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000377 logging.info('os.remove(%s)' % file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000378 os.remove(file_path)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000379 elif os.path.isdir(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000380 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000381 gclient_utils.RemoveDirectory(file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000382 else:
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000383 logging.error('no idea what is %s.\nYou just found a bug in gclient'
384 ', please ping maruel@chromium.org ASAP!' % file_path)
385 except EnvironmentError:
386 logging.error('Failed to remove %s.' % file_path)
387
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000388 try:
389 # svn revert is so broken we don't even use it. Using
390 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000391 self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000392 file_list)
393 except OSError, e:
394 # Maybe the directory disapeared meanwhile. We don't want it to throw an
395 # exception.
396 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000397
msb@chromium.org0f282062009-11-06 20:14:02 +0000398 def revinfo(self, options, args, file_list):
399 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000400 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000401 return self.CaptureHeadRevision(self.url)
msb@chromium.org0f282062009-11-06 20:14:02 +0000402
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000403 def runhooks(self, options, args, file_list):
404 self.status(options, args, file_list)
405
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000406 def status(self, options, args, file_list):
407 """Display status information."""
408 path = os.path.join(self._root_dir, self.relpath)
409 command = ['status']
410 command.extend(args)
411 if not os.path.isdir(path):
412 # svn status won't work if the directory doesn't exist.
413 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
414 "does not exist."
415 % (' '.join(command), path))
416 # There's no file list to retrieve.
417 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000418 self.RunAndGetFileList(options, command, path, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000419
420 def pack(self, options, args, file_list):
421 """Generates a patch file which can be applied to the root of the
422 repository."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000423 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000424 path = os.path.join(self._root_dir, self.relpath)
425 command = ['diff']
426 command.extend(args)
427 # Simple class which tracks which file is being diffed and
428 # replaces instances of its file name in the original and
429 # working copy lines of the svn diff output.
430 class DiffFilterer(object):
431 index_string = "Index: "
432 original_prefix = "--- "
433 working_prefix = "+++ "
434
435 def __init__(self, relpath):
436 # Note that we always use '/' as the path separator to be
437 # consistent with svn's cygwin-style output on Windows
438 self._relpath = relpath.replace("\\", "/")
439 self._current_file = ""
440 self._replacement_file = ""
441
442 def SetCurrentFile(self, file):
443 self._current_file = file
444 # Note that we always use '/' as the path separator to be
445 # consistent with svn's cygwin-style output on Windows
446 self._replacement_file = self._relpath + '/' + file
447
448 def ReplaceAndPrint(self, line):
449 print(line.replace(self._current_file, self._replacement_file))
450
451 def Filter(self, line):
452 if (line.startswith(self.index_string)):
453 self.SetCurrentFile(line[len(self.index_string):])
454 self.ReplaceAndPrint(line)
455 else:
456 if (line.startswith(self.original_prefix) or
457 line.startswith(self.working_prefix)):
458 self.ReplaceAndPrint(line)
459 else:
460 print line
461
462 filterer = DiffFilterer(self.relpath)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000463 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)