blob: cc11612c3789ff6300b84ae4e639e36d764f6c0a [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
maruel@chromium.orgee4071d2009-12-22 22:25:37 +00009import posixpath
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000010import re
11import subprocess
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000012
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +000013import scm
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000014import gclient_utils
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000015
16
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000017class DiffFilterer(object):
18 """Simple class which tracks which file is being diffed and
19 replaces instances of its file name in the original and
msb@chromium.orgd6504212010-01-13 17:34:31 +000020 working copy lines of the svn/git diff output."""
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000021 index_string = "Index: "
22 original_prefix = "--- "
23 working_prefix = "+++ "
24
25 def __init__(self, relpath):
26 # Note that we always use '/' as the path separator to be
27 # consistent with svn's cygwin-style output on Windows
28 self._relpath = relpath.replace("\\", "/")
29 self._current_file = ""
30 self._replacement_file = ""
31
32 def SetCurrentFile(self, file):
33 self._current_file = file
34 # Note that we always use '/' as the path separator to be
35 # consistent with svn's cygwin-style output on Windows
36 self._replacement_file = posixpath.join(self._relpath, file)
37
38 def ReplaceAndPrint(self, line):
39 print(line.replace(self._current_file, self._replacement_file))
40
41 def Filter(self, line):
42 if (line.startswith(self.index_string)):
43 self.SetCurrentFile(line[len(self.index_string):])
44 self.ReplaceAndPrint(line)
45 else:
46 if (line.startswith(self.original_prefix) or
47 line.startswith(self.working_prefix)):
48 self.ReplaceAndPrint(line)
49 else:
50 print line
51
52
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000053### SCM abstraction layer
54
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000055# Factory Method for SCM wrapper creation
56
57def CreateSCM(url=None, root_dir=None, relpath=None, scm_name='svn'):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000058 scm_map = {
59 'svn' : SVNWrapper,
msb@chromium.orge28e4982009-09-25 20:51:45 +000060 'git' : GitWrapper,
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000061 }
msb@chromium.orge28e4982009-09-25 20:51:45 +000062
msb@chromium.org1b8779a2009-11-19 18:11:39 +000063 orig_url = url
64
65 if url:
66 url, _ = gclient_utils.SplitUrlRevision(url)
67 if url.startswith('git:') or url.startswith('ssh:') or url.endswith('.git'):
68 scm_name = 'git'
msb@chromium.orge28e4982009-09-25 20:51:45 +000069
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000070 if not scm_name in scm_map:
71 raise gclient_utils.Error('Unsupported scm %s' % scm_name)
msb@chromium.org1b8779a2009-11-19 18:11:39 +000072 return scm_map[scm_name](orig_url, root_dir, relpath, scm_name)
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000073
74
75# SCMWrapper base class
76
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000077class SCMWrapper(object):
78 """Add necessary glue between all the supported SCM.
79
msb@chromium.orgd6504212010-01-13 17:34:31 +000080 This is the abstraction layer to bind to different SCM.
81 """
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +000082 def __init__(self, url=None, root_dir=None, relpath=None,
83 scm_name='svn'):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000084 self.scm_name = scm_name
85 self.url = url
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +000086 self._root_dir = root_dir
87 if self._root_dir:
88 self._root_dir = self._root_dir.replace('/', os.sep)
89 self.relpath = relpath
90 if self.relpath:
91 self.relpath = self.relpath.replace('/', os.sep)
msb@chromium.orge28e4982009-09-25 20:51:45 +000092 if self.relpath and self._root_dir:
93 self.checkout_path = os.path.join(self._root_dir, self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000094
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000095 def RunCommand(self, command, options, args, file_list=None):
96 # file_list will have all files that are modified appended to it.
maruel@chromium.orgde754ac2009-09-17 18:04:50 +000097 if file_list is None:
98 file_list = []
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000099
msb@chromium.org0f282062009-11-06 20:14:02 +0000100 commands = ['cleanup', 'export', 'update', 'revert', 'revinfo',
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000101 'status', 'diff', 'pack', 'runhooks']
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000102
103 if not command in commands:
104 raise gclient_utils.Error('Unknown command %s' % command)
105
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000106 if not command in dir(self):
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000107 raise gclient_utils.Error('Command %s not implemented in %s wrapper' % (
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000108 command, self.scm_name))
109
110 return getattr(self, command)(options, args, file_list)
111
112
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000113class GitWrapper(SCMWrapper):
msb@chromium.orge28e4982009-09-25 20:51:45 +0000114 """Wrapper for Git"""
115
116 def cleanup(self, options, args, file_list):
msb@chromium.orgd8a63782010-01-25 17:47:05 +0000117 """'Cleanup' the repo.
118
119 There's no real git equivalent for the svn cleanup command, do a no-op.
120 """
msb@chromium.org3904caa2010-01-25 17:37:46 +0000121 __pychecker__ = 'unusednames=options,args,file_list'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000122
123 def diff(self, options, args, file_list):
msb@chromium.org3904caa2010-01-25 17:37:46 +0000124 __pychecker__ = 'unusednames=options,args,file_list'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000125 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
126 self._Run(['diff', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000127
128 def export(self, options, args, file_list):
msb@chromium.orgd6504212010-01-13 17:34:31 +0000129 """Export a clean directory tree into the given path.
130
131 Exports into the specified directory, creating the path if it does
132 already exist.
133 """
msb@chromium.org3904caa2010-01-25 17:37:46 +0000134 __pychecker__ = 'unusednames=options,file_list'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000135 assert len(args) == 1
136 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
137 if not os.path.exists(export_path):
138 os.makedirs(export_path)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000139 self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path],
140 redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000141
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000142 def pack(self, options, args, file_list):
143 """Generates a patch file which can be applied to the root of the
msb@chromium.orgd6504212010-01-13 17:34:31 +0000144 repository.
145
146 The patch file is generated from a diff of the merge base of HEAD and
147 its upstream branch.
148 """
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000149 __pychecker__ = 'unusednames=options,args,file_list'
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000150 path = os.path.join(self._root_dir, self.relpath)
151 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
152 command = ['diff', merge_base]
153 filterer = DiffFilterer(self.relpath)
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000154 scm.GIT.RunAndFilterOutput(command, path, False, False, filterer.Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000155
msb@chromium.orge28e4982009-09-25 20:51:45 +0000156 def update(self, options, args, file_list):
157 """Runs git to update or transparently checkout the working copy.
158
159 All updated files will be appended to file_list.
160
161 Raises:
162 Error: if can't get URL for relative path.
163 """
164
165 if args:
166 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
167
nasser@codeaurora.orgece406f2010-02-23 17:29:15 +0000168 self._CheckMinVersion("1.6.6")
msb@chromium.org923a0372009-12-11 20:42:43 +0000169
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000170 default_rev = "refs/heads/master"
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000171 url, revision = gclient_utils.SplitUrlRevision(self.url)
172 rev_str = ""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000173 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000174 # Override the revision number.
175 revision = str(options.revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000176 if not revision:
177 revision = default_rev
msb@chromium.orge28e4982009-09-25 20:51:45 +0000178
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000179 rev_str = ' at %s' % revision
180 files = []
181
182 printed_path = False
183 verbose = []
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000184 if options.verbose:
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000185 print("\n_____ %s%s" % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000186 verbose = ['--verbose']
187 printed_path = True
188
189 if revision.startswith('refs/heads/'):
190 rev_type = "branch"
191 elif revision.startswith('origin/'):
192 # For compatability with old naming, translate 'origin' to 'refs/heads'
193 revision = revision.replace('origin/', 'refs/heads/')
194 rev_type = "branch"
195 else:
196 # hash is also a tag, only make a distinction at checkout
197 rev_type = "hash"
198
msb@chromium.orge28e4982009-09-25 20:51:45 +0000199 if not os.path.exists(self.checkout_path):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000200 self._Clone(rev_type, revision, url, options.verbose)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000201 files = self._Run(['ls-files']).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000202 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000203 if not verbose:
204 # Make the output a little prettier. It's nice to have some whitespace
205 # between projects when cloning.
206 print ""
msb@chromium.orge28e4982009-09-25 20:51:45 +0000207 return
208
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000209 if not os.path.exists(os.path.join(self.checkout_path, '.git')):
210 raise gclient_utils.Error('\n____ %s%s\n'
211 '\tPath is not a git repo. No .git dir.\n'
212 '\tTo resolve:\n'
213 '\t\trm -rf %s\n'
214 '\tAnd run gclient sync again\n'
215 % (self.relpath, rev_str, self.relpath))
216
msb@chromium.org5bde4852009-12-14 16:47:12 +0000217 cur_branch = self._GetCurrentBranch()
218
219 # Check if we are in a rebase conflict
220 if cur_branch is None:
221 raise gclient_utils.Error('\n____ %s%s\n'
222 '\tAlready in a conflict, i.e. (no branch).\n'
223 '\tFix the conflict and run gclient again.\n'
224 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
225 '\tSee man git-rebase for details.\n'
226 % (self.relpath, rev_str))
227
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000228 # Cases:
229 # 1) current branch based on a hash (could be git-svn)
230 # - try to rebase onto the new upstream (hash or branch)
231 # 2) current branch based on a remote branch with local committed changes,
232 # but the DEPS file switched to point to a hash
233 # - rebase those changes on top of the hash
234 # 3) current branch based on a remote with or without changes, no switch
235 # - see if we can FF, if not, prompt the user for rebase, merge, or stop
236 # 4) current branch based on a remote, switches to a new remote
237 # - exit
238
239 # GetUpstream returns something like 'refs/remotes/origin/master' for a
240 # tracking branch
241 # or 'master' if not a tracking branch (it's based on a specific rev/hash)
242 # or it returns None if it couldn't find an upstream
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000243 upstream_branch = scm.GIT.GetUpstream(self.checkout_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000244 if not upstream_branch or not upstream_branch.startswith('refs/remotes'):
245 current_type = "hash"
246 logging.debug("Current branch is based off a specific rev and is not "
247 "tracking an upstream.")
248 elif upstream_branch.startswith('refs/remotes'):
249 current_type = "branch"
250 else:
251 raise gclient_utils.Error('Invalid Upstream')
252
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000253 # Update the remotes first so we have all the refs.
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000254 for _ in range(3):
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000255 try:
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000256 remote_output, remote_err = scm.GIT.Capture(
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000257 ['remote'] + verbose + ['update'],
258 self.checkout_path,
259 print_error=False)
260 break
261 except gclient_utils.CheckCallError, e:
262 # Hackish but at that point, git is known to work so just checking for
263 # 502 in stderr should be fine.
264 if '502' in e.stderr:
265 print str(e)
266 print "Retrying..."
267 continue
268 raise e
269
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000270 if verbose:
271 print remote_output.strip()
272 # git remote update prints to stderr when used with --verbose
273 print remote_err.strip()
274
275 # This is a big hammer, debatable if it should even be here...
davemoore@chromium.org793796d2010-02-19 17:27:41 +0000276 if options.force or options.reset:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000277 self._Run(['reset', '--hard', 'HEAD'], redirect_stdout=False)
278
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000279 if current_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000280 # case 1
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000281 if scm.GIT.IsGitSvn(self.checkout_path) and upstream_branch is not None:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000282 # Our git-svn branch (upstream_branch) is our upstream
283 self._AttemptRebase(upstream_branch, files, verbose=options.verbose,
284 newbase=revision, printed_path=printed_path)
285 printed_path = True
286 else:
287 # Can't find a merge-base since we don't know our upstream. That makes
288 # this command VERY likely to produce a rebase failure. For now we
289 # assume origin is our upstream since that's what the old behavior was.
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000290 upstream_branch = 'origin'
291 if options.revision:
292 upstream_branch = revision
293 self._AttemptRebase(upstream_branch, files=files,
294 verbose=options.verbose, printed_path=printed_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000295 printed_path = True
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000296 elif rev_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000297 # case 2
298 self._AttemptRebase(upstream_branch, files, verbose=options.verbose,
299 newbase=revision, printed_path=printed_path)
300 printed_path = True
301 elif revision.replace('heads', 'remotes/origin') != upstream_branch:
302 # case 4
303 new_base = revision.replace('heads', 'remotes/origin')
304 if not printed_path:
305 print("\n_____ %s%s" % (self.relpath, rev_str))
306 switch_error = ("Switching upstream branch from %s to %s\n"
307 % (upstream_branch, new_base) +
308 "Please merge or rebase manually:\n" +
309 "cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
310 "OR git checkout -b <some new branch> %s" % new_base)
311 raise gclient_utils.Error(switch_error)
312 else:
313 # case 3 - the default case
314 files = self._Run(['diff', upstream_branch, '--name-only']).split()
315 if verbose:
316 print "Trying fast-forward merge to branch : %s" % upstream_branch
317 try:
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000318 merge_output, merge_err = scm.GIT.Capture(['merge', '--ff-only',
319 upstream_branch],
320 self.checkout_path,
321 print_error=False)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000322 except gclient_utils.CheckCallError, e:
323 if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr):
324 if not printed_path:
325 print("\n_____ %s%s" % (self.relpath, rev_str))
326 printed_path = True
327 while True:
328 try:
329 action = str(raw_input("Cannot fast-forward merge, attempt to "
330 "rebase? (y)es / (q)uit / (s)kip : "))
331 except ValueError:
332 gclient_utils.Error('Invalid Character')
333 continue
334 if re.match(r'yes|y', action, re.I):
335 self._AttemptRebase(upstream_branch, files,
336 verbose=options.verbose,
337 printed_path=printed_path)
338 printed_path = True
339 break
340 elif re.match(r'quit|q', action, re.I):
341 raise gclient_utils.Error("Can't fast-forward, please merge or "
342 "rebase manually.\n"
343 "cd %s && git " % self.checkout_path
344 + "rebase %s" % upstream_branch)
345 elif re.match(r'skip|s', action, re.I):
346 print "Skipping %s" % self.relpath
347 return
348 else:
349 print "Input not recognized"
350 elif re.match("error: Your local changes to '.*' would be "
351 "overwritten by merge. Aborting.\nPlease, commit your "
352 "changes or stash them before you can merge.\n",
353 e.stderr):
354 if not printed_path:
355 print("\n_____ %s%s" % (self.relpath, rev_str))
356 printed_path = True
357 raise gclient_utils.Error(e.stderr)
358 else:
359 # Some other problem happened with the merge
360 logging.error("Error during fast-forward merge in %s!" % self.relpath)
361 print e.stderr
362 raise
363 else:
364 # Fast-forward merge was successful
365 if not re.match('Already up-to-date.', merge_output) or verbose:
366 if not printed_path:
367 print("\n_____ %s%s" % (self.relpath, rev_str))
368 printed_path = True
369 print merge_output.strip()
370 if merge_err:
371 print "Merge produced error output:\n%s" % merge_err.strip()
372 if not verbose:
373 # Make the output a little prettier. It's nice to have some
374 # whitespace between projects when syncing.
375 print ""
376
377 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.org5bde4852009-12-14 16:47:12 +0000378
379 # If the rebase generated a conflict, abort and ask user to fix
380 if self._GetCurrentBranch() is None:
381 raise gclient_utils.Error('\n____ %s%s\n'
382 '\nConflict while rebasing this branch.\n'
383 'Fix the conflict and run gclient again.\n'
384 'See man git-rebase for details.\n'
385 % (self.relpath, rev_str))
386
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000387 if verbose:
388 print "Checked out revision %s" % self.revinfo(options, (), None)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000389
390 def revert(self, options, args, file_list):
391 """Reverts local modifications.
392
393 All reverted files will be appended to file_list.
394 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000395 __pychecker__ = 'unusednames=args'
msb@chromium.org260c6532009-10-28 03:22:35 +0000396 path = os.path.join(self._root_dir, self.relpath)
397 if not os.path.isdir(path):
398 # revert won't work if the directory doesn't exist. It needs to
399 # checkout instead.
400 print("\n_____ %s is missing, synching instead" % self.relpath)
401 # Don't reuse the args.
402 return self.update(options, [], file_list)
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000403 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
404 files = self._Run(['diff', merge_base, '--name-only']).split()
405 self._Run(['reset', '--hard', merge_base], redirect_stdout=False)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000406 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
407
msb@chromium.org0f282062009-11-06 20:14:02 +0000408 def revinfo(self, options, args, file_list):
409 """Display revision"""
msb@chromium.org3904caa2010-01-25 17:37:46 +0000410 __pychecker__ = 'unusednames=options,args,file_list'
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000411 return self._Run(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000412
msb@chromium.orge28e4982009-09-25 20:51:45 +0000413 def runhooks(self, options, args, file_list):
414 self.status(options, args, file_list)
415
416 def status(self, options, args, file_list):
417 """Display status information."""
msb@chromium.org3904caa2010-01-25 17:37:46 +0000418 __pychecker__ = 'unusednames=options,args'
msb@chromium.orge28e4982009-09-25 20:51:45 +0000419 if not os.path.isdir(self.checkout_path):
420 print('\n________ couldn\'t run status in %s:\nThe directory '
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000421 'does not exist.' % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000422 else:
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000423 merge_base = self._Run(['merge-base', 'HEAD', 'origin'])
424 self._Run(['diff', '--name-status', merge_base], redirect_stdout=False)
425 files = self._Run(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000426 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
427
msb@chromium.orge6f78352010-01-13 17:05:33 +0000428 def FullUrlForRelativeUrl(self, url):
429 # Strip from last '/'
430 # Equivalent to unix basename
431 base_url = self.url
432 return base_url[:base_url.rfind('/')] + url
433
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000434 def _Clone(self, rev_type, revision, url, verbose=False):
435 """Clone a git repository from the given URL.
436
437 Once we've cloned the repo, we checkout a working branch based off the
438 specified revision."""
439 if not verbose:
440 # git clone doesn't seem to insert a newline properly before printing
441 # to stdout
442 print ""
443
444 clone_cmd = ['clone']
445 if verbose:
446 clone_cmd.append('--verbose')
447 clone_cmd.extend([url, self.checkout_path])
448
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000449 for _ in range(3):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000450 try:
451 self._Run(clone_cmd, cwd=self._root_dir, redirect_stdout=False)
452 break
453 except gclient_utils.Error, e:
454 # TODO(maruel): Hackish, should be fixed by moving _Run() to
455 # CheckCall().
456 # Too bad we don't have access to the actual output.
457 # We should check for "transfer closed with NNN bytes remaining to
458 # read". In the meantime, just make sure .git exists.
459 if (e.args[0] == 'git command clone returned 128' and
460 os.path.exists(os.path.join(self.checkout_path, '.git'))):
461 print str(e)
462 print "Retrying..."
463 continue
464 raise e
465
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000466 if rev_type == "branch":
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000467 short_rev = revision.replace('refs/heads/', '')
468 new_branch = revision.replace('heads', 'remotes/origin')
469 elif revision.startswith('refs/tags/'):
470 short_rev = revision.replace('refs/tags/', '')
471 new_branch = revision
472 else:
473 # revision is a specific sha1 hash
474 short_rev = revision
475 new_branch = revision
476
477 cur_branch = self._GetCurrentBranch()
478 if cur_branch != short_rev:
479 self._Run(['checkout', '-b', short_rev, new_branch],
480 redirect_stdout=False)
481
482 def _AttemptRebase(self, upstream, files, verbose=False, newbase=None,
483 branch=None, printed_path=False):
484 """Attempt to rebase onto either upstream or, if specified, newbase."""
485 files.extend(self._Run(['diff', upstream, '--name-only']).split())
486 revision = upstream
487 if newbase:
488 revision = newbase
489 if not printed_path:
490 print "\n_____ %s : Attempting rebase onto %s..." % (self.relpath,
491 revision)
492 printed_path = True
493 else:
494 print "Attempting rebase onto %s..." % revision
495
496 # Build the rebase command here using the args
497 # git rebase [options] [--onto <newbase>] <upstream> [<branch>]
498 rebase_cmd = ['rebase']
499 if verbose:
500 rebase_cmd.append('--verbose')
501 if newbase:
502 rebase_cmd.extend(['--onto', newbase])
503 rebase_cmd.append(upstream)
504 if branch:
505 rebase_cmd.append(branch)
506
507 try:
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000508 rebase_output, rebase_err = scm.GIT.Capture(rebase_cmd,
509 self.checkout_path,
510 print_error=False)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000511 except gclient_utils.CheckCallError, e:
512 if re.match(r'cannot rebase: you have unstaged changes', e.stderr) or \
513 re.match(r'cannot rebase: your index contains uncommitted changes',
514 e.stderr):
515 while True:
516 rebase_action = str(raw_input("Cannot rebase because of unstaged "
517 "changes.\n'git reset --hard HEAD' ?\n"
518 "WARNING: destroys any uncommitted "
519 "work in your current branch!"
520 " (y)es / (q)uit / (s)how : "))
521 if re.match(r'yes|y', rebase_action, re.I):
522 self._Run(['reset', '--hard', 'HEAD'], redirect_stdout=False)
523 # Should this be recursive?
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000524 rebase_output, rebase_err = scm.GIT.Capture(rebase_cmd,
525 self.checkout_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000526 break
527 elif re.match(r'quit|q', rebase_action, re.I):
528 raise gclient_utils.Error("Please merge or rebase manually\n"
529 "cd %s && git " % self.checkout_path
530 + "%s" % ' '.join(rebase_cmd))
531 elif re.match(r'show|s', rebase_action, re.I):
532 print "\n%s" % e.stderr.strip()
533 continue
534 else:
535 gclient_utils.Error("Input not recognized")
536 continue
537 elif re.search(r'^CONFLICT', e.stdout, re.M):
538 raise gclient_utils.Error("Conflict while rebasing this branch.\n"
539 "Fix the conflict and run gclient again.\n"
540 "See 'man git-rebase' for details.\n")
541 else:
542 print e.stdout.strip()
543 print "Rebase produced error output:\n%s" % e.stderr.strip()
544 raise gclient_utils.Error("Unrecognized error, please merge or rebase "
545 "manually.\ncd %s && git " %
546 self.checkout_path
547 + "%s" % ' '.join(rebase_cmd))
548
549 print rebase_output.strip()
550 if rebase_err:
551 print "Rebase produced error output:\n%s" % rebase_err.strip()
552 if not verbose:
553 # Make the output a little prettier. It's nice to have some
554 # whitespace between projects when syncing.
555 print ""
556
msb@chromium.org923a0372009-12-11 20:42:43 +0000557 def _CheckMinVersion(self, min_version):
maruel@chromium.orgd0f854a2010-03-11 19:35:53 +0000558 (ok, current_version) = scm.GIT.AssertVersion(min_version)
559 if not ok:
560 raise gclient_utils.Error('git version %s < minimum required %s' %
561 (current_version, min_version))
msb@chromium.org923a0372009-12-11 20:42:43 +0000562
msb@chromium.org5bde4852009-12-14 16:47:12 +0000563 def _GetCurrentBranch(self):
564 # Returns name of current branch
565 # Returns None if inside a (no branch)
566 tokens = self._Run(['branch']).split()
567 branch = tokens[tokens.index('*') + 1]
568 if branch == '(no':
569 return None
570 return branch
571
maruel@chromium.org2de10252010-02-08 01:10:39 +0000572 def _Run(self, args, cwd=None, redirect_stdout=True):
573 # TODO(maruel): Merge with Capture or better gclient_utils.CheckCall().
maruel@chromium.orgffe96f02009-12-09 18:39:15 +0000574 if cwd is None:
575 cwd = self.checkout_path
maruel@chromium.org2de10252010-02-08 01:10:39 +0000576 stdout = None
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000577 if redirect_stdout:
maruel@chromium.org2de10252010-02-08 01:10:39 +0000578 stdout = subprocess.PIPE
msb@chromium.orge28e4982009-09-25 20:51:45 +0000579 if cwd == None:
580 cwd = self.checkout_path
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000581 cmd = [scm.GIT.COMMAND]
msb@chromium.orge28e4982009-09-25 20:51:45 +0000582 cmd.extend(args)
maruel@chromium.orgf3909bf2010-01-08 01:14:51 +0000583 logging.debug(cmd)
584 try:
585 sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
586 output = sp.communicate()[0]
587 except OSError:
588 raise gclient_utils.Error("git command '%s' failed to run." %
589 ' '.join(cmd) + "\nCheck that you have git installed.")
maruel@chromium.org2de10252010-02-08 01:10:39 +0000590 if sp.returncode:
msb@chromium.orge28e4982009-09-25 20:51:45 +0000591 raise gclient_utils.Error('git command %s returned %d' %
592 (args[0], sp.returncode))
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +0000593 if output is not None:
msb@chromium.orge8e60e52009-11-02 21:50:56 +0000594 return output.strip()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000595
596
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000597class SVNWrapper(SCMWrapper):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000598 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000599
600 def cleanup(self, options, args, file_list):
601 """Cleanup working copy."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000602 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000603 command = ['cleanup']
604 command.extend(args)
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000605 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000606
607 def diff(self, options, args, file_list):
608 # NOTE: This function does not currently modify file_list.
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000609 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000610 command = ['diff']
611 command.extend(args)
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000612 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000613
614 def export(self, options, args, file_list):
msb@chromium.orgd6504212010-01-13 17:34:31 +0000615 """Export a clean directory tree into the given path."""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000616 __pychecker__ = 'unusednames=file_list,options'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000617 assert len(args) == 1
618 export_path = os.path.abspath(os.path.join(args[0], self.relpath))
619 try:
620 os.makedirs(export_path)
621 except OSError:
622 pass
623 assert os.path.exists(export_path)
624 command = ['export', '--force', '.']
625 command.append(export_path)
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000626 scm.SVN.Run(command, os.path.join(self._root_dir, self.relpath))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000627
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000628 def pack(self, options, args, file_list):
629 """Generates a patch file which can be applied to the root of the
630 repository."""
631 __pychecker__ = 'unusednames=file_list,options'
632 path = os.path.join(self._root_dir, self.relpath)
633 command = ['diff']
634 command.extend(args)
635
636 filterer = DiffFilterer(self.relpath)
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000637 scm.SVN.RunAndFilterOutput(command, path, False, False, filterer.Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000638
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000639 def update(self, options, args, file_list):
msb@chromium.orgd6504212010-01-13 17:34:31 +0000640 """Runs svn to update or transparently checkout the working copy.
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000641
642 All updated files will be appended to file_list.
643
644 Raises:
645 Error: if can't get URL for relative path.
646 """
647 # Only update if git is not controlling the directory.
648 checkout_path = os.path.join(self._root_dir, self.relpath)
649 git_path = os.path.join(self._root_dir, self.relpath, '.git')
650 if os.path.exists(git_path):
651 print("________ found .git directory; skipping %s" % self.relpath)
652 return
653
654 if args:
655 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
656
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000657 url, revision = gclient_utils.SplitUrlRevision(self.url)
658 base_url = url
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000659 forced_revision = False
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000660 rev_str = ""
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000661 if options.revision:
662 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000663 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000664 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000665 forced_revision = True
666 url = '%s@%s' % (url, revision)
msb@chromium.org770ff9e2009-09-23 17:18:18 +0000667 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000668
669 if not os.path.exists(checkout_path):
670 # We need to checkout.
671 command = ['checkout', url, checkout_path]
672 if revision:
673 command.extend(['--revision', str(revision)])
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000674 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000675 return
676
677 # Get the existing scm url and the revision number of the current checkout.
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000678 from_info = scm.SVN.CaptureInfo(os.path.join(checkout_path, '.'), '.')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000679 if not from_info:
680 raise gclient_utils.Error("Can't update/checkout %r if an unversioned "
681 "directory is present. Delete the directory "
682 "and try again." %
683 checkout_path)
684
maruel@chromium.org7753d242009-10-07 17:40:24 +0000685 if options.manually_grab_svn_rev:
686 # Retrieve the current HEAD version because svn is slow at null updates.
687 if not revision:
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000688 from_info_live = scm.SVN.CaptureInfo(from_info['URL'], '.')
maruel@chromium.org7753d242009-10-07 17:40:24 +0000689 revision = str(from_info_live['Revision'])
690 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000691
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000692 if from_info['URL'] != base_url:
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000693 to_info = scm.SVN.CaptureInfo(url, '.')
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +0000694 if not to_info.get('Repository Root') or not to_info.get('UUID'):
695 # The url is invalid or the server is not accessible, it's safer to bail
696 # out right now.
697 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000698 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
699 and (from_info['UUID'] == to_info['UUID']))
700 if can_switch:
701 print("\n_____ relocating %s to a new checkout" % self.relpath)
702 # We have different roots, so check if we can switch --relocate.
703 # Subversion only permits this if the repository UUIDs match.
704 # Perform the switch --relocate, then rewrite the from_url
705 # to reflect where we "are now." (This is the same way that
706 # Subversion itself handles the metadata when switch --relocate
707 # is used.) This makes the checks below for whether we
708 # can update to a revision or have to switch to a different
709 # branch work as expected.
710 # TODO(maruel): TEST ME !
711 command = ["switch", "--relocate",
712 from_info['Repository Root'],
713 to_info['Repository Root'],
714 self.relpath]
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000715 scm.SVN.Run(command, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000716 from_info['URL'] = from_info['URL'].replace(
717 from_info['Repository Root'],
718 to_info['Repository Root'])
719 else:
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000720 if scm.SVN.CaptureStatus(checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000721 raise gclient_utils.Error("Can't switch the checkout to %s; UUID "
722 "don't match and there is local changes "
723 "in %s. Delete the directory and "
724 "try again." % (url, checkout_path))
725 # Ok delete it.
726 print("\n_____ switching %s to a new checkout" % self.relpath)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000727 gclient_utils.RemoveDirectory(checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000728 # We need to checkout.
729 command = ['checkout', url, checkout_path]
730 if revision:
731 command.extend(['--revision', str(revision)])
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000732 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000733 return
734
735
736 # If the provided url has a revision number that matches the revision
737 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +0000738 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000739 if options.verbose or not forced_revision:
740 print("\n_____ %s%s" % (self.relpath, rev_str))
741 return
742
743 command = ["update", checkout_path]
744 if revision:
745 command.extend(['--revision', str(revision)])
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000746 scm.SVN.RunAndGetFileList(options, command, self._root_dir, file_list)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000747
748 def revert(self, options, args, file_list):
749 """Reverts local modifications. Subversion specific.
750
751 All reverted files will be appended to file_list, even if Subversion
752 doesn't know about them.
753 """
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000754 __pychecker__ = 'unusednames=args'
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000755 path = os.path.join(self._root_dir, self.relpath)
756 if not os.path.isdir(path):
757 # svn revert won't work if the directory doesn't exist. It needs to
758 # checkout instead.
759 print("\n_____ %s is missing, synching instead" % self.relpath)
760 # Don't reuse the args.
761 return self.update(options, [], file_list)
762
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000763 for file_status in scm.SVN.CaptureStatus(path):
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000764 file_path = os.path.join(path, file_status[1])
765 if file_status[0][0] == 'X':
maruel@chromium.org754960e2009-09-21 12:31:05 +0000766 # Ignore externals.
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000767 logging.info('Ignoring external %s' % file_path)
maruel@chromium.org754960e2009-09-21 12:31:05 +0000768 continue
769
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000770 if logging.getLogger().isEnabledFor(logging.INFO):
771 logging.info('%s%s' % (file[0], file[1]))
772 else:
773 print(file_path)
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000774 if file_status[0].isspace():
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000775 logging.error('No idea what is the status of %s.\n'
776 'You just found a bug in gclient, please ping '
777 'maruel@chromium.org ASAP!' % file_path)
778 # svn revert is really stupid. It fails on inconsistent line-endings,
779 # on switched directories, etc. So take no chance and delete everything!
780 try:
781 if not os.path.exists(file_path):
782 pass
maruel@chromium.orgd2e78ff2010-01-11 20:37:19 +0000783 elif os.path.isfile(file_path) or os.path.islink(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000784 logging.info('os.remove(%s)' % file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000785 os.remove(file_path)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000786 elif os.path.isdir(file_path):
maruel@chromium.org754960e2009-09-21 12:31:05 +0000787 logging.info('gclient_utils.RemoveDirectory(%s)' % file_path)
bradnelson@google.com8f9c69f2009-09-17 00:48:28 +0000788 gclient_utils.RemoveDirectory(file_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000789 else:
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000790 logging.error('no idea what is %s.\nYou just found a bug in gclient'
791 ', please ping maruel@chromium.org ASAP!' % file_path)
792 except EnvironmentError:
793 logging.error('Failed to remove %s.' % file_path)
794
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000795 try:
796 # svn revert is so broken we don't even use it. Using
797 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000798 scm.SVN.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path,
799 file_list)
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000800 except OSError, e:
801 # Maybe the directory disapeared meanwhile. We don't want it to throw an
802 # exception.
803 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000804
msb@chromium.org0f282062009-11-06 20:14:02 +0000805 def revinfo(self, options, args, file_list):
806 """Display revision"""
maruel@chromium.orge3608df2009-11-10 20:22:57 +0000807 __pychecker__ = 'unusednames=args,file_list,options'
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000808 return scm.SVN.CaptureHeadRevision(self.url)
msb@chromium.org0f282062009-11-06 20:14:02 +0000809
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000810 def runhooks(self, options, args, file_list):
811 self.status(options, args, file_list)
812
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000813 def status(self, options, args, file_list):
814 """Display status information."""
815 path = os.path.join(self._root_dir, self.relpath)
816 command = ['status']
817 command.extend(args)
818 if not os.path.isdir(path):
819 # svn status won't work if the directory doesn't exist.
820 print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory "
821 "does not exist."
822 % (' '.join(command), path))
823 # There's no file list to retrieve.
824 else:
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000825 scm.SVN.RunAndGetFileList(options, command, path, file_list)
msb@chromium.orge6f78352010-01-13 17:05:33 +0000826
827 def FullUrlForRelativeUrl(self, url):
828 # Find the forth '/' and strip from there. A bit hackish.
829 return '/'.join(self.url.split('/')[:4]) + url