blob: 25b652410423a2a4ea7066f2efe228894069496b [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
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000060 def RunCommand(self, command, options, args, file_list=None):
61 # file_list will have all files that are modified appended to it.
maruel@chromium.orgde754ac2009-09-17 18:04:50 +000062 if file_list is None:
63 file_list = []
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000064
msb@chromium.org0f282062009-11-06 20:14:02 +000065 commands = ['cleanup', 'export', 'update', 'revert', 'revinfo',
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000066 'status', 'diff', 'pack', 'runhooks']
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000067
68 if not command in commands:
69 raise gclient_utils.Error('Unknown command %s' % command)
70
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000071 if not command in dir(self):
72 raise gclient_utils.Error('Command %s not implemnted in %s wrapper' % (
73 command, self.scm_name))
74
75 return getattr(self, command)(options, args, file_list)
76
77
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000078class GitWrapper(SCMWrapper, scm.GIT):
msb@chromium.orge28e4982009-09-25 20:51:45 +000079 """Wrapper for Git"""
80
81 def cleanup(self, options, args, file_list):
82 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +000083 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000084 self._Run(['prune'], redirect_stdout=False)
85 self._Run(['fsck'], redirect_stdout=False)
86 self._Run(['gc'], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +000087
88 def diff(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +000089 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000090 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
91 self._Run(['diff', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +000092
93 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +000094 __pychecker__ = 'unusednames=file_list,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +000095 assert len(args) == 1
96 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
97 if not os.path.exists(export_path):
98 os.makedirs(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000099 self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path],
100 redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000101
102 def update(self, options, args, file_list):
103 """Runs git to update or transparently checkout the working copy.
104
105 All updated files will be appended to file_list.
106
107 Raises:
108 Error: if can't get URL for relative path.
109 """
110
111 if args:
112 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
113
msb@chromium.org83376f22009-12-11 22:25:31 +0000114 self._CheckMinVersion("1.6")
msb@chromium.org923a0372009-12-11 20:42:43 +0000115
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000116 url, revision = gclient_utils.SplitUrlRevision(self.url)
117 rev_str = ""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000118 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000119 # Override the revision number.
120 revision = str(options.revision)
121 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000122 rev_str = ' at %s' % revision
msb@chromium.orge28e4982009-09-25 20:51:45 +0000123
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000124 if options.verbose:
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000125 print("\n_____ %s%s" % (self.relpath, rev_str))
126
msb@chromium.orge28e4982009-09-25 20:51:45 +0000127 if not os.path.exists(self.checkout_path):
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000128 self._Run(['clone', url, self.checkout_path],
129 cwd=self._root_dir, redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000130 if revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000131 self._Run(['reset', '--hard', revision], redirect_stdout=False)
132 files = self._Run(['ls-files']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000133 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
134 return
135
msb@chromium.orge28e4982009-09-25 20:51:45 +0000136 new_base = 'origin'
137 if revision:
138 new_base = revision
msb@chromium.org5bde4852009-12-14 16:47:12 +0000139 cur_branch = self._GetCurrentBranch()
140
141 # Check if we are in a rebase conflict
142 if cur_branch is None:
143 raise gclient_utils.Error('\n____ %s%s\n'
144 '\tAlready in a conflict, i.e. (no branch).\n'
145 '\tFix the conflict and run gclient again.\n'
146 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
147 '\tSee man git-rebase for details.\n'
148 % (self.relpath, rev_str))
149
msb@chromium.orgf2370632009-11-25 00:22:11 +0000150 merge_base = self._Run(['merge-base', 'HEAD', new_base])
151 self._Run(['remote', 'update'], redirect_stdout=False)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000152 files = self._Run(['diff', new_base, '--name-only']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000153 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.orgf2370632009-11-25 00:22:11 +0000154 self._Run(['rebase', '-v', '--onto', new_base, merge_base, cur_branch],
msb@chromium.org5bde4852009-12-14 16:47:12 +0000155 redirect_stdout=False, checkrc=False)
156
157 # If the rebase generated a conflict, abort and ask user to fix
158 if self._GetCurrentBranch() is None:
159 raise gclient_utils.Error('\n____ %s%s\n'
160 '\nConflict while rebasing this branch.\n'
161 'Fix the conflict and run gclient again.\n'
162 'See man git-rebase for details.\n'
163 % (self.relpath, rev_str))
164
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000165 print "Checked out revision %s." % self.revinfo(options, (), None)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000166
167 def revert(self, options, args, file_list):
168 """Reverts local modifications.
169
170 All reverted files will be appended to file_list.
171 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000172 __pychecker__ = 'unusednames=args'
msb@chromium.org260c6532009-10-28 03:22:35 +0000173 path = os.path.join(self._root_dir, self.relpath)
174 if not os.path.isdir(path):
175 # revert won't work if the directory doesn't exist. It needs to
176 # checkout instead.
177 print("\n_____ %s is missing, synching instead" % self.relpath)
178 # Don't reuse the args.
179 return self.update(options, [], file_list)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000180 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
181 files = self._Run(['diff', merge_base, '--name-only']).split()
182 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000183 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
184
msb@chromium.org0f282062009-11-06 20:14:02 +0000185 def revinfo(self, options, args, file_list):
186 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000187 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000188 return self._Run(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000189
msb@chromium.orge28e4982009-09-25 20:51:45 +0000190 def runhooks(self, options, args, file_list):
191 self.status(options, args, file_list)
192
193 def status(self, options, args, file_list):
194 """Display status information."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000195 __pychecker__ = 'unusednames=args,options'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000196 if not os.path.isdir(self.checkout_path):
197 print('\n________ couldn\'t run status in %s:\nThe directory '
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000198 'does not exist.' % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000199 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000200 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
201 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
202 files = self._Run(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000203 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
204
msb@chromium.org923a0372009-12-11 20:42:43 +0000205 def _CheckMinVersion(self, min_version):
msb@chromium.org83376f22009-12-11 22:25:31 +0000206 def only_int(val):
207 if val.isdigit():
208 return int(val)
209 else:
210 return 0
msb@chromium.orgba9b2392009-12-11 23:30:13 +0000211 version = self._Run(['--version'], cwd='.').split()[-1]
msb@chromium.org83376f22009-12-11 22:25:31 +0000212 version_list = map(only_int, version.split('.'))
msb@chromium.org923a0372009-12-11 20:42:43 +0000213 min_version_list = map(int, min_version.split('.'))
214 for min_ver in min_version_list:
215 ver = version_list.pop(0)
216 if min_ver > ver:
217 raise gclient_utils.Error('git version %s < minimum required %s' %
218 (version, min_version))
219 elif min_ver < ver:
220 return
221
msb@chromium.org5bde4852009-12-14 16:47:12 +0000222 def _GetCurrentBranch(self):
223 # Returns name of current branch
224 # Returns None if inside a (no branch)
225 tokens = self._Run(['branch']).split()
226 branch = tokens[tokens.index('*') + 1]
227 if branch == '(no':
228 return None
229 return branch
230
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000231 def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True):
232 # TODO(maruel): Merge with Capture?
maruel@chromium.orgffe96f02009-12-09 18:39:15 +0000233 if cwd is None:
234 cwd = self.checkout_path
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000235 stdout=None
236 if redirect_stdout:
237 stdout=subprocess.PIPE
msb@chromium.orge28e4982009-09-25 20:51:45 +0000238 if cwd == None:
239 cwd = self.checkout_path
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000240 cmd = [self.COMMAND]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000241 cmd.extend(args)
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000242 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
maruel@chromium.orgffe96f02009-12-09 18:39:15 +0000243 output = sp.communicate()[0]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000244 if checkrc and sp.returncode:
245 raise gclient_utils.Error('git command %s returned %d' %
246 (args[0], sp.returncode))
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000247 if output is not None:
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000248 return output.strip()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000249
250
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000251class SVNWrapper(SCMWrapper, scm.SVN):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000252 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000253
254 def cleanup(self, options, args, file_list):
255 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000256 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000257 command = ['cleanup']
258 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000259 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000260
261 def diff(self, options, args, file_list):
262 # NOTE: This function does not currently modify file_list.
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000263 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000264 command = ['diff']
265 command.extend(args)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000266 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000267
268 def export(self, options, args, file_list):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000269 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000270 assert len(args) == 1
271 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
272 try:
273 os.makedirs(export_path)
274 except OSError:
275 pass
276 assert os.path.exists(export_path)
277 command = ['export', '--force', '.']
278 command.append(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000279 self.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000280
281 def update(self, options, args, file_list):
282 """Runs SCM to update or transparently checkout the working copy.
283
284 All updated files will be appended to file_list.
285
286 Raises:
287 Error: if can't get URL for relative path.
288 """
289 # Only update if git is not controlling the directory.
290 checkout_path = os.path.join(self._root_dir, self.relpath)
291 git_path = os.path.join(self._root_dir, self.relpath, '.git')
292 if os.path.exists(git_path):
293 print("________ found .git directory; skipping %s" % self.relpath)
294 return
295
296 if args:
297 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
298
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000299 url, revision = gclient_utils.SplitUrlRevision(self.url)
300 base_url = url
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000301 forced_revision = False
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000302 rev_str = ""
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000303 if options.revision:
304 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000305 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000306 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000307 forced_revision = True
308 url = '%s@%s' % (url, revision)
msb@chromium.org770ff9e2009-09-23 17:18:18 +0000309 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000310
311 if not os.path.exists(checkout_path):
312 # We need to checkout.
313 command = ['checkout', url, checkout_path]
314 if revision:
315 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000316 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000317 return
318
319 # Get the existing scm url and the revision number of the current checkout.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000320 from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000321 if not from_info:
322 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
323 "directory is present. Delete the directory "
324 "and try again." %
325 checkout_path)
326
maruel@chromium.org7753d242009-10-07 17:40:24 +0000327 if options.manually_grab_svn_rev:
328 # Retrieve the current HEAD version because svn is slow at null updates.
329 if not revision:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000330 from_info_live = self.CaptureInfo(from_info['URL'], '.')
maruel@chromium.org7753d242009-10-07 17:40:24 +0000331 revision = str(from_info_live['Revision'])
332 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000333
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000334 if from_info['URL'] != base_url:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000335 to_info = self.CaptureInfo(url, '.')
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +0000336 if not to_info.get('Repository Root') or not to_info.get('UUID'):
337 # The url is invalid or the server is not accessible, it's safer to bail
338 # out right now.
339 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000340 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
341 and (from_info['UUID'] == to_info['UUID']))
342 if can_switch:
343 print("\n_____ relocating %s to a new checkout" % self.relpath)
344 # We have different roots, so check if we can switch --relocate.
345 # Subversion only permits this if the repository UUIDs match.
346 # Perform the switch --relocate, then rewrite the from_url
347 # to reflect where we "are now." (This is the same way that
348 # Subversion itself handles the metadata when switch --relocate
349 # is used.) This makes the checks below for whether we
350 # can update to a revision or have to switch to a different
351 # branch work as expected.
352 # TODO(maruel): TEST ME !
353 command = ["switch", "--relocate",
354 from_info['Repository Root'],
355 to_info['Repository Root'],
356 self.relpath]
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000357 self.Run(command, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000358 from_info['URL'] = from_info['URL'].replace(
359 from_info['Repository Root'],
360 to_info['Repository Root'])
361 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000362 if self.CaptureStatus(checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000363 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
364 "don't match and there is local changes "
365 "in %s. Delete the directory and "
366 "try again." % (url, checkout_path))
367 # Ok delete it.
368 print("\n_____ switching %s to a new checkout" % self.relpath)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000369 gclient_utils.RemoveDirectory(checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000370 # We need to checkout.
371 command = ['checkout', url, checkout_path]
372 if revision:
373 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000374 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000375 return
376
377
378 # If the provided url has a revision number that matches the revision
379 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +0000380 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000381 if options.verbose or not forced_revision:
382 print("\n_____ %s%s" % (self.relpath, rev_str))
383 return
384
385 command = ["update", checkout_path]
386 if revision:
387 command.extend(['--revision', str(revision)])
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000388 self.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000389
390 def revert(self, options, args, file_list):
391 """Reverts local modifications. Subversion specific.
392
393 All reverted files will be appended to file_list, even if Subversion
394 doesn't know about them.
395 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000396 __pychecker__ = 'unusednames=args'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000397 path = os.path.join(self._root_dir, self.relpath)
398 if not os.path.isdir(path):
399 # svn revert won't work if the directory doesn't exist. It needs to
400 # checkout instead.
401 print("\n_____ %s is missing, synching instead" % self.relpath)
402 # Don't reuse the args.
403 return self.update(options, [], file_list)
404
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000405 for file_status in self.CaptureStatus(path):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000406 file_path = os.path.join(path, file_status[1])
407 if file_status[0][0] == 'X':
maruel@chromium.org754960e2009-09-21 12:31:05 +0000408 # Ignore externals.
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000409 logging.info('Ignoring external %s' % file_path)
maruel@chromium.org754960e2009-09-21 12:31:05 +0000410 continue
411
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000412 if logging.getLogger().isEnabledFor(logging.INFO):
413 logging.info('%s%s' % (file[0], file[1]))
414 else:
415 print(file_path)
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000416 if file_status[0].isspace():
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000417 logging.error('No idea what is the status of %s.\n'
418 'You just found a bug in gclient, please ping '
419 'maruel@chromium.org ASAP!' % file_path)
420 # svn revert is really stupid. It fails on inconsistent line-endings,
421 # on switched directories, etc. So take no chance and delete everything!
422 try:
423 if not os.path.exists(file_path):
424 pass
425 elif os.path.isfile(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000426 logging.info('os.remove(%s)' % file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000427 os.remove(file_path)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000428 elif os.path.isdir(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000429 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000430 gclient_utils.RemoveDirectory(file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000431 else:
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000432 logging.error('no idea what is %s.\nYou just found a bug in gclient'
433 ', please ping maruel@chromium.org ASAP!' % file_path)
434 except EnvironmentError:
435 logging.error('Failed to remove %s.' % file_path)
436
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000437 try:
438 # svn revert is so broken we don't even use it. Using
439 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000440 self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000441 file_list)
442 except OSError, e:
443 # Maybe the directory disapeared meanwhile. We don't want it to throw an
444 # exception.
445 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000446
msb@chromium.org0f282062009-11-06 20:14:02 +0000447 def revinfo(self, options, args, file_list):
448 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000449 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000450 return self.CaptureHeadRevision(self.url)
msb@chromium.org0f282062009-11-06 20:14:02 +0000451
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000452 def runhooks(self, options, args, file_list):
453 self.status(options, args, file_list)
454
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000455 def status(self, options, args, file_list):
456 """Display status information."""
457 path = os.path.join(self._root_dir, self.relpath)
458 command = ['status']
459 command.extend(args)
460 if not os.path.isdir(path):
461 # svn status won't work if the directory doesn't exist.
462 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
463 "does not exist."
464 % (' '.join(command), path))
465 # There's no file list to retrieve.
466 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000467 self.RunAndGetFileList(options, command, path, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000468
469 def pack(self, options, args, file_list):
470 """Generates a patch file which can be applied to the root of the
471 repository."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000472 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000473 path = os.path.join(self._root_dir, self.relpath)
474 command = ['diff']
475 command.extend(args)
476 # Simple class which tracks which file is being diffed and
477 # replaces instances of its file name in the original and
478 # working copy lines of the svn diff output.
479 class DiffFilterer(object):
480 index_string = "Index: "
481 original_prefix = "--- "
482 working_prefix = "+++ "
483
484 def __init__(self, relpath):
485 # Note that we always use '/' as the path separator to be
486 # consistent with svn's cygwin-style output on Windows
487 self._relpath = relpath.replace("\\", "/")
488 self._current_file = ""
489 self._replacement_file = ""
490
491 def SetCurrentFile(self, file):
492 self._current_file = file
493 # Note that we always use '/' as the path separator to be
494 # consistent with svn's cygwin-style output on Windows
495 self._replacement_file = self._relpath + '/' + file
496
497 def ReplaceAndPrint(self, line):
498 print(line.replace(self._current_file, self._replacement_file))
499
500 def Filter(self, line):
501 if (line.startswith(self.index_string)):
502 self.SetCurrentFile(line[len(self.index_string):])
503 self.ReplaceAndPrint(line)
504 else:
505 if (line.startswith(self.original_prefix) or
506 line.startswith(self.working_prefix)):
507 self.ReplaceAndPrint(line)
508 else:
509 print line
510
511 filterer = DiffFilterer(self.relpath)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000512 self.RunAndFilterOutput(command, path, False, False, filterer.Filter)