blob: 5e64afcedd8cf25a0ea4c89ab8cb4b34510ab56d [file] [log] [blame]
maruel@chromium.orgbffad372011-09-08 17:54:22 +00001# Copyright (c) 2011 The Chromium Authors. All rights reserved.
maruel@chromium.org5aeb7dd2009-11-17 18:09:01 +00002# 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
maruel@chromium.org90541732011-04-01 17:54:18 +000011import sys
maruel@chromium.orgfd876172010-04-30 14:01:05 +000012import time
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000013
14import gclient_utils
maruel@chromium.org31cb48a2011-04-04 18:01:36 +000015import scm
16import subprocess2
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000017
18
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000019class DiffFilterer(object):
20 """Simple class which tracks which file is being diffed and
21 replaces instances of its file name in the original and
msb@chromium.orgd6504212010-01-13 17:34:31 +000022 working copy lines of the svn/git diff output."""
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000023 index_string = "Index: "
24 original_prefix = "--- "
25 working_prefix = "+++ "
26
maruel@chromium.org77e4eca2010-09-21 13:23:07 +000027 def __init__(self, relpath):
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000028 # Note that we always use '/' as the path separator to be
29 # consistent with svn's cygwin-style output on Windows
30 self._relpath = relpath.replace("\\", "/")
31 self._current_file = ""
32 self._replacement_file = ""
33
maruel@chromium.org6e29d572010-06-04 17:32:20 +000034 def SetCurrentFile(self, current_file):
35 self._current_file = current_file
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000036 # Note that we always use '/' as the path separator to be
37 # consistent with svn's cygwin-style output on Windows
maruel@chromium.org6e29d572010-06-04 17:32:20 +000038 self._replacement_file = posixpath.join(self._relpath, current_file)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000039
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +000040 def _Replace(self, line):
41 return line.replace(self._current_file, self._replacement_file)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000042
43 def Filter(self, line):
44 if (line.startswith(self.index_string)):
45 self.SetCurrentFile(line[len(self.index_string):])
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +000046 line = self._Replace(line)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000047 else:
48 if (line.startswith(self.original_prefix) or
49 line.startswith(self.working_prefix)):
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +000050 line = self._Replace(line)
maruel@chromium.org77e4eca2010-09-21 13:23:07 +000051 print(line)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000052
53
maruel@chromium.org90541732011-04-01 17:54:18 +000054def ask_for_data(prompt):
55 try:
56 return raw_input(prompt)
57 except KeyboardInterrupt:
58 # Hide the exception.
59 sys.exit(1)
60
61
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000062### SCM abstraction layer
63
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000064# Factory Method for SCM wrapper creation
65
maruel@chromium.org9eda4112010-06-11 18:56:10 +000066def GetScmName(url):
67 if url:
68 url, _ = gclient_utils.SplitUrlRevision(url)
69 if (url.startswith('git://') or url.startswith('ssh://') or
70 url.endswith('.git')):
71 return 'git'
maruel@chromium.orgb74dca22010-06-11 20:10:40 +000072 elif (url.startswith('http://') or url.startswith('https://') or
maruel@chromium.org54a07a22010-06-14 19:07:39 +000073 url.startswith('svn://') or url.startswith('svn+ssh://')):
maruel@chromium.org9eda4112010-06-11 18:56:10 +000074 return 'svn'
75 return None
76
77
78def CreateSCM(url, root_dir=None, relpath=None):
79 SCM_MAP = {
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000080 'svn' : SVNWrapper,
msb@chromium.orge28e4982009-09-25 20:51:45 +000081 'git' : GitWrapper,
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000082 }
msb@chromium.orge28e4982009-09-25 20:51:45 +000083
maruel@chromium.org9eda4112010-06-11 18:56:10 +000084 scm_name = GetScmName(url)
85 if not scm_name in SCM_MAP:
86 raise gclient_utils.Error('No SCM found for url %s' % url)
87 return SCM_MAP[scm_name](url, root_dir, relpath)
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000088
89
90# SCMWrapper base class
91
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000092class SCMWrapper(object):
93 """Add necessary glue between all the supported SCM.
94
msb@chromium.orgd6504212010-01-13 17:34:31 +000095 This is the abstraction layer to bind to different SCM.
96 """
maruel@chromium.org9eda4112010-06-11 18:56:10 +000097 def __init__(self, url=None, root_dir=None, relpath=None):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000098 self.url = url
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +000099 self._root_dir = root_dir
100 if self._root_dir:
101 self._root_dir = self._root_dir.replace('/', os.sep)
102 self.relpath = relpath
103 if self.relpath:
104 self.relpath = self.relpath.replace('/', os.sep)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000105 if self.relpath and self._root_dir:
106 self.checkout_path = os.path.join(self._root_dir, self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000107
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000108 def RunCommand(self, command, options, args, file_list=None):
109 # file_list will have all files that are modified appended to it.
maruel@chromium.orgde754ac2009-09-17 18:04:50 +0000110 if file_list is None:
111 file_list = []
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000112
phajdan.jr@chromium.org6e043f72011-05-02 07:24:32 +0000113 commands = ['cleanup', 'update', 'updatesingle', 'revert',
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000114 'revinfo', 'status', 'diff', 'pack', 'runhooks']
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000115
116 if not command in commands:
117 raise gclient_utils.Error('Unknown command %s' % command)
118
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000119 if not command in dir(self):
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000120 raise gclient_utils.Error('Command %s not implemented in %s wrapper' % (
maruel@chromium.org9eda4112010-06-11 18:56:10 +0000121 command, self.__class__.__name__))
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000122
123 return getattr(self, command)(options, args, file_list)
124
125
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000126class GitWrapper(SCMWrapper):
msb@chromium.orge28e4982009-09-25 20:51:45 +0000127 """Wrapper for Git"""
128
floitsch@google.comeaab7842011-04-28 09:07:58 +0000129 def GetRevisionDate(self, revision):
130 """Returns the given revision's date in ISO-8601 format (which contains the
131 time zone)."""
132 # TODO(floitsch): get the time-stamp of the given revision and not just the
133 # time-stamp of the currently checked out revision.
134 return self._Capture(['log', '-n', '1', '--format=%ai'])
135
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000136 @staticmethod
137 def cleanup(options, args, file_list):
msb@chromium.orgd8a63782010-01-25 17:47:05 +0000138 """'Cleanup' the repo.
139
140 There's no real git equivalent for the svn cleanup command, do a no-op.
141 """
msb@chromium.orge28e4982009-09-25 20:51:45 +0000142
143 def diff(self, options, args, file_list):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000144 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org37e89872010-09-07 16:11:33 +0000145 self._Run(['diff', merge_base], options)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000146
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000147 def pack(self, options, args, file_list):
148 """Generates a patch file which can be applied to the root of the
msb@chromium.orgd6504212010-01-13 17:34:31 +0000149 repository.
150
151 The patch file is generated from a diff of the merge base of HEAD and
152 its upstream branch.
153 """
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000154 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org17d01792010-09-01 18:07:10 +0000155 gclient_utils.CheckCallAndFilter(
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000156 ['git', 'diff', merge_base],
157 cwd=self.checkout_path,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000158 filter_fn=DiffFilterer(self.relpath).Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000159
msb@chromium.orge28e4982009-09-25 20:51:45 +0000160 def update(self, options, args, file_list):
161 """Runs git to update or transparently checkout the working copy.
162
163 All updated files will be appended to file_list.
164
165 Raises:
166 Error: if can't get URL for relative path.
167 """
msb@chromium.orge28e4982009-09-25 20:51:45 +0000168 if args:
169 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
170
nasser@codeaurora.orgece406f2010-02-23 17:29:15 +0000171 self._CheckMinVersion("1.6.6")
msb@chromium.org923a0372009-12-11 20:42:43 +0000172
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000173 default_rev = "refs/heads/master"
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000174 url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000175 rev_str = ""
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000176 revision = deps_revision
msb@chromium.orge28e4982009-09-25 20:51:45 +0000177 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000178 # Override the revision number.
179 revision = str(options.revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000180 if not revision:
181 revision = default_rev
msb@chromium.orge28e4982009-09-25 20:51:45 +0000182
floitsch@google.comeaab7842011-04-28 09:07:58 +0000183 if gclient_utils.IsDateRevision(revision):
184 # Date-revisions only work on git-repositories if the reflog hasn't
185 # expired yet. Use rev-list to get the corresponding revision.
186 # git rev-list -n 1 --before='time-stamp' branchname
187 if options.transitive:
188 print('Warning: --transitive only works for SVN repositories.')
189 revision = default_rev
190
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000191 rev_str = ' at %s' % revision
192 files = []
193
194 printed_path = False
195 verbose = []
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000196 if options.verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000197 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000198 verbose = ['--verbose']
199 printed_path = True
200
201 if revision.startswith('refs/heads/'):
202 rev_type = "branch"
203 elif revision.startswith('origin/'):
204 # For compatability with old naming, translate 'origin' to 'refs/heads'
205 revision = revision.replace('origin/', 'refs/heads/')
206 rev_type = "branch"
207 else:
208 # hash is also a tag, only make a distinction at checkout
209 rev_type = "hash"
210
msb@chromium.orge28e4982009-09-25 20:51:45 +0000211 if not os.path.exists(self.checkout_path):
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000212 self._Clone(revision, url, options)
thomasvl@chromium.org858d6452011-03-24 17:59:20 +0000213 files = self._Capture(['ls-files']).splitlines()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000214 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000215 if not verbose:
216 # Make the output a little prettier. It's nice to have some whitespace
217 # between projects when cloning.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000218 print('')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000219 return
220
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000221 if not os.path.exists(os.path.join(self.checkout_path, '.git')):
222 raise gclient_utils.Error('\n____ %s%s\n'
223 '\tPath is not a git repo. No .git dir.\n'
224 '\tTo resolve:\n'
225 '\t\trm -rf %s\n'
226 '\tAnd run gclient sync again\n'
227 % (self.relpath, rev_str, self.relpath))
228
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000229 # See if the url has changed (the unittests use git://foo for the url, let
230 # that through).
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000231 current_url = self._Capture(['config', 'remote.origin.url'])
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000232 # TODO(maruel): Delete url != 'git://foo' since it's just to make the
233 # unit test pass. (and update the comment above)
234 if current_url != url and url != 'git://foo':
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000235 print('_____ switching %s to a new upstream' % self.relpath)
236 # Make sure it's clean
237 self._CheckClean(rev_str)
238 # Switch over to the new upstream
239 self._Run(['remote', 'set-url', 'origin', url], options)
240 quiet = []
241 if not options.verbose:
242 quiet = ['--quiet']
243 self._Run(['fetch', 'origin', '--prune'] + quiet, options)
244 self._Run(['reset', '--hard', 'origin/master'] + quiet, options)
245 files = self._Capture(['ls-files']).splitlines()
246 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
247 return
248
msb@chromium.org5bde4852009-12-14 16:47:12 +0000249 cur_branch = self._GetCurrentBranch()
250
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000251 # Cases:
msb@chromium.org786fb682010-06-02 15:16:23 +0000252 # 0) HEAD is detached. Probably from our initial clone.
253 # - make sure HEAD is contained by a named ref, then update.
254 # Cases 1-4. HEAD is a branch.
255 # 1) current branch is not tracking a remote branch (could be git-svn)
256 # - try to rebase onto the new hash or branch
257 # 2) current branch is tracking a remote branch with local committed
258 # changes, but the DEPS file switched to point to a hash
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000259 # - rebase those changes on top of the hash
msb@chromium.org786fb682010-06-02 15:16:23 +0000260 # 3) current branch is tracking a remote branch w/or w/out changes,
261 # no switch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000262 # - see if we can FF, if not, prompt the user for rebase, merge, or stop
msb@chromium.org786fb682010-06-02 15:16:23 +0000263 # 4) current branch is tracking a remote branch, switches to a different
264 # remote branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000265 # - exit
266
maruel@chromium.org81e012c2010-04-29 16:07:24 +0000267 # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for
268 # a tracking branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000269 # or 'master' if not a tracking branch (it's based on a specific rev/hash)
270 # or it returns None if it couldn't find an upstream
msb@chromium.org786fb682010-06-02 15:16:23 +0000271 if cur_branch is None:
272 upstream_branch = None
273 current_type = "detached"
274 logging.debug("Detached HEAD")
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000275 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000276 upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
277 if not upstream_branch or not upstream_branch.startswith('refs/remotes'):
278 current_type = "hash"
279 logging.debug("Current branch is not tracking an upstream (remote)"
280 " branch.")
281 elif upstream_branch.startswith('refs/remotes'):
282 current_type = "branch"
283 else:
284 raise gclient_utils.Error('Invalid Upstream: %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000285
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000286 # Update the remotes first so we have all the refs.
cbentzel@chromium.org2aee2292010-09-03 14:15:25 +0000287 backoff_time = 5
maruel@chromium.orgfd876172010-04-30 14:01:05 +0000288 for _ in range(10):
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000289 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000290 remote_output = scm.GIT.Capture(
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000291 ['remote'] + verbose + ['update'],
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000292 cwd=self.checkout_path)
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000293 break
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000294 except subprocess2.CalledProcessError, e:
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000295 # Hackish but at that point, git is known to work so just checking for
296 # 502 in stderr should be fine.
297 if '502' in e.stderr:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000298 print(str(e))
299 print('Sleeping %.1f seconds and retrying...' % backoff_time)
cbentzel@chromium.org2aee2292010-09-03 14:15:25 +0000300 time.sleep(backoff_time)
301 backoff_time *= 1.3
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000302 continue
maruel@chromium.orgfd876172010-04-30 14:01:05 +0000303 raise
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000304
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000305 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000306 print(remote_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000307
308 # This is a big hammer, debatable if it should even be here...
davemoore@chromium.org793796d2010-02-19 17:27:41 +0000309 if options.force or options.reset:
maruel@chromium.org37e89872010-09-07 16:11:33 +0000310 self._Run(['reset', '--hard', 'HEAD'], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000311
msb@chromium.org786fb682010-06-02 15:16:23 +0000312 if current_type == 'detached':
313 # case 0
314 self._CheckClean(rev_str)
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000315 self._CheckDetachedHead(rev_str, options)
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000316 self._Capture(['checkout', '--quiet', '%s' % revision])
msb@chromium.org786fb682010-06-02 15:16:23 +0000317 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000318 print('\n_____ %s%s' % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000319 elif current_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000320 # case 1
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000321 if scm.GIT.IsGitSvn(self.checkout_path) and upstream_branch is not None:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000322 # Our git-svn branch (upstream_branch) is our upstream
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000323 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000324 newbase=revision, printed_path=printed_path)
325 printed_path = True
326 else:
327 # Can't find a merge-base since we don't know our upstream. That makes
328 # this command VERY likely to produce a rebase failure. For now we
329 # assume origin is our upstream since that's what the old behavior was.
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000330 upstream_branch = 'origin'
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000331 if options.revision or deps_revision:
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000332 upstream_branch = revision
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000333 self._AttemptRebase(upstream_branch, files, options,
334 printed_path=printed_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000335 printed_path = True
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000336 elif rev_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000337 # case 2
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000338 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000339 newbase=revision, printed_path=printed_path)
340 printed_path = True
341 elif revision.replace('heads', 'remotes/origin') != upstream_branch:
342 # case 4
343 new_base = revision.replace('heads', 'remotes/origin')
344 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000345 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000346 switch_error = ("Switching upstream branch from %s to %s\n"
347 % (upstream_branch, new_base) +
348 "Please merge or rebase manually:\n" +
349 "cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
350 "OR git checkout -b <some new branch> %s" % new_base)
351 raise gclient_utils.Error(switch_error)
352 else:
353 # case 3 - the default case
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000354 files = self._Capture(['diff', upstream_branch, '--name-only']).split()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000355 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000356 print('Trying fast-forward merge to branch : %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000357 try:
bauerb@chromium.org2aad1b22011-07-22 12:00:41 +0000358 merge_args = ['merge']
359 if not options.merge:
360 merge_args.append('--ff-only')
361 merge_args.append(upstream_branch)
362 merge_output = scm.GIT.Capture(merge_args, cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000363 except subprocess2.CalledProcessError, e:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000364 if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr):
365 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000366 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000367 printed_path = True
368 while True:
369 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000370 # TODO(maruel): That can't work with --jobs.
maruel@chromium.org90541732011-04-01 17:54:18 +0000371 action = ask_for_data(
372 'Cannot fast-forward merge, attempt to rebase? '
373 '(y)es / (q)uit / (s)kip : ')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000374 except ValueError:
maruel@chromium.org90541732011-04-01 17:54:18 +0000375 raise gclient_utils.Error('Invalid Character')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000376 if re.match(r'yes|y', action, re.I):
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000377 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000378 printed_path=printed_path)
379 printed_path = True
380 break
381 elif re.match(r'quit|q', action, re.I):
382 raise gclient_utils.Error("Can't fast-forward, please merge or "
383 "rebase manually.\n"
384 "cd %s && git " % self.checkout_path
385 + "rebase %s" % upstream_branch)
386 elif re.match(r'skip|s', action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000387 print('Skipping %s' % self.relpath)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000388 return
389 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000390 print('Input not recognized')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000391 elif re.match("error: Your local changes to '.*' would be "
392 "overwritten by merge. Aborting.\nPlease, commit your "
393 "changes or stash them before you can merge.\n",
394 e.stderr):
395 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000396 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000397 printed_path = True
398 raise gclient_utils.Error(e.stderr)
399 else:
400 # Some other problem happened with the merge
401 logging.error("Error during fast-forward merge in %s!" % self.relpath)
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000402 print(e.stderr)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000403 raise
404 else:
405 # Fast-forward merge was successful
406 if not re.match('Already up-to-date.', merge_output) or verbose:
407 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000408 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000409 printed_path = True
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000410 print(merge_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000411 if not verbose:
412 # Make the output a little prettier. It's nice to have some
413 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000414 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000415
416 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.org5bde4852009-12-14 16:47:12 +0000417
418 # If the rebase generated a conflict, abort and ask user to fix
msb@chromium.org786fb682010-06-02 15:16:23 +0000419 if self._IsRebasing():
msb@chromium.org5bde4852009-12-14 16:47:12 +0000420 raise gclient_utils.Error('\n____ %s%s\n'
421 '\nConflict while rebasing this branch.\n'
422 'Fix the conflict and run gclient again.\n'
423 'See man git-rebase for details.\n'
424 % (self.relpath, rev_str))
425
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000426 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000427 print('Checked out revision %s' % self.revinfo(options, (), None))
msb@chromium.orge28e4982009-09-25 20:51:45 +0000428
429 def revert(self, options, args, file_list):
430 """Reverts local modifications.
431
432 All reverted files will be appended to file_list.
433 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000434 if not os.path.isdir(self.checkout_path):
msb@chromium.org260c6532009-10-28 03:22:35 +0000435 # revert won't work if the directory doesn't exist. It needs to
436 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000437 print('\n_____ %s is missing, synching instead' % self.relpath)
msb@chromium.org260c6532009-10-28 03:22:35 +0000438 # Don't reuse the args.
439 return self.update(options, [], file_list)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000440
441 default_rev = "refs/heads/master"
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000442 _, deps_revision = gclient_utils.SplitUrlRevision(self.url)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000443 if not deps_revision:
444 deps_revision = default_rev
445 if deps_revision.startswith('refs/heads/'):
446 deps_revision = deps_revision.replace('refs/heads/', 'origin/')
447
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000448 files = self._Capture(['diff', deps_revision, '--name-only']).split()
maruel@chromium.org37e89872010-09-07 16:11:33 +0000449 self._Run(['reset', '--hard', deps_revision], options)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000450 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
451
msb@chromium.org0f282062009-11-06 20:14:02 +0000452 def revinfo(self, options, args, file_list):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000453 """Returns revision"""
454 return self._Capture(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000455
msb@chromium.orge28e4982009-09-25 20:51:45 +0000456 def runhooks(self, options, args, file_list):
457 self.status(options, args, file_list)
458
459 def status(self, options, args, file_list):
460 """Display status information."""
461 if not os.path.isdir(self.checkout_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000462 print(('\n________ couldn\'t run status in %s:\n'
463 'The directory does not exist.') % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000464 else:
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000465 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org37e89872010-09-07 16:11:33 +0000466 self._Run(['diff', '--name-status', merge_base], options)
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000467 files = self._Capture(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000468 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
469
msb@chromium.orge6f78352010-01-13 17:05:33 +0000470 def FullUrlForRelativeUrl(self, url):
471 # Strip from last '/'
472 # Equivalent to unix basename
473 base_url = self.url
474 return base_url[:base_url.rfind('/')] + url
475
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000476 def _Clone(self, revision, url, options):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000477 """Clone a git repository from the given URL.
478
msb@chromium.org786fb682010-06-02 15:16:23 +0000479 Once we've cloned the repo, we checkout a working branch if the specified
480 revision is a branch head. If it is a tag or a specific commit, then we
481 leave HEAD detached as it makes future updates simpler -- in this case the
482 user should first create a new branch or switch to an existing branch before
483 making changes in the repo."""
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000484 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000485 # git clone doesn't seem to insert a newline properly before printing
486 # to stdout
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000487 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000488
489 clone_cmd = ['clone']
msb@chromium.org786fb682010-06-02 15:16:23 +0000490 if revision.startswith('refs/heads/'):
491 clone_cmd.extend(['-b', revision.replace('refs/heads/', '')])
492 detach_head = False
493 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000494 detach_head = True
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000495 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000496 clone_cmd.append('--verbose')
497 clone_cmd.extend([url, self.checkout_path])
498
nsylvain@chromium.org328c3c72011-06-01 20:50:27 +0000499 # If the parent directory does not exist, Git clone on Windows will not
500 # create it, so we need to do it manually.
501 parent_dir = os.path.dirname(self.checkout_path)
502 if not os.path.exists(parent_dir):
503 os.makedirs(parent_dir)
504
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000505 for _ in range(3):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000506 try:
maruel@chromium.org37e89872010-09-07 16:11:33 +0000507 self._Run(clone_cmd, options, cwd=self._root_dir)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000508 break
maruel@chromium.org31cb48a2011-04-04 18:01:36 +0000509 except (gclient_utils.Error, subprocess2.CalledProcessError), e:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000510 # TODO(maruel): Hackish, should be fixed by moving _Run() to
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000511 # subprocess2.check_output().
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000512 # Too bad we don't have access to the actual output.
513 # We should check for "transfer closed with NNN bytes remaining to
514 # read". In the meantime, just make sure .git exists.
515 if (e.args[0] == 'git command clone returned 128' and
516 os.path.exists(os.path.join(self.checkout_path, '.git'))):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000517 print(str(e))
518 print('Retrying...')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000519 continue
520 raise e
521
msb@chromium.org786fb682010-06-02 15:16:23 +0000522 if detach_head:
523 # Squelch git's very verbose detached HEAD warning and use our own
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000524 self._Capture(['checkout', '--quiet', '%s' % revision])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000525 print(
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000526 ('Checked out %s to a detached HEAD. Before making any commits\n'
527 'in this repo, you should use \'git checkout <branch>\' to switch to\n'
528 'an existing branch or use \'git checkout origin -b <branch>\' to\n'
529 'create a new branch for your work.') % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000530
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000531 def _AttemptRebase(self, upstream, files, options, newbase=None,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000532 branch=None, printed_path=False):
533 """Attempt to rebase onto either upstream or, if specified, newbase."""
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000534 files.extend(self._Capture(['diff', upstream, '--name-only']).split())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000535 revision = upstream
536 if newbase:
537 revision = newbase
538 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000539 print('\n_____ %s : Attempting rebase onto %s...' % (
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000540 self.relpath, revision))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000541 printed_path = True
542 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000543 print('Attempting rebase onto %s...' % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000544
545 # Build the rebase command here using the args
546 # git rebase [options] [--onto <newbase>] <upstream> [<branch>]
547 rebase_cmd = ['rebase']
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000548 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000549 rebase_cmd.append('--verbose')
550 if newbase:
551 rebase_cmd.extend(['--onto', newbase])
552 rebase_cmd.append(upstream)
553 if branch:
554 rebase_cmd.append(branch)
555
556 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000557 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000558 except subprocess2.CalledProcessError, e:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000559 if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or
560 re.match(r'cannot rebase: your index contains uncommitted changes',
561 e.stderr)):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000562 while True:
maruel@chromium.org90541732011-04-01 17:54:18 +0000563 rebase_action = ask_for_data(
564 'Cannot rebase because of unstaged changes.\n'
565 '\'git reset --hard HEAD\' ?\n'
566 'WARNING: destroys any uncommitted work in your current branch!'
567 ' (y)es / (q)uit / (s)how : ')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000568 if re.match(r'yes|y', rebase_action, re.I):
maruel@chromium.org37e89872010-09-07 16:11:33 +0000569 self._Run(['reset', '--hard', 'HEAD'], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000570 # Should this be recursive?
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000571 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000572 break
573 elif re.match(r'quit|q', rebase_action, re.I):
574 raise gclient_utils.Error("Please merge or rebase manually\n"
575 "cd %s && git " % self.checkout_path
576 + "%s" % ' '.join(rebase_cmd))
577 elif re.match(r'show|s', rebase_action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000578 print('\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000579 continue
580 else:
581 gclient_utils.Error("Input not recognized")
582 continue
583 elif re.search(r'^CONFLICT', e.stdout, re.M):
584 raise gclient_utils.Error("Conflict while rebasing this branch.\n"
585 "Fix the conflict and run gclient again.\n"
586 "See 'man git-rebase' for details.\n")
587 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000588 print(e.stdout.strip())
589 print('Rebase produced error output:\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000590 raise gclient_utils.Error("Unrecognized error, please merge or rebase "
591 "manually.\ncd %s && git " %
592 self.checkout_path
593 + "%s" % ' '.join(rebase_cmd))
594
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000595 print(rebase_output.strip())
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000596 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000597 # Make the output a little prettier. It's nice to have some
598 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000599 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000600
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000601 @staticmethod
602 def _CheckMinVersion(min_version):
maruel@chromium.orgd0f854a2010-03-11 19:35:53 +0000603 (ok, current_version) = scm.GIT.AssertVersion(min_version)
604 if not ok:
605 raise gclient_utils.Error('git version %s < minimum required %s' %
606 (current_version, min_version))
msb@chromium.org923a0372009-12-11 20:42:43 +0000607
msb@chromium.org786fb682010-06-02 15:16:23 +0000608 def _IsRebasing(self):
609 # Check for any of REBASE-i/REBASE-m/REBASE/AM. Unfortunately git doesn't
610 # have a plumbing command to determine whether a rebase is in progress, so
611 # for now emualate (more-or-less) git-rebase.sh / git-completion.bash
612 g = os.path.join(self.checkout_path, '.git')
613 return (
614 os.path.isdir(os.path.join(g, "rebase-merge")) or
615 os.path.isdir(os.path.join(g, "rebase-apply")))
616
617 def _CheckClean(self, rev_str):
618 # Make sure the tree is clean; see git-rebase.sh for reference
619 try:
620 scm.GIT.Capture(['update-index', '--ignore-submodules', '--refresh'],
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000621 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000622 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000623 raise gclient_utils.Error('\n____ %s%s\n'
624 '\tYou have unstaged changes.\n'
625 '\tPlease commit, stash, or reset.\n'
626 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000627 try:
628 scm.GIT.Capture(['diff-index', '--cached', '--name-status', '-r',
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000629 '--ignore-submodules', 'HEAD', '--'],
630 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000631 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000632 raise gclient_utils.Error('\n____ %s%s\n'
633 '\tYour index contains uncommitted changes\n'
634 '\tPlease commit, stash, or reset.\n'
635 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000636
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000637 def _CheckDetachedHead(self, rev_str, options):
msb@chromium.org786fb682010-06-02 15:16:23 +0000638 # HEAD is detached. Make sure it is safe to move away from (i.e., it is
639 # reference by a commit). If not, error out -- most likely a rebase is
640 # in progress, try to detect so we can give a better error.
641 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000642 scm.GIT.Capture(['name-rev', '--no-undefined', 'HEAD'],
643 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000644 except subprocess2.CalledProcessError:
msb@chromium.org786fb682010-06-02 15:16:23 +0000645 # Commit is not contained by any rev. See if the user is rebasing:
646 if self._IsRebasing():
647 # Punt to the user
648 raise gclient_utils.Error('\n____ %s%s\n'
649 '\tAlready in a conflict, i.e. (no branch).\n'
650 '\tFix the conflict and run gclient again.\n'
651 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
652 '\tSee man git-rebase for details.\n'
653 % (self.relpath, rev_str))
654 # Let's just save off the commit so we can proceed.
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000655 name = ('saved-by-gclient-' +
656 self._Capture(['rev-parse', '--short', 'HEAD']))
657 self._Capture(['branch', name])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000658 print('\n_____ found an unreferenced commit and saved it as \'%s\'' %
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000659 name)
msb@chromium.org786fb682010-06-02 15:16:23 +0000660
msb@chromium.org5bde4852009-12-14 16:47:12 +0000661 def _GetCurrentBranch(self):
msb@chromium.org786fb682010-06-02 15:16:23 +0000662 # Returns name of current branch or None for detached HEAD
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000663 branch = self._Capture(['rev-parse', '--abbrev-ref=strict', 'HEAD'])
msb@chromium.org786fb682010-06-02 15:16:23 +0000664 if branch == 'HEAD':
msb@chromium.org5bde4852009-12-14 16:47:12 +0000665 return None
666 return branch
667
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000668 def _Capture(self, args):
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000669 return subprocess2.check_output(
670 ['git'] + args, cwd=self.checkout_path).strip()
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000671
maruel@chromium.org37e89872010-09-07 16:11:33 +0000672 def _Run(self, args, options, **kwargs):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000673 kwargs.setdefault('cwd', self.checkout_path)
maruel@chromium.org37e89872010-09-07 16:11:33 +0000674 gclient_utils.CheckCallAndFilterAndHeader(['git'] + args,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000675 always=options.verbose, **kwargs)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000676
677
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000678class SVNWrapper(SCMWrapper):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000679 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000680
floitsch@google.comeaab7842011-04-28 09:07:58 +0000681 def GetRevisionDate(self, revision):
682 """Returns the given revision's date in ISO-8601 format (which contains the
683 time zone)."""
684 date = scm.SVN.Capture(['propget', '--revprop', 'svn:date', '-r', revision,
685 os.path.join(self.checkout_path, '.')])
686 return date.strip()
687
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000688 def cleanup(self, options, args, file_list):
689 """Cleanup working copy."""
maruel@chromium.org669600d2010-09-01 19:06:31 +0000690 self._Run(['cleanup'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000691
692 def diff(self, options, args, file_list):
693 # NOTE: This function does not currently modify file_list.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000694 if not os.path.isdir(self.checkout_path):
695 raise gclient_utils.Error('Directory %s is not present.' %
696 self.checkout_path)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000697 self._Run(['diff'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000698
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000699 def pack(self, options, args, file_list):
700 """Generates a patch file which can be applied to the root of the
701 repository."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000702 if not os.path.isdir(self.checkout_path):
703 raise gclient_utils.Error('Directory %s is not present.' %
704 self.checkout_path)
705 gclient_utils.CheckCallAndFilter(
706 ['svn', 'diff', '-x', '--ignore-eol-style'] + args,
707 cwd=self.checkout_path,
708 print_stdout=False,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000709 filter_fn=DiffFilterer(self.relpath).Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000710
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000711 def update(self, options, args, file_list):
msb@chromium.orgd6504212010-01-13 17:34:31 +0000712 """Runs svn to update or transparently checkout the working copy.
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000713
714 All updated files will be appended to file_list.
715
716 Raises:
717 Error: if can't get URL for relative path.
718 """
morrita@chromium.org21dca0e2010-10-05 00:55:12 +0000719 # Only update if git or hg is not controlling the directory.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000720 git_path = os.path.join(self.checkout_path, '.git')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000721 if os.path.exists(git_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000722 print('________ found .git directory; skipping %s' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000723 return
724
morrita@chromium.org21dca0e2010-10-05 00:55:12 +0000725 hg_path = os.path.join(self.checkout_path, '.hg')
726 if os.path.exists(hg_path):
727 print('________ found .hg directory; skipping %s' % self.relpath)
728 return
729
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000730 if args:
731 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
732
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000733 # revision is the revision to match. It is None if no revision is specified,
734 # i.e. the 'deps ain't pinned'.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000735 url, revision = gclient_utils.SplitUrlRevision(self.url)
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000736 # Keep the original unpinned url for reference in case the repo is switched.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000737 base_url = url
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000738 if options.revision:
739 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000740 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000741 if revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000742 forced_revision = True
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000743 # Reconstruct the url.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000744 url = '%s@%s' % (url, revision)
msb@chromium.org770ff9e2009-09-23 17:18:18 +0000745 rev_str = ' at %s' % revision
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000746 else:
747 forced_revision = False
748 rev_str = ''
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000749
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000750 if not os.path.exists(self.checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000751 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000752 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000753 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000754 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000755 return
756
757 # Get the existing scm url and the revision number of the current checkout.
maruel@chromium.org54019f32010-09-09 13:50:11 +0000758 try:
759 from_info = scm.SVN.CaptureInfo(os.path.join(self.checkout_path, '.'))
maruel@chromium.org31cb48a2011-04-04 18:01:36 +0000760 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.org54019f32010-09-09 13:50:11 +0000761 raise gclient_utils.Error(
762 ('Can\'t update/checkout %s if an unversioned directory is present. '
763 'Delete the directory and try again.') % self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000764
maruel@chromium.orge407c9a2010-08-09 19:11:37 +0000765 # Look for locked directories.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000766 dir_info = scm.SVN.CaptureStatus(os.path.join(self.checkout_path, '.'))
767 if [True for d in dir_info
768 if d[0][2] == 'L' and d[1] == self.checkout_path]:
maruel@chromium.orge407c9a2010-08-09 19:11:37 +0000769 # The current directory is locked, clean it up.
maruel@chromium.org669600d2010-09-01 19:06:31 +0000770 self._Run(['cleanup'], options)
maruel@chromium.orge407c9a2010-08-09 19:11:37 +0000771
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000772 # Retrieve the current HEAD version because svn is slow at null updates.
773 if options.manually_grab_svn_rev and not revision:
maruel@chromium.org54019f32010-09-09 13:50:11 +0000774 from_info_live = scm.SVN.CaptureInfo(from_info['URL'])
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000775 revision = str(from_info_live['Revision'])
776 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000777
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000778 if from_info['URL'] != base_url:
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000779 # The repository url changed, need to switch.
maruel@chromium.org54019f32010-09-09 13:50:11 +0000780 try:
781 to_info = scm.SVN.CaptureInfo(url)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +0000782 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +0000783 # The url is invalid or the server is not accessible, it's safer to bail
784 # out right now.
785 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000786 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
787 and (from_info['UUID'] == to_info['UUID']))
788 if can_switch:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000789 print('\n_____ relocating %s to a new checkout' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000790 # We have different roots, so check if we can switch --relocate.
791 # Subversion only permits this if the repository UUIDs match.
792 # Perform the switch --relocate, then rewrite the from_url
793 # to reflect where we "are now." (This is the same way that
794 # Subversion itself handles the metadata when switch --relocate
795 # is used.) This makes the checks below for whether we
796 # can update to a revision or have to switch to a different
797 # branch work as expected.
798 # TODO(maruel): TEST ME !
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000799 command = ['switch', '--relocate',
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000800 from_info['Repository Root'],
801 to_info['Repository Root'],
802 self.relpath]
maruel@chromium.org669600d2010-09-01 19:06:31 +0000803 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000804 from_info['URL'] = from_info['URL'].replace(
805 from_info['Repository Root'],
806 to_info['Repository Root'])
807 else:
maruel@chromium.org3294f522010-08-18 19:54:57 +0000808 if not options.force and not options.reset:
maruel@chromium.org86f0f952010-08-10 17:17:19 +0000809 # Look for local modifications but ignore unversioned files.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000810 for status in scm.SVN.CaptureStatus(self.checkout_path):
maruel@chromium.org86f0f952010-08-10 17:17:19 +0000811 if status[0] != '?':
812 raise gclient_utils.Error(
813 ('Can\'t switch the checkout to %s; UUID don\'t match and '
814 'there is local changes in %s. Delete the directory and '
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000815 'try again.') % (url, self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000816 # Ok delete it.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000817 print('\n_____ switching %s to a new checkout' % self.relpath)
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000818 gclient_utils.RemoveDirectory(self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000819 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000820 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000821 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000822 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000823 return
824
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000825 # If the provided url has a revision number that matches the revision
826 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +0000827 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000828 if options.verbose or not forced_revision:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000829 print('\n_____ %s%s' % (self.relpath, rev_str))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000830 return
831
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000832 command = ['update', self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000833 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000834 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000835
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000836 def updatesingle(self, options, args, file_list):
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000837 filename = args.pop()
tony@chromium.org57564662010-04-14 02:35:12 +0000838 if scm.SVN.AssertVersion("1.5")[0]:
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000839 if not os.path.exists(os.path.join(self.checkout_path, '.svn')):
tony@chromium.org57564662010-04-14 02:35:12 +0000840 # Create an empty checkout and then update the one file we want. Future
841 # operations will only apply to the one file we checked out.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000842 command = ["checkout", "--depth", "empty", self.url, self.checkout_path]
maruel@chromium.org669600d2010-09-01 19:06:31 +0000843 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000844 if os.path.exists(os.path.join(self.checkout_path, filename)):
845 os.remove(os.path.join(self.checkout_path, filename))
tony@chromium.org57564662010-04-14 02:35:12 +0000846 command = ["update", filename]
maruel@chromium.org669600d2010-09-01 19:06:31 +0000847 self._RunAndGetFileList(command, options, file_list)
tony@chromium.org57564662010-04-14 02:35:12 +0000848 # After the initial checkout, we can use update as if it were any other
849 # dep.
850 self.update(options, args, file_list)
851 else:
852 # If the installed version of SVN doesn't support --depth, fallback to
853 # just exporting the file. This has the downside that revision
854 # information is not stored next to the file, so we will have to
855 # re-export the file every time we sync.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000856 if not os.path.exists(self.checkout_path):
857 os.makedirs(self.checkout_path)
tony@chromium.org57564662010-04-14 02:35:12 +0000858 command = ["export", os.path.join(self.url, filename),
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000859 os.path.join(self.checkout_path, filename)]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000860 command = self._AddAdditionalUpdateFlags(command, options,
861 options.revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000862 self._Run(command, options, cwd=self._root_dir)
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000863
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000864 def revert(self, options, args, file_list):
865 """Reverts local modifications. Subversion specific.
866
867 All reverted files will be appended to file_list, even if Subversion
868 doesn't know about them.
869 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000870 if not os.path.isdir(self.checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000871 # svn revert won't work if the directory doesn't exist. It needs to
872 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000873 print('\n_____ %s is missing, synching instead' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000874 # Don't reuse the args.
875 return self.update(options, [], file_list)
876
maruel@chromium.org07ab60e2011-02-08 21:54:00 +0000877 def printcb(file_status):
878 file_list.append(file_status[1])
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000879 if logging.getLogger().isEnabledFor(logging.INFO):
maruel@chromium.org07ab60e2011-02-08 21:54:00 +0000880 logging.info('%s%s' % (file_status[0], file_status[1]))
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000881 else:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +0000882 print(os.path.join(self.checkout_path, file_status[1]))
883 scm.SVN.Revert(self.checkout_path, callback=printcb)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +0000884
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000885 try:
886 # svn revert is so broken we don't even use it. Using
887 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org07ab60e2011-02-08 21:54:00 +0000888 # file_list will contain duplicates.
maruel@chromium.org669600d2010-09-01 19:06:31 +0000889 self._RunAndGetFileList(['update', '--revision', 'BASE'], options,
890 file_list)
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000891 except OSError, e:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +0000892 # Maybe the directory disapeared meanwhile. Do not throw an exception.
maruel@chromium.org810a50b2009-10-05 23:03:18 +0000893 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000894
msb@chromium.org0f282062009-11-06 20:14:02 +0000895 def revinfo(self, options, args, file_list):
896 """Display revision"""
maruel@chromium.org54019f32010-09-09 13:50:11 +0000897 try:
898 return scm.SVN.CaptureRevision(self.checkout_path)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +0000899 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.org54019f32010-09-09 13:50:11 +0000900 return None
msb@chromium.org0f282062009-11-06 20:14:02 +0000901
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000902 def runhooks(self, options, args, file_list):
903 self.status(options, args, file_list)
904
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000905 def status(self, options, args, file_list):
906 """Display status information."""
maruel@chromium.org669600d2010-09-01 19:06:31 +0000907 command = ['status'] + args
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000908 if not os.path.isdir(self.checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000909 # svn status won't work if the directory doesn't exist.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000910 print(('\n________ couldn\'t run \'%s\' in \'%s\':\n'
911 'The directory does not exist.') %
912 (' '.join(command), self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000913 # There's no file list to retrieve.
914 else:
maruel@chromium.org669600d2010-09-01 19:06:31 +0000915 self._RunAndGetFileList(command, options, file_list)
msb@chromium.orge6f78352010-01-13 17:05:33 +0000916
917 def FullUrlForRelativeUrl(self, url):
918 # Find the forth '/' and strip from there. A bit hackish.
919 return '/'.join(self.url.split('/')[:4]) + url
tony@chromium.org99828122010-06-04 01:41:02 +0000920
maruel@chromium.org669600d2010-09-01 19:06:31 +0000921 def _Run(self, args, options, **kwargs):
922 """Runs a commands that goes to stdout."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000923 kwargs.setdefault('cwd', self.checkout_path)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000924 gclient_utils.CheckCallAndFilterAndHeader(['svn'] + args,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000925 always=options.verbose, **kwargs)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000926
927 def _RunAndGetFileList(self, args, options, file_list, cwd=None):
928 """Runs a commands that goes to stdout and grabs the file listed."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000929 cwd = cwd or self.checkout_path
maruel@chromium.orgce117f62011-01-17 20:04:25 +0000930 scm.SVN.RunAndGetFileList(
931 options.verbose,
932 args + ['--ignore-externals'],
933 cwd=cwd,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000934 file_list=file_list)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000935
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000936 @staticmethod
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000937 def _AddAdditionalUpdateFlags(command, options, revision):
tony@chromium.org99828122010-06-04 01:41:02 +0000938 """Add additional flags to command depending on what options are set.
939 command should be a list of strings that represents an svn command.
940
941 This method returns a new list to be used as a command."""
942 new_command = command[:]
943 if revision:
944 new_command.extend(['--revision', str(revision).strip()])
945 # --force was added to 'svn update' in svn 1.5.
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000946 if ((options.force or options.manually_grab_svn_rev) and
947 scm.SVN.AssertVersion("1.5")[0]):
tony@chromium.org99828122010-06-04 01:41:02 +0000948 new_command.append('--force')
949 return new_command