blob: a3e902cf64e20044778b1bd293080794c86ffbe9 [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.org83376f22009-12-11 22:25:31 +0000118 self._CheckMinVersion("1.6")
msb@chromium.org923a0372009-12-11 20:42:43 +0000119
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000120 url, revision = gclient_utils.SplitUrlRevision(self.url)
121 rev_str = ""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000122 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000123 # Override the revision number.
124 revision = str(options.revision)
125 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000126 rev_str = ' at %s' % revision
msb@chromium.orge28e4982009-09-25 20:51:45 +0000127
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000128 if options.verbose:
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000129 print("\n_____ %s%s" % (self.relpath, rev_str))
130
msb@chromium.orge28e4982009-09-25 20:51:45 +0000131 if not os.path.exists(self.checkout_path):
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000132 self._Run(['clone', url, self.checkout_path],
133 cwd=self._root_dir, redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000134 if revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000135 self._Run(['reset', '--hard', revision], redirect_stdout=False)
136 files = self._Run(['ls-files']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000137 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
138 return
139
msb@chromium.orge28e4982009-09-25 20:51:45 +0000140 new_base = 'origin'
141 if revision:
142 new_base = revision
msb@chromium.org5bde4852009-12-14 16:47:12 +0000143 cur_branch = self._GetCurrentBranch()
144
145 # Check if we are in a rebase conflict
146 if cur_branch is None:
147 raise gclient_utils.Error('\n____ %s%s\n'
148 '\tAlready in a conflict, i.e. (no branch).\n'
149 '\tFix the conflict and run gclient again.\n'
150 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
151 '\tSee man git-rebase for details.\n'
152 % (self.relpath, rev_str))
153
msb@chromium.orgf2370632009-11-25 00:22:11 +0000154 merge_base = self._Run(['merge-base', 'HEAD', new_base])
155 self._Run(['remote', 'update'], redirect_stdout=False)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000156 files = self._Run(['diff', new_base, '--name-only']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000157 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.orgf2370632009-11-25 00:22:11 +0000158 self._Run(['rebase', '-v', '--onto', new_base, merge_base, cur_branch],
msb@chromium.org5bde4852009-12-14 16:47:12 +0000159 redirect_stdout=False, checkrc=False)
160
161 # If the rebase generated a conflict, abort and ask user to fix
162 if self._GetCurrentBranch() is None:
163 raise gclient_utils.Error('\n____ %s%s\n'
164 '\nConflict while rebasing this branch.\n'
165 'Fix the conflict and run gclient again.\n'
166 'See man git-rebase for details.\n'
167 % (self.relpath, rev_str))
168
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000169 print "Checked out revision %s." % self.revinfo(options, (), None)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000170
171 def revert(self, options, args, file_list):
172 """Reverts local modifications.
173
174 All reverted files will be appended to file_list.
175 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000176 __pychecker__ = 'unusednames=args'
msb@chromium.org260c6532009-10-28 03:22:35 +0000177 path = os.path.join(self._root_dir, self.relpath)
178 if not os.path.isdir(path):
179 # revert won't work if the directory doesn't exist. It needs to
180 # checkout instead.
181 print("\n_____ %s is missing, synching instead" % self.relpath)
182 # Don't reuse the args.
183 return self.update(options, [], file_list)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000184 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
185 files = self._Run(['diff', merge_base, '--name-only']).split()
186 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000187 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
188
msb@chromium.org0f282062009-11-06 20:14:02 +0000189 def revinfo(self, options, args, file_list):
190 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000191 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000192 return self._Run(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000193
msb@chromium.orge28e4982009-09-25 20:51:45 +0000194 def runhooks(self, options, args, file_list):
195 self.status(options, args, file_list)
196
197 def status(self, options, args, file_list):
198 """Display status information."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000199 __pychecker__ = 'unusednames=args,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000200 if not os.path.isdir(self.checkout_path):
201 print('\n________ couldn\'t run status in %s:\nThe directory '
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000202 'does not exist.' % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000203 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000204 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
205 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
206 files = self._Run(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000207 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
208
msb@chromium.org923a0372009-12-11 20:42:43 +0000209 def _CheckMinVersion(self, min_version):
msb@chromium.org83376f22009-12-11 22:25:31 +0000210 def only_int(val):
211 if val.isdigit():
212 return int(val)
213 else:
214 return 0
msb@chromium.orgba9b2392009-12-11 23:30:13 +0000215 version = self._Run(['--version'], cwd='.').split()[-1]
msb@chromium.org83376f22009-12-11 22:25:31 +0000216 version_list = map(only_int, version.split('.'))
msb@chromium.org923a0372009-12-11 20:42:43 +0000217 min_version_list = map(int, min_version.split('.'))
218 for min_ver in min_version_list:
219 ver = version_list.pop(0)
220 if min_ver > ver:
221 raise gclient_utils.Error('git version %s < minimum required %s' %
222 (version, min_version))
223 elif min_ver < ver:
224 return
225
msb@chromium.org5bde4852009-12-14 16:47:12 +0000226 def _GetCurrentBranch(self):
227 # Returns name of current branch
228 # Returns None if inside a (no branch)
229 tokens = self._Run(['branch']).split()
230 branch = tokens[tokens.index('*') + 1]
231 if branch == '(no':
232 return None
233 return branch
234
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000235 def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True):
236 # TODO(maruel): Merge with Capture?
maruel@chromium.orgffe96f02009-12-09 18:39:15 +0000237 if cwd is None:
238 cwd = self.checkout_path
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000239 stdout=None
240 if redirect_stdout:
241 stdout=subprocess.PIPE
msb@chromium.orge28e4982009-09-25 20:51:45 +0000242 if cwd == None:
243 cwd = self.checkout_path
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000244 cmd = [self.COMMAND]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000245 cmd.extend(args)
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000246 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
maruel@chromium.orgffe96f02009-12-09 18:39:15 +0000247 output = sp.communicate()[0]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000248 if checkrc and sp.returncode:
249 raise gclient_utils.Error('git command %s returned %d' %
250 (args[0], sp.returncode))
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000251 if output is not None:
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000252 return output.strip()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000253
254
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000255class SVNWrapper(SCMWrapper, scm.SVN):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000256 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000257
258 def cleanup(self, options, args, file_list):
259 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000260 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000261 command = ['cleanup']
262 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000263 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000264
265 def diff(self, options, args, file_list):
266 # NOTE: This function does not currently modify file_list.
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000267 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000268 command = ['diff']
269 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000270 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000271
272 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000273 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000274 assert len(args) == 1
275 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
276 try:
277 os.makedirs(export_path)
278 except OSError:
279 pass
280 assert os.path.exists(export_path)
281 command = ['export', '--force', '.']
282 command.append(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000283 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000284
285 def update(self, options, args, file_list):
286 """Runs SCM to update or transparently checkout the working copy.
287
288 All updated files will be appended to file_list.
289
290 Raises:
291 Error: if can't get URL for relative path.
292 """
293 # Only update if git is not controlling the directory.
294 checkout_path = os.path.join(self._root_dir, self.relpath)
295 git_path = os.path.join(self._root_dir, self.relpath, '.git')
296 if os.path.exists(git_path):
297 print("________ found .git directory; skipping %s" % self.relpath)
298 return
299
300 if args:
301 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
302
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000303 url, revision = gclient_utils.SplitUrlRevision(self.url)
304 base_url = url
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000305 forced_revision = False
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000306 rev_str = ""
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000307 if options.revision:
308 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000309 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000310 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000311 forced_revision = True
312 url = '%s@%s' % (url, revision)
msb@chromium.org770ff9e2009-09-23 17:18:18 +0000313 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000314
315 if not os.path.exists(checkout_path):
316 # We need to checkout.
317 command = ['checkout', url, checkout_path]
318 if revision:
319 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000320 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000321 return
322
323 # Get the existing scm url and the revision number of the current checkout.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000324 from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000325 if not from_info:
326 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
327 "directory is present. Delete the directory "
328 "and try again." %
329 checkout_path)
330
maruel@chromium.org7753d242009-10-07 17:40:24 +0000331 if options.manually_grab_svn_rev:
332 # Retrieve the current HEAD version because svn is slow at null updates.
333 if not revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000334 from_info_live = self.CaptureInfo(from_info['URL'], '.')
maruel@chromium.org7753d242009-10-07 17:40:24 +0000335 revision = str(from_info_live['Revision'])
336 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000337
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000338 if from_info['URL'] != base_url:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000339 to_info = self.CaptureInfo(url, '.')
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +0000340 if not to_info.get('Repository Root') or not to_info.get('UUID'):
341 # The url is invalid or the server is not accessible, it's safer to bail
342 # out right now.
343 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000344 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
345 and (from_info['UUID'] == to_info['UUID']))
346 if can_switch:
347 print("\n_____ relocating %s to a new checkout" % self.relpath)
348 # We have different roots, so check if we can switch --relocate.
349 # Subversion only permits this if the repository UUIDs match.
350 # Perform the switch --relocate, then rewrite the from_url
351 # to reflect where we "are now." (This is the same way that
352 # Subversion itself handles the metadata when switch --relocate
353 # is used.) This makes the checks below for whether we
354 # can update to a revision or have to switch to a different
355 # branch work as expected.
356 # TODO(maruel): TEST ME !
357 command = ["switch", "--relocate",
358 from_info['Repository Root'],
359 to_info['Repository Root'],
360 self.relpath]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000361 self.Run(command, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000362 from_info['URL'] = from_info['URL'].replace(
363 from_info['Repository Root'],
364 to_info['Repository Root'])
365 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000366 if self.CaptureStatus(checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000367 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
368 "don't match and there is local changes "
369 "in %s. Delete the directory and "
370 "try again." % (url, checkout_path))
371 # Ok delete it.
372 print("\n_____ switching %s to a new checkout" % self.relpath)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000373 gclient_utils.RemoveDirectory(checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000374 # We need to checkout.
375 command = ['checkout', url, checkout_path]
376 if revision:
377 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000378 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000379 return
380
381
382 # If the provided url has a revision number that matches the revision
383 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +0000384 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000385 if options.verbose or not forced_revision:
386 print("\n_____ %s%s" % (self.relpath, rev_str))
387 return
388
389 command = ["update", checkout_path]
390 if revision:
391 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000392 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000393
394 def revert(self, options, args, file_list):
395 """Reverts local modifications. Subversion specific.
396
397 All reverted files will be appended to file_list, even if Subversion
398 doesn't know about them.
399 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000400 __pychecker__ = 'unusednames=args'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000401 path = os.path.join(self._root_dir, self.relpath)
402 if not os.path.isdir(path):
403 # svn revert won't work if the directory doesn't exist. It needs to
404 # checkout instead.
405 print("\n_____ %s is missing, synching instead" % self.relpath)
406 # Don't reuse the args.
407 return self.update(options, [], file_list)
408
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000409 for file_status in self.CaptureStatus(path):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000410 file_path = os.path.join(path, file_status[1])
411 if file_status[0][0] == 'X':
maruel@chromium.org754960e2009-09-21 12:31:05 +0000412 # Ignore externals.
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000413 logging.info('Ignoring external %s' % file_path)
maruel@chromium.org754960e2009-09-21 12:31:05 +0000414 continue
415
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000416 if logging.getLogger().isEnabledFor(logging.INFO):
417 logging.info('%s%s' % (file[0], file[1]))
418 else:
419 print(file_path)
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000420 if file_status[0].isspace():
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000421 logging.error('No idea what is the status of %s.\n'
422 'You just found a bug in gclient, please ping '
423 'maruel@chromium.org ASAP!' % file_path)
424 # svn revert is really stupid. It fails on inconsistent line-endings,
425 # on switched directories, etc. So take no chance and delete everything!
426 try:
427 if not os.path.exists(file_path):
428 pass
429 elif os.path.isfile(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000430 logging.info('os.remove(%s)' % file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000431 os.remove(file_path)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000432 elif os.path.isdir(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000433 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000434 gclient_utils.RemoveDirectory(file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000435 else:
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000436 logging.error('no idea what is %s.\nYou just found a bug in gclient'
437 ', please ping maruel@chromium.org ASAP!' % file_path)
438 except EnvironmentError:
439 logging.error('Failed to remove %s.' % file_path)
440
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000441 try:
442 # svn revert is so broken we don't even use it. Using
443 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000444 self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000445 file_list)
446 except OSError, e:
447 # Maybe the directory disapeared meanwhile. We don't want it to throw an
448 # exception.
449 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000450
msb@chromium.org0f282062009-11-06 20:14:02 +0000451 def revinfo(self, options, args, file_list):
452 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000453 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000454 return self.CaptureHeadRevision(self.url)
msb@chromium.org0f282062009-11-06 20:14:02 +0000455
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000456 def runhooks(self, options, args, file_list):
457 self.status(options, args, file_list)
458
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000459 def status(self, options, args, file_list):
460 """Display status information."""
461 path = os.path.join(self._root_dir, self.relpath)
462 command = ['status']
463 command.extend(args)
464 if not os.path.isdir(path):
465 # svn status won't work if the directory doesn't exist.
466 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
467 "does not exist."
468 % (' '.join(command), path))
469 # There's no file list to retrieve.
470 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000471 self.RunAndGetFileList(options, command, path, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000472
473 def pack(self, options, args, file_list):
474 """Generates a patch file which can be applied to the root of the
475 repository."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000476 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000477 path = os.path.join(self._root_dir, self.relpath)
478 command = ['diff']
479 command.extend(args)
480 # Simple class which tracks which file is being diffed and
481 # replaces instances of its file name in the original and
482 # working copy lines of the svn diff output.
483 class DiffFilterer(object):
484 index_string = "Index: "
485 original_prefix = "--- "
486 working_prefix = "+++ "
487
488 def __init__(self, relpath):
489 # Note that we always use '/' as the path separator to be
490 # consistent with svn's cygwin-style output on Windows
491 self._relpath = relpath.replace("\\", "/")
492 self._current_file = ""
493 self._replacement_file = ""
494
495 def SetCurrentFile(self, file):
496 self._current_file = file
497 # Note that we always use '/' as the path separator to be
498 # consistent with svn's cygwin-style output on Windows
499 self._replacement_file = self._relpath + '/' + file
500
501 def ReplaceAndPrint(self, line):
502 print(line.replace(self._current_file, self._replacement_file))
503
504 def Filter(self, line):
505 if (line.startswith(self.index_string)):
506 self.SetCurrentFile(line[len(self.index_string):])
507 self.ReplaceAndPrint(line)
508 else:
509 if (line.startswith(self.original_prefix) or
510 line.startswith(self.working_prefix)):
511 self.ReplaceAndPrint(line)
512 else:
513 print line
514
515 filterer = DiffFilterer(self.relpath)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000516 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)