blob: aa5a05d33b6c60692382915553df50f92ab7e885 [file] [log] [blame]
[email protected]5aeb7dd2009-11-17 18:09:011# 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.
[email protected]5f3eee32009-09-17 00:34:304
[email protected]d5800f12009-11-12 20:03:435"""Gclient-specific SCM-specific operations."""
[email protected]5f3eee32009-09-17 00:34:306
[email protected]754960e2009-09-21 12:31:057import logging
[email protected]5f3eee32009-09-17 00:34:308import os
9import re
10import subprocess
[email protected]5f3eee32009-09-17 00:34:3011
[email protected]5aeb7dd2009-11-17 18:09:0112import scm
[email protected]5f3eee32009-09-17 00:34:3013import gclient_utils
[email protected]5f3eee32009-09-17 00:34:3014
15
16### SCM abstraction layer
17
[email protected]cb5442b2009-09-22 16:51:2418# 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,
[email protected]e28e4982009-09-25 20:51:4524 'git' : GitWrapper,
[email protected]cb5442b2009-09-22 16:51:2425 }
[email protected]e28e4982009-09-25 20:51:4526
[email protected]1b8779a2009-11-19 18:11:3927 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'
[email protected]e28e4982009-09-25 20:51:4533
[email protected]cb5442b2009-09-22 16:51:2434 if not scm_name in scm_map:
35 raise gclient_utils.Error('Unsupported scm %s' % scm_name)
[email protected]1b8779a2009-11-19 18:11:3936 return scm_map[scm_name](orig_url, root_dir, relpath, scm_name)
[email protected]cb5442b2009-09-22 16:51:2437
38
39# SCMWrapper base class
40
[email protected]5f3eee32009-09-17 00:34:3041class 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."""
[email protected]5e73b0c2009-09-18 19:47:4847 def __init__(self, url=None, root_dir=None, relpath=None,
48 scm_name='svn'):
[email protected]5f3eee32009-09-17 00:34:3049 self.scm_name = scm_name
50 self.url = url
[email protected]5e73b0c2009-09-18 19:47:4851 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)
[email protected]e28e4982009-09-25 20:51:4557 if self.relpath and self._root_dir:
58 self.checkout_path = os.path.join(self._root_dir, self.relpath)
[email protected]5f3eee32009-09-17 00:34:3059
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.
[email protected]de754ac2009-09-17 18:04:5066 if file_list is None:
67 file_list = []
[email protected]5f3eee32009-09-17 00:34:3068
[email protected]0f282062009-11-06 20:14:0269 commands = ['cleanup', 'export', 'update', 'revert', 'revinfo',
[email protected]cb5442b2009-09-22 16:51:2470 'status', 'diff', 'pack', 'runhooks']
[email protected]5f3eee32009-09-17 00:34:3071
72 if not command in commands:
73 raise gclient_utils.Error('Unknown command %s' % command)
74
[email protected]cb5442b2009-09-22 16:51:2475 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
[email protected]5aeb7dd2009-11-17 18:09:0182class GitWrapper(SCMWrapper, scm.GIT):
[email protected]e28e4982009-09-25 20:51:4583 """Wrapper for Git"""
84
85 def cleanup(self, options, args, file_list):
86 """Cleanup working copy."""
[email protected]e3608df2009-11-10 20:22:5787 __pychecker__ = 'unusednames=args,file_list,options'
[email protected]5aeb7dd2009-11-17 18:09:0188 self._Run(['prune'], redirect_stdout=False)
89 self._Run(['fsck'], redirect_stdout=False)
90 self._Run(['gc'], redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:4591
92 def diff(self, options, args, file_list):
[email protected]e3608df2009-11-10 20:22:5793 __pychecker__ = 'unusednames=args,file_list,options'
[email protected]5aeb7dd2009-11-17 18:09:0194 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
95 self._Run(['diff', merge_base], redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:4596
97 def export(self, options, args, file_list):
[email protected]e3608df2009-11-10 20:22:5798 __pychecker__ = 'unusednames=file_list,options'
[email protected]e28e4982009-09-25 20:51:4599 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)
[email protected]5aeb7dd2009-11-17 18:09:01103 self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path],
104 redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45105
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
[email protected]923a0372009-12-11 20:42:43118 self._CheckMinVersion("1.6.1")
119
[email protected]ac915bb2009-11-13 17:03:01120 url, revision = gclient_utils.SplitUrlRevision(self.url)
121 rev_str = ""
[email protected]e28e4982009-09-25 20:51:45122 if options.revision:
[email protected]ac915bb2009-11-13 17:03:01123 # Override the revision number.
124 revision = str(options.revision)
125 if revision:
[email protected]ac915bb2009-11-13 17:03:01126 rev_str = ' at %s' % revision
[email protected]e28e4982009-09-25 20:51:45127
[email protected]b1a22bf2009-11-07 02:33:50128 if options.verbose:
[email protected]b1a22bf2009-11-07 02:33:50129 print("\n_____ %s%s" % (self.relpath, rev_str))
130
[email protected]e28e4982009-09-25 20:51:45131 if not os.path.exists(self.checkout_path):
[email protected]5aeb7dd2009-11-17 18:09:01132 self._Run(['clone', url, self.checkout_path],
133 cwd=self._root_dir, redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45134 if revision:
[email protected]5aeb7dd2009-11-17 18:09:01135 self._Run(['reset', '--hard', revision], redirect_stdout=False)
136 files = self._Run(['ls-files']).split()
[email protected]e28e4982009-09-25 20:51:45137 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
138 return
139
[email protected]e28e4982009-09-25 20:51:45140 new_base = 'origin'
141 if revision:
142 new_base = revision
[email protected]f2370632009-11-25 00:22:11143 cur_branch = self._Run(['symbolic-ref', 'HEAD']).split('/')[-1]
144 merge_base = self._Run(['merge-base', 'HEAD', new_base])
145 self._Run(['remote', 'update'], redirect_stdout=False)
[email protected]5aeb7dd2009-11-17 18:09:01146 files = self._Run(['diff', new_base, '--name-only']).split()
[email protected]e28e4982009-09-25 20:51:45147 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
[email protected]f2370632009-11-25 00:22:11148 self._Run(['rebase', '-v', '--onto', new_base, merge_base, cur_branch],
149 redirect_stdout=False)
[email protected]b1a22bf2009-11-07 02:33:50150 print "Checked out revision %s." % self.revinfo(options, (), None)
[email protected]e28e4982009-09-25 20:51:45151
152 def revert(self, options, args, file_list):
153 """Reverts local modifications.
154
155 All reverted files will be appended to file_list.
156 """
[email protected]e3608df2009-11-10 20:22:57157 __pychecker__ = 'unusednames=args'
[email protected]260c6532009-10-28 03:22:35158 path = os.path.join(self._root_dir, self.relpath)
159 if not os.path.isdir(path):
160 # revert won't work if the directory doesn't exist. It needs to
161 # checkout instead.
162 print("\n_____ %s is missing, synching instead" % self.relpath)
163 # Don't reuse the args.
164 return self.update(options, [], file_list)
[email protected]5aeb7dd2009-11-17 18:09:01165 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
166 files = self._Run(['diff', merge_base, '--name-only']).split()
167 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45168 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
169
[email protected]0f282062009-11-06 20:14:02170 def revinfo(self, options, args, file_list):
171 """Display revision"""
[email protected]e3608df2009-11-10 20:22:57172 __pychecker__ = 'unusednames=args,file_list,options'
[email protected]5aeb7dd2009-11-17 18:09:01173 return self._Run(['rev-parse', 'HEAD'])
[email protected]0f282062009-11-06 20:14:02174
[email protected]e28e4982009-09-25 20:51:45175 def runhooks(self, options, args, file_list):
176 self.status(options, args, file_list)
177
178 def status(self, options, args, file_list):
179 """Display status information."""
[email protected]e3608df2009-11-10 20:22:57180 __pychecker__ = 'unusednames=args,options'
[email protected]e28e4982009-09-25 20:51:45181 if not os.path.isdir(self.checkout_path):
182 print('\n________ couldn\'t run status in %s:\nThe directory '
[email protected]e3608df2009-11-10 20:22:57183 'does not exist.' % self.checkout_path)
[email protected]e28e4982009-09-25 20:51:45184 else:
[email protected]5aeb7dd2009-11-17 18:09:01185 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
186 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
187 files = self._Run(['diff', '--name-only', merge_base]).split()
[email protected]e28e4982009-09-25 20:51:45188 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
189
[email protected]923a0372009-12-11 20:42:43190 def _CheckMinVersion(self, min_version):
191 version = self._Run(['--version']).split()[-1]
192 version_list = map(int, version.split('.'))
193 min_version_list = map(int, min_version.split('.'))
194 for min_ver in min_version_list:
195 ver = version_list.pop(0)
196 if min_ver > ver:
197 raise gclient_utils.Error('git version %s < minimum required %s' %
198 (version, min_version))
199 elif min_ver < ver:
200 return
201
[email protected]5aeb7dd2009-11-17 18:09:01202 def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True):
203 # TODO(maruel): Merge with Capture?
[email protected]ffe96f02009-12-09 18:39:15204 if cwd is None:
205 cwd = self.checkout_path
[email protected]e8e60e52009-11-02 21:50:56206 stdout=None
207 if redirect_stdout:
208 stdout=subprocess.PIPE
[email protected]e28e4982009-09-25 20:51:45209 if cwd == None:
210 cwd = self.checkout_path
[email protected]5aeb7dd2009-11-17 18:09:01211 cmd = [self.COMMAND]
[email protected]e28e4982009-09-25 20:51:45212 cmd.extend(args)
[email protected]e8e60e52009-11-02 21:50:56213 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
[email protected]ffe96f02009-12-09 18:39:15214 output = sp.communicate()[0]
[email protected]e28e4982009-09-25 20:51:45215 if checkrc and sp.returncode:
216 raise gclient_utils.Error('git command %s returned %d' %
217 (args[0], sp.returncode))
[email protected]5aeb7dd2009-11-17 18:09:01218 if output is not None:
[email protected]e8e60e52009-11-02 21:50:56219 return output.strip()
[email protected]e28e4982009-09-25 20:51:45220
221
[email protected]5aeb7dd2009-11-17 18:09:01222class SVNWrapper(SCMWrapper, scm.SVN):
[email protected]cb5442b2009-09-22 16:51:24223 """ Wrapper for SVN """
[email protected]5f3eee32009-09-17 00:34:30224
225 def cleanup(self, options, args, file_list):
226 """Cleanup working copy."""
[email protected]e3608df2009-11-10 20:22:57227 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30228 command = ['cleanup']
229 command.extend(args)
[email protected]5aeb7dd2009-11-17 18:09:01230 self.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30231
232 def diff(self, options, args, file_list):
233 # NOTE: This function does not currently modify file_list.
[email protected]e3608df2009-11-10 20:22:57234 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30235 command = ['diff']
236 command.extend(args)
[email protected]5aeb7dd2009-11-17 18:09:01237 self.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30238
239 def export(self, options, args, file_list):
[email protected]e3608df2009-11-10 20:22:57240 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30241 assert len(args) == 1
242 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
243 try:
244 os.makedirs(export_path)
245 except OSError:
246 pass
247 assert os.path.exists(export_path)
248 command = ['export', '--force', '.']
249 command.append(export_path)
[email protected]5aeb7dd2009-11-17 18:09:01250 self.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30251
252 def update(self, options, args, file_list):
253 """Runs SCM to update or transparently checkout the working copy.
254
255 All updated files will be appended to file_list.
256
257 Raises:
258 Error: if can't get URL for relative path.
259 """
260 # Only update if git is not controlling the directory.
261 checkout_path = os.path.join(self._root_dir, self.relpath)
262 git_path = os.path.join(self._root_dir, self.relpath, '.git')
263 if os.path.exists(git_path):
264 print("________ found .git directory; skipping %s" % self.relpath)
265 return
266
267 if args:
268 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
269
[email protected]ac915bb2009-11-13 17:03:01270 url, revision = gclient_utils.SplitUrlRevision(self.url)
271 base_url = url
[email protected]5f3eee32009-09-17 00:34:30272 forced_revision = False
[email protected]ac915bb2009-11-13 17:03:01273 rev_str = ""
[email protected]5f3eee32009-09-17 00:34:30274 if options.revision:
275 # Override the revision number.
[email protected]ac915bb2009-11-13 17:03:01276 revision = str(options.revision)
[email protected]5f3eee32009-09-17 00:34:30277 if revision:
[email protected]ac915bb2009-11-13 17:03:01278 forced_revision = True
279 url = '%s@%s' % (url, revision)
[email protected]770ff9e2009-09-23 17:18:18280 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30281
282 if not os.path.exists(checkout_path):
283 # We need to checkout.
284 command = ['checkout', url, checkout_path]
285 if revision:
286 command.extend(['--revision', str(revision)])
[email protected]5aeb7dd2009-11-17 18:09:01287 self.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30288 return
289
290 # Get the existing scm url and the revision number of the current checkout.
[email protected]5aeb7dd2009-11-17 18:09:01291 from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.')
[email protected]5f3eee32009-09-17 00:34:30292 if not from_info:
293 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
294 "directory is present. Delete the directory "
295 "and try again." %
296 checkout_path)
297
[email protected]7753d242009-10-07 17:40:24298 if options.manually_grab_svn_rev:
299 # Retrieve the current HEAD version because svn is slow at null updates.
300 if not revision:
[email protected]5aeb7dd2009-11-17 18:09:01301 from_info_live = self.CaptureInfo(from_info['URL'], '.')
[email protected]7753d242009-10-07 17:40:24302 revision = str(from_info_live['Revision'])
303 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30304
[email protected]ac915bb2009-11-13 17:03:01305 if from_info['URL'] != base_url:
[email protected]5aeb7dd2009-11-17 18:09:01306 to_info = self.CaptureInfo(url, '.')
[email protected]e2ce0c72009-09-23 16:14:18307 if not to_info.get('Repository Root') or not to_info.get('UUID'):
308 # The url is invalid or the server is not accessible, it's safer to bail
309 # out right now.
310 raise gclient_utils.Error('This url is unreachable: %s' % url)
[email protected]5f3eee32009-09-17 00:34:30311 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
312 and (from_info['UUID'] == to_info['UUID']))
313 if can_switch:
314 print("\n_____ relocating %s to a new checkout" % self.relpath)
315 # We have different roots, so check if we can switch --relocate.
316 # Subversion only permits this if the repository UUIDs match.
317 # Perform the switch --relocate, then rewrite the from_url
318 # to reflect where we "are now." (This is the same way that
319 # Subversion itself handles the metadata when switch --relocate
320 # is used.) This makes the checks below for whether we
321 # can update to a revision or have to switch to a different
322 # branch work as expected.
323 # TODO(maruel): TEST ME !
324 command = ["switch", "--relocate",
325 from_info['Repository Root'],
326 to_info['Repository Root'],
327 self.relpath]
[email protected]5aeb7dd2009-11-17 18:09:01328 self.Run(command, self._root_dir)
[email protected]5f3eee32009-09-17 00:34:30329 from_info['URL'] = from_info['URL'].replace(
330 from_info['Repository Root'],
331 to_info['Repository Root'])
332 else:
[email protected]5aeb7dd2009-11-17 18:09:01333 if self.CaptureStatus(checkout_path):
[email protected]5f3eee32009-09-17 00:34:30334 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
335 "don't match and there is local changes "
336 "in %s. Delete the directory and "
337 "try again." % (url, checkout_path))
338 # Ok delete it.
339 print("\n_____ switching %s to a new checkout" % self.relpath)
[email protected]8f9c69f2009-09-17 00:48:28340 gclient_utils.RemoveDirectory(checkout_path)
[email protected]5f3eee32009-09-17 00:34:30341 # We need to checkout.
342 command = ['checkout', url, checkout_path]
343 if revision:
344 command.extend(['--revision', str(revision)])
[email protected]5aeb7dd2009-11-17 18:09:01345 self.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30346 return
347
348
349 # If the provided url has a revision number that matches the revision
350 # number of the existing directory, then we don't need to bother updating.
[email protected]2e0c6852009-09-24 00:02:07351 if not options.force and str(from_info['Revision']) == revision:
[email protected]5f3eee32009-09-17 00:34:30352 if options.verbose or not forced_revision:
353 print("\n_____ %s%s" % (self.relpath, rev_str))
354 return
355
356 command = ["update", checkout_path]
357 if revision:
358 command.extend(['--revision', str(revision)])
[email protected]5aeb7dd2009-11-17 18:09:01359 self.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30360
361 def revert(self, options, args, file_list):
362 """Reverts local modifications. Subversion specific.
363
364 All reverted files will be appended to file_list, even if Subversion
365 doesn't know about them.
366 """
[email protected]e3608df2009-11-10 20:22:57367 __pychecker__ = 'unusednames=args'
[email protected]5f3eee32009-09-17 00:34:30368 path = os.path.join(self._root_dir, self.relpath)
369 if not os.path.isdir(path):
370 # svn revert won't work if the directory doesn't exist. It needs to
371 # checkout instead.
372 print("\n_____ %s is missing, synching instead" % self.relpath)
373 # Don't reuse the args.
374 return self.update(options, [], file_list)
375
[email protected]5aeb7dd2009-11-17 18:09:01376 for file_status in self.CaptureStatus(path):
[email protected]e3608df2009-11-10 20:22:57377 file_path = os.path.join(path, file_status[1])
378 if file_status[0][0] == 'X':
[email protected]754960e2009-09-21 12:31:05379 # Ignore externals.
[email protected]aa3dd472009-09-21 19:02:48380 logging.info('Ignoring external %s' % file_path)
[email protected]754960e2009-09-21 12:31:05381 continue
382
[email protected]aa3dd472009-09-21 19:02:48383 if logging.getLogger().isEnabledFor(logging.INFO):
384 logging.info('%s%s' % (file[0], file[1]))
385 else:
386 print(file_path)
[email protected]e3608df2009-11-10 20:22:57387 if file_status[0].isspace():
[email protected]aa3dd472009-09-21 19:02:48388 logging.error('No idea what is the status of %s.\n'
389 'You just found a bug in gclient, please ping '
390 '[email protected] ASAP!' % file_path)
391 # svn revert is really stupid. It fails on inconsistent line-endings,
392 # on switched directories, etc. So take no chance and delete everything!
393 try:
394 if not os.path.exists(file_path):
395 pass
396 elif os.path.isfile(file_path):
[email protected]754960e2009-09-21 12:31:05397 logging.info('os.remove(%s)' % file_path)
[email protected]5f3eee32009-09-17 00:34:30398 os.remove(file_path)
[email protected]aa3dd472009-09-21 19:02:48399 elif os.path.isdir(file_path):
[email protected]754960e2009-09-21 12:31:05400 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
[email protected]8f9c69f2009-09-17 00:48:28401 gclient_utils.RemoveDirectory(file_path)
[email protected]5f3eee32009-09-17 00:34:30402 else:
[email protected]aa3dd472009-09-21 19:02:48403 logging.error('no idea what is %s.\nYou just found a bug in gclient'
404 ', please ping [email protected] ASAP!' % file_path)
405 except EnvironmentError:
406 logging.error('Failed to remove %s.' % file_path)
407
[email protected]810a50b2009-10-05 23:03:18408 try:
409 # svn revert is so broken we don't even use it. Using
410 # "svn up --revision BASE" achieve the same effect.
[email protected]5aeb7dd2009-11-17 18:09:01411 self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
[email protected]810a50b2009-10-05 23:03:18412 file_list)
413 except OSError, e:
414 # Maybe the directory disapeared meanwhile. We don't want it to throw an
415 # exception.
416 logging.error('Failed to update:\n%s' % str(e))
[email protected]5f3eee32009-09-17 00:34:30417
[email protected]0f282062009-11-06 20:14:02418 def revinfo(self, options, args, file_list):
419 """Display revision"""
[email protected]e3608df2009-11-10 20:22:57420 __pychecker__ = 'unusednames=args,file_list,options'
[email protected]5aeb7dd2009-11-17 18:09:01421 return self.CaptureHeadRevision(self.url)
[email protected]0f282062009-11-06 20:14:02422
[email protected]cb5442b2009-09-22 16:51:24423 def runhooks(self, options, args, file_list):
424 self.status(options, args, file_list)
425
[email protected]5f3eee32009-09-17 00:34:30426 def status(self, options, args, file_list):
427 """Display status information."""
428 path = os.path.join(self._root_dir, self.relpath)
429 command = ['status']
430 command.extend(args)
431 if not os.path.isdir(path):
432 # svn status won't work if the directory doesn't exist.
433 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
434 "does not exist."
435 % (' '.join(command), path))
436 # There's no file list to retrieve.
437 else:
[email protected]5aeb7dd2009-11-17 18:09:01438 self.RunAndGetFileList(options, command, path, file_list)
[email protected]5f3eee32009-09-17 00:34:30439
440 def pack(self, options, args, file_list):
441 """Generates a patch file which can be applied to the root of the
442 repository."""
[email protected]e3608df2009-11-10 20:22:57443 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30444 path = os.path.join(self._root_dir, self.relpath)
445 command = ['diff']
446 command.extend(args)
447 # Simple class which tracks which file is being diffed and
448 # replaces instances of its file name in the original and
449 # working copy lines of the svn diff output.
450 class DiffFilterer(object):
451 index_string = "Index: "
452 original_prefix = "--- "
453 working_prefix = "+++ "
454
455 def __init__(self, relpath):
456 # Note that we always use '/' as the path separator to be
457 # consistent with svn's cygwin-style output on Windows
458 self._relpath = relpath.replace("\\", "/")
459 self._current_file = ""
460 self._replacement_file = ""
461
462 def SetCurrentFile(self, file):
463 self._current_file = file
464 # Note that we always use '/' as the path separator to be
465 # consistent with svn's cygwin-style output on Windows
466 self._replacement_file = self._relpath + '/' + file
467
468 def ReplaceAndPrint(self, line):
469 print(line.replace(self._current_file, self._replacement_file))
470
471 def Filter(self, line):
472 if (line.startswith(self.index_string)):
473 self.SetCurrentFile(line[len(self.index_string):])
474 self.ReplaceAndPrint(line)
475 else:
476 if (line.startswith(self.original_prefix) or
477 line.startswith(self.working_prefix)):
478 self.ReplaceAndPrint(line)
479 else:
480 print line
481
482 filterer = DiffFilterer(self.relpath)
[email protected]5aeb7dd2009-11-17 18:09:01483 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)