blob: 5629446a69e02a06f9cfe1eebe187fd968e7826f [file] [log] [blame]
steveblock@chromium.org93567042012-02-15 01:02:26 +00001# Copyright (c) 2012 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
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000019class DiffFiltererWrapper(object):
20 """Simple base class which tracks which file is being diffed and
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000021 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."""
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000023 index_string = None
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000024 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("\\", "/")
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000031 self._current_file = None
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000032
maruel@chromium.org6e29d572010-06-04 17:32:20 +000033 def SetCurrentFile(self, current_file):
34 self._current_file = current_file
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000035
iannucci@chromium.org3830a672013-02-19 20:15:14 +000036 @property
37 def _replacement_file(self):
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000038 return posixpath.join(self._relpath, self._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
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000054class SvnDiffFilterer(DiffFiltererWrapper):
55 index_string = "Index: "
56
57
58class GitDiffFilterer(DiffFiltererWrapper):
59 index_string = "diff --git "
60
61 def SetCurrentFile(self, current_file):
62 # Get filename by parsing "a/<filename> b/<filename>"
63 self._current_file = current_file[:(len(current_file)/2)][2:]
64
65 def _Replace(self, line):
66 return re.sub("[a|b]/" + self._current_file, self._replacement_file, line)
67
68
maruel@chromium.org90541732011-04-01 17:54:18 +000069def ask_for_data(prompt):
70 try:
71 return raw_input(prompt)
72 except KeyboardInterrupt:
73 # Hide the exception.
74 sys.exit(1)
75
76
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000077### SCM abstraction layer
78
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000079# Factory Method for SCM wrapper creation
80
maruel@chromium.org9eda4112010-06-11 18:56:10 +000081def GetScmName(url):
82 if url:
83 url, _ = gclient_utils.SplitUrlRevision(url)
84 if (url.startswith('git://') or url.startswith('ssh://') or
igorgatis@gmail.com4e075672011-11-21 16:35:08 +000085 url.startswith('git+http://') or url.startswith('git+https://') or
maruel@chromium.org9eda4112010-06-11 18:56:10 +000086 url.endswith('.git')):
87 return 'git'
maruel@chromium.orgb74dca22010-06-11 20:10:40 +000088 elif (url.startswith('http://') or url.startswith('https://') or
maruel@chromium.org54a07a22010-06-14 19:07:39 +000089 url.startswith('svn://') or url.startswith('svn+ssh://')):
maruel@chromium.org9eda4112010-06-11 18:56:10 +000090 return 'svn'
91 return None
92
93
94def CreateSCM(url, root_dir=None, relpath=None):
95 SCM_MAP = {
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000096 'svn' : SVNWrapper,
msb@chromium.orge28e4982009-09-25 20:51:45 +000097 'git' : GitWrapper,
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000098 }
msb@chromium.orge28e4982009-09-25 20:51:45 +000099
maruel@chromium.org9eda4112010-06-11 18:56:10 +0000100 scm_name = GetScmName(url)
101 if not scm_name in SCM_MAP:
102 raise gclient_utils.Error('No SCM found for url %s' % url)
mukai@chromium.org9e3e82c2012-04-18 12:55:43 +0000103 scm_class = SCM_MAP[scm_name]
104 if not scm_class.BinaryExists():
105 raise gclient_utils.Error('%s command not found' % scm_name)
106 return scm_class(url, root_dir, relpath)
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000107
108
109# SCMWrapper base class
110
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000111class SCMWrapper(object):
112 """Add necessary glue between all the supported SCM.
113
msb@chromium.orgd6504212010-01-13 17:34:31 +0000114 This is the abstraction layer to bind to different SCM.
115 """
maruel@chromium.org9eda4112010-06-11 18:56:10 +0000116 def __init__(self, url=None, root_dir=None, relpath=None):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000117 self.url = url
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +0000118 self._root_dir = root_dir
119 if self._root_dir:
120 self._root_dir = self._root_dir.replace('/', os.sep)
121 self.relpath = relpath
122 if self.relpath:
123 self.relpath = self.relpath.replace('/', os.sep)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000124 if self.relpath and self._root_dir:
125 self.checkout_path = os.path.join(self._root_dir, self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000126
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000127 def RunCommand(self, command, options, args, file_list=None):
128 # file_list will have all files that are modified appended to it.
maruel@chromium.orgde754ac2009-09-17 18:04:50 +0000129 if file_list is None:
130 file_list = []
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000131
phajdan.jr@chromium.org6e043f72011-05-02 07:24:32 +0000132 commands = ['cleanup', 'update', 'updatesingle', 'revert',
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000133 'revinfo', 'status', 'diff', 'pack', 'runhooks']
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000134
135 if not command in commands:
136 raise gclient_utils.Error('Unknown command %s' % command)
137
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000138 if not command in dir(self):
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000139 raise gclient_utils.Error('Command %s not implemented in %s wrapper' % (
maruel@chromium.org9eda4112010-06-11 18:56:10 +0000140 command, self.__class__.__name__))
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000141
142 return getattr(self, command)(options, args, file_list)
143
144
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000145class GitWrapper(SCMWrapper):
msb@chromium.orge28e4982009-09-25 20:51:45 +0000146 """Wrapper for Git"""
147
igorgatis@gmail.com4e075672011-11-21 16:35:08 +0000148 def __init__(self, url=None, root_dir=None, relpath=None):
149 """Removes 'git+' fake prefix from git URL."""
150 if url.startswith('git+http://') or url.startswith('git+https://'):
151 url = url[4:]
152 SCMWrapper.__init__(self, url, root_dir, relpath)
153
mukai@chromium.org9e3e82c2012-04-18 12:55:43 +0000154 @staticmethod
155 def BinaryExists():
156 """Returns true if the command exists."""
157 try:
158 # We assume git is newer than 1.7. See: crbug.com/114483
159 result, version = scm.GIT.AssertVersion('1.7')
160 if not result:
161 raise gclient_utils.Error('Git version is older than 1.7: %s' % version)
162 return result
163 except OSError:
164 return False
165
floitsch@google.comeaab7842011-04-28 09:07:58 +0000166 def GetRevisionDate(self, revision):
167 """Returns the given revision's date in ISO-8601 format (which contains the
168 time zone)."""
169 # TODO(floitsch): get the time-stamp of the given revision and not just the
170 # time-stamp of the currently checked out revision.
171 return self._Capture(['log', '-n', '1', '--format=%ai'])
172
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000173 @staticmethod
174 def cleanup(options, args, file_list):
msb@chromium.orgd8a63782010-01-25 17:47:05 +0000175 """'Cleanup' the repo.
176
177 There's no real git equivalent for the svn cleanup command, do a no-op.
178 """
msb@chromium.orge28e4982009-09-25 20:51:45 +0000179
180 def diff(self, options, args, file_list):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000181 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org37e89872010-09-07 16:11:33 +0000182 self._Run(['diff', merge_base], options)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000183
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000184 def pack(self, options, args, file_list):
185 """Generates a patch file which can be applied to the root of the
msb@chromium.orgd6504212010-01-13 17:34:31 +0000186 repository.
187
188 The patch file is generated from a diff of the merge base of HEAD and
189 its upstream branch.
190 """
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000191 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org17d01792010-09-01 18:07:10 +0000192 gclient_utils.CheckCallAndFilter(
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000193 ['git', 'diff', merge_base],
194 cwd=self.checkout_path,
haitao.feng@intel.com306080c2012-05-04 13:11:29 +0000195 filter_fn=GitDiffFilterer(self.relpath).Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000196
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000197 def UpdateSubmoduleConfig(self):
198 submod_cmd = ['git', 'config', '-f', '$toplevel/.git/config',
199 'submodule.$name.ignore', '||',
200 'git', 'config', '-f', '$toplevel/.git/config',
szager@chromium.org37e4f232012-06-21 21:47:42 +0000201 'submodule.$name.ignore', 'all']
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000202 cmd = ['git', 'submodule', '--quiet', 'foreach', ' '.join(submod_cmd)]
szager@chromium.org78f5c162012-06-22 22:34:25 +0000203 cmd2 = ['git', 'config', 'diff.ignoreSubmodules', 'all']
szager@chromium.org987b0612012-07-09 23:41:08 +0000204 cmd3 = ['git', 'config', 'branch.autosetupmerge']
205 kwargs = {'cwd': self.checkout_path,
206 'print_stdout': False,
207 'filter_fn': lambda x: None}
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000208 try:
szager@chromium.org987b0612012-07-09 23:41:08 +0000209 gclient_utils.CheckCallAndFilter(cmd, **kwargs)
210 gclient_utils.CheckCallAndFilter(cmd2, **kwargs)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000211 except subprocess2.CalledProcessError:
212 # Not a fatal error, or even very interesting in a non-git-submodule
213 # world. So just keep it quiet.
214 pass
szager@chromium.org987b0612012-07-09 23:41:08 +0000215 try:
216 gclient_utils.CheckCallAndFilter(cmd3, **kwargs)
217 except subprocess2.CalledProcessError:
218 gclient_utils.CheckCallAndFilter(cmd3 + ['always'], **kwargs)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000219
msb@chromium.orge28e4982009-09-25 20:51:45 +0000220 def update(self, options, args, file_list):
221 """Runs git to update or transparently checkout the working copy.
222
223 All updated files will be appended to file_list.
224
225 Raises:
226 Error: if can't get URL for relative path.
227 """
msb@chromium.orge28e4982009-09-25 20:51:45 +0000228 if args:
229 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
230
nasser@codeaurora.orgece406f2010-02-23 17:29:15 +0000231 self._CheckMinVersion("1.6.6")
msb@chromium.org923a0372009-12-11 20:42:43 +0000232
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000233 default_rev = "refs/heads/master"
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000234 url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000235 rev_str = ""
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000236 revision = deps_revision
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000237 managed = True
msb@chromium.orge28e4982009-09-25 20:51:45 +0000238 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000239 # Override the revision number.
240 revision = str(options.revision)
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000241 if revision == 'unmanaged':
242 revision = None
243 managed = False
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000244 if not revision:
245 revision = default_rev
msb@chromium.orge28e4982009-09-25 20:51:45 +0000246
floitsch@google.comeaab7842011-04-28 09:07:58 +0000247 if gclient_utils.IsDateRevision(revision):
248 # Date-revisions only work on git-repositories if the reflog hasn't
249 # expired yet. Use rev-list to get the corresponding revision.
250 # git rev-list -n 1 --before='time-stamp' branchname
251 if options.transitive:
252 print('Warning: --transitive only works for SVN repositories.')
253 revision = default_rev
254
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000255 rev_str = ' at %s' % revision
256 files = []
257
258 printed_path = False
259 verbose = []
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000260 if options.verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000261 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000262 verbose = ['--verbose']
263 printed_path = True
264
265 if revision.startswith('refs/heads/'):
266 rev_type = "branch"
267 elif revision.startswith('origin/'):
268 # For compatability with old naming, translate 'origin' to 'refs/heads'
269 revision = revision.replace('origin/', 'refs/heads/')
270 rev_type = "branch"
271 else:
272 # hash is also a tag, only make a distinction at checkout
273 rev_type = "hash"
274
szager@google.com873e6672012-03-13 18:53:36 +0000275 if not os.path.exists(self.checkout_path) or (
276 os.path.isdir(self.checkout_path) and
277 not os.listdir(self.checkout_path)):
maruel@chromium.org6c48a302011-10-20 23:44:20 +0000278 gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path))
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000279 self._Clone(revision, url, options)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000280 self.UpdateSubmoduleConfig()
thomasvl@chromium.org858d6452011-03-24 17:59:20 +0000281 files = self._Capture(['ls-files']).splitlines()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000282 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000283 if not verbose:
284 # Make the output a little prettier. It's nice to have some whitespace
285 # between projects when cloning.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000286 print('')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000287 return
288
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000289 if not managed:
szager@chromium.orgf5cc4272012-06-21 22:38:07 +0000290 self.UpdateSubmoduleConfig()
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000291 print ('________ unmanaged solution; skipping %s' % self.relpath)
292 return
293
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000294 if not os.path.exists(os.path.join(self.checkout_path, '.git')):
295 raise gclient_utils.Error('\n____ %s%s\n'
296 '\tPath is not a git repo. No .git dir.\n'
297 '\tTo resolve:\n'
298 '\t\trm -rf %s\n'
299 '\tAnd run gclient sync again\n'
300 % (self.relpath, rev_str, self.relpath))
301
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000302 # See if the url has changed (the unittests use git://foo for the url, let
303 # that through).
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000304 current_url = self._Capture(['config', 'remote.origin.url'])
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000305 # TODO(maruel): Delete url != 'git://foo' since it's just to make the
306 # unit test pass. (and update the comment above)
307 if current_url != url and url != 'git://foo':
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000308 print('_____ switching %s to a new upstream' % self.relpath)
309 # Make sure it's clean
310 self._CheckClean(rev_str)
311 # Switch over to the new upstream
312 self._Run(['remote', 'set-url', 'origin', url], options)
313 quiet = []
314 if not options.verbose:
315 quiet = ['--quiet']
316 self._Run(['fetch', 'origin', '--prune'] + quiet, options)
bauerb@chromium.org610060e2012-11-19 14:11:35 +0000317 self._Run(['reset', '--hard', revision] + quiet, options)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000318 self.UpdateSubmoduleConfig()
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000319 files = self._Capture(['ls-files']).splitlines()
320 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
321 return
322
msb@chromium.org5bde4852009-12-14 16:47:12 +0000323 cur_branch = self._GetCurrentBranch()
324
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000325 # Cases:
msb@chromium.org786fb682010-06-02 15:16:23 +0000326 # 0) HEAD is detached. Probably from our initial clone.
327 # - make sure HEAD is contained by a named ref, then update.
328 # Cases 1-4. HEAD is a branch.
329 # 1) current branch is not tracking a remote branch (could be git-svn)
330 # - try to rebase onto the new hash or branch
331 # 2) current branch is tracking a remote branch with local committed
332 # changes, but the DEPS file switched to point to a hash
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000333 # - rebase those changes on top of the hash
msb@chromium.org786fb682010-06-02 15:16:23 +0000334 # 3) current branch is tracking a remote branch w/or w/out changes,
335 # no switch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000336 # - see if we can FF, if not, prompt the user for rebase, merge, or stop
msb@chromium.org786fb682010-06-02 15:16:23 +0000337 # 4) current branch is tracking a remote branch, switches to a different
338 # remote branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000339 # - exit
340
maruel@chromium.org81e012c2010-04-29 16:07:24 +0000341 # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for
342 # a tracking branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000343 # or 'master' if not a tracking branch (it's based on a specific rev/hash)
344 # or it returns None if it couldn't find an upstream
msb@chromium.org786fb682010-06-02 15:16:23 +0000345 if cur_branch is None:
346 upstream_branch = None
347 current_type = "detached"
348 logging.debug("Detached HEAD")
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000349 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000350 upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
351 if not upstream_branch or not upstream_branch.startswith('refs/remotes'):
352 current_type = "hash"
353 logging.debug("Current branch is not tracking an upstream (remote)"
354 " branch.")
355 elif upstream_branch.startswith('refs/remotes'):
356 current_type = "branch"
357 else:
358 raise gclient_utils.Error('Invalid Upstream: %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000359
maruel@chromium.org92067382012-06-28 19:58:41 +0000360 if (not re.match(r'^[0-9a-fA-F]{40}$', revision) or
361 not scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=revision)):
bauerb@chromium.orgcbd20a42012-06-27 13:49:27 +0000362 # Update the remotes first so we have all the refs.
363 backoff_time = 5
364 for _ in range(10):
365 try:
366 remote_output = scm.GIT.Capture(
367 ['remote'] + verbose + ['update'],
368 cwd=self.checkout_path)
369 break
370 except subprocess2.CalledProcessError, e:
371 # Hackish but at that point, git is known to work so just checking for
372 # 502 in stderr should be fine.
373 if '502' in e.stderr:
374 print(str(e))
375 print('Sleeping %.1f seconds and retrying...' % backoff_time)
376 time.sleep(backoff_time)
377 backoff_time *= 1.3
378 continue
379 raise
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000380
bauerb@chromium.orgcbd20a42012-06-27 13:49:27 +0000381 if verbose:
382 print(remote_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000383
384 # This is a big hammer, debatable if it should even be here...
davemoore@chromium.org793796d2010-02-19 17:27:41 +0000385 if options.force or options.reset:
maruel@chromium.org37e89872010-09-07 16:11:33 +0000386 self._Run(['reset', '--hard', 'HEAD'], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000387
msb@chromium.org786fb682010-06-02 15:16:23 +0000388 if current_type == 'detached':
389 # case 0
390 self._CheckClean(rev_str)
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000391 self._CheckDetachedHead(rev_str, options)
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000392 self._Capture(['checkout', '--quiet', '%s' % revision])
msb@chromium.org786fb682010-06-02 15:16:23 +0000393 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000394 print('\n_____ %s%s' % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000395 elif current_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000396 # case 1
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000397 if scm.GIT.IsGitSvn(self.checkout_path) and upstream_branch is not None:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000398 # Our git-svn branch (upstream_branch) is our upstream
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000399 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000400 newbase=revision, printed_path=printed_path)
401 printed_path = True
402 else:
403 # Can't find a merge-base since we don't know our upstream. That makes
404 # this command VERY likely to produce a rebase failure. For now we
405 # assume origin is our upstream since that's what the old behavior was.
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000406 upstream_branch = 'origin'
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000407 if options.revision or deps_revision:
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000408 upstream_branch = revision
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000409 self._AttemptRebase(upstream_branch, files, options,
410 printed_path=printed_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000411 printed_path = True
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000412 elif rev_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000413 # case 2
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000414 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000415 newbase=revision, printed_path=printed_path)
416 printed_path = True
417 elif revision.replace('heads', 'remotes/origin') != upstream_branch:
418 # case 4
419 new_base = revision.replace('heads', 'remotes/origin')
420 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000421 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000422 switch_error = ("Switching upstream branch from %s to %s\n"
423 % (upstream_branch, new_base) +
424 "Please merge or rebase manually:\n" +
425 "cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
426 "OR git checkout -b <some new branch> %s" % new_base)
427 raise gclient_utils.Error(switch_error)
428 else:
429 # case 3 - the default case
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000430 files = self._Capture(['diff', upstream_branch, '--name-only']).split()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000431 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000432 print('Trying fast-forward merge to branch : %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000433 try:
bauerb@chromium.org2aad1b22011-07-22 12:00:41 +0000434 merge_args = ['merge']
435 if not options.merge:
436 merge_args.append('--ff-only')
437 merge_args.append(upstream_branch)
438 merge_output = scm.GIT.Capture(merge_args, cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000439 except subprocess2.CalledProcessError, e:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000440 if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr):
441 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000442 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000443 printed_path = True
444 while True:
445 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000446 # TODO(maruel): That can't work with --jobs.
maruel@chromium.org90541732011-04-01 17:54:18 +0000447 action = ask_for_data(
448 'Cannot fast-forward merge, attempt to rebase? '
449 '(y)es / (q)uit / (s)kip : ')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000450 except ValueError:
maruel@chromium.org90541732011-04-01 17:54:18 +0000451 raise gclient_utils.Error('Invalid Character')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000452 if re.match(r'yes|y', action, re.I):
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000453 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000454 printed_path=printed_path)
455 printed_path = True
456 break
457 elif re.match(r'quit|q', action, re.I):
458 raise gclient_utils.Error("Can't fast-forward, please merge or "
459 "rebase manually.\n"
460 "cd %s && git " % self.checkout_path
461 + "rebase %s" % upstream_branch)
462 elif re.match(r'skip|s', action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000463 print('Skipping %s' % self.relpath)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000464 return
465 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000466 print('Input not recognized')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000467 elif re.match("error: Your local changes to '.*' would be "
468 "overwritten by merge. Aborting.\nPlease, commit your "
469 "changes or stash them before you can merge.\n",
470 e.stderr):
471 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000472 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000473 printed_path = True
474 raise gclient_utils.Error(e.stderr)
475 else:
476 # Some other problem happened with the merge
477 logging.error("Error during fast-forward merge in %s!" % self.relpath)
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000478 print(e.stderr)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000479 raise
480 else:
481 # Fast-forward merge was successful
482 if not re.match('Already up-to-date.', merge_output) or verbose:
483 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000484 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000485 printed_path = True
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000486 print(merge_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000487 if not verbose:
488 # Make the output a little prettier. It's nice to have some
489 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000490 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000491
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000492 self.UpdateSubmoduleConfig()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000493 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.org5bde4852009-12-14 16:47:12 +0000494
495 # If the rebase generated a conflict, abort and ask user to fix
msb@chromium.org786fb682010-06-02 15:16:23 +0000496 if self._IsRebasing():
msb@chromium.org5bde4852009-12-14 16:47:12 +0000497 raise gclient_utils.Error('\n____ %s%s\n'
498 '\nConflict while rebasing this branch.\n'
499 'Fix the conflict and run gclient again.\n'
500 'See man git-rebase for details.\n'
501 % (self.relpath, rev_str))
502
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000503 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000504 print('Checked out revision %s' % self.revinfo(options, (), None))
msb@chromium.orge28e4982009-09-25 20:51:45 +0000505
steveblock@chromium.org98e69452012-02-16 16:36:43 +0000506 # If --reset and --delete_unversioned_trees are specified, remove any
507 # untracked directories.
508 if options.reset and options.delete_unversioned_trees:
509 # GIT.CaptureStatus() uses 'dit diff' to compare to a specific SHA1 (the
510 # merge-base by default), so doesn't include untracked files. So we use
511 # 'git ls-files --directory --others --exclude-standard' here directly.
512 paths = scm.GIT.Capture(
513 ['ls-files', '--directory', '--others', '--exclude-standard'],
514 self.checkout_path)
515 for path in (p for p in paths.splitlines() if p.endswith('/')):
516 full_path = os.path.join(self.checkout_path, path)
517 if not os.path.islink(full_path):
518 print('\n_____ removing unversioned directory %s' % path)
519 gclient_utils.RemoveDirectory(full_path)
520
521
msb@chromium.orge28e4982009-09-25 20:51:45 +0000522 def revert(self, options, args, file_list):
523 """Reverts local modifications.
524
525 All reverted files will be appended to file_list.
526 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000527 if not os.path.isdir(self.checkout_path):
msb@chromium.org260c6532009-10-28 03:22:35 +0000528 # revert won't work if the directory doesn't exist. It needs to
529 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000530 print('\n_____ %s is missing, synching instead' % self.relpath)
msb@chromium.org260c6532009-10-28 03:22:35 +0000531 # Don't reuse the args.
532 return self.update(options, [], file_list)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000533
534 default_rev = "refs/heads/master"
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000535 _, deps_revision = gclient_utils.SplitUrlRevision(self.url)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000536 if not deps_revision:
537 deps_revision = default_rev
538 if deps_revision.startswith('refs/heads/'):
539 deps_revision = deps_revision.replace('refs/heads/', 'origin/')
540
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000541 files = self._Capture(['diff', deps_revision, '--name-only']).split()
maruel@chromium.org37e89872010-09-07 16:11:33 +0000542 self._Run(['reset', '--hard', deps_revision], options)
lliabraa@chromium.orgade83db2012-09-27 14:06:49 +0000543 self._Run(['clean', '-f', '-d'], options)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000544 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
545
msb@chromium.org0f282062009-11-06 20:14:02 +0000546 def revinfo(self, options, args, file_list):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000547 """Returns revision"""
548 return self._Capture(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000549
msb@chromium.orge28e4982009-09-25 20:51:45 +0000550 def runhooks(self, options, args, file_list):
551 self.status(options, args, file_list)
552
553 def status(self, options, args, file_list):
554 """Display status information."""
555 if not os.path.isdir(self.checkout_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000556 print(('\n________ couldn\'t run status in %s:\n'
557 'The directory does not exist.') % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000558 else:
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000559 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org37e89872010-09-07 16:11:33 +0000560 self._Run(['diff', '--name-status', merge_base], options)
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000561 files = self._Capture(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000562 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
563
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000564 def GetUsableRev(self, rev, options):
565 """Finds a useful revision for this repository.
566
567 If SCM is git-svn and the head revision is less than |rev|, git svn fetch
568 will be called on the source."""
569 sha1 = None
iannucci@chromium.org3830a672013-02-19 20:15:14 +0000570 if not os.path.isdir(self.checkout_path):
571 raise gclient_utils.Error(
572 ( 'We could not find a valid hash for safesync_url response "%s".\n'
573 'Safesync URLs with a git checkout currently require the repo to\n'
574 'be cloned without a safesync_url before adding the safesync_url.\n'
575 'For more info, see: '
576 'http://code.google.com/p/chromium/wiki/UsingNewGit'
577 '#Initial_checkout' ) % rev)
578 elif rev.isdigit() and len(rev) < 7:
579 # Handles an SVN rev. As an optimization, only verify an SVN revision as
580 # [0-9]{1,6} for now to avoid making a network request.
581 if scm.GIT.IsGitSvn(cwd=self.checkout_path):
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000582 local_head = scm.GIT.GetGitSvnHeadRev(cwd=self.checkout_path)
583 if not local_head or local_head < int(rev):
dbeam@chromium.org2a75fdb2012-02-15 01:32:57 +0000584 try:
585 logging.debug('Looking for git-svn configuration optimizations.')
586 if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
587 cwd=self.checkout_path):
588 scm.GIT.Capture(['fetch'], cwd=self.checkout_path)
589 except subprocess2.CalledProcessError:
590 logging.debug('git config --get svn-remote.svn.fetch failed, '
591 'ignoring possible optimization.')
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000592 if options.verbose:
593 print('Running git svn fetch. This might take a while.\n')
594 scm.GIT.Capture(['svn', 'fetch'], cwd=self.checkout_path)
szager@google.com312a6a42012-10-11 21:19:42 +0000595 try:
szager@chromium.orgc51def32012-10-15 18:50:37 +0000596 sha1 = scm.GIT.GetBlessedSha1ForSvnRev(
597 cwd=self.checkout_path, rev=rev)
szager@google.com312a6a42012-10-11 21:19:42 +0000598 except gclient_utils.Error, e:
599 sha1 = e.message
600 print('\nWarning: Could not find a git revision with accurate\n'
601 '.DEPS.git that maps to SVN revision %s. Sync-ing to\n'
602 'the closest sane git revision, which is:\n'
603 ' %s\n' % (rev, e.message))
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000604 if not sha1:
605 raise gclient_utils.Error(
606 ( 'It appears that either your git-svn remote is incorrectly\n'
607 'configured or the revision in your safesync_url is\n'
608 'higher than git-svn remote\'s HEAD as we couldn\'t find a\n'
609 'corresponding git hash for SVN rev %s.' ) % rev)
iannucci@chromium.org3830a672013-02-19 20:15:14 +0000610 else:
611 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
612 sha1 = rev
613 else:
614 # May exist in origin, but we don't have it yet, so fetch and look
615 # again.
616 scm.GIT.Capture(['fetch', 'origin'], cwd=self.checkout_path)
617 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
618 sha1 = rev
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000619
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000620 if not sha1:
621 raise gclient_utils.Error(
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000622 ( 'We could not find a valid hash for safesync_url response "%s".\n'
623 'Safesync URLs with a git checkout currently require a git-svn\n'
624 'remote or a safesync_url that provides git sha1s. Please add a\n'
625 'git-svn remote or change your safesync_url. For more info, see:\n'
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000626 'http://code.google.com/p/chromium/wiki/UsingNewGit'
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000627 '#Initial_checkout' ) % rev)
628
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000629 return sha1
630
msb@chromium.orge6f78352010-01-13 17:05:33 +0000631 def FullUrlForRelativeUrl(self, url):
632 # Strip from last '/'
633 # Equivalent to unix basename
634 base_url = self.url
635 return base_url[:base_url.rfind('/')] + url
636
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000637 def _Clone(self, revision, url, options):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000638 """Clone a git repository from the given URL.
639
msb@chromium.org786fb682010-06-02 15:16:23 +0000640 Once we've cloned the repo, we checkout a working branch if the specified
641 revision is a branch head. If it is a tag or a specific commit, then we
642 leave HEAD detached as it makes future updates simpler -- in this case the
643 user should first create a new branch or switch to an existing branch before
644 making changes in the repo."""
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000645 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000646 # git clone doesn't seem to insert a newline properly before printing
647 # to stdout
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000648 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000649
szager@google.com85d3e3a2011-10-07 17:12:00 +0000650 clone_cmd = ['clone', '--progress']
msb@chromium.org786fb682010-06-02 15:16:23 +0000651 if revision.startswith('refs/heads/'):
652 clone_cmd.extend(['-b', revision.replace('refs/heads/', '')])
653 detach_head = False
654 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000655 detach_head = True
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000656 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000657 clone_cmd.append('--verbose')
mmoss@chromium.org468ed062013-03-08 18:07:16 +0000658 # Don't assume 'with_branch_heads' is added by 'gclient sync' setup, since
659 # _Clone() can by reached in roundabout ways (e.g. 'gclient revert').
660 if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
661 clone_cmd.extend(['--config', 'remote.origin.fetch=+refs/branch-heads/*:'
662 'refs/remotes/branch-heads/*'])
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000663 clone_cmd.extend([url, self.checkout_path])
664
nsylvain@chromium.org328c3c72011-06-01 20:50:27 +0000665 # If the parent directory does not exist, Git clone on Windows will not
666 # create it, so we need to do it manually.
667 parent_dir = os.path.dirname(self.checkout_path)
668 if not os.path.exists(parent_dir):
maruel@chromium.org6c48a302011-10-20 23:44:20 +0000669 gclient_utils.safe_makedirs(parent_dir)
nsylvain@chromium.org328c3c72011-06-01 20:50:27 +0000670
szager@google.com85d3e3a2011-10-07 17:12:00 +0000671 percent_re = re.compile('.* ([0-9]{1,2})% .*')
672 def _GitFilter(line):
673 # git uses an escape sequence to clear the line; elide it.
674 esc = line.find(unichr(033))
675 if esc > -1:
676 line = line[:esc]
677 match = percent_re.match(line)
678 if not match or not int(match.group(1)) % 10:
679 print '%s' % line
680
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000681 for _ in range(3):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000682 try:
szager@google.com85d3e3a2011-10-07 17:12:00 +0000683 self._Run(clone_cmd, options, cwd=self._root_dir, filter_fn=_GitFilter,
684 print_stdout=False)
mmoss@chromium.org468ed062013-03-08 18:07:16 +0000685 # Update the "branch-heads" remote-tracking branches, since clone
686 # doesn't automatically fetch those, and we might need it to checkout a
687 # specific revision below.
688 if (hasattr(options, 'with_branch_heads') and
689 options.with_branch_heads):
690 fetch_cmd = ['fetch', 'origin']
691 if options.verbose:
692 fetch_cmd.append('--verbose')
693 self._Run(fetch_cmd, options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000694 break
maruel@chromium.org2a5b6a22011-09-09 14:03:12 +0000695 except subprocess2.CalledProcessError, e:
696 # Too bad we don't have access to the actual output yet.
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000697 # We should check for "transfer closed with NNN bytes remaining to
698 # read". In the meantime, just make sure .git exists.
maruel@chromium.org2a5b6a22011-09-09 14:03:12 +0000699 if (e.returncode == 128 and
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000700 os.path.exists(os.path.join(self.checkout_path, '.git'))):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000701 print(str(e))
702 print('Retrying...')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000703 continue
704 raise e
705
msb@chromium.org786fb682010-06-02 15:16:23 +0000706 if detach_head:
707 # Squelch git's very verbose detached HEAD warning and use our own
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000708 self._Capture(['checkout', '--quiet', '%s' % revision])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000709 print(
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000710 ('Checked out %s to a detached HEAD. Before making any commits\n'
711 'in this repo, you should use \'git checkout <branch>\' to switch to\n'
712 'an existing branch or use \'git checkout origin -b <branch>\' to\n'
713 'create a new branch for your work.') % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000714
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000715 def _AttemptRebase(self, upstream, files, options, newbase=None,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000716 branch=None, printed_path=False):
717 """Attempt to rebase onto either upstream or, if specified, newbase."""
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000718 files.extend(self._Capture(['diff', upstream, '--name-only']).split())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000719 revision = upstream
720 if newbase:
721 revision = newbase
722 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000723 print('\n_____ %s : Attempting rebase onto %s...' % (
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000724 self.relpath, revision))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000725 printed_path = True
726 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000727 print('Attempting rebase onto %s...' % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000728
729 # Build the rebase command here using the args
730 # git rebase [options] [--onto <newbase>] <upstream> [<branch>]
731 rebase_cmd = ['rebase']
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000732 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000733 rebase_cmd.append('--verbose')
734 if newbase:
735 rebase_cmd.extend(['--onto', newbase])
736 rebase_cmd.append(upstream)
737 if branch:
738 rebase_cmd.append(branch)
739
740 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000741 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000742 except subprocess2.CalledProcessError, e:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000743 if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or
744 re.match(r'cannot rebase: your index contains uncommitted changes',
745 e.stderr)):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000746 while True:
maruel@chromium.org90541732011-04-01 17:54:18 +0000747 rebase_action = ask_for_data(
748 'Cannot rebase because of unstaged changes.\n'
749 '\'git reset --hard HEAD\' ?\n'
750 'WARNING: destroys any uncommitted work in your current branch!'
751 ' (y)es / (q)uit / (s)how : ')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000752 if re.match(r'yes|y', rebase_action, re.I):
maruel@chromium.org37e89872010-09-07 16:11:33 +0000753 self._Run(['reset', '--hard', 'HEAD'], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000754 # Should this be recursive?
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000755 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000756 break
757 elif re.match(r'quit|q', rebase_action, re.I):
758 raise gclient_utils.Error("Please merge or rebase manually\n"
759 "cd %s && git " % self.checkout_path
760 + "%s" % ' '.join(rebase_cmd))
761 elif re.match(r'show|s', rebase_action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000762 print('\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000763 continue
764 else:
765 gclient_utils.Error("Input not recognized")
766 continue
767 elif re.search(r'^CONFLICT', e.stdout, re.M):
768 raise gclient_utils.Error("Conflict while rebasing this branch.\n"
769 "Fix the conflict and run gclient again.\n"
770 "See 'man git-rebase' for details.\n")
771 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000772 print(e.stdout.strip())
773 print('Rebase produced error output:\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000774 raise gclient_utils.Error("Unrecognized error, please merge or rebase "
775 "manually.\ncd %s && git " %
776 self.checkout_path
777 + "%s" % ' '.join(rebase_cmd))
778
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000779 print(rebase_output.strip())
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000780 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000781 # Make the output a little prettier. It's nice to have some
782 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000783 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000784
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000785 @staticmethod
786 def _CheckMinVersion(min_version):
maruel@chromium.orgd0f854a2010-03-11 19:35:53 +0000787 (ok, current_version) = scm.GIT.AssertVersion(min_version)
788 if not ok:
789 raise gclient_utils.Error('git version %s < minimum required %s' %
790 (current_version, min_version))
msb@chromium.org923a0372009-12-11 20:42:43 +0000791
msb@chromium.org786fb682010-06-02 15:16:23 +0000792 def _IsRebasing(self):
793 # Check for any of REBASE-i/REBASE-m/REBASE/AM. Unfortunately git doesn't
794 # have a plumbing command to determine whether a rebase is in progress, so
795 # for now emualate (more-or-less) git-rebase.sh / git-completion.bash
796 g = os.path.join(self.checkout_path, '.git')
797 return (
798 os.path.isdir(os.path.join(g, "rebase-merge")) or
799 os.path.isdir(os.path.join(g, "rebase-apply")))
800
801 def _CheckClean(self, rev_str):
802 # Make sure the tree is clean; see git-rebase.sh for reference
803 try:
804 scm.GIT.Capture(['update-index', '--ignore-submodules', '--refresh'],
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000805 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000806 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000807 raise gclient_utils.Error('\n____ %s%s\n'
808 '\tYou have unstaged changes.\n'
809 '\tPlease commit, stash, or reset.\n'
810 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000811 try:
812 scm.GIT.Capture(['diff-index', '--cached', '--name-status', '-r',
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000813 '--ignore-submodules', 'HEAD', '--'],
814 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000815 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000816 raise gclient_utils.Error('\n____ %s%s\n'
817 '\tYour index contains uncommitted changes\n'
818 '\tPlease commit, stash, or reset.\n'
819 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000820
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000821 def _CheckDetachedHead(self, rev_str, options):
msb@chromium.org786fb682010-06-02 15:16:23 +0000822 # HEAD is detached. Make sure it is safe to move away from (i.e., it is
823 # reference by a commit). If not, error out -- most likely a rebase is
824 # in progress, try to detect so we can give a better error.
825 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000826 scm.GIT.Capture(['name-rev', '--no-undefined', 'HEAD'],
827 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000828 except subprocess2.CalledProcessError:
msb@chromium.org786fb682010-06-02 15:16:23 +0000829 # Commit is not contained by any rev. See if the user is rebasing:
830 if self._IsRebasing():
831 # Punt to the user
832 raise gclient_utils.Error('\n____ %s%s\n'
833 '\tAlready in a conflict, i.e. (no branch).\n'
834 '\tFix the conflict and run gclient again.\n'
835 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
836 '\tSee man git-rebase for details.\n'
837 % (self.relpath, rev_str))
838 # Let's just save off the commit so we can proceed.
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000839 name = ('saved-by-gclient-' +
840 self._Capture(['rev-parse', '--short', 'HEAD']))
841 self._Capture(['branch', name])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000842 print('\n_____ found an unreferenced commit and saved it as \'%s\'' %
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000843 name)
msb@chromium.org786fb682010-06-02 15:16:23 +0000844
msb@chromium.org5bde4852009-12-14 16:47:12 +0000845 def _GetCurrentBranch(self):
msb@chromium.org786fb682010-06-02 15:16:23 +0000846 # Returns name of current branch or None for detached HEAD
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000847 branch = self._Capture(['rev-parse', '--abbrev-ref=strict', 'HEAD'])
msb@chromium.org786fb682010-06-02 15:16:23 +0000848 if branch == 'HEAD':
msb@chromium.org5bde4852009-12-14 16:47:12 +0000849 return None
850 return branch
851
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000852 def _Capture(self, args):
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000853 return subprocess2.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000854 ['git'] + args,
855 stderr=subprocess2.PIPE,
856 cwd=self.checkout_path).strip()
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000857
maruel@chromium.org37e89872010-09-07 16:11:33 +0000858 def _Run(self, args, options, **kwargs):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000859 kwargs.setdefault('cwd', self.checkout_path)
szager@google.com85d3e3a2011-10-07 17:12:00 +0000860 kwargs.setdefault('print_stdout', True)
861 stdout = kwargs.get('stdout', sys.stdout)
862 stdout.write('\n________ running \'git %s\' in \'%s\'\n' % (
863 ' '.join(args), kwargs['cwd']))
864 gclient_utils.CheckCallAndFilter(['git'] + args, **kwargs)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000865
866
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000867class SVNWrapper(SCMWrapper):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000868 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000869
mukai@chromium.org9e3e82c2012-04-18 12:55:43 +0000870 @staticmethod
871 def BinaryExists():
872 """Returns true if the command exists."""
873 try:
874 result, version = scm.SVN.AssertVersion('1.4')
875 if not result:
876 raise gclient_utils.Error('SVN version is older than 1.4: %s' % version)
877 return result
878 except OSError:
879 return False
880
floitsch@google.comeaab7842011-04-28 09:07:58 +0000881 def GetRevisionDate(self, revision):
882 """Returns the given revision's date in ISO-8601 format (which contains the
883 time zone)."""
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +0000884 date = scm.SVN.Capture(
885 ['propget', '--revprop', 'svn:date', '-r', revision],
886 os.path.join(self.checkout_path, '.'))
floitsch@google.comeaab7842011-04-28 09:07:58 +0000887 return date.strip()
888
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000889 def cleanup(self, options, args, file_list):
890 """Cleanup working copy."""
maruel@chromium.org669600d2010-09-01 19:06:31 +0000891 self._Run(['cleanup'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000892
893 def diff(self, options, args, file_list):
894 # NOTE: This function does not currently modify file_list.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000895 if not os.path.isdir(self.checkout_path):
896 raise gclient_utils.Error('Directory %s is not present.' %
897 self.checkout_path)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000898 self._Run(['diff'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000899
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000900 def pack(self, options, args, file_list):
901 """Generates a patch file which can be applied to the root of the
902 repository."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000903 if not os.path.isdir(self.checkout_path):
904 raise gclient_utils.Error('Directory %s is not present.' %
905 self.checkout_path)
906 gclient_utils.CheckCallAndFilter(
907 ['svn', 'diff', '-x', '--ignore-eol-style'] + args,
908 cwd=self.checkout_path,
909 print_stdout=False,
haitao.feng@intel.com306080c2012-05-04 13:11:29 +0000910 filter_fn=SvnDiffFilterer(self.relpath).Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000911
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000912 def update(self, options, args, file_list):
msb@chromium.orgd6504212010-01-13 17:34:31 +0000913 """Runs svn to update or transparently checkout the working copy.
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000914
915 All updated files will be appended to file_list.
916
917 Raises:
918 Error: if can't get URL for relative path.
919 """
morrita@chromium.org21dca0e2010-10-05 00:55:12 +0000920 # Only update if git or hg is not controlling the directory.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000921 git_path = os.path.join(self.checkout_path, '.git')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000922 if os.path.exists(git_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000923 print('________ found .git directory; skipping %s' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000924 return
925
morrita@chromium.org21dca0e2010-10-05 00:55:12 +0000926 hg_path = os.path.join(self.checkout_path, '.hg')
927 if os.path.exists(hg_path):
928 print('________ found .hg directory; skipping %s' % self.relpath)
929 return
930
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000931 if args:
932 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
933
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000934 # revision is the revision to match. It is None if no revision is specified,
935 # i.e. the 'deps ain't pinned'.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000936 url, revision = gclient_utils.SplitUrlRevision(self.url)
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000937 # Keep the original unpinned url for reference in case the repo is switched.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000938 base_url = url
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000939 managed = True
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000940 if options.revision:
941 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000942 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000943 if revision:
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000944 if revision != 'unmanaged':
945 forced_revision = True
946 # Reconstruct the url.
947 url = '%s@%s' % (url, revision)
948 rev_str = ' at %s' % revision
949 else:
950 managed = False
951 revision = None
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000952 else:
953 forced_revision = False
954 rev_str = ''
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000955
davidjames@chromium.org13349e22012-11-15 17:11:28 +0000956 # Get the existing scm url and the revision number of the current checkout.
957 exists = os.path.exists(self.checkout_path)
958 if exists and managed:
959 try:
960 from_info = scm.SVN.CaptureLocalInfo(
961 [], os.path.join(self.checkout_path, '.'))
962 except (gclient_utils.Error, subprocess2.CalledProcessError):
963 if options.reset and options.delete_unversioned_trees:
964 print 'Removing troublesome path %s' % self.checkout_path
965 gclient_utils.rmtree(self.checkout_path)
966 exists = False
967 else:
968 msg = ('Can\'t update/checkout %s if an unversioned directory is '
969 'present. Delete the directory and try again.')
970 raise gclient_utils.Error(msg % self.checkout_path)
971
972 if not exists:
maruel@chromium.org6c48a302011-10-20 23:44:20 +0000973 gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000974 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000975 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +0000976 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000977 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000978 return
979
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000980 if not managed:
981 print ('________ unmanaged solution; skipping %s' % self.relpath)
982 return
983
maruel@chromium.org49fcb0c2011-09-23 14:34:38 +0000984 if 'URL' not in from_info:
985 raise gclient_utils.Error(
986 ('gclient is confused. Couldn\'t get the url for %s.\n'
987 'Try using @unmanaged.\n%s') % (
988 self.checkout_path, from_info))
989
maruel@chromium.orge407c9a2010-08-09 19:11:37 +0000990 # Look for locked directories.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +0000991 dir_info = scm.SVN.CaptureStatus(
992 None, os.path.join(self.checkout_path, '.'))
phajdan.jr@chromium.orgd558c4b2011-09-22 18:56:24 +0000993 if any(d[0][2] == 'L' for d in dir_info):
994 try:
995 self._Run(['cleanup', self.checkout_path], options)
996 except subprocess2.CalledProcessError, e:
997 # Get the status again, svn cleanup may have cleaned up at least
998 # something.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +0000999 dir_info = scm.SVN.CaptureStatus(
1000 None, os.path.join(self.checkout_path, '.'))
phajdan.jr@chromium.orgd558c4b2011-09-22 18:56:24 +00001001
1002 # Try to fix the failures by removing troublesome files.
1003 for d in dir_info:
1004 if d[0][2] == 'L':
1005 if d[0][0] == '!' and options.force:
1006 print 'Removing troublesome path %s' % d[1]
1007 gclient_utils.rmtree(d[1])
1008 else:
1009 print 'Not removing troublesome path %s automatically.' % d[1]
1010 if d[0][0] == '!':
1011 print 'You can pass --force to enable automatic removal.'
1012 raise e
maruel@chromium.orge407c9a2010-08-09 19:11:37 +00001013
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001014 # Retrieve the current HEAD version because svn is slow at null updates.
1015 if options.manually_grab_svn_rev and not revision:
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001016 from_info_live = scm.SVN.CaptureRemoteInfo(from_info['URL'])
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001017 revision = str(from_info_live['Revision'])
1018 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001019
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001020 if from_info['URL'] != base_url:
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001021 # The repository url changed, need to switch.
maruel@chromium.org54019f32010-09-09 13:50:11 +00001022 try:
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001023 to_info = scm.SVN.CaptureRemoteInfo(url)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +00001024 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +00001025 # The url is invalid or the server is not accessible, it's safer to bail
1026 # out right now.
1027 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001028 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
1029 and (from_info['UUID'] == to_info['UUID']))
1030 if can_switch:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001031 print('\n_____ relocating %s to a new checkout' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001032 # We have different roots, so check if we can switch --relocate.
1033 # Subversion only permits this if the repository UUIDs match.
1034 # Perform the switch --relocate, then rewrite the from_url
1035 # to reflect where we "are now." (This is the same way that
1036 # Subversion itself handles the metadata when switch --relocate
1037 # is used.) This makes the checks below for whether we
1038 # can update to a revision or have to switch to a different
1039 # branch work as expected.
1040 # TODO(maruel): TEST ME !
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001041 command = ['switch', '--relocate',
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001042 from_info['Repository Root'],
1043 to_info['Repository Root'],
1044 self.relpath]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001045 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001046 from_info['URL'] = from_info['URL'].replace(
1047 from_info['Repository Root'],
1048 to_info['Repository Root'])
1049 else:
maruel@chromium.org3294f522010-08-18 19:54:57 +00001050 if not options.force and not options.reset:
maruel@chromium.org86f0f952010-08-10 17:17:19 +00001051 # Look for local modifications but ignore unversioned files.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001052 for status in scm.SVN.CaptureStatus(None, self.checkout_path):
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001053 if status[0][0] != '?':
maruel@chromium.org86f0f952010-08-10 17:17:19 +00001054 raise gclient_utils.Error(
1055 ('Can\'t switch the checkout to %s; UUID don\'t match and '
1056 'there is local changes in %s. Delete the directory and '
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001057 'try again.') % (url, self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001058 # Ok delete it.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001059 print('\n_____ switching %s to a new checkout' % self.relpath)
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001060 gclient_utils.RemoveDirectory(self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001061 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001062 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001063 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001064 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001065 return
1066
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001067 # If the provided url has a revision number that matches the revision
1068 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +00001069 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001070 if options.verbose or not forced_revision:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001071 print('\n_____ %s%s' % (self.relpath, rev_str))
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001072 else:
1073 command = ['update', self.checkout_path]
1074 command = self._AddAdditionalUpdateFlags(command, options, revision)
1075 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001076
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001077 # If --reset and --delete_unversioned_trees are specified, remove any
1078 # untracked files and directories.
1079 if options.reset and options.delete_unversioned_trees:
1080 for status in scm.SVN.CaptureStatus(None, self.checkout_path):
1081 full_path = os.path.join(self.checkout_path, status[1])
1082 if (status[0][0] == '?'
1083 and os.path.isdir(full_path)
1084 and not os.path.islink(full_path)):
1085 print('\n_____ removing unversioned directory %s' % status[1])
1086 gclient_utils.RemoveDirectory(full_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001087
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001088 def updatesingle(self, options, args, file_list):
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001089 filename = args.pop()
tony@chromium.org57564662010-04-14 02:35:12 +00001090 if scm.SVN.AssertVersion("1.5")[0]:
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001091 if not os.path.exists(os.path.join(self.checkout_path, '.svn')):
tony@chromium.org57564662010-04-14 02:35:12 +00001092 # Create an empty checkout and then update the one file we want. Future
1093 # operations will only apply to the one file we checked out.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001094 command = ["checkout", "--depth", "empty", self.url, self.checkout_path]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001095 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001096 if os.path.exists(os.path.join(self.checkout_path, filename)):
1097 os.remove(os.path.join(self.checkout_path, filename))
tony@chromium.org57564662010-04-14 02:35:12 +00001098 command = ["update", filename]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001099 self._RunAndGetFileList(command, options, file_list)
tony@chromium.org57564662010-04-14 02:35:12 +00001100 # After the initial checkout, we can use update as if it were any other
1101 # dep.
1102 self.update(options, args, file_list)
1103 else:
1104 # If the installed version of SVN doesn't support --depth, fallback to
1105 # just exporting the file. This has the downside that revision
1106 # information is not stored next to the file, so we will have to
1107 # re-export the file every time we sync.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001108 if not os.path.exists(self.checkout_path):
maruel@chromium.org6c48a302011-10-20 23:44:20 +00001109 gclient_utils.safe_makedirs(self.checkout_path)
tony@chromium.org57564662010-04-14 02:35:12 +00001110 command = ["export", os.path.join(self.url, filename),
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001111 os.path.join(self.checkout_path, filename)]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001112 command = self._AddAdditionalUpdateFlags(command, options,
1113 options.revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001114 self._Run(command, options, cwd=self._root_dir)
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001115
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001116 def revert(self, options, args, file_list):
1117 """Reverts local modifications. Subversion specific.
1118
1119 All reverted files will be appended to file_list, even if Subversion
1120 doesn't know about them.
1121 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001122 if not os.path.isdir(self.checkout_path):
maruel@chromium.orgc0cc0872011-10-12 17:02:41 +00001123 if os.path.exists(self.checkout_path):
1124 gclient_utils.rmtree(self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001125 # svn revert won't work if the directory doesn't exist. It needs to
1126 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001127 print('\n_____ %s is missing, synching instead' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001128 # Don't reuse the args.
1129 return self.update(options, [], file_list)
1130
maruel@chromium.orgc0cc0872011-10-12 17:02:41 +00001131 if not os.path.isdir(os.path.join(self.checkout_path, '.svn')):
1132 if os.path.isdir(os.path.join(self.checkout_path, '.git')):
1133 print('________ found .git directory; skipping %s' % self.relpath)
1134 return
1135 if os.path.isdir(os.path.join(self.checkout_path, '.hg')):
1136 print('________ found .hg directory; skipping %s' % self.relpath)
1137 return
1138 if not options.force:
1139 raise gclient_utils.Error('Invalid checkout path, aborting')
1140 print(
1141 '\n_____ %s is not a valid svn checkout, synching instead' %
1142 self.relpath)
1143 gclient_utils.rmtree(self.checkout_path)
1144 # Don't reuse the args.
1145 return self.update(options, [], file_list)
1146
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001147 def printcb(file_status):
1148 file_list.append(file_status[1])
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001149 if logging.getLogger().isEnabledFor(logging.INFO):
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001150 logging.info('%s%s' % (file_status[0], file_status[1]))
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001151 else:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001152 print(os.path.join(self.checkout_path, file_status[1]))
1153 scm.SVN.Revert(self.checkout_path, callback=printcb)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001154
maruel@chromium.org8b322b32011-11-01 19:05:50 +00001155 # Revert() may delete the directory altogether.
1156 if not os.path.isdir(self.checkout_path):
1157 # Don't reuse the args.
1158 return self.update(options, [], file_list)
1159
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001160 try:
1161 # svn revert is so broken we don't even use it. Using
1162 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001163 # file_list will contain duplicates.
maruel@chromium.org669600d2010-09-01 19:06:31 +00001164 self._RunAndGetFileList(['update', '--revision', 'BASE'], options,
1165 file_list)
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001166 except OSError, e:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001167 # Maybe the directory disapeared meanwhile. Do not throw an exception.
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001168 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001169
msb@chromium.org0f282062009-11-06 20:14:02 +00001170 def revinfo(self, options, args, file_list):
1171 """Display revision"""
maruel@chromium.org54019f32010-09-09 13:50:11 +00001172 try:
1173 return scm.SVN.CaptureRevision(self.checkout_path)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +00001174 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.org54019f32010-09-09 13:50:11 +00001175 return None
msb@chromium.org0f282062009-11-06 20:14:02 +00001176
msb@chromium.orgcb5442b2009-09-22 16:51:24 +00001177 def runhooks(self, options, args, file_list):
1178 self.status(options, args, file_list)
1179
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001180 def status(self, options, args, file_list):
1181 """Display status information."""
maruel@chromium.org669600d2010-09-01 19:06:31 +00001182 command = ['status'] + args
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001183 if not os.path.isdir(self.checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001184 # svn status won't work if the directory doesn't exist.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001185 print(('\n________ couldn\'t run \'%s\' in \'%s\':\n'
1186 'The directory does not exist.') %
1187 (' '.join(command), self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001188 # There's no file list to retrieve.
1189 else:
maruel@chromium.org669600d2010-09-01 19:06:31 +00001190 self._RunAndGetFileList(command, options, file_list)
msb@chromium.orge6f78352010-01-13 17:05:33 +00001191
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +00001192 def GetUsableRev(self, rev, options):
1193 """Verifies the validity of the revision for this repository."""
1194 if not scm.SVN.IsValidRevision(url='%s@%s' % (self.url, rev)):
1195 raise gclient_utils.Error(
1196 ( '%s isn\'t a valid revision. Please check that your safesync_url is\n'
1197 'correct.') % rev)
1198 return rev
1199
msb@chromium.orge6f78352010-01-13 17:05:33 +00001200 def FullUrlForRelativeUrl(self, url):
1201 # Find the forth '/' and strip from there. A bit hackish.
1202 return '/'.join(self.url.split('/')[:4]) + url
tony@chromium.org99828122010-06-04 01:41:02 +00001203
maruel@chromium.org669600d2010-09-01 19:06:31 +00001204 def _Run(self, args, options, **kwargs):
1205 """Runs a commands that goes to stdout."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001206 kwargs.setdefault('cwd', self.checkout_path)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001207 gclient_utils.CheckCallAndFilterAndHeader(['svn'] + args,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001208 always=options.verbose, **kwargs)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001209
1210 def _RunAndGetFileList(self, args, options, file_list, cwd=None):
1211 """Runs a commands that goes to stdout and grabs the file listed."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001212 cwd = cwd or self.checkout_path
maruel@chromium.orgce117f62011-01-17 20:04:25 +00001213 scm.SVN.RunAndGetFileList(
1214 options.verbose,
1215 args + ['--ignore-externals'],
1216 cwd=cwd,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001217 file_list=file_list)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001218
maruel@chromium.org6e29d572010-06-04 17:32:20 +00001219 @staticmethod
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001220 def _AddAdditionalUpdateFlags(command, options, revision):
tony@chromium.org99828122010-06-04 01:41:02 +00001221 """Add additional flags to command depending on what options are set.
1222 command should be a list of strings that represents an svn command.
1223
1224 This method returns a new list to be used as a command."""
1225 new_command = command[:]
1226 if revision:
1227 new_command.extend(['--revision', str(revision).strip()])
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001228 # We don't want interaction when jobs are used.
1229 if options.jobs > 1:
1230 new_command.append('--non-interactive')
tony@chromium.org99828122010-06-04 01:41:02 +00001231 # --force was added to 'svn update' in svn 1.5.
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001232 # --accept was added to 'svn update' in svn 1.6.
1233 if not scm.SVN.AssertVersion('1.5')[0]:
1234 return new_command
1235
1236 # It's annoying to have it block in the middle of a sync, just sensible
1237 # defaults.
1238 if options.force:
tony@chromium.org99828122010-06-04 01:41:02 +00001239 new_command.append('--force')
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001240 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1241 new_command.extend(('--accept', 'theirs-conflict'))
1242 elif options.manually_grab_svn_rev:
1243 new_command.append('--force')
1244 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1245 new_command.extend(('--accept', 'postpone'))
1246 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1247 new_command.extend(('--accept', 'postpone'))
tony@chromium.org99828122010-06-04 01:41:02 +00001248 return new_command