blob: 034dc2df6c8bdef41dc7f8460bc8ca356036756a [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
msb@chromium.org1b8779a2009-11-19 18:11:39 +000027 orig_url = url
28
29 if url:
30 url, _ = gclient_utils.SplitUrlRevision(url)
31 if url.startswith('git:') or url.startswith('ssh:') or url.endswith('.git'):
32 scm_name = 'git'
msb@chromium.orge28e4982009-09-25 20:51:45 +000033
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000034 if not scm_name in scm_map:
35 raise gclient_utils.Error('Unsupported scm %s' % scm_name)
msb@chromium.org1b8779a2009-11-19 18:11:39 +000036 return scm_map[scm_name](orig_url, root_dir, relpath, scm_name)
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000037
38
39# SCMWrapper base class
40
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000041class SCMWrapper(object):
42 """Add necessary glue between all the supported SCM.
43
44 This is the abstraction layer to bind to different SCM. Since currently only
45 subversion is supported, a lot of subersionism remains. This can be sorted out
46 once another SCM is supported."""
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +000047 def __init__(self, url=None, root_dir=None, relpath=None,
48 scm_name='svn'):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000049 self.scm_name = scm_name
50 self.url = url
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +000051 self._root_dir = root_dir
52 if self._root_dir:
53 self._root_dir = self._root_dir.replace('/', os.sep)
54 self.relpath = relpath
55 if self.relpath:
56 self.relpath = self.relpath.replace('/', os.sep)
msb@chromium.orge28e4982009-09-25 20:51:45 +000057 if self.relpath and self._root_dir:
58 self.checkout_path = os.path.join(self._root_dir, self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000059
60 def FullUrlForRelativeUrl(self, url):
61 # Find the forth '/' and strip from there. A bit hackish.
62 return '/'.join(self.url.split('/')[:4]) + url
63
64 def RunCommand(self, command, options, args, file_list=None):
65 # file_list will have all files that are modified appended to it.
maruel@chromium.orgde754ac2009-09-17 18:04:50 +000066 if file_list is None:
67 file_list = []
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000068
msb@chromium.org0f282062009-11-06 20:14:02 +000069 commands = ['cleanup', 'export', 'update', 'revert', 'revinfo',
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000070 'status', 'diff', 'pack', 'runhooks']
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000071
72 if not command in commands:
73 raise gclient_utils.Error('Unknown command %s' % command)
74
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000075 if not command in dir(self):
76 raise gclient_utils.Error('Command %s not implemnted in %s wrapper' % (
77 command, self.scm_name))
78
79 return getattr(self, command)(options, args, file_list)
80
81
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000082class GitWrapper(SCMWrapper, scm.GIT):
msb@chromium.orge28e4982009-09-25 20:51:45 +000083 """Wrapper for Git"""
84
85 def cleanup(self, options, args, file_list):
86 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +000087 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000088 self._Run(['prune'], redirect_stdout=False)
89 self._Run(['fsck'], redirect_stdout=False)
90 self._Run(['gc'], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +000091
92 def diff(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +000093 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000094 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
95 self._Run(['diff', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +000096
97 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +000098 __pychecker__ = 'unusednames=file_list,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +000099 assert len(args) == 1
100 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
101 if not os.path.exists(export_path):
102 os.makedirs(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000103 self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path],
104 redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000105
106 def update(self, options, args, file_list):
107 """Runs git to update or transparently checkout the working copy.
108
109 All updated files will be appended to file_list.
110
111 Raises:
112 Error: if can't get URL for relative path.
113 """
114
115 if args:
116 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
117
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000118 url, revision = gclient_utils.SplitUrlRevision(self.url)
119 rev_str = ""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000120 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000121 # Override the revision number.
122 revision = str(options.revision)
123 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000124 rev_str = ' at %s' % revision
msb@chromium.orge28e4982009-09-25 20:51:45 +0000125
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000126 if options.verbose:
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000127 print("\n_____ %s%s" % (self.relpath, rev_str))
128
msb@chromium.orge28e4982009-09-25 20:51:45 +0000129 if not os.path.exists(self.checkout_path):
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000130 self._Run(['clone', url, self.checkout_path],
131 cwd=self._root_dir, redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000132 if revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000133 self._Run(['reset', '--hard', revision], redirect_stdout=False)
134 files = self._Run(['ls-files']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000135 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
136 return
137
msb@chromium.orge28e4982009-09-25 20:51:45 +0000138 new_base = 'origin'
139 if revision:
140 new_base = revision
msb@chromium.orgf2370632009-11-25 00:22:11 +0000141 cur_branch = self._Run(['symbolic-ref', 'HEAD']).split('/')[-1]
142 merge_base = self._Run(['merge-base', 'HEAD', new_base])
143 self._Run(['remote', 'update'], redirect_stdout=False)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000144 files = self._Run(['diff', new_base, '--name-only']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000145 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.orgf2370632009-11-25 00:22:11 +0000146 self._Run(['rebase', '-v', '--onto', new_base, merge_base, cur_branch],
147 redirect_stdout=False)
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000148 print "Checked out revision %s." % self.revinfo(options, (), None)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000149
150 def revert(self, options, args, file_list):
151 """Reverts local modifications.
152
153 All reverted files will be appended to file_list.
154 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000155 __pychecker__ = 'unusednames=args'
msb@chromium.org260c6532009-10-28 03:22:35 +0000156 path = os.path.join(self._root_dir, self.relpath)
157 if not os.path.isdir(path):
158 # revert won't work if the directory doesn't exist. It needs to
159 # checkout instead.
160 print("\n_____ %s is missing, synching instead" % self.relpath)
161 # Don't reuse the args.
162 return self.update(options, [], file_list)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000163 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
164 files = self._Run(['diff', merge_base, '--name-only']).split()
165 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000166 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
167
msb@chromium.org0f282062009-11-06 20:14:02 +0000168 def revinfo(self, options, args, file_list):
169 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000170 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000171 return self._Run(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000172
msb@chromium.orge28e4982009-09-25 20:51:45 +0000173 def runhooks(self, options, args, file_list):
174 self.status(options, args, file_list)
175
176 def status(self, options, args, file_list):
177 """Display status information."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000178 __pychecker__ = 'unusednames=args,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000179 if not os.path.isdir(self.checkout_path):
180 print('\n________ couldn\'t run status in %s:\nThe directory '
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000181 'does not exist.' % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000182 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000183 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
184 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
185 files = self._Run(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000186 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
187
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000188 def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True):
189 # TODO(maruel): Merge with Capture?
maruel@chromium.orgffe96f02009-12-09 18:39:15 +0000190 if cwd is None:
191 cwd = self.checkout_path
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000192 stdout=None
193 if redirect_stdout:
194 stdout=subprocess.PIPE
msb@chromium.orge28e4982009-09-25 20:51:45 +0000195 if cwd == None:
196 cwd = self.checkout_path
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000197 cmd = [self.COMMAND]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000198 cmd.extend(args)
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000199 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
maruel@chromium.orgffe96f02009-12-09 18:39:15 +0000200 output = sp.communicate()[0]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000201 if checkrc and sp.returncode:
202 raise gclient_utils.Error('git command %s returned %d' %
203 (args[0], sp.returncode))
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000204 if output is not None:
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000205 return output.strip()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000206
207
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000208class SVNWrapper(SCMWrapper, scm.SVN):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000209 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000210
211 def cleanup(self, options, args, file_list):
212 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000213 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000214 command = ['cleanup']
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 diff(self, options, args, file_list):
219 # NOTE: This function does not currently modify 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 command = ['diff']
222 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000223 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000224
225 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000226 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000227 assert len(args) == 1
228 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
229 try:
230 os.makedirs(export_path)
231 except OSError:
232 pass
233 assert os.path.exists(export_path)
234 command = ['export', '--force', '.']
235 command.append(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000236 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000237
238 def update(self, options, args, file_list):
239 """Runs SCM to update or transparently checkout the working copy.
240
241 All updated files will be appended to file_list.
242
243 Raises:
244 Error: if can't get URL for relative path.
245 """
246 # Only update if git is not controlling the directory.
247 checkout_path = os.path.join(self._root_dir, self.relpath)
248 git_path = os.path.join(self._root_dir, self.relpath, '.git')
249 if os.path.exists(git_path):
250 print("________ found .git directory; skipping %s" % self.relpath)
251 return
252
253 if args:
254 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
255
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000256 url, revision = gclient_utils.SplitUrlRevision(self.url)
257 base_url = url
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000258 forced_revision = False
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000259 rev_str = ""
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000260 if options.revision:
261 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000262 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000263 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000264 forced_revision = True
265 url = '%s@%s' % (url, revision)
msb@chromium.org770ff9e2009-09-23 17:18:18 +0000266 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000267
268 if not os.path.exists(checkout_path):
269 # We need to checkout.
270 command = ['checkout', url, checkout_path]
271 if revision:
272 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000273 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000274 return
275
276 # Get the existing scm url and the revision number of the current checkout.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000277 from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000278 if not from_info:
279 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
280 "directory is present. Delete the directory "
281 "and try again." %
282 checkout_path)
283
maruel@chromium.org7753d242009-10-07 17:40:24 +0000284 if options.manually_grab_svn_rev:
285 # Retrieve the current HEAD version because svn is slow at null updates.
286 if not revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000287 from_info_live = self.CaptureInfo(from_info['URL'], '.')
maruel@chromium.org7753d242009-10-07 17:40:24 +0000288 revision = str(from_info_live['Revision'])
289 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000290
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000291 if from_info['URL'] != base_url:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000292 to_info = self.CaptureInfo(url, '.')
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +0000293 if not to_info.get('Repository Root') or not to_info.get('UUID'):
294 # The url is invalid or the server is not accessible, it's safer to bail
295 # out right now.
296 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000297 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
298 and (from_info['UUID'] == to_info['UUID']))
299 if can_switch:
300 print("\n_____ relocating %s to a new checkout" % self.relpath)
301 # We have different roots, so check if we can switch --relocate.
302 # Subversion only permits this if the repository UUIDs match.
303 # Perform the switch --relocate, then rewrite the from_url
304 # to reflect where we "are now." (This is the same way that
305 # Subversion itself handles the metadata when switch --relocate
306 # is used.) This makes the checks below for whether we
307 # can update to a revision or have to switch to a different
308 # branch work as expected.
309 # TODO(maruel): TEST ME !
310 command = ["switch", "--relocate",
311 from_info['Repository Root'],
312 to_info['Repository Root'],
313 self.relpath]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000314 self.Run(command, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000315 from_info['URL'] = from_info['URL'].replace(
316 from_info['Repository Root'],
317 to_info['Repository Root'])
318 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000319 if self.CaptureStatus(checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000320 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
321 "don't match and there is local changes "
322 "in %s. Delete the directory and "
323 "try again." % (url, checkout_path))
324 # Ok delete it.
325 print("\n_____ switching %s to a new checkout" % self.relpath)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000326 gclient_utils.RemoveDirectory(checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000327 # We need to checkout.
328 command = ['checkout', url, checkout_path]
329 if revision:
330 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000331 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000332 return
333
334
335 # If the provided url has a revision number that matches the revision
336 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +0000337 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000338 if options.verbose or not forced_revision:
339 print("\n_____ %s%s" % (self.relpath, rev_str))
340 return
341
342 command = ["update", checkout_path]
343 if revision:
344 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000345 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000346
347 def revert(self, options, args, file_list):
348 """Reverts local modifications. Subversion specific.
349
350 All reverted files will be appended to file_list, even if Subversion
351 doesn't know about them.
352 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000353 __pychecker__ = 'unusednames=args'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000354 path = os.path.join(self._root_dir, self.relpath)
355 if not os.path.isdir(path):
356 # svn revert won't work if the directory doesn't exist. It needs to
357 # checkout instead.
358 print("\n_____ %s is missing, synching instead" % self.relpath)
359 # Don't reuse the args.
360 return self.update(options, [], file_list)
361
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000362 for file_status in self.CaptureStatus(path):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000363 file_path = os.path.join(path, file_status[1])
364 if file_status[0][0] == 'X':
maruel@chromium.org754960e2009-09-21 12:31:05 +0000365 # Ignore externals.
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000366 logging.info('Ignoring external %s' % file_path)
maruel@chromium.org754960e2009-09-21 12:31:05 +0000367 continue
368
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000369 if logging.getLogger().isEnabledFor(logging.INFO):
370 logging.info('%s%s' % (file[0], file[1]))
371 else:
372 print(file_path)
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000373 if file_status[0].isspace():
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000374 logging.error('No idea what is the status of %s.\n'
375 'You just found a bug in gclient, please ping '
376 'maruel@chromium.org ASAP!' % file_path)
377 # svn revert is really stupid. It fails on inconsistent line-endings,
378 # on switched directories, etc. So take no chance and delete everything!
379 try:
380 if not os.path.exists(file_path):
381 pass
382 elif os.path.isfile(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000383 logging.info('os.remove(%s)' % file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000384 os.remove(file_path)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000385 elif os.path.isdir(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000386 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000387 gclient_utils.RemoveDirectory(file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000388 else:
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000389 logging.error('no idea what is %s.\nYou just found a bug in gclient'
390 ', please ping maruel@chromium.org ASAP!' % file_path)
391 except EnvironmentError:
392 logging.error('Failed to remove %s.' % file_path)
393
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000394 try:
395 # svn revert is so broken we don't even use it. Using
396 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000397 self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000398 file_list)
399 except OSError, e:
400 # Maybe the directory disapeared meanwhile. We don't want it to throw an
401 # exception.
402 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000403
msb@chromium.org0f282062009-11-06 20:14:02 +0000404 def revinfo(self, options, args, file_list):
405 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000406 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000407 return self.CaptureHeadRevision(self.url)
msb@chromium.org0f282062009-11-06 20:14:02 +0000408
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000409 def runhooks(self, options, args, file_list):
410 self.status(options, args, file_list)
411
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000412 def status(self, options, args, file_list):
413 """Display status information."""
414 path = os.path.join(self._root_dir, self.relpath)
415 command = ['status']
416 command.extend(args)
417 if not os.path.isdir(path):
418 # svn status won't work if the directory doesn't exist.
419 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
420 "does not exist."
421 % (' '.join(command), path))
422 # There's no file list to retrieve.
423 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000424 self.RunAndGetFileList(options, command, path, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000425
426 def pack(self, options, args, file_list):
427 """Generates a patch file which can be applied to the root of the
428 repository."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000429 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000430 path = os.path.join(self._root_dir, self.relpath)
431 command = ['diff']
432 command.extend(args)
433 # Simple class which tracks which file is being diffed and
434 # replaces instances of its file name in the original and
435 # working copy lines of the svn diff output.
436 class DiffFilterer(object):
437 index_string = "Index: "
438 original_prefix = "--- "
439 working_prefix = "+++ "
440
441 def __init__(self, relpath):
442 # Note that we always use '/' as the path separator to be
443 # consistent with svn's cygwin-style output on Windows
444 self._relpath = relpath.replace("\\", "/")
445 self._current_file = ""
446 self._replacement_file = ""
447
448 def SetCurrentFile(self, file):
449 self._current_file = file
450 # Note that we always use '/' as the path separator to be
451 # consistent with svn's cygwin-style output on Windows
452 self._replacement_file = self._relpath + '/' + file
453
454 def ReplaceAndPrint(self, line):
455 print(line.replace(self._current_file, self._replacement_file))
456
457 def Filter(self, line):
458 if (line.startswith(self.index_string)):
459 self.SetCurrentFile(line[len(self.index_string):])
460 self.ReplaceAndPrint(line)
461 else:
462 if (line.startswith(self.original_prefix) or
463 line.startswith(self.working_prefix)):
464 self.ReplaceAndPrint(line)
465 else:
466 print line
467
468 filterer = DiffFilterer(self.relpath)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000469 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)