blob: 472cac8baf7dcc42030750e4dfd165df3c14e4e4 [file] [log] [blame]
[email protected]95f0f4e2010-05-22 00:55:261# Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]5aeb7dd2009-11-17 18:09:012# 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]fd876172010-04-30 14:01:0512import time
[email protected]5f3eee32009-09-17 00:34:3013
[email protected]5aeb7dd2009-11-17 18:09:0114import scm
[email protected]5f3eee32009-09-17 00:34:3015import gclient_utils
[email protected]5f3eee32009-09-17 00:34:3016
17
[email protected]ee4071d2009-12-22 22:25:3718class DiffFilterer(object):
19 """Simple class which tracks which file is being diffed and
20 replaces instances of its file name in the original and
[email protected]d6504212010-01-13 17:34:3121 working copy lines of the svn/git diff output."""
[email protected]ee4071d2009-12-22 22:25:3722 index_string = "Index: "
23 original_prefix = "--- "
24 working_prefix = "+++ "
25
26 def __init__(self, relpath):
27 # Note that we always use '/' as the path separator to be
28 # consistent with svn's cygwin-style output on Windows
29 self._relpath = relpath.replace("\\", "/")
30 self._current_file = ""
31 self._replacement_file = ""
32
33 def SetCurrentFile(self, file):
34 self._current_file = file
35 # Note that we always use '/' as the path separator to be
36 # consistent with svn's cygwin-style output on Windows
37 self._replacement_file = posixpath.join(self._relpath, file)
38
39 def ReplaceAndPrint(self, line):
40 print(line.replace(self._current_file, self._replacement_file))
41
42 def Filter(self, line):
43 if (line.startswith(self.index_string)):
44 self.SetCurrentFile(line[len(self.index_string):])
45 self.ReplaceAndPrint(line)
46 else:
47 if (line.startswith(self.original_prefix) or
48 line.startswith(self.working_prefix)):
49 self.ReplaceAndPrint(line)
50 else:
51 print line
52
53
[email protected]5f3eee32009-09-17 00:34:3054### SCM abstraction layer
55
[email protected]cb5442b2009-09-22 16:51:2456# Factory Method for SCM wrapper creation
57
58def CreateSCM(url=None, root_dir=None, relpath=None, scm_name='svn'):
[email protected]cb5442b2009-09-22 16:51:2459 scm_map = {
60 'svn' : SVNWrapper,
[email protected]e28e4982009-09-25 20:51:4561 'git' : GitWrapper,
[email protected]cb5442b2009-09-22 16:51:2462 }
[email protected]e28e4982009-09-25 20:51:4563
[email protected]1b8779a2009-11-19 18:11:3964 orig_url = url
65
66 if url:
67 url, _ = gclient_utils.SplitUrlRevision(url)
68 if url.startswith('git:') or url.startswith('ssh:') or url.endswith('.git'):
69 scm_name = 'git'
[email protected]e28e4982009-09-25 20:51:4570
[email protected]cb5442b2009-09-22 16:51:2471 if not scm_name in scm_map:
72 raise gclient_utils.Error('Unsupported scm %s' % scm_name)
[email protected]1b8779a2009-11-19 18:11:3973 return scm_map[scm_name](orig_url, root_dir, relpath, scm_name)
[email protected]cb5442b2009-09-22 16:51:2474
75
76# SCMWrapper base class
77
[email protected]5f3eee32009-09-17 00:34:3078class SCMWrapper(object):
79 """Add necessary glue between all the supported SCM.
80
[email protected]d6504212010-01-13 17:34:3181 This is the abstraction layer to bind to different SCM.
82 """
[email protected]5e73b0c2009-09-18 19:47:4883 def __init__(self, url=None, root_dir=None, relpath=None,
84 scm_name='svn'):
[email protected]5f3eee32009-09-17 00:34:3085 self.scm_name = scm_name
86 self.url = url
[email protected]5e73b0c2009-09-18 19:47:4887 self._root_dir = root_dir
88 if self._root_dir:
89 self._root_dir = self._root_dir.replace('/', os.sep)
90 self.relpath = relpath
91 if self.relpath:
92 self.relpath = self.relpath.replace('/', os.sep)
[email protected]e28e4982009-09-25 20:51:4593 if self.relpath and self._root_dir:
94 self.checkout_path = os.path.join(self._root_dir, self.relpath)
[email protected]5f3eee32009-09-17 00:34:3095
[email protected]5f3eee32009-09-17 00:34:3096 def RunCommand(self, command, options, args, file_list=None):
97 # file_list will have all files that are modified appended to it.
[email protected]de754ac2009-09-17 18:04:5098 if file_list is None:
99 file_list = []
[email protected]5f3eee32009-09-17 00:34:30100
[email protected]4b5b1772010-04-08 01:52:56101 commands = ['cleanup', 'export', 'update', 'updatesingle', 'revert',
102 'revinfo', 'status', 'diff', 'pack', 'runhooks']
[email protected]5f3eee32009-09-17 00:34:30103
104 if not command in commands:
105 raise gclient_utils.Error('Unknown command %s' % command)
106
[email protected]cb5442b2009-09-22 16:51:24107 if not command in dir(self):
[email protected]ee4071d2009-12-22 22:25:37108 raise gclient_utils.Error('Command %s not implemented in %s wrapper' % (
[email protected]cb5442b2009-09-22 16:51:24109 command, self.scm_name))
110
111 return getattr(self, command)(options, args, file_list)
112
113
[email protected]55e724e2010-03-11 19:36:49114class GitWrapper(SCMWrapper):
[email protected]e28e4982009-09-25 20:51:45115 """Wrapper for Git"""
116
117 def cleanup(self, options, args, file_list):
[email protected]d8a63782010-01-25 17:47:05118 """'Cleanup' the repo.
119
120 There's no real git equivalent for the svn cleanup command, do a no-op.
121 """
[email protected]3904caa2010-01-25 17:37:46122 __pychecker__ = 'unusednames=options,args,file_list'
[email protected]e28e4982009-09-25 20:51:45123
124 def diff(self, options, args, file_list):
[email protected]3904caa2010-01-25 17:37:46125 __pychecker__ = 'unusednames=options,args,file_list'
[email protected]5aeb7dd2009-11-17 18:09:01126 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
127 self._Run(['diff', merge_base], redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45128
129 def export(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31130 """Export a clean directory tree into the given path.
131
132 Exports into the specified directory, creating the path if it does
133 already exist.
134 """
[email protected]3904caa2010-01-25 17:37:46135 __pychecker__ = 'unusednames=options,file_list'
[email protected]e28e4982009-09-25 20:51:45136 assert len(args) == 1
137 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
138 if not os.path.exists(export_path):
139 os.makedirs(export_path)
[email protected]5aeb7dd2009-11-17 18:09:01140 self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path],
141 redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45142
[email protected]ee4071d2009-12-22 22:25:37143 def pack(self, options, args, file_list):
144 """Generates a patch file which can be applied to the root of the
[email protected]d6504212010-01-13 17:34:31145 repository.
146
147 The patch file is generated from a diff of the merge base of HEAD and
148 its upstream branch.
149 """
[email protected]55e724e2010-03-11 19:36:49150 __pychecker__ = 'unusednames=options,args,file_list'
[email protected]ee4071d2009-12-22 22:25:37151 path = os.path.join(self._root_dir, self.relpath)
152 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
153 command = ['diff', merge_base]
154 filterer = DiffFilterer(self.relpath)
[email protected]55e724e2010-03-11 19:36:49155 scm.GIT.RunAndFilterOutput(command, path, False, False, filterer.Filter)
[email protected]ee4071d2009-12-22 22:25:37156
[email protected]e28e4982009-09-25 20:51:45157 def update(self, options, args, file_list):
158 """Runs git to update or transparently checkout the working copy.
159
160 All updated files will be appended to file_list.
161
162 Raises:
163 Error: if can't get URL for relative path.
164 """
165
166 if args:
167 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
168
[email protected]ece406f2010-02-23 17:29:15169 self._CheckMinVersion("1.6.6")
[email protected]923a0372009-12-11 20:42:43170
[email protected]d90ba3f2010-02-23 14:42:57171 default_rev = "refs/heads/master"
[email protected]7080e942010-03-15 15:06:16172 url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
[email protected]ac915bb2009-11-13 17:03:01173 rev_str = ""
[email protected]7080e942010-03-15 15:06:16174 revision = deps_revision
[email protected]e28e4982009-09-25 20:51:45175 if options.revision:
[email protected]ac915bb2009-11-13 17:03:01176 # Override the revision number.
177 revision = str(options.revision)
[email protected]d90ba3f2010-02-23 14:42:57178 if not revision:
179 revision = default_rev
[email protected]e28e4982009-09-25 20:51:45180
[email protected]d90ba3f2010-02-23 14:42:57181 rev_str = ' at %s' % revision
182 files = []
183
184 printed_path = False
185 verbose = []
[email protected]b1a22bf2009-11-07 02:33:50186 if options.verbose:
[email protected]b1a22bf2009-11-07 02:33:50187 print("\n_____ %s%s" % (self.relpath, rev_str))
[email protected]d90ba3f2010-02-23 14:42:57188 verbose = ['--verbose']
189 printed_path = True
190
191 if revision.startswith('refs/heads/'):
192 rev_type = "branch"
193 elif revision.startswith('origin/'):
194 # For compatability with old naming, translate 'origin' to 'refs/heads'
195 revision = revision.replace('origin/', 'refs/heads/')
196 rev_type = "branch"
197 else:
198 # hash is also a tag, only make a distinction at checkout
199 rev_type = "hash"
200
[email protected]e28e4982009-09-25 20:51:45201 if not os.path.exists(self.checkout_path):
[email protected]d90ba3f2010-02-23 14:42:57202 self._Clone(rev_type, revision, url, options.verbose)
[email protected]5aeb7dd2009-11-17 18:09:01203 files = self._Run(['ls-files']).split()
[email protected]e28e4982009-09-25 20:51:45204 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
[email protected]d90ba3f2010-02-23 14:42:57205 if not verbose:
206 # Make the output a little prettier. It's nice to have some whitespace
207 # between projects when cloning.
208 print ""
[email protected]e28e4982009-09-25 20:51:45209 return
210
[email protected]e4af1ab2010-01-13 21:26:09211 if not os.path.exists(os.path.join(self.checkout_path, '.git')):
212 raise gclient_utils.Error('\n____ %s%s\n'
213 '\tPath is not a git repo. No .git dir.\n'
214 '\tTo resolve:\n'
215 '\t\trm -rf %s\n'
216 '\tAnd run gclient sync again\n'
217 % (self.relpath, rev_str, self.relpath))
218
[email protected]5bde4852009-12-14 16:47:12219 cur_branch = self._GetCurrentBranch()
220
221 # Check if we are in a rebase conflict
222 if cur_branch is None:
223 raise gclient_utils.Error('\n____ %s%s\n'
224 '\tAlready in a conflict, i.e. (no branch).\n'
225 '\tFix the conflict and run gclient again.\n'
226 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
227 '\tSee man git-rebase for details.\n'
228 % (self.relpath, rev_str))
229
[email protected]d90ba3f2010-02-23 14:42:57230 # Cases:
231 # 1) current branch based on a hash (could be git-svn)
232 # - try to rebase onto the new upstream (hash or branch)
233 # 2) current branch based on a remote branch with local committed changes,
234 # but the DEPS file switched to point to a hash
235 # - rebase those changes on top of the hash
236 # 3) current branch based on a remote with or without changes, no switch
237 # - see if we can FF, if not, prompt the user for rebase, merge, or stop
238 # 4) current branch based on a remote, switches to a new remote
239 # - exit
240
[email protected]81e012c2010-04-29 16:07:24241 # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for
242 # a tracking branch
[email protected]d90ba3f2010-02-23 14:42:57243 # or 'master' if not a tracking branch (it's based on a specific rev/hash)
244 # or it returns None if it couldn't find an upstream
[email protected]81e012c2010-04-29 16:07:24245 upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
[email protected]d90ba3f2010-02-23 14:42:57246 if not upstream_branch or not upstream_branch.startswith('refs/remotes'):
247 current_type = "hash"
248 logging.debug("Current branch is based off a specific rev and is not "
249 "tracking an upstream.")
250 elif upstream_branch.startswith('refs/remotes'):
251 current_type = "branch"
252 else:
253 raise gclient_utils.Error('Invalid Upstream')
254
[email protected]0b1c2462010-03-02 00:48:14255 # Update the remotes first so we have all the refs.
[email protected]fd876172010-04-30 14:01:05256 for _ in range(10):
[email protected]0b1c2462010-03-02 00:48:14257 try:
[email protected]55e724e2010-03-11 19:36:49258 remote_output, remote_err = scm.GIT.Capture(
[email protected]0b1c2462010-03-02 00:48:14259 ['remote'] + verbose + ['update'],
260 self.checkout_path,
261 print_error=False)
262 break
[email protected]982984e2010-05-11 20:57:49263 except gclient_utils.CheckCallError, e:
[email protected]0b1c2462010-03-02 00:48:14264 # Hackish but at that point, git is known to work so just checking for
265 # 502 in stderr should be fine.
266 if '502' in e.stderr:
267 print str(e)
[email protected]fd876172010-04-30 14:01:05268 print "Sleeping 15 seconds and retrying..."
269 time.sleep(15)
[email protected]0b1c2462010-03-02 00:48:14270 continue
[email protected]fd876172010-04-30 14:01:05271 raise
[email protected]0b1c2462010-03-02 00:48:14272
[email protected]d90ba3f2010-02-23 14:42:57273 if verbose:
274 print remote_output.strip()
275 # git remote update prints to stderr when used with --verbose
276 print remote_err.strip()
277
278 # This is a big hammer, debatable if it should even be here...
[email protected]793796d2010-02-19 17:27:41279 if options.force or options.reset:
[email protected]d90ba3f2010-02-23 14:42:57280 self._Run(['reset', '--hard', 'HEAD'], redirect_stdout=False)
281
[email protected]55e724e2010-03-11 19:36:49282 if current_type == 'hash':
[email protected]d90ba3f2010-02-23 14:42:57283 # case 1
[email protected]55e724e2010-03-11 19:36:49284 if scm.GIT.IsGitSvn(self.checkout_path) and upstream_branch is not None:
[email protected]d90ba3f2010-02-23 14:42:57285 # Our git-svn branch (upstream_branch) is our upstream
286 self._AttemptRebase(upstream_branch, files, verbose=options.verbose,
287 newbase=revision, printed_path=printed_path)
288 printed_path = True
289 else:
290 # Can't find a merge-base since we don't know our upstream. That makes
291 # this command VERY likely to produce a rebase failure. For now we
292 # assume origin is our upstream since that's what the old behavior was.
[email protected]3b29de12010-03-08 18:34:28293 upstream_branch = 'origin'
[email protected]7080e942010-03-15 15:06:16294 if options.revision or deps_revision:
[email protected]3b29de12010-03-08 18:34:28295 upstream_branch = revision
296 self._AttemptRebase(upstream_branch, files=files,
297 verbose=options.verbose, printed_path=printed_path)
[email protected]d90ba3f2010-02-23 14:42:57298 printed_path = True
[email protected]55e724e2010-03-11 19:36:49299 elif rev_type == 'hash':
[email protected]d90ba3f2010-02-23 14:42:57300 # case 2
301 self._AttemptRebase(upstream_branch, files, verbose=options.verbose,
302 newbase=revision, printed_path=printed_path)
303 printed_path = True
304 elif revision.replace('heads', 'remotes/origin') != upstream_branch:
305 # case 4
306 new_base = revision.replace('heads', 'remotes/origin')
307 if not printed_path:
308 print("\n_____ %s%s" % (self.relpath, rev_str))
309 switch_error = ("Switching upstream branch from %s to %s\n"
310 % (upstream_branch, new_base) +
311 "Please merge or rebase manually:\n" +
312 "cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
313 "OR git checkout -b <some new branch> %s" % new_base)
314 raise gclient_utils.Error(switch_error)
315 else:
316 # case 3 - the default case
317 files = self._Run(['diff', upstream_branch, '--name-only']).split()
318 if verbose:
319 print "Trying fast-forward merge to branch : %s" % upstream_branch
320 try:
[email protected]55e724e2010-03-11 19:36:49321 merge_output, merge_err = scm.GIT.Capture(['merge', '--ff-only',
322 upstream_branch],
323 self.checkout_path,
324 print_error=False)
[email protected]d90ba3f2010-02-23 14:42:57325 except gclient_utils.CheckCallError, e:
326 if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr):
327 if not printed_path:
328 print("\n_____ %s%s" % (self.relpath, rev_str))
329 printed_path = True
330 while True:
331 try:
332 action = str(raw_input("Cannot fast-forward merge, attempt to "
333 "rebase? (y)es / (q)uit / (s)kip : "))
334 except ValueError:
335 gclient_utils.Error('Invalid Character')
336 continue
337 if re.match(r'yes|y', action, re.I):
338 self._AttemptRebase(upstream_branch, files,
339 verbose=options.verbose,
340 printed_path=printed_path)
341 printed_path = True
342 break
343 elif re.match(r'quit|q', action, re.I):
344 raise gclient_utils.Error("Can't fast-forward, please merge or "
345 "rebase manually.\n"
346 "cd %s && git " % self.checkout_path
347 + "rebase %s" % upstream_branch)
348 elif re.match(r'skip|s', action, re.I):
349 print "Skipping %s" % self.relpath
350 return
351 else:
352 print "Input not recognized"
353 elif re.match("error: Your local changes to '.*' would be "
354 "overwritten by merge. Aborting.\nPlease, commit your "
355 "changes or stash them before you can merge.\n",
356 e.stderr):
357 if not printed_path:
358 print("\n_____ %s%s" % (self.relpath, rev_str))
359 printed_path = True
360 raise gclient_utils.Error(e.stderr)
361 else:
362 # Some other problem happened with the merge
363 logging.error("Error during fast-forward merge in %s!" % self.relpath)
364 print e.stderr
365 raise
366 else:
367 # Fast-forward merge was successful
368 if not re.match('Already up-to-date.', merge_output) or verbose:
369 if not printed_path:
370 print("\n_____ %s%s" % (self.relpath, rev_str))
371 printed_path = True
372 print merge_output.strip()
373 if merge_err:
374 print "Merge produced error output:\n%s" % merge_err.strip()
375 if not verbose:
376 # Make the output a little prettier. It's nice to have some
377 # whitespace between projects when syncing.
378 print ""
379
380 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
[email protected]5bde4852009-12-14 16:47:12381
382 # If the rebase generated a conflict, abort and ask user to fix
383 if self._GetCurrentBranch() is None:
384 raise gclient_utils.Error('\n____ %s%s\n'
385 '\nConflict while rebasing this branch.\n'
386 'Fix the conflict and run gclient again.\n'
387 'See man git-rebase for details.\n'
388 % (self.relpath, rev_str))
389
[email protected]d90ba3f2010-02-23 14:42:57390 if verbose:
391 print "Checked out revision %s" % self.revinfo(options, (), None)
[email protected]e28e4982009-09-25 20:51:45392
393 def revert(self, options, args, file_list):
394 """Reverts local modifications.
395
396 All reverted files will be appended to file_list.
397 """
[email protected]e3608df2009-11-10 20:22:57398 __pychecker__ = 'unusednames=args'
[email protected]260c6532009-10-28 03:22:35399 path = os.path.join(self._root_dir, self.relpath)
400 if not os.path.isdir(path):
401 # revert won't work if the directory doesn't exist. It needs to
402 # checkout instead.
403 print("\n_____ %s is missing, synching instead" % self.relpath)
404 # Don't reuse the args.
405 return self.update(options, [], file_list)
[email protected]b2b46312010-04-30 20:58:03406
407 default_rev = "refs/heads/master"
408 url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
409 if not deps_revision:
410 deps_revision = default_rev
411 if deps_revision.startswith('refs/heads/'):
412 deps_revision = deps_revision.replace('refs/heads/', 'origin/')
413
414 files = self._Run(['diff', deps_revision, '--name-only']).split()
415 self._Run(['reset', '--hard', deps_revision], redirect_stdout=False)
[email protected]e28e4982009-09-25 20:51:45416 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
417
[email protected]0f282062009-11-06 20:14:02418 def revinfo(self, options, args, file_list):
419 """Display revision"""
[email protected]3904caa2010-01-25 17:37:46420 __pychecker__ = 'unusednames=options,args,file_list'
[email protected]5aeb7dd2009-11-17 18:09:01421 return self._Run(['rev-parse', 'HEAD'])
[email protected]0f282062009-11-06 20:14:02422
[email protected]e28e4982009-09-25 20:51:45423 def runhooks(self, options, args, file_list):
424 self.status(options, args, file_list)
425
426 def status(self, options, args, file_list):
427 """Display status information."""
[email protected]3904caa2010-01-25 17:37:46428 __pychecker__ = 'unusednames=options,args'
[email protected]e28e4982009-09-25 20:51:45429 if not os.path.isdir(self.checkout_path):
430 print('\n________ couldn\'t run status in %s:\nThe directory '
[email protected]e3608df2009-11-10 20:22:57431 'does not exist.' % self.checkout_path)
[email protected]e28e4982009-09-25 20:51:45432 else:
[email protected]5aeb7dd2009-11-17 18:09:01433 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
434 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
435 files = self._Run(['diff', '--name-only', merge_base]).split()
[email protected]e28e4982009-09-25 20:51:45436 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
437
[email protected]e6f78352010-01-13 17:05:33438 def FullUrlForRelativeUrl(self, url):
439 # Strip from last '/'
440 # Equivalent to unix basename
441 base_url = self.url
442 return base_url[:base_url.rfind('/')] + url
443
[email protected]d90ba3f2010-02-23 14:42:57444 def _Clone(self, rev_type, revision, url, verbose=False):
445 """Clone a git repository from the given URL.
446
447 Once we've cloned the repo, we checkout a working branch based off the
448 specified revision."""
449 if not verbose:
450 # git clone doesn't seem to insert a newline properly before printing
451 # to stdout
452 print ""
453
454 clone_cmd = ['clone']
455 if verbose:
456 clone_cmd.append('--verbose')
457 clone_cmd.extend([url, self.checkout_path])
458
[email protected]55e724e2010-03-11 19:36:49459 for _ in range(3):
[email protected]d90ba3f2010-02-23 14:42:57460 try:
461 self._Run(clone_cmd, cwd=self._root_dir, redirect_stdout=False)
462 break
463 except gclient_utils.Error, e:
464 # TODO(maruel): Hackish, should be fixed by moving _Run() to
465 # CheckCall().
466 # Too bad we don't have access to the actual output.
467 # We should check for "transfer closed with NNN bytes remaining to
468 # read". In the meantime, just make sure .git exists.
469 if (e.args[0] == 'git command clone returned 128' and
470 os.path.exists(os.path.join(self.checkout_path, '.git'))):
471 print str(e)
472 print "Retrying..."
473 continue
474 raise e
475
[email protected]55e724e2010-03-11 19:36:49476 if rev_type == "branch":
[email protected]d90ba3f2010-02-23 14:42:57477 short_rev = revision.replace('refs/heads/', '')
478 new_branch = revision.replace('heads', 'remotes/origin')
479 elif revision.startswith('refs/tags/'):
480 short_rev = revision.replace('refs/tags/', '')
481 new_branch = revision
482 else:
483 # revision is a specific sha1 hash
484 short_rev = revision
485 new_branch = revision
486
487 cur_branch = self._GetCurrentBranch()
488 if cur_branch != short_rev:
489 self._Run(['checkout', '-b', short_rev, new_branch],
490 redirect_stdout=False)
491
492 def _AttemptRebase(self, upstream, files, verbose=False, newbase=None,
493 branch=None, printed_path=False):
494 """Attempt to rebase onto either upstream or, if specified, newbase."""
495 files.extend(self._Run(['diff', upstream, '--name-only']).split())
496 revision = upstream
497 if newbase:
498 revision = newbase
499 if not printed_path:
500 print "\n_____ %s : Attempting rebase onto %s..." % (self.relpath,
501 revision)
502 printed_path = True
503 else:
504 print "Attempting rebase onto %s..." % revision
505
506 # Build the rebase command here using the args
507 # git rebase [options] [--onto <newbase>] <upstream> [<branch>]
508 rebase_cmd = ['rebase']
509 if verbose:
510 rebase_cmd.append('--verbose')
511 if newbase:
512 rebase_cmd.extend(['--onto', newbase])
513 rebase_cmd.append(upstream)
514 if branch:
515 rebase_cmd.append(branch)
516
517 try:
[email protected]55e724e2010-03-11 19:36:49518 rebase_output, rebase_err = scm.GIT.Capture(rebase_cmd,
519 self.checkout_path,
520 print_error=False)
[email protected]d90ba3f2010-02-23 14:42:57521 except gclient_utils.CheckCallError, e:
522 if re.match(r'cannot rebase: you have unstaged changes', e.stderr) or \
523 re.match(r'cannot rebase: your index contains uncommitted changes',
524 e.stderr):
525 while True:
526 rebase_action = str(raw_input("Cannot rebase because of unstaged "
527 "changes.\n'git reset --hard HEAD' ?\n"
528 "WARNING: destroys any uncommitted "
529 "work in your current branch!"
530 " (y)es / (q)uit / (s)how : "))
531 if re.match(r'yes|y', rebase_action, re.I):
532 self._Run(['reset', '--hard', 'HEAD'], redirect_stdout=False)
533 # Should this be recursive?
[email protected]55e724e2010-03-11 19:36:49534 rebase_output, rebase_err = scm.GIT.Capture(rebase_cmd,
535 self.checkout_path)
[email protected]d90ba3f2010-02-23 14:42:57536 break
537 elif re.match(r'quit|q', rebase_action, re.I):
538 raise gclient_utils.Error("Please merge or rebase manually\n"
539 "cd %s && git " % self.checkout_path
540 + "%s" % ' '.join(rebase_cmd))
541 elif re.match(r'show|s', rebase_action, re.I):
542 print "\n%s" % e.stderr.strip()
543 continue
544 else:
545 gclient_utils.Error("Input not recognized")
546 continue
547 elif re.search(r'^CONFLICT', e.stdout, re.M):
548 raise gclient_utils.Error("Conflict while rebasing this branch.\n"
549 "Fix the conflict and run gclient again.\n"
550 "See 'man git-rebase' for details.\n")
551 else:
552 print e.stdout.strip()
553 print "Rebase produced error output:\n%s" % e.stderr.strip()
554 raise gclient_utils.Error("Unrecognized error, please merge or rebase "
555 "manually.\ncd %s && git " %
556 self.checkout_path
557 + "%s" % ' '.join(rebase_cmd))
558
559 print rebase_output.strip()
560 if rebase_err:
561 print "Rebase produced error output:\n%s" % rebase_err.strip()
562 if not verbose:
563 # Make the output a little prettier. It's nice to have some
564 # whitespace between projects when syncing.
565 print ""
566
[email protected]923a0372009-12-11 20:42:43567 def _CheckMinVersion(self, min_version):
[email protected]d0f854a2010-03-11 19:35:53568 (ok, current_version) = scm.GIT.AssertVersion(min_version)
569 if not ok:
570 raise gclient_utils.Error('git version %s < minimum required %s' %
571 (current_version, min_version))
[email protected]923a0372009-12-11 20:42:43572
[email protected]5bde4852009-12-14 16:47:12573 def _GetCurrentBranch(self):
574 # Returns name of current branch
575 # Returns None if inside a (no branch)
576 tokens = self._Run(['branch']).split()
[email protected]4d9da402010-05-19 17:12:31577 if not '*' in tokens:
578 return None
[email protected]5bde4852009-12-14 16:47:12579 branch = tokens[tokens.index('*') + 1]
580 if branch == '(no':
581 return None
582 return branch
583
[email protected]2de10252010-02-08 01:10:39584 def _Run(self, args, cwd=None, redirect_stdout=True):
585 # TODO(maruel): Merge with Capture or better gclient_utils.CheckCall().
[email protected]ffe96f02009-12-09 18:39:15586 if cwd is None:
587 cwd = self.checkout_path
[email protected]2de10252010-02-08 01:10:39588 stdout = None
[email protected]e8e60e52009-11-02 21:50:56589 if redirect_stdout:
[email protected]2de10252010-02-08 01:10:39590 stdout = subprocess.PIPE
[email protected]e28e4982009-09-25 20:51:45591 if cwd == None:
592 cwd = self.checkout_path
[email protected]55e724e2010-03-11 19:36:49593 cmd = [scm.GIT.COMMAND]
[email protected]e28e4982009-09-25 20:51:45594 cmd.extend(args)
[email protected]f3909bf2010-01-08 01:14:51595 logging.debug(cmd)
596 try:
597 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
598 output = sp.communicate()[0]
599 except OSError:
600 raise gclient_utils.Error("git command '%s' failed to run." %
601 ' '.join(cmd) + "\nCheck that you have git installed.")
[email protected]2de10252010-02-08 01:10:39602 if sp.returncode:
[email protected]e28e4982009-09-25 20:51:45603 raise gclient_utils.Error('git command %s returned %d' %
604 (args[0], sp.returncode))
[email protected]5aeb7dd2009-11-17 18:09:01605 if output is not None:
[email protected]e8e60e52009-11-02 21:50:56606 return output.strip()
[email protected]e28e4982009-09-25 20:51:45607
608
[email protected]55e724e2010-03-11 19:36:49609class SVNWrapper(SCMWrapper):
[email protected]cb5442b2009-09-22 16:51:24610 """ Wrapper for SVN """
[email protected]5f3eee32009-09-17 00:34:30611
612 def cleanup(self, options, args, file_list):
613 """Cleanup working copy."""
[email protected]e3608df2009-11-10 20:22:57614 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30615 command = ['cleanup']
616 command.extend(args)
[email protected]55e724e2010-03-11 19:36:49617 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30618
619 def diff(self, options, args, file_list):
620 # NOTE: This function does not currently modify file_list.
[email protected]e3608df2009-11-10 20:22:57621 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30622 command = ['diff']
623 command.extend(args)
[email protected]55e724e2010-03-11 19:36:49624 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30625
626 def export(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31627 """Export a clean directory tree into the given path."""
[email protected]e3608df2009-11-10 20:22:57628 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30629 assert len(args) == 1
630 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
631 try:
632 os.makedirs(export_path)
633 except OSError:
634 pass
635 assert os.path.exists(export_path)
636 command = ['export', '--force', '.']
637 command.append(export_path)
[email protected]55e724e2010-03-11 19:36:49638 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30639
[email protected]ee4071d2009-12-22 22:25:37640 def pack(self, options, args, file_list):
641 """Generates a patch file which can be applied to the root of the
642 repository."""
643 __pychecker__ = 'unusednames=file_list,options'
644 path = os.path.join(self._root_dir, self.relpath)
645 command = ['diff']
646 command.extend(args)
647
648 filterer = DiffFilterer(self.relpath)
[email protected]55e724e2010-03-11 19:36:49649 scm.SVN.RunAndFilterOutput(command, path, False, False, filterer.Filter)
[email protected]ee4071d2009-12-22 22:25:37650
[email protected]5f3eee32009-09-17 00:34:30651 def update(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31652 """Runs svn to update or transparently checkout the working copy.
[email protected]5f3eee32009-09-17 00:34:30653
654 All updated files will be appended to file_list.
655
656 Raises:
657 Error: if can't get URL for relative path.
658 """
659 # Only update if git is not controlling the directory.
660 checkout_path = os.path.join(self._root_dir, self.relpath)
661 git_path = os.path.join(self._root_dir, self.relpath, '.git')
662 if os.path.exists(git_path):
663 print("________ found .git directory; skipping %s" % self.relpath)
664 return
665
666 if args:
667 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
668
[email protected]ac915bb2009-11-13 17:03:01669 url, revision = gclient_utils.SplitUrlRevision(self.url)
670 base_url = url
[email protected]5f3eee32009-09-17 00:34:30671 forced_revision = False
[email protected]ac915bb2009-11-13 17:03:01672 rev_str = ""
[email protected]5f3eee32009-09-17 00:34:30673 if options.revision:
674 # Override the revision number.
[email protected]ac915bb2009-11-13 17:03:01675 revision = str(options.revision)
[email protected]5f3eee32009-09-17 00:34:30676 if revision:
[email protected]ac915bb2009-11-13 17:03:01677 forced_revision = True
678 url = '%s@%s' % (url, revision)
[email protected]770ff9e2009-09-23 17:18:18679 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30680
681 if not os.path.exists(checkout_path):
682 # We need to checkout.
683 command = ['checkout', url, checkout_path]
684 if revision:
[email protected]95f0f4e2010-05-22 00:55:26685 command.extend(['--revision', str(revision).strip()])
[email protected]55e724e2010-03-11 19:36:49686 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30687 return
688
689 # Get the existing scm url and the revision number of the current checkout.
[email protected]55e724e2010-03-11 19:36:49690 from_info = scm.SVN.CaptureInfo(os.path.join(checkout_path, '.'), '.')
[email protected]5f3eee32009-09-17 00:34:30691 if not from_info:
692 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
693 "directory is present. Delete the directory "
694 "and try again." %
695 checkout_path)
696
[email protected]7753d242009-10-07 17:40:24697 if options.manually_grab_svn_rev:
698 # Retrieve the current HEAD version because svn is slow at null updates.
699 if not revision:
[email protected]55e724e2010-03-11 19:36:49700 from_info_live = scm.SVN.CaptureInfo(from_info['URL'], '.')
[email protected]7753d242009-10-07 17:40:24701 revision = str(from_info_live['Revision'])
702 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30703
[email protected]ac915bb2009-11-13 17:03:01704 if from_info['URL'] != base_url:
[email protected]55e724e2010-03-11 19:36:49705 to_info = scm.SVN.CaptureInfo(url, '.')
[email protected]e2ce0c72009-09-23 16:14:18706 if not to_info.get('Repository Root') or not to_info.get('UUID'):
707 # The url is invalid or the server is not accessible, it's safer to bail
708 # out right now.
709 raise gclient_utils.Error('This url is unreachable: %s' % url)
[email protected]5f3eee32009-09-17 00:34:30710 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
711 and (from_info['UUID'] == to_info['UUID']))
712 if can_switch:
713 print("\n_____ relocating %s to a new checkout" % self.relpath)
714 # We have different roots, so check if we can switch --relocate.
715 # Subversion only permits this if the repository UUIDs match.
716 # Perform the switch --relocate, then rewrite the from_url
717 # to reflect where we "are now." (This is the same way that
718 # Subversion itself handles the metadata when switch --relocate
719 # is used.) This makes the checks below for whether we
720 # can update to a revision or have to switch to a different
721 # branch work as expected.
722 # TODO(maruel): TEST ME !
723 command = ["switch", "--relocate",
724 from_info['Repository Root'],
725 to_info['Repository Root'],
726 self.relpath]
[email protected]55e724e2010-03-11 19:36:49727 scm.SVN.Run(command, self._root_dir)
[email protected]5f3eee32009-09-17 00:34:30728 from_info['URL'] = from_info['URL'].replace(
729 from_info['Repository Root'],
730 to_info['Repository Root'])
731 else:
[email protected]55e724e2010-03-11 19:36:49732 if scm.SVN.CaptureStatus(checkout_path):
[email protected]5f3eee32009-09-17 00:34:30733 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
734 "don't match and there is local changes "
735 "in %s. Delete the directory and "
736 "try again." % (url, checkout_path))
737 # Ok delete it.
738 print("\n_____ switching %s to a new checkout" % self.relpath)
[email protected]8f9c69f2009-09-17 00:48:28739 gclient_utils.RemoveDirectory(checkout_path)
[email protected]5f3eee32009-09-17 00:34:30740 # We need to checkout.
741 command = ['checkout', url, checkout_path]
742 if revision:
[email protected]95f0f4e2010-05-22 00:55:26743 command.extend(['--revision', str(revision).strip()])
[email protected]55e724e2010-03-11 19:36:49744 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30745 return
746
747
748 # If the provided url has a revision number that matches the revision
749 # number of the existing directory, then we don't need to bother updating.
[email protected]2e0c6852009-09-24 00:02:07750 if not options.force and str(from_info['Revision']) == revision:
[email protected]5f3eee32009-09-17 00:34:30751 if options.verbose or not forced_revision:
752 print("\n_____ %s%s" % (self.relpath, rev_str))
753 return
754
755 command = ["update", checkout_path]
756 if revision:
[email protected]95f0f4e2010-05-22 00:55:26757 command.extend(['--revision', str(revision).strip()])
[email protected]55e724e2010-03-11 19:36:49758 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30759
[email protected]4b5b1772010-04-08 01:52:56760 def updatesingle(self, options, args, file_list):
761 checkout_path = os.path.join(self._root_dir, self.relpath)
762 filename = args.pop()
[email protected]57564662010-04-14 02:35:12763 if scm.SVN.AssertVersion("1.5")[0]:
764 if not os.path.exists(os.path.join(checkout_path, '.svn')):
765 # Create an empty checkout and then update the one file we want. Future
766 # operations will only apply to the one file we checked out.
767 command = ["checkout", "--depth", "empty", self.url, checkout_path]
768 scm.SVN.Run(command, self._root_dir)
769 if os.path.exists(os.path.join(checkout_path, filename)):
770 os.remove(os.path.join(checkout_path, filename))
771 command = ["update", filename]
772 scm.SVN.RunAndGetFileList(options, command, checkout_path, file_list)
773 # After the initial checkout, we can use update as if it were any other
774 # dep.
775 self.update(options, args, file_list)
776 else:
777 # If the installed version of SVN doesn't support --depth, fallback to
778 # just exporting the file. This has the downside that revision
779 # information is not stored next to the file, so we will have to
780 # re-export the file every time we sync.
781 if not os.path.exists(checkout_path):
782 os.makedirs(checkout_path)
783 command = ["export", os.path.join(self.url, filename),
784 os.path.join(checkout_path, filename)]
785 if options.revision:
[email protected]95f0f4e2010-05-22 00:55:26786 command.extend(['--revision', str(options.revision).strip()])
[email protected]4b5b1772010-04-08 01:52:56787 scm.SVN.Run(command, self._root_dir)
[email protected]4b5b1772010-04-08 01:52:56788
[email protected]5f3eee32009-09-17 00:34:30789 def revert(self, options, args, file_list):
790 """Reverts local modifications. Subversion specific.
791
792 All reverted files will be appended to file_list, even if Subversion
793 doesn't know about them.
794 """
[email protected]e3608df2009-11-10 20:22:57795 __pychecker__ = 'unusednames=args'
[email protected]5f3eee32009-09-17 00:34:30796 path = os.path.join(self._root_dir, self.relpath)
797 if not os.path.isdir(path):
798 # svn revert won't work if the directory doesn't exist. It needs to
799 # checkout instead.
800 print("\n_____ %s is missing, synching instead" % self.relpath)
801 # Don't reuse the args.
802 return self.update(options, [], file_list)
803
[email protected]55e724e2010-03-11 19:36:49804 for file_status in scm.SVN.CaptureStatus(path):
[email protected]e3608df2009-11-10 20:22:57805 file_path = os.path.join(path, file_status[1])
806 if file_status[0][0] == 'X':
[email protected]754960e2009-09-21 12:31:05807 # Ignore externals.
[email protected]aa3dd472009-09-21 19:02:48808 logging.info('Ignoring external %s' % file_path)
[email protected]754960e2009-09-21 12:31:05809 continue
810
[email protected]aa3dd472009-09-21 19:02:48811 if logging.getLogger().isEnabledFor(logging.INFO):
812 logging.info('%s%s' % (file[0], file[1]))
813 else:
814 print(file_path)
[email protected]e3608df2009-11-10 20:22:57815 if file_status[0].isspace():
[email protected]aa3dd472009-09-21 19:02:48816 logging.error('No idea what is the status of %s.\n'
817 'You just found a bug in gclient, please ping '
818 '[email protected] ASAP!' % file_path)
819 # svn revert is really stupid. It fails on inconsistent line-endings,
820 # on switched directories, etc. So take no chance and delete everything!
821 try:
822 if not os.path.exists(file_path):
823 pass
[email protected]d2e78ff2010-01-11 20:37:19824 elif os.path.isfile(file_path) or os.path.islink(file_path):
[email protected]754960e2009-09-21 12:31:05825 logging.info('os.remove(%s)' % file_path)
[email protected]5f3eee32009-09-17 00:34:30826 os.remove(file_path)
[email protected]aa3dd472009-09-21 19:02:48827 elif os.path.isdir(file_path):
[email protected]754960e2009-09-21 12:31:05828 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
[email protected]8f9c69f2009-09-17 00:48:28829 gclient_utils.RemoveDirectory(file_path)
[email protected]5f3eee32009-09-17 00:34:30830 else:
[email protected]aa3dd472009-09-21 19:02:48831 logging.error('no idea what is %s.\nYou just found a bug in gclient'
832 ', please ping [email protected] ASAP!' % file_path)
833 except EnvironmentError:
834 logging.error('Failed to remove %s.' % file_path)
835
[email protected]810a50b2009-10-05 23:03:18836 try:
837 # svn revert is so broken we don't even use it. Using
838 # "svn up --revision BASE" achieve the same effect.
[email protected]55e724e2010-03-11 19:36:49839 scm.SVN.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
840 file_list)
[email protected]810a50b2009-10-05 23:03:18841 except OSError, e:
842 # Maybe the directory disapeared meanwhile. We don't want it to throw an
843 # exception.
844 logging.error('Failed to update:\n%s' % str(e))
[email protected]5f3eee32009-09-17 00:34:30845
[email protected]0f282062009-11-06 20:14:02846 def revinfo(self, options, args, file_list):
847 """Display revision"""
[email protected]e3608df2009-11-10 20:22:57848 __pychecker__ = 'unusednames=args,file_list,options'
[email protected]5d63eb82010-03-24 23:22:09849 return scm.SVN.CaptureBaseRevision(self.checkout_path)
[email protected]0f282062009-11-06 20:14:02850
[email protected]cb5442b2009-09-22 16:51:24851 def runhooks(self, options, args, file_list):
852 self.status(options, args, file_list)
853
[email protected]5f3eee32009-09-17 00:34:30854 def status(self, options, args, file_list):
855 """Display status information."""
856 path = os.path.join(self._root_dir, self.relpath)
857 command = ['status']
858 command.extend(args)
859 if not os.path.isdir(path):
860 # svn status won't work if the directory doesn't exist.
861 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
862 "does not exist."
863 % (' '.join(command), path))
864 # There's no file list to retrieve.
865 else:
[email protected]55e724e2010-03-11 19:36:49866 scm.SVN.RunAndGetFileList(options, command, path, file_list)
[email protected]e6f78352010-01-13 17:05:33867
868 def FullUrlForRelativeUrl(self, url):
869 # Find the forth '/' and strip from there. A bit hackish.
870 return '/'.join(self.url.split('/')[:4]) + url