blob: bacbb6557626f872eadf440f19f77af9116bf7a3 [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
[email protected]ee4071d2009-12-22 22:25:379import posixpath
[email protected]5f3eee32009-09-17 00:34:3010import re
11import subprocess
[email protected]5f3eee32009-09-17 00:34:3012
[email protected]5aeb7dd2009-11-17 18:09:0113import scm
[email protected]5f3eee32009-09-17 00:34:3014import gclient_utils
[email protected]5f3eee32009-09-17 00:34:3015
16
[email protected]ee4071d2009-12-22 22:25:3717class DiffFilterer(object):
18 """Simple class which tracks which file is being diffed and
19 replaces instances of its file name in the original and
[email protected]d6504212010-01-13 17:34:3120 working copy lines of the svn/git diff output."""
[email protected]ee4071d2009-12-22 22:25:3721 index_string = "Index: "
22 original_prefix = "--- "
23 working_prefix = "+++ "
24
25 def __init__(self, relpath):
26 # Note that we always use '/' as the path separator to be
27 # consistent with svn's cygwin-style output on Windows
28 self._relpath = relpath.replace("\\", "/")
29 self._current_file = ""
30 self._replacement_file = ""
31
32 def SetCurrentFile(self, file):
33 self._current_file = file
34 # Note that we always use '/' as the path separator to be
35 # consistent with svn's cygwin-style output on Windows
36 self._replacement_file = posixpath.join(self._relpath, file)
37
38 def ReplaceAndPrint(self, line):
39 print(line.replace(self._current_file, self._replacement_file))
40
41 def Filter(self, line):
42 if (line.startswith(self.index_string)):
43 self.SetCurrentFile(line[len(self.index_string):])
44 self.ReplaceAndPrint(line)
45 else:
46 if (line.startswith(self.original_prefix) or
47 line.startswith(self.working_prefix)):
48 self.ReplaceAndPrint(line)
49 else:
50 print line
51
52
[email protected]5f3eee32009-09-17 00:34:3053### SCM abstraction layer
54
[email protected]cb5442b2009-09-22 16:51:2455# Factory Method for SCM wrapper creation
56
57def CreateSCM(url=None, root_dir=None, relpath=None, scm_name='svn'):
[email protected]cb5442b2009-09-22 16:51:2458 scm_map = {
59 'svn' : SVNWrapper,
[email protected]e28e4982009-09-25 20:51:4560 'git' : GitWrapper,
[email protected]cb5442b2009-09-22 16:51:2461 }
[email protected]e28e4982009-09-25 20:51:4562
[email protected]1b8779a2009-11-19 18:11:3963 orig_url = url
64
65 if url:
66 url, _ = gclient_utils.SplitUrlRevision(url)
67 if url.startswith('git:') or url.startswith('ssh:') or url.endswith('.git'):
68 scm_name = 'git'
[email protected]e28e4982009-09-25 20:51:4569
[email protected]cb5442b2009-09-22 16:51:2470 if not scm_name in scm_map:
71 raise gclient_utils.Error('Unsupported scm %s' % scm_name)
[email protected]1b8779a2009-11-19 18:11:3972 return scm_map[scm_name](orig_url, root_dir, relpath, scm_name)
[email protected]cb5442b2009-09-22 16:51:2473
74
75# SCMWrapper base class
76
[email protected]5f3eee32009-09-17 00:34:3077class SCMWrapper(object):
78 """Add necessary glue between all the supported SCM.
79
[email protected]d6504212010-01-13 17:34:3180 This is the abstraction layer to bind to different SCM.
81 """
[email protected]5e73b0c2009-09-18 19:47:4882 def __init__(self, url=None, root_dir=None, relpath=None,
83 scm_name='svn'):
[email protected]5f3eee32009-09-17 00:34:3084 self.scm_name = scm_name
85 self.url = url
[email protected]5e73b0c2009-09-18 19:47:4886 self._root_dir = root_dir
87 if self._root_dir:
88 self._root_dir = self._root_dir.replace('/', os.sep)
89 self.relpath = relpath
90 if self.relpath:
91 self.relpath = self.relpath.replace('/', os.sep)
[email protected]e28e4982009-09-25 20:51:4592 if self.relpath and self._root_dir:
93 self.checkout_path = os.path.join(self._root_dir, self.relpath)
[email protected]5f3eee32009-09-17 00:34:3094
[email protected]5f3eee32009-09-17 00:34:3095 def RunCommand(self, command, options, args, file_list=None):
96 # file_list will have all files that are modified appended to it.
[email protected]de754ac2009-09-17 18:04:5097 if file_list is None:
98 file_list = []
[email protected]5f3eee32009-09-17 00:34:3099
[email protected]0f282062009-11-06 20:14:02100 commands = ['cleanup', 'export', 'update', 'revert', 'revinfo',
[email protected]cb5442b2009-09-22 16:51:24101 'status', 'diff', 'pack', 'runhooks']
[email protected]5f3eee32009-09-17 00:34:30102
103 if not command in commands:
104 raise gclient_utils.Error('Unknown command %s' % command)
105
[email protected]cb5442b2009-09-22 16:51:24106 if not command in dir(self):
[email protected]ee4071d2009-12-22 22:25:37107 raise gclient_utils.Error('Command %s not implemented in %s wrapper' % (
[email protected]cb5442b2009-09-22 16:51:24108 command, self.scm_name))
109
110 return getattr(self, command)(options, args, file_list)
111
112
[email protected]5aeb7dd2009-11-17 18:09:01113class GitWrapper(SCMWrapper, scm.GIT):
[email protected]e28e4982009-09-25 20:51:45114 """Wrapper for Git"""
115
116 def cleanup(self, options, args, file_list):
[email protected]d8a63782010-01-25 17:47:05117 """'Cleanup' the repo.
118
119 There's no real git equivalent for the svn cleanup command, do a no-op.
120 """
[email protected]3904caa2010-01-25 17:37:46121 __pychecker__ = 'unusednames=options,args,file_list'
[email protected]e28e4982009-09-25 20:51:45122
123 def diff(self, options, args, file_list):
[email protected]3904caa2010-01-25 17:37:46124 __pychecker__ = 'unusednames=options,args,file_list'
[email protected]5aeb7dd2009-11-17 18:09:01125 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
126 self._Run(['diff', merge_base], redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45127
128 def export(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31129 """Export a clean directory tree into the given path.
130
131 Exports into the specified directory, creating the path if it does
132 already exist.
133 """
[email protected]3904caa2010-01-25 17:37:46134 __pychecker__ = 'unusednames=options,file_list'
[email protected]e28e4982009-09-25 20:51:45135 assert len(args) == 1
136 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
137 if not os.path.exists(export_path):
138 os.makedirs(export_path)
[email protected]5aeb7dd2009-11-17 18:09:01139 self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path],
140 redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45141
[email protected]ee4071d2009-12-22 22:25:37142 def pack(self, options, args, file_list):
143 """Generates a patch file which can be applied to the root of the
[email protected]d6504212010-01-13 17:34:31144 repository.
145
146 The patch file is generated from a diff of the merge base of HEAD and
147 its upstream branch.
148 """
[email protected]3904caa2010-01-25 17:37:46149 __pychecker__ = 'unusednames=options,file_list'
[email protected]ee4071d2009-12-22 22:25:37150 path = os.path.join(self._root_dir, self.relpath)
151 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
152 command = ['diff', merge_base]
153 filterer = DiffFilterer(self.relpath)
154 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)
155
[email protected]e28e4982009-09-25 20:51:45156 def update(self, options, args, file_list):
157 """Runs git to update or transparently checkout the working copy.
158
159 All updated files will be appended to file_list.
160
161 Raises:
162 Error: if can't get URL for relative path.
163 """
164
165 if args:
166 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
167
[email protected]83376f22009-12-11 22:25:31168 self._CheckMinVersion("1.6")
[email protected]923a0372009-12-11 20:42:43169
[email protected]ac915bb2009-11-13 17:03:01170 url, revision = gclient_utils.SplitUrlRevision(self.url)
171 rev_str = ""
[email protected]e28e4982009-09-25 20:51:45172 if options.revision:
[email protected]ac915bb2009-11-13 17:03:01173 # Override the revision number.
174 revision = str(options.revision)
175 if revision:
[email protected]ac915bb2009-11-13 17:03:01176 rev_str = ' at %s' % revision
[email protected]e28e4982009-09-25 20:51:45177
[email protected]b1a22bf2009-11-07 02:33:50178 if options.verbose:
[email protected]b1a22bf2009-11-07 02:33:50179 print("\n_____ %s%s" % (self.relpath, rev_str))
180
[email protected]e28e4982009-09-25 20:51:45181 if not os.path.exists(self.checkout_path):
[email protected]5aeb7dd2009-11-17 18:09:01182 self._Run(['clone', url, self.checkout_path],
183 cwd=self._root_dir, redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45184 if revision:
[email protected]5aeb7dd2009-11-17 18:09:01185 self._Run(['reset', '--hard', revision], redirect_stdout=False)
186 files = self._Run(['ls-files']).split()
[email protected]e28e4982009-09-25 20:51:45187 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
188 return
189
[email protected]e4af1ab2010-01-13 21:26:09190 if not os.path.exists(os.path.join(self.checkout_path, '.git')):
191 raise gclient_utils.Error('\n____ %s%s\n'
192 '\tPath is not a git repo. No .git dir.\n'
193 '\tTo resolve:\n'
194 '\t\trm -rf %s\n'
195 '\tAnd run gclient sync again\n'
196 % (self.relpath, rev_str, self.relpath))
197
[email protected]e28e4982009-09-25 20:51:45198 new_base = 'origin'
199 if revision:
200 new_base = revision
[email protected]5bde4852009-12-14 16:47:12201 cur_branch = self._GetCurrentBranch()
202
203 # Check if we are in a rebase conflict
204 if cur_branch is None:
205 raise gclient_utils.Error('\n____ %s%s\n'
206 '\tAlready in a conflict, i.e. (no branch).\n'
207 '\tFix the conflict and run gclient again.\n'
208 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
209 '\tSee man git-rebase for details.\n'
210 % (self.relpath, rev_str))
211
[email protected]f2370632009-11-25 00:22:11212 merge_base = self._Run(['merge-base', 'HEAD', new_base])
213 self._Run(['remote', 'update'], redirect_stdout=False)
[email protected]5aeb7dd2009-11-17 18:09:01214 files = self._Run(['diff', new_base, '--name-only']).split()
[email protected]e28e4982009-09-25 20:51:45215 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
[email protected]eafef3b2010-01-25 17:42:27216 if options.force:
217 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
[email protected]f2370632009-11-25 00:22:11218 self._Run(['rebase', '-v', '--onto', new_base, merge_base, cur_branch],
[email protected]5bde4852009-12-14 16:47:12219 redirect_stdout=False, checkrc=False)
220
221 # If the rebase generated a conflict, abort and ask user to fix
222 if self._GetCurrentBranch() is None:
223 raise gclient_utils.Error('\n____ %s%s\n'
224 '\nConflict while rebasing this branch.\n'
225 'Fix the conflict and run gclient again.\n'
226 'See man git-rebase for details.\n'
227 % (self.relpath, rev_str))
228
[email protected]b1a22bf2009-11-07 02:33:50229 print "Checked out revision %s." % self.revinfo(options, (), None)
[email protected]e28e4982009-09-25 20:51:45230
231 def revert(self, options, args, file_list):
232 """Reverts local modifications.
233
234 All reverted files will be appended to file_list.
235 """
[email protected]e3608df2009-11-10 20:22:57236 __pychecker__ = 'unusednames=args'
[email protected]260c6532009-10-28 03:22:35237 path = os.path.join(self._root_dir, self.relpath)
238 if not os.path.isdir(path):
239 # revert won't work if the directory doesn't exist. It needs to
240 # checkout instead.
241 print("\n_____ %s is missing, synching instead" % self.relpath)
242 # Don't reuse the args.
243 return self.update(options, [], file_list)
[email protected]5aeb7dd2009-11-17 18:09:01244 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
245 files = self._Run(['diff', merge_base, '--name-only']).split()
246 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45247 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
248
[email protected]0f282062009-11-06 20:14:02249 def revinfo(self, options, args, file_list):
250 """Display revision"""
[email protected]3904caa2010-01-25 17:37:46251 __pychecker__ = 'unusednames=options,args,file_list'
[email protected]5aeb7dd2009-11-17 18:09:01252 return self._Run(['rev-parse', 'HEAD'])
[email protected]0f282062009-11-06 20:14:02253
[email protected]e28e4982009-09-25 20:51:45254 def runhooks(self, options, args, file_list):
255 self.status(options, args, file_list)
256
257 def status(self, options, args, file_list):
258 """Display status information."""
[email protected]3904caa2010-01-25 17:37:46259 __pychecker__ = 'unusednames=options,args'
[email protected]e28e4982009-09-25 20:51:45260 if not os.path.isdir(self.checkout_path):
261 print('\n________ couldn\'t run status in %s:\nThe directory '
[email protected]e3608df2009-11-10 20:22:57262 'does not exist.' % self.checkout_path)
[email protected]e28e4982009-09-25 20:51:45263 else:
[email protected]5aeb7dd2009-11-17 18:09:01264 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
265 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
266 files = self._Run(['diff', '--name-only', merge_base]).split()
[email protected]e28e4982009-09-25 20:51:45267 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
268
[email protected]e6f78352010-01-13 17:05:33269 def FullUrlForRelativeUrl(self, url):
270 # Strip from last '/'
271 # Equivalent to unix basename
272 base_url = self.url
273 return base_url[:base_url.rfind('/')] + url
274
[email protected]923a0372009-12-11 20:42:43275 def _CheckMinVersion(self, min_version):
[email protected]83376f22009-12-11 22:25:31276 def only_int(val):
277 if val.isdigit():
278 return int(val)
279 else:
280 return 0
[email protected]ba9b2392009-12-11 23:30:13281 version = self._Run(['--version'], cwd='.').split()[-1]
[email protected]83376f22009-12-11 22:25:31282 version_list = map(only_int, version.split('.'))
[email protected]923a0372009-12-11 20:42:43283 min_version_list = map(int, min_version.split('.'))
284 for min_ver in min_version_list:
285 ver = version_list.pop(0)
286 if min_ver > ver:
287 raise gclient_utils.Error('git version %s < minimum required %s' %
288 (version, min_version))
289 elif min_ver < ver:
290 return
291
[email protected]5bde4852009-12-14 16:47:12292 def _GetCurrentBranch(self):
293 # Returns name of current branch
294 # Returns None if inside a (no branch)
295 tokens = self._Run(['branch']).split()
296 branch = tokens[tokens.index('*') + 1]
297 if branch == '(no':
298 return None
299 return branch
300
[email protected]5aeb7dd2009-11-17 18:09:01301 def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True):
302 # TODO(maruel): Merge with Capture?
[email protected]ffe96f02009-12-09 18:39:15303 if cwd is None:
304 cwd = self.checkout_path
[email protected]e8e60e52009-11-02 21:50:56305 stdout=None
306 if redirect_stdout:
307 stdout=subprocess.PIPE
[email protected]e28e4982009-09-25 20:51:45308 if cwd == None:
309 cwd = self.checkout_path
[email protected]5aeb7dd2009-11-17 18:09:01310 cmd = [self.COMMAND]
[email protected]e28e4982009-09-25 20:51:45311 cmd.extend(args)
[email protected]f3909bf2010-01-08 01:14:51312 logging.debug(cmd)
313 try:
314 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
315 output = sp.communicate()[0]
316 except OSError:
317 raise gclient_utils.Error("git command '%s' failed to run." %
318 ' '.join(cmd) + "\nCheck that you have git installed.")
[email protected]e28e4982009-09-25 20:51:45319 if checkrc and sp.returncode:
320 raise gclient_utils.Error('git command %s returned %d' %
321 (args[0], sp.returncode))
[email protected]5aeb7dd2009-11-17 18:09:01322 if output is not None:
[email protected]e8e60e52009-11-02 21:50:56323 return output.strip()
[email protected]e28e4982009-09-25 20:51:45324
325
[email protected]5aeb7dd2009-11-17 18:09:01326class SVNWrapper(SCMWrapper, scm.SVN):
[email protected]cb5442b2009-09-22 16:51:24327 """ Wrapper for SVN """
[email protected]5f3eee32009-09-17 00:34:30328
329 def cleanup(self, options, args, file_list):
330 """Cleanup working copy."""
[email protected]e3608df2009-11-10 20:22:57331 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30332 command = ['cleanup']
333 command.extend(args)
[email protected]5aeb7dd2009-11-17 18:09:01334 self.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30335
336 def diff(self, options, args, file_list):
337 # NOTE: This function does not currently modify file_list.
[email protected]e3608df2009-11-10 20:22:57338 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30339 command = ['diff']
340 command.extend(args)
[email protected]5aeb7dd2009-11-17 18:09:01341 self.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30342
343 def export(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31344 """Export a clean directory tree into the given path."""
[email protected]e3608df2009-11-10 20:22:57345 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30346 assert len(args) == 1
347 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
348 try:
349 os.makedirs(export_path)
350 except OSError:
351 pass
352 assert os.path.exists(export_path)
353 command = ['export', '--force', '.']
354 command.append(export_path)
[email protected]5aeb7dd2009-11-17 18:09:01355 self.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30356
[email protected]ee4071d2009-12-22 22:25:37357 def pack(self, options, args, file_list):
358 """Generates a patch file which can be applied to the root of the
359 repository."""
360 __pychecker__ = 'unusednames=file_list,options'
361 path = os.path.join(self._root_dir, self.relpath)
362 command = ['diff']
363 command.extend(args)
364
365 filterer = DiffFilterer(self.relpath)
366 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)
367
[email protected]5f3eee32009-09-17 00:34:30368 def update(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31369 """Runs svn to update or transparently checkout the working copy.
[email protected]5f3eee32009-09-17 00:34:30370
371 All updated files will be appended to file_list.
372
373 Raises:
374 Error: if can't get URL for relative path.
375 """
376 # Only update if git is not controlling the directory.
377 checkout_path = os.path.join(self._root_dir, self.relpath)
378 git_path = os.path.join(self._root_dir, self.relpath, '.git')
379 if os.path.exists(git_path):
380 print("________ found .git directory; skipping %s" % self.relpath)
381 return
382
383 if args:
384 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
385
[email protected]ac915bb2009-11-13 17:03:01386 url, revision = gclient_utils.SplitUrlRevision(self.url)
387 base_url = url
[email protected]5f3eee32009-09-17 00:34:30388 forced_revision = False
[email protected]ac915bb2009-11-13 17:03:01389 rev_str = ""
[email protected]5f3eee32009-09-17 00:34:30390 if options.revision:
391 # Override the revision number.
[email protected]ac915bb2009-11-13 17:03:01392 revision = str(options.revision)
[email protected]5f3eee32009-09-17 00:34:30393 if revision:
[email protected]ac915bb2009-11-13 17:03:01394 forced_revision = True
395 url = '%s@%s' % (url, revision)
[email protected]770ff9e2009-09-23 17:18:18396 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30397
398 if not os.path.exists(checkout_path):
399 # We need to checkout.
400 command = ['checkout', url, checkout_path]
401 if revision:
402 command.extend(['--revision', str(revision)])
[email protected]5aeb7dd2009-11-17 18:09:01403 self.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30404 return
405
406 # Get the existing scm url and the revision number of the current checkout.
[email protected]5aeb7dd2009-11-17 18:09:01407 from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.')
[email protected]5f3eee32009-09-17 00:34:30408 if not from_info:
409 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
410 "directory is present. Delete the directory "
411 "and try again." %
412 checkout_path)
413
[email protected]7753d242009-10-07 17:40:24414 if options.manually_grab_svn_rev:
415 # Retrieve the current HEAD version because svn is slow at null updates.
416 if not revision:
[email protected]5aeb7dd2009-11-17 18:09:01417 from_info_live = self.CaptureInfo(from_info['URL'], '.')
[email protected]7753d242009-10-07 17:40:24418 revision = str(from_info_live['Revision'])
419 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30420
[email protected]ac915bb2009-11-13 17:03:01421 if from_info['URL'] != base_url:
[email protected]5aeb7dd2009-11-17 18:09:01422 to_info = self.CaptureInfo(url, '.')
[email protected]e2ce0c72009-09-23 16:14:18423 if not to_info.get('Repository Root') or not to_info.get('UUID'):
424 # The url is invalid or the server is not accessible, it's safer to bail
425 # out right now.
426 raise gclient_utils.Error('This url is unreachable: %s' % url)
[email protected]5f3eee32009-09-17 00:34:30427 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
428 and (from_info['UUID'] == to_info['UUID']))
429 if can_switch:
430 print("\n_____ relocating %s to a new checkout" % self.relpath)
431 # We have different roots, so check if we can switch --relocate.
432 # Subversion only permits this if the repository UUIDs match.
433 # Perform the switch --relocate, then rewrite the from_url
434 # to reflect where we "are now." (This is the same way that
435 # Subversion itself handles the metadata when switch --relocate
436 # is used.) This makes the checks below for whether we
437 # can update to a revision or have to switch to a different
438 # branch work as expected.
439 # TODO(maruel): TEST ME !
440 command = ["switch", "--relocate",
441 from_info['Repository Root'],
442 to_info['Repository Root'],
443 self.relpath]
[email protected]5aeb7dd2009-11-17 18:09:01444 self.Run(command, self._root_dir)
[email protected]5f3eee32009-09-17 00:34:30445 from_info['URL'] = from_info['URL'].replace(
446 from_info['Repository Root'],
447 to_info['Repository Root'])
448 else:
[email protected]5aeb7dd2009-11-17 18:09:01449 if self.CaptureStatus(checkout_path):
[email protected]5f3eee32009-09-17 00:34:30450 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
451 "don't match and there is local changes "
452 "in %s. Delete the directory and "
453 "try again." % (url, checkout_path))
454 # Ok delete it.
455 print("\n_____ switching %s to a new checkout" % self.relpath)
[email protected]8f9c69f2009-09-17 00:48:28456 gclient_utils.RemoveDirectory(checkout_path)
[email protected]5f3eee32009-09-17 00:34:30457 # We need to checkout.
458 command = ['checkout', url, checkout_path]
459 if revision:
460 command.extend(['--revision', str(revision)])
[email protected]5aeb7dd2009-11-17 18:09:01461 self.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30462 return
463
464
465 # If the provided url has a revision number that matches the revision
466 # number of the existing directory, then we don't need to bother updating.
[email protected]2e0c6852009-09-24 00:02:07467 if not options.force and str(from_info['Revision']) == revision:
[email protected]5f3eee32009-09-17 00:34:30468 if options.verbose or not forced_revision:
469 print("\n_____ %s%s" % (self.relpath, rev_str))
470 return
471
472 command = ["update", checkout_path]
473 if revision:
474 command.extend(['--revision', str(revision)])
[email protected]5aeb7dd2009-11-17 18:09:01475 self.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30476
477 def revert(self, options, args, file_list):
478 """Reverts local modifications. Subversion specific.
479
480 All reverted files will be appended to file_list, even if Subversion
481 doesn't know about them.
482 """
[email protected]e3608df2009-11-10 20:22:57483 __pychecker__ = 'unusednames=args'
[email protected]5f3eee32009-09-17 00:34:30484 path = os.path.join(self._root_dir, self.relpath)
485 if not os.path.isdir(path):
486 # svn revert won't work if the directory doesn't exist. It needs to
487 # checkout instead.
488 print("\n_____ %s is missing, synching instead" % self.relpath)
489 # Don't reuse the args.
490 return self.update(options, [], file_list)
491
[email protected]5aeb7dd2009-11-17 18:09:01492 for file_status in self.CaptureStatus(path):
[email protected]e3608df2009-11-10 20:22:57493 file_path = os.path.join(path, file_status[1])
494 if file_status[0][0] == 'X':
[email protected]754960e2009-09-21 12:31:05495 # Ignore externals.
[email protected]aa3dd472009-09-21 19:02:48496 logging.info('Ignoring external %s' % file_path)
[email protected]754960e2009-09-21 12:31:05497 continue
498
[email protected]aa3dd472009-09-21 19:02:48499 if logging.getLogger().isEnabledFor(logging.INFO):
500 logging.info('%s%s' % (file[0], file[1]))
501 else:
502 print(file_path)
[email protected]e3608df2009-11-10 20:22:57503 if file_status[0].isspace():
[email protected]aa3dd472009-09-21 19:02:48504 logging.error('No idea what is the status of %s.\n'
505 'You just found a bug in gclient, please ping '
506 '[email protected] ASAP!' % file_path)
507 # svn revert is really stupid. It fails on inconsistent line-endings,
508 # on switched directories, etc. So take no chance and delete everything!
509 try:
510 if not os.path.exists(file_path):
511 pass
[email protected]d2e78ff2010-01-11 20:37:19512 elif os.path.isfile(file_path) or os.path.islink(file_path):
[email protected]754960e2009-09-21 12:31:05513 logging.info('os.remove(%s)' % file_path)
[email protected]5f3eee32009-09-17 00:34:30514 os.remove(file_path)
[email protected]aa3dd472009-09-21 19:02:48515 elif os.path.isdir(file_path):
[email protected]754960e2009-09-21 12:31:05516 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
[email protected]8f9c69f2009-09-17 00:48:28517 gclient_utils.RemoveDirectory(file_path)
[email protected]5f3eee32009-09-17 00:34:30518 else:
[email protected]aa3dd472009-09-21 19:02:48519 logging.error('no idea what is %s.\nYou just found a bug in gclient'
520 ', please ping [email protected] ASAP!' % file_path)
521 except EnvironmentError:
522 logging.error('Failed to remove %s.' % file_path)
523
[email protected]810a50b2009-10-05 23:03:18524 try:
525 # svn revert is so broken we don't even use it. Using
526 # "svn up --revision BASE" achieve the same effect.
[email protected]5aeb7dd2009-11-17 18:09:01527 self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
[email protected]810a50b2009-10-05 23:03:18528 file_list)
529 except OSError, e:
530 # Maybe the directory disapeared meanwhile. We don't want it to throw an
531 # exception.
532 logging.error('Failed to update:\n%s' % str(e))
[email protected]5f3eee32009-09-17 00:34:30533
[email protected]0f282062009-11-06 20:14:02534 def revinfo(self, options, args, file_list):
535 """Display revision"""
[email protected]e3608df2009-11-10 20:22:57536 __pychecker__ = 'unusednames=args,file_list,options'
[email protected]5aeb7dd2009-11-17 18:09:01537 return self.CaptureHeadRevision(self.url)
[email protected]0f282062009-11-06 20:14:02538
[email protected]cb5442b2009-09-22 16:51:24539 def runhooks(self, options, args, file_list):
540 self.status(options, args, file_list)
541
[email protected]5f3eee32009-09-17 00:34:30542 def status(self, options, args, file_list):
543 """Display status information."""
544 path = os.path.join(self._root_dir, self.relpath)
545 command = ['status']
546 command.extend(args)
547 if not os.path.isdir(path):
548 # svn status won't work if the directory doesn't exist.
549 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
550 "does not exist."
551 % (' '.join(command), path))
552 # There's no file list to retrieve.
553 else:
[email protected]5aeb7dd2009-11-17 18:09:01554 self.RunAndGetFileList(options, command, path, file_list)
[email protected]e6f78352010-01-13 17:05:33555
556 def FullUrlForRelativeUrl(self, url):
557 # Find the forth '/' and strip from there. A bit hackish.
558 return '/'.join(self.url.split('/')[:4]) + url