blob: 3a0b5371ce68198bd32483c4d0a9406952e14819 [file] [log] [blame]
[email protected]5aeb7dd2009-11-17 18:09:011# Copyright (c) 2009 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
[email protected]5f3eee32009-09-17 00:34:304
[email protected]d5800f12009-11-12 20:03:435"""Gclient-specific SCM-specific operations."""
[email protected]5f3eee32009-09-17 00:34:306
[email protected]754960e2009-09-21 12:31:057import logging
[email protected]5f3eee32009-09-17 00:34:308import os
[email protected]ee4071d2009-12-22 22:25:379import posixpath
[email protected]5f3eee32009-09-17 00:34:3010import re
11import subprocess
[email protected]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()
577 branch = tokens[tokens.index('*') + 1]
578 if branch == '(no':
579 return None
580 return branch
581
[email protected]2de10252010-02-08 01:10:39582 def _Run(self, args, cwd=None, redirect_stdout=True):
583 # TODO(maruel): Merge with Capture or better gclient_utils.CheckCall().
[email protected]ffe96f02009-12-09 18:39:15584 if cwd is None:
585 cwd = self.checkout_path
[email protected]2de10252010-02-08 01:10:39586 stdout = None
[email protected]e8e60e52009-11-02 21:50:56587 if redirect_stdout:
[email protected]2de10252010-02-08 01:10:39588 stdout = subprocess.PIPE
[email protected]e28e4982009-09-25 20:51:45589 if cwd == None:
590 cwd = self.checkout_path
[email protected]55e724e2010-03-11 19:36:49591 cmd = [scm.GIT.COMMAND]
[email protected]e28e4982009-09-25 20:51:45592 cmd.extend(args)
[email protected]f3909bf2010-01-08 01:14:51593 logging.debug(cmd)
594 try:
595 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
596 output = sp.communicate()[0]
597 except OSError:
598 raise gclient_utils.Error("git command '%s' failed to run." %
599 ' '.join(cmd) + "\nCheck that you have git installed.")
[email protected]2de10252010-02-08 01:10:39600 if sp.returncode:
[email protected]e28e4982009-09-25 20:51:45601 raise gclient_utils.Error('git command %s returned %d' %
602 (args[0], sp.returncode))
[email protected]5aeb7dd2009-11-17 18:09:01603 if output is not None:
[email protected]e8e60e52009-11-02 21:50:56604 return output.strip()
[email protected]e28e4982009-09-25 20:51:45605
606
[email protected]55e724e2010-03-11 19:36:49607class SVNWrapper(SCMWrapper):
[email protected]cb5442b2009-09-22 16:51:24608 """ Wrapper for SVN """
[email protected]5f3eee32009-09-17 00:34:30609
610 def cleanup(self, options, args, file_list):
611 """Cleanup working copy."""
[email protected]e3608df2009-11-10 20:22:57612 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30613 command = ['cleanup']
614 command.extend(args)
[email protected]55e724e2010-03-11 19:36:49615 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30616
617 def diff(self, options, args, file_list):
618 # NOTE: This function does not currently modify file_list.
[email protected]e3608df2009-11-10 20:22:57619 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30620 command = ['diff']
621 command.extend(args)
[email protected]55e724e2010-03-11 19:36:49622 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30623
624 def export(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31625 """Export a clean directory tree into the given path."""
[email protected]e3608df2009-11-10 20:22:57626 __pychecker__ = 'unusednames=file_list,options'
[email protected]5f3eee32009-09-17 00:34:30627 assert len(args) == 1
628 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
629 try:
630 os.makedirs(export_path)
631 except OSError:
632 pass
633 assert os.path.exists(export_path)
634 command = ['export', '--force', '.']
635 command.append(export_path)
[email protected]55e724e2010-03-11 19:36:49636 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
[email protected]5f3eee32009-09-17 00:34:30637
[email protected]ee4071d2009-12-22 22:25:37638 def pack(self, options, args, file_list):
639 """Generates a patch file which can be applied to the root of the
640 repository."""
641 __pychecker__ = 'unusednames=file_list,options'
642 path = os.path.join(self._root_dir, self.relpath)
643 command = ['diff']
644 command.extend(args)
645
646 filterer = DiffFilterer(self.relpath)
[email protected]55e724e2010-03-11 19:36:49647 scm.SVN.RunAndFilterOutput(command, path, False, False, filterer.Filter)
[email protected]ee4071d2009-12-22 22:25:37648
[email protected]5f3eee32009-09-17 00:34:30649 def update(self, options, args, file_list):
[email protected]d6504212010-01-13 17:34:31650 """Runs svn to update or transparently checkout the working copy.
[email protected]5f3eee32009-09-17 00:34:30651
652 All updated files will be appended to file_list.
653
654 Raises:
655 Error: if can't get URL for relative path.
656 """
657 # Only update if git is not controlling the directory.
658 checkout_path = os.path.join(self._root_dir, self.relpath)
659 git_path = os.path.join(self._root_dir, self.relpath, '.git')
660 if os.path.exists(git_path):
661 print("________ found .git directory; skipping %s" % self.relpath)
662 return
663
664 if args:
665 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
666
[email protected]ac915bb2009-11-13 17:03:01667 url, revision = gclient_utils.SplitUrlRevision(self.url)
668 base_url = url
[email protected]5f3eee32009-09-17 00:34:30669 forced_revision = False
[email protected]ac915bb2009-11-13 17:03:01670 rev_str = ""
[email protected]5f3eee32009-09-17 00:34:30671 if options.revision:
672 # Override the revision number.
[email protected]ac915bb2009-11-13 17:03:01673 revision = str(options.revision)
[email protected]5f3eee32009-09-17 00:34:30674 if revision:
[email protected]ac915bb2009-11-13 17:03:01675 forced_revision = True
676 url = '%s@%s' % (url, revision)
[email protected]770ff9e2009-09-23 17:18:18677 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30678
679 if not os.path.exists(checkout_path):
680 # We need to checkout.
681 command = ['checkout', url, checkout_path]
682 if revision:
683 command.extend(['--revision', str(revision)])
[email protected]55e724e2010-03-11 19:36:49684 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30685 return
686
687 # Get the existing scm url and the revision number of the current checkout.
[email protected]55e724e2010-03-11 19:36:49688 from_info = scm.SVN.CaptureInfo(os.path.join(checkout_path, '.'), '.')
[email protected]5f3eee32009-09-17 00:34:30689 if not from_info:
690 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
691 "directory is present. Delete the directory "
692 "and try again." %
693 checkout_path)
694
[email protected]7753d242009-10-07 17:40:24695 if options.manually_grab_svn_rev:
696 # Retrieve the current HEAD version because svn is slow at null updates.
697 if not revision:
[email protected]55e724e2010-03-11 19:36:49698 from_info_live = scm.SVN.CaptureInfo(from_info['URL'], '.')
[email protected]7753d242009-10-07 17:40:24699 revision = str(from_info_live['Revision'])
700 rev_str = ' at %s' % revision
[email protected]5f3eee32009-09-17 00:34:30701
[email protected]ac915bb2009-11-13 17:03:01702 if from_info['URL'] != base_url:
[email protected]55e724e2010-03-11 19:36:49703 to_info = scm.SVN.CaptureInfo(url, '.')
[email protected]e2ce0c72009-09-23 16:14:18704 if not to_info.get('Repository Root') or not to_info.get('UUID'):
705 # The url is invalid or the server is not accessible, it's safer to bail
706 # out right now.
707 raise gclient_utils.Error('This url is unreachable: %s' % url)
[email protected]5f3eee32009-09-17 00:34:30708 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
709 and (from_info['UUID'] == to_info['UUID']))
710 if can_switch:
711 print("\n_____ relocating %s to a new checkout" % self.relpath)
712 # We have different roots, so check if we can switch --relocate.
713 # Subversion only permits this if the repository UUIDs match.
714 # Perform the switch --relocate, then rewrite the from_url
715 # to reflect where we "are now." (This is the same way that
716 # Subversion itself handles the metadata when switch --relocate
717 # is used.) This makes the checks below for whether we
718 # can update to a revision or have to switch to a different
719 # branch work as expected.
720 # TODO(maruel): TEST ME !
721 command = ["switch", "--relocate",
722 from_info['Repository Root'],
723 to_info['Repository Root'],
724 self.relpath]
[email protected]55e724e2010-03-11 19:36:49725 scm.SVN.Run(command, self._root_dir)
[email protected]5f3eee32009-09-17 00:34:30726 from_info['URL'] = from_info['URL'].replace(
727 from_info['Repository Root'],
728 to_info['Repository Root'])
729 else:
[email protected]55e724e2010-03-11 19:36:49730 if scm.SVN.CaptureStatus(checkout_path):
[email protected]5f3eee32009-09-17 00:34:30731 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
732 "don't match and there is local changes "
733 "in %s. Delete the directory and "
734 "try again." % (url, checkout_path))
735 # Ok delete it.
736 print("\n_____ switching %s to a new checkout" % self.relpath)
[email protected]8f9c69f2009-09-17 00:48:28737 gclient_utils.RemoveDirectory(checkout_path)
[email protected]5f3eee32009-09-17 00:34:30738 # We need to checkout.
739 command = ['checkout', url, checkout_path]
740 if revision:
741 command.extend(['--revision', str(revision)])
[email protected]55e724e2010-03-11 19:36:49742 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30743 return
744
745
746 # If the provided url has a revision number that matches the revision
747 # number of the existing directory, then we don't need to bother updating.
[email protected]2e0c6852009-09-24 00:02:07748 if not options.force and str(from_info['Revision']) == revision:
[email protected]5f3eee32009-09-17 00:34:30749 if options.verbose or not forced_revision:
750 print("\n_____ %s%s" % (self.relpath, rev_str))
751 return
752
753 command = ["update", checkout_path]
754 if revision:
755 command.extend(['--revision', str(revision)])
[email protected]55e724e2010-03-11 19:36:49756 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
[email protected]5f3eee32009-09-17 00:34:30757
[email protected]4b5b1772010-04-08 01:52:56758 def updatesingle(self, options, args, file_list):
759 checkout_path = os.path.join(self._root_dir, self.relpath)
760 filename = args.pop()
[email protected]57564662010-04-14 02:35:12761 if scm.SVN.AssertVersion("1.5")[0]:
762 if not os.path.exists(os.path.join(checkout_path, '.svn')):
763 # Create an empty checkout and then update the one file we want. Future
764 # operations will only apply to the one file we checked out.
765 command = ["checkout", "--depth", "empty", self.url, checkout_path]
766 scm.SVN.Run(command, self._root_dir)
767 if os.path.exists(os.path.join(checkout_path, filename)):
768 os.remove(os.path.join(checkout_path, filename))
769 command = ["update", filename]
770 scm.SVN.RunAndGetFileList(options, command, checkout_path, file_list)
771 # After the initial checkout, we can use update as if it were any other
772 # dep.
773 self.update(options, args, file_list)
774 else:
775 # If the installed version of SVN doesn't support --depth, fallback to
776 # just exporting the file. This has the downside that revision
777 # information is not stored next to the file, so we will have to
778 # re-export the file every time we sync.
779 if not os.path.exists(checkout_path):
780 os.makedirs(checkout_path)
781 command = ["export", os.path.join(self.url, filename),
782 os.path.join(checkout_path, filename)]
783 if options.revision:
784 command.extend(['--revision', str(options.revision)])
[email protected]4b5b1772010-04-08 01:52:56785 scm.SVN.Run(command, self._root_dir)
[email protected]4b5b1772010-04-08 01:52:56786
[email protected]5f3eee32009-09-17 00:34:30787 def revert(self, options, args, file_list):
788 """Reverts local modifications. Subversion specific.
789
790 All reverted files will be appended to file_list, even if Subversion
791 doesn't know about them.
792 """
[email protected]e3608df2009-11-10 20:22:57793 __pychecker__ = 'unusednames=args'
[email protected]5f3eee32009-09-17 00:34:30794 path = os.path.join(self._root_dir, self.relpath)
795 if not os.path.isdir(path):
796 # svn revert won't work if the directory doesn't exist. It needs to
797 # checkout instead.
798 print("\n_____ %s is missing, synching instead" % self.relpath)
799 # Don't reuse the args.
800 return self.update(options, [], file_list)
801
[email protected]55e724e2010-03-11 19:36:49802 for file_status in scm.SVN.CaptureStatus(path):
[email protected]e3608df2009-11-10 20:22:57803 file_path = os.path.join(path, file_status[1])
804 if file_status[0][0] == 'X':
[email protected]754960e2009-09-21 12:31:05805 # Ignore externals.
[email protected]aa3dd472009-09-21 19:02:48806 logging.info('Ignoring external %s' % file_path)
[email protected]754960e2009-09-21 12:31:05807 continue
808
[email protected]aa3dd472009-09-21 19:02:48809 if logging.getLogger().isEnabledFor(logging.INFO):
810 logging.info('%s%s' % (file[0], file[1]))
811 else:
812 print(file_path)
[email protected]e3608df2009-11-10 20:22:57813 if file_status[0].isspace():
[email protected]aa3dd472009-09-21 19:02:48814 logging.error('No idea what is the status of %s.\n'
815 'You just found a bug in gclient, please ping '
816 '[email protected] ASAP!' % file_path)
817 # svn revert is really stupid. It fails on inconsistent line-endings,
818 # on switched directories, etc. So take no chance and delete everything!
819 try:
820 if not os.path.exists(file_path):
821 pass
[email protected]d2e78ff2010-01-11 20:37:19822 elif os.path.isfile(file_path) or os.path.islink(file_path):
[email protected]754960e2009-09-21 12:31:05823 logging.info('os.remove(%s)' % file_path)
[email protected]5f3eee32009-09-17 00:34:30824 os.remove(file_path)
[email protected]aa3dd472009-09-21 19:02:48825 elif os.path.isdir(file_path):
[email protected]754960e2009-09-21 12:31:05826 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
[email protected]8f9c69f2009-09-17 00:48:28827 gclient_utils.RemoveDirectory(file_path)
[email protected]5f3eee32009-09-17 00:34:30828 else:
[email protected]aa3dd472009-09-21 19:02:48829 logging.error('no idea what is %s.\nYou just found a bug in gclient'
830 ', please ping [email protected] ASAP!' % file_path)
831 except EnvironmentError:
832 logging.error('Failed to remove %s.' % file_path)
833
[email protected]810a50b2009-10-05 23:03:18834 try:
835 # svn revert is so broken we don't even use it. Using
836 # "svn up --revision BASE" achieve the same effect.
[email protected]55e724e2010-03-11 19:36:49837 scm.SVN.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
838 file_list)
[email protected]810a50b2009-10-05 23:03:18839 except OSError, e:
840 # Maybe the directory disapeared meanwhile. We don't want it to throw an
841 # exception.
842 logging.error('Failed to update:\n%s' % str(e))
[email protected]5f3eee32009-09-17 00:34:30843
[email protected]0f282062009-11-06 20:14:02844 def revinfo(self, options, args, file_list):
845 """Display revision"""
[email protected]e3608df2009-11-10 20:22:57846 __pychecker__ = 'unusednames=args,file_list,options'
[email protected]5d63eb82010-03-24 23:22:09847 return scm.SVN.CaptureBaseRevision(self.checkout_path)
[email protected]0f282062009-11-06 20:14:02848
[email protected]cb5442b2009-09-22 16:51:24849 def runhooks(self, options, args, file_list):
850 self.status(options, args, file_list)
851
[email protected]5f3eee32009-09-17 00:34:30852 def status(self, options, args, file_list):
853 """Display status information."""
854 path = os.path.join(self._root_dir, self.relpath)
855 command = ['status']
856 command.extend(args)
857 if not os.path.isdir(path):
858 # svn status won't work if the directory doesn't exist.
859 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
860 "does not exist."
861 % (' '.join(command), path))
862 # There's no file list to retrieve.
863 else:
[email protected]55e724e2010-03-11 19:36:49864 scm.SVN.RunAndGetFileList(options, command, path, file_list)
[email protected]e6f78352010-01-13 17:05:33865
866 def FullUrlForRelativeUrl(self, url):
867 # Find the forth '/' and strip from there. A bit hackish.
868 return '/'.join(self.url.split('/')[:4]) + url