blob: c3c5377fd9e8f45733b56de2fbfd0ecd4d0a24d4 [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:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000122 rev_str = ' at %s' % revision
msb@chromium.orge28e4982009-09-25 20:51:45 +0000123
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000124 if options.verbose:
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000125 print("\n_____ %s%s" % (self.relpath, rev_str))
126
msb@chromium.orge28e4982009-09-25 20:51:45 +0000127 if not os.path.exists(self.checkout_path):
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000128 self._Run(['clone', url, self.checkout_path],
129 cwd=self._root_dir, redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000130 if revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000131 self._Run(['reset', '--hard', revision], redirect_stdout=False)
132 files = self._Run(['ls-files']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000133 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
134 return
135
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000136 self._Run(['remote', 'update'], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000137 new_base = 'origin'
138 if revision:
139 new_base = revision
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000140 files = self._Run(['diff', new_base, '--name-only']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000141 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000142 self._Run(['rebase', '-v', new_base], redirect_stdout=False)
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000143 print "Checked out revision %s." % self.revinfo(options, (), None)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000144
145 def revert(self, options, args, file_list):
146 """Reverts local modifications.
147
148 All reverted files will be appended to file_list.
149 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000150 __pychecker__ = 'unusednames=args'
msb@chromium.org260c6532009-10-28 03:22:35 +0000151 path = os.path.join(self._root_dir, self.relpath)
152 if not os.path.isdir(path):
153 # revert won't work if the directory doesn't exist. It needs to
154 # checkout instead.
155 print("\n_____ %s is missing, synching instead" % self.relpath)
156 # Don't reuse the args.
157 return self.update(options, [], file_list)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000158 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
159 files = self._Run(['diff', merge_base, '--name-only']).split()
160 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000161 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
162
msb@chromium.org0f282062009-11-06 20:14:02 +0000163 def revinfo(self, options, args, file_list):
164 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000165 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000166 return self._Run(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000167
msb@chromium.orge28e4982009-09-25 20:51:45 +0000168 def runhooks(self, options, args, file_list):
169 self.status(options, args, file_list)
170
171 def status(self, options, args, file_list):
172 """Display status information."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000173 __pychecker__ = 'unusednames=args,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000174 if not os.path.isdir(self.checkout_path):
175 print('\n________ couldn\'t run status in %s:\nThe directory '
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000176 'does not exist.' % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000177 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000178 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
179 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
180 files = self._Run(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000181 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
182
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000183 def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True):
184 # TODO(maruel): Merge with Capture?
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000185 stdout=None
186 if redirect_stdout:
187 stdout=subprocess.PIPE
msb@chromium.orge28e4982009-09-25 20:51:45 +0000188 if cwd == None:
189 cwd = self.checkout_path
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000190 cmd = [self.COMMAND]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000191 cmd.extend(args)
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000192 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000193 if checkrc and sp.returncode:
194 raise gclient_utils.Error('git command %s returned %d' %
195 (args[0], sp.returncode))
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000196 output = sp.communicate()[0]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000197 if output is not None:
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000198 return output.strip()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000199
200
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000201class SVNWrapper(SCMWrapper, scm.SVN):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000202 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000203
204 def cleanup(self, options, args, file_list):
205 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000206 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000207 command = ['cleanup']
208 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000209 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000210
211 def diff(self, options, args, file_list):
212 # NOTE: This function does not currently modify file_list.
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000213 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000214 command = ['diff']
215 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000216 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000217
218 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000219 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000220 assert len(args) == 1
221 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
222 try:
223 os.makedirs(export_path)
224 except OSError:
225 pass
226 assert os.path.exists(export_path)
227 command = ['export', '--force', '.']
228 command.append(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000229 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000230
231 def update(self, options, args, file_list):
232 """Runs SCM to update or transparently checkout the working copy.
233
234 All updated files will be appended to file_list.
235
236 Raises:
237 Error: if can't get URL for relative path.
238 """
239 # Only update if git is not controlling the directory.
240 checkout_path = os.path.join(self._root_dir, self.relpath)
241 git_path = os.path.join(self._root_dir, self.relpath, '.git')
242 if os.path.exists(git_path):
243 print("________ found .git directory; skipping %s" % self.relpath)
244 return
245
246 if args:
247 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
248
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000249 url, revision = gclient_utils.SplitUrlRevision(self.url)
250 base_url = url
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000251 forced_revision = False
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000252 rev_str = ""
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000253 if options.revision:
254 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000255 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000256 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000257 forced_revision = True
258 url = '%s@%s' % (url, revision)
msb@chromium.org770ff9e2009-09-23 17:18:18 +0000259 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000260
261 if not os.path.exists(checkout_path):
262 # We need to checkout.
263 command = ['checkout', url, checkout_path]
264 if revision:
265 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000266 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000267 return
268
269 # Get the existing scm url and the revision number of the current checkout.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000270 from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000271 if not from_info:
272 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
273 "directory is present. Delete the directory "
274 "and try again." %
275 checkout_path)
276
maruel@chromium.org7753d242009-10-07 17:40:24 +0000277 if options.manually_grab_svn_rev:
278 # Retrieve the current HEAD version because svn is slow at null updates.
279 if not revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000280 from_info_live = self.CaptureInfo(from_info['URL'], '.')
maruel@chromium.org7753d242009-10-07 17:40:24 +0000281 revision = str(from_info_live['Revision'])
282 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000283
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000284 if from_info['URL'] != base_url:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000285 to_info = self.CaptureInfo(url, '.')
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +0000286 if not to_info.get('Repository Root') or not to_info.get('UUID'):
287 # The url is invalid or the server is not accessible, it's safer to bail
288 # out right now.
289 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000290 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
291 and (from_info['UUID'] == to_info['UUID']))
292 if can_switch:
293 print("\n_____ relocating %s to a new checkout" % self.relpath)
294 # We have different roots, so check if we can switch --relocate.
295 # Subversion only permits this if the repository UUIDs match.
296 # Perform the switch --relocate, then rewrite the from_url
297 # to reflect where we "are now." (This is the same way that
298 # Subversion itself handles the metadata when switch --relocate
299 # is used.) This makes the checks below for whether we
300 # can update to a revision or have to switch to a different
301 # branch work as expected.
302 # TODO(maruel): TEST ME !
303 command = ["switch", "--relocate",
304 from_info['Repository Root'],
305 to_info['Repository Root'],
306 self.relpath]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000307 self.Run(command, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000308 from_info['URL'] = from_info['URL'].replace(
309 from_info['Repository Root'],
310 to_info['Repository Root'])
311 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000312 if self.CaptureStatus(checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000313 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
314 "don't match and there is local changes "
315 "in %s. Delete the directory and "
316 "try again." % (url, checkout_path))
317 # Ok delete it.
318 print("\n_____ switching %s to a new checkout" % self.relpath)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000319 gclient_utils.RemoveDirectory(checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000320 # We need to checkout.
321 command = ['checkout', url, checkout_path]
322 if revision:
323 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000324 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000325 return
326
327
328 # If the provided url has a revision number that matches the revision
329 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +0000330 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000331 if options.verbose or not forced_revision:
332 print("\n_____ %s%s" % (self.relpath, rev_str))
333 return
334
335 command = ["update", checkout_path]
336 if revision:
337 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000338 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000339
340 def revert(self, options, args, file_list):
341 """Reverts local modifications. Subversion specific.
342
343 All reverted files will be appended to file_list, even if Subversion
344 doesn't know about them.
345 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000346 __pychecker__ = 'unusednames=args'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000347 path = os.path.join(self._root_dir, self.relpath)
348 if not os.path.isdir(path):
349 # svn revert won't work if the directory doesn't exist. It needs to
350 # checkout instead.
351 print("\n_____ %s is missing, synching instead" % self.relpath)
352 # Don't reuse the args.
353 return self.update(options, [], file_list)
354
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000355 for file_status in self.CaptureStatus(path):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000356 file_path = os.path.join(path, file_status[1])
357 if file_status[0][0] == 'X':
maruel@chromium.org754960e2009-09-21 12:31:05 +0000358 # Ignore externals.
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000359 logging.info('Ignoring external %s' % file_path)
maruel@chromium.org754960e2009-09-21 12:31:05 +0000360 continue
361
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000362 if logging.getLogger().isEnabledFor(logging.INFO):
363 logging.info('%s%s' % (file[0], file[1]))
364 else:
365 print(file_path)
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000366 if file_status[0].isspace():
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000367 logging.error('No idea what is the status of %s.\n'
368 'You just found a bug in gclient, please ping '
369 'maruel@chromium.org ASAP!' % file_path)
370 # svn revert is really stupid. It fails on inconsistent line-endings,
371 # on switched directories, etc. So take no chance and delete everything!
372 try:
373 if not os.path.exists(file_path):
374 pass
375 elif os.path.isfile(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000376 logging.info('os.remove(%s)' % file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000377 os.remove(file_path)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000378 elif os.path.isdir(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000379 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000380 gclient_utils.RemoveDirectory(file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000381 else:
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000382 logging.error('no idea what is %s.\nYou just found a bug in gclient'
383 ', please ping maruel@chromium.org ASAP!' % file_path)
384 except EnvironmentError:
385 logging.error('Failed to remove %s.' % file_path)
386
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000387 try:
388 # svn revert is so broken we don't even use it. Using
389 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000390 self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000391 file_list)
392 except OSError, e:
393 # Maybe the directory disapeared meanwhile. We don't want it to throw an
394 # exception.
395 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000396
msb@chromium.org0f282062009-11-06 20:14:02 +0000397 def revinfo(self, options, args, file_list):
398 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000399 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000400 return self.CaptureHeadRevision(self.url)
msb@chromium.org0f282062009-11-06 20:14:02 +0000401
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000402 def runhooks(self, options, args, file_list):
403 self.status(options, args, file_list)
404
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000405 def status(self, options, args, file_list):
406 """Display status information."""
407 path = os.path.join(self._root_dir, self.relpath)
408 command = ['status']
409 command.extend(args)
410 if not os.path.isdir(path):
411 # svn status won't work if the directory doesn't exist.
412 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
413 "does not exist."
414 % (' '.join(command), path))
415 # There's no file list to retrieve.
416 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000417 self.RunAndGetFileList(options, command, path, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000418
419 def pack(self, options, args, file_list):
420 """Generates a patch file which can be applied to the root of the
421 repository."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000422 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000423 path = os.path.join(self._root_dir, self.relpath)
424 command = ['diff']
425 command.extend(args)
426 # Simple class which tracks which file is being diffed and
427 # replaces instances of its file name in the original and
428 # working copy lines of the svn diff output.
429 class DiffFilterer(object):
430 index_string = "Index: "
431 original_prefix = "--- "
432 working_prefix = "+++ "
433
434 def __init__(self, relpath):
435 # Note that we always use '/' as the path separator to be
436 # consistent with svn's cygwin-style output on Windows
437 self._relpath = relpath.replace("\\", "/")
438 self._current_file = ""
439 self._replacement_file = ""
440
441 def SetCurrentFile(self, file):
442 self._current_file = file
443 # Note that we always use '/' as the path separator to be
444 # consistent with svn's cygwin-style output on Windows
445 self._replacement_file = self._relpath + '/' + file
446
447 def ReplaceAndPrint(self, line):
448 print(line.replace(self._current_file, self._replacement_file))
449
450 def Filter(self, line):
451 if (line.startswith(self.index_string)):
452 self.SetCurrentFile(line[len(self.index_string):])
453 self.ReplaceAndPrint(line)
454 else:
455 if (line.startswith(self.original_prefix) or
456 line.startswith(self.working_prefix)):
457 self.ReplaceAndPrint(line)
458 else:
459 print line
460
461 filterer = DiffFilterer(self.relpath)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000462 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)