blob: 2cdee7df8a1d7823678c9b19048890fe5c04f2c6 [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
szager@chromium.org71cbb502013-04-19 23:30:15 +000019THIS_FILE_PATH = os.path.abspath(__file__)
20
21
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000022class DiffFiltererWrapper(object):
23 """Simple base class which tracks which file is being diffed and
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000024 replaces instances of its file name in the original and
msb@chromium.orgd6504212010-01-13 17:34:31 +000025 working copy lines of the svn/git diff output."""
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000026 index_string = None
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000027 original_prefix = "--- "
28 working_prefix = "+++ "
29
maruel@chromium.org77e4eca2010-09-21 13:23:07 +000030 def __init__(self, relpath):
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000031 # Note that we always use '/' as the path separator to be
32 # consistent with svn's cygwin-style output on Windows
33 self._relpath = relpath.replace("\\", "/")
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000034 self._current_file = None
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000035
maruel@chromium.org6e29d572010-06-04 17:32:20 +000036 def SetCurrentFile(self, current_file):
37 self._current_file = current_file
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000038
iannucci@chromium.org3830a672013-02-19 20:15:14 +000039 @property
40 def _replacement_file(self):
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000041 return posixpath.join(self._relpath, self._current_file)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000042
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +000043 def _Replace(self, line):
44 return line.replace(self._current_file, self._replacement_file)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000045
46 def Filter(self, line):
47 if (line.startswith(self.index_string)):
48 self.SetCurrentFile(line[len(self.index_string):])
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +000049 line = self._Replace(line)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000050 else:
51 if (line.startswith(self.original_prefix) or
52 line.startswith(self.working_prefix)):
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +000053 line = self._Replace(line)
maruel@chromium.org77e4eca2010-09-21 13:23:07 +000054 print(line)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +000055
56
haitao.feng@intel.com306080c2012-05-04 13:11:29 +000057class SvnDiffFilterer(DiffFiltererWrapper):
58 index_string = "Index: "
59
60
61class GitDiffFilterer(DiffFiltererWrapper):
62 index_string = "diff --git "
63
64 def SetCurrentFile(self, current_file):
65 # Get filename by parsing "a/<filename> b/<filename>"
66 self._current_file = current_file[:(len(current_file)/2)][2:]
67
68 def _Replace(self, line):
69 return re.sub("[a|b]/" + self._current_file, self._replacement_file, line)
70
71
bratell@opera.com18fa4542013-05-21 13:30:46 +000072def ask_for_data(prompt, options):
73 if options.jobs > 1:
74 raise gclient_utils.Error("Background task requires input. Rerun "
75 "gclient with --jobs=1 so that\n"
76 "interaction is possible.")
maruel@chromium.org90541732011-04-01 17:54:18 +000077 try:
78 return raw_input(prompt)
79 except KeyboardInterrupt:
80 # Hide the exception.
81 sys.exit(1)
82
83
maruel@chromium.org5f3eee32009-09-17 00:34:30 +000084### SCM abstraction layer
85
msb@chromium.orgcb5442b2009-09-22 16:51:24 +000086# Factory Method for SCM wrapper creation
87
maruel@chromium.org9eda4112010-06-11 18:56:10 +000088def GetScmName(url):
89 if url:
90 url, _ = gclient_utils.SplitUrlRevision(url)
91 if (url.startswith('git://') or url.startswith('ssh://') or
igorgatis@gmail.com4e075672011-11-21 16:35:08 +000092 url.startswith('git+http://') or url.startswith('git+https://') or
maruel@chromium.org9eda4112010-06-11 18:56:10 +000093 url.endswith('.git')):
94 return 'git'
maruel@chromium.orgb74dca22010-06-11 20:10:40 +000095 elif (url.startswith('http://') or url.startswith('https://') or
maruel@chromium.org54a07a22010-06-14 19:07:39 +000096 url.startswith('svn://') or url.startswith('svn+ssh://')):
maruel@chromium.org9eda4112010-06-11 18:56:10 +000097 return 'svn'
98 return None
99
100
101def CreateSCM(url, root_dir=None, relpath=None):
102 SCM_MAP = {
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000103 'svn' : SVNWrapper,
msb@chromium.orge28e4982009-09-25 20:51:45 +0000104 'git' : GitWrapper,
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000105 }
msb@chromium.orge28e4982009-09-25 20:51:45 +0000106
maruel@chromium.org9eda4112010-06-11 18:56:10 +0000107 scm_name = GetScmName(url)
108 if not scm_name in SCM_MAP:
109 raise gclient_utils.Error('No SCM found for url %s' % url)
mukai@chromium.org9e3e82c2012-04-18 12:55:43 +0000110 scm_class = SCM_MAP[scm_name]
111 if not scm_class.BinaryExists():
112 raise gclient_utils.Error('%s command not found' % scm_name)
113 return scm_class(url, root_dir, relpath)
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000114
115
116# SCMWrapper base class
117
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000118class SCMWrapper(object):
119 """Add necessary glue between all the supported SCM.
120
msb@chromium.orgd6504212010-01-13 17:34:31 +0000121 This is the abstraction layer to bind to different SCM.
122 """
szager@chromium.org12b07e72013-05-03 22:06:34 +0000123 nag_timer = 30
szager@chromium.org41da24b2013-05-23 19:37:04 +0000124 nag_max = 6
szager@chromium.org12b07e72013-05-03 22:06:34 +0000125
maruel@chromium.org9eda4112010-06-11 18:56:10 +0000126 def __init__(self, url=None, root_dir=None, relpath=None):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000127 self.url = url
maruel@chromium.org5e73b0c2009-09-18 19:47:48 +0000128 self._root_dir = root_dir
129 if self._root_dir:
130 self._root_dir = self._root_dir.replace('/', os.sep)
131 self.relpath = relpath
132 if self.relpath:
133 self.relpath = self.relpath.replace('/', os.sep)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000134 if self.relpath and self._root_dir:
135 self.checkout_path = os.path.join(self._root_dir, self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000136
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000137 def RunCommand(self, command, options, args, file_list=None):
138 # file_list will have all files that are modified appended to it.
maruel@chromium.orgde754ac2009-09-17 18:04:50 +0000139 if file_list is None:
140 file_list = []
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000141
phajdan.jr@chromium.org6e043f72011-05-02 07:24:32 +0000142 commands = ['cleanup', 'update', 'updatesingle', 'revert',
tony@chromium.org4b5b1772010-04-08 01:52:56 +0000143 'revinfo', 'status', 'diff', 'pack', 'runhooks']
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000144
145 if not command in commands:
146 raise gclient_utils.Error('Unknown command %s' % command)
147
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000148 if not command in dir(self):
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000149 raise gclient_utils.Error('Command %s not implemented in %s wrapper' % (
maruel@chromium.org9eda4112010-06-11 18:56:10 +0000150 command, self.__class__.__name__))
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000151
152 return getattr(self, command)(options, args, file_list)
153
154
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000155class GitWrapper(SCMWrapper):
msb@chromium.orge28e4982009-09-25 20:51:45 +0000156 """Wrapper for Git"""
157
igorgatis@gmail.com4e075672011-11-21 16:35:08 +0000158 def __init__(self, url=None, root_dir=None, relpath=None):
159 """Removes 'git+' fake prefix from git URL."""
160 if url.startswith('git+http://') or url.startswith('git+https://'):
161 url = url[4:]
162 SCMWrapper.__init__(self, url, root_dir, relpath)
163
mukai@chromium.org9e3e82c2012-04-18 12:55:43 +0000164 @staticmethod
165 def BinaryExists():
166 """Returns true if the command exists."""
167 try:
168 # We assume git is newer than 1.7. See: crbug.com/114483
169 result, version = scm.GIT.AssertVersion('1.7')
170 if not result:
171 raise gclient_utils.Error('Git version is older than 1.7: %s' % version)
172 return result
173 except OSError:
174 return False
175
xusydoc@chromium.org885a9602013-05-31 09:54:40 +0000176 def GetCheckoutRoot(self):
177 return scm.GIT.GetCheckoutRoot(self.checkout_path)
178
floitsch@google.comeaab7842011-04-28 09:07:58 +0000179 def GetRevisionDate(self, revision):
180 """Returns the given revision's date in ISO-8601 format (which contains the
181 time zone)."""
182 # TODO(floitsch): get the time-stamp of the given revision and not just the
183 # time-stamp of the currently checked out revision.
184 return self._Capture(['log', '-n', '1', '--format=%ai'])
185
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000186 @staticmethod
187 def cleanup(options, args, file_list):
msb@chromium.orgd8a63782010-01-25 17:47:05 +0000188 """'Cleanup' the repo.
189
190 There's no real git equivalent for the svn cleanup command, do a no-op.
191 """
msb@chromium.orge28e4982009-09-25 20:51:45 +0000192
193 def diff(self, options, args, file_list):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000194 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org37e89872010-09-07 16:11:33 +0000195 self._Run(['diff', merge_base], options)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000196
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000197 def pack(self, options, args, file_list):
198 """Generates a patch file which can be applied to the root of the
msb@chromium.orgd6504212010-01-13 17:34:31 +0000199 repository.
200
201 The patch file is generated from a diff of the merge base of HEAD and
202 its upstream branch.
203 """
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000204 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org17d01792010-09-01 18:07:10 +0000205 gclient_utils.CheckCallAndFilter(
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000206 ['git', 'diff', merge_base],
207 cwd=self.checkout_path,
szager@chromium.org12b07e72013-05-03 22:06:34 +0000208 nag_timer=self.nag_timer,
209 nag_max=self.nag_max,
haitao.feng@intel.com306080c2012-05-04 13:11:29 +0000210 filter_fn=GitDiffFilterer(self.relpath).Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000211
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000212 def UpdateSubmoduleConfig(self):
213 submod_cmd = ['git', 'config', '-f', '$toplevel/.git/config',
214 'submodule.$name.ignore', '||',
215 'git', 'config', '-f', '$toplevel/.git/config',
szager@chromium.org37e4f232012-06-21 21:47:42 +0000216 'submodule.$name.ignore', 'all']
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000217 cmd = ['git', 'submodule', '--quiet', 'foreach', ' '.join(submod_cmd)]
szager@chromium.org78f5c162012-06-22 22:34:25 +0000218 cmd2 = ['git', 'config', 'diff.ignoreSubmodules', 'all']
szager@chromium.org987b0612012-07-09 23:41:08 +0000219 cmd3 = ['git', 'config', 'branch.autosetupmerge']
iannucci@chromium.org08b21bf2013-04-05 03:38:10 +0000220 cmd4 = ['git', 'config', 'fetch.recurseSubmodules', 'false']
szager@chromium.org987b0612012-07-09 23:41:08 +0000221 kwargs = {'cwd': self.checkout_path,
222 'print_stdout': False,
szager@chromium.org12b07e72013-05-03 22:06:34 +0000223 'nag_timer': self.nag_timer,
224 'nag_max': self.nag_max,
szager@chromium.org987b0612012-07-09 23:41:08 +0000225 'filter_fn': lambda x: None}
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000226 try:
szager@chromium.org987b0612012-07-09 23:41:08 +0000227 gclient_utils.CheckCallAndFilter(cmd, **kwargs)
228 gclient_utils.CheckCallAndFilter(cmd2, **kwargs)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000229 except subprocess2.CalledProcessError:
230 # Not a fatal error, or even very interesting in a non-git-submodule
231 # world. So just keep it quiet.
232 pass
szager@chromium.org987b0612012-07-09 23:41:08 +0000233 try:
234 gclient_utils.CheckCallAndFilter(cmd3, **kwargs)
235 except subprocess2.CalledProcessError:
236 gclient_utils.CheckCallAndFilter(cmd3 + ['always'], **kwargs)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000237
iannucci@chromium.org08b21bf2013-04-05 03:38:10 +0000238 gclient_utils.CheckCallAndFilter(cmd4, **kwargs)
239
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000240 def _FetchAndReset(self, revision, file_list, options):
241 """Equivalent to git fetch; git reset."""
242 quiet = []
243 if not options.verbose:
244 quiet = ['--quiet']
245 self._UpdateBranchHeads(options, fetch=False)
246 self._Run(['fetch', 'origin', '--prune'] + quiet, options)
247 self._Run(['reset', '--hard', revision] + quiet, options)
248 self.UpdateSubmoduleConfig()
249 files = self._Capture(['ls-files']).splitlines()
250 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
251
msb@chromium.orge28e4982009-09-25 20:51:45 +0000252 def update(self, options, args, file_list):
253 """Runs git to update or transparently checkout the working copy.
254
255 All updated files will be appended to file_list.
256
257 Raises:
258 Error: if can't get URL for relative path.
259 """
msb@chromium.orge28e4982009-09-25 20:51:45 +0000260 if args:
261 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
262
nasser@codeaurora.orgece406f2010-02-23 17:29:15 +0000263 self._CheckMinVersion("1.6.6")
msb@chromium.org923a0372009-12-11 20:42:43 +0000264
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000265 default_rev = "refs/heads/master"
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000266 url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000267 rev_str = ""
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000268 revision = deps_revision
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000269 managed = True
msb@chromium.orge28e4982009-09-25 20:51:45 +0000270 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000271 # Override the revision number.
272 revision = str(options.revision)
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000273 if revision == 'unmanaged':
274 revision = None
275 managed = False
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000276 if not revision:
277 revision = default_rev
msb@chromium.orge28e4982009-09-25 20:51:45 +0000278
floitsch@google.comeaab7842011-04-28 09:07:58 +0000279 if gclient_utils.IsDateRevision(revision):
280 # Date-revisions only work on git-repositories if the reflog hasn't
281 # expired yet. Use rev-list to get the corresponding revision.
282 # git rev-list -n 1 --before='time-stamp' branchname
283 if options.transitive:
284 print('Warning: --transitive only works for SVN repositories.')
285 revision = default_rev
286
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000287 rev_str = ' at %s' % revision
288 files = []
289
290 printed_path = False
291 verbose = []
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000292 if options.verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000293 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000294 verbose = ['--verbose']
295 printed_path = True
296
297 if revision.startswith('refs/heads/'):
298 rev_type = "branch"
299 elif revision.startswith('origin/'):
300 # For compatability with old naming, translate 'origin' to 'refs/heads'
301 revision = revision.replace('origin/', 'refs/heads/')
302 rev_type = "branch"
303 else:
304 # hash is also a tag, only make a distinction at checkout
305 rev_type = "hash"
306
szager@google.com873e6672012-03-13 18:53:36 +0000307 if not os.path.exists(self.checkout_path) or (
308 os.path.isdir(self.checkout_path) and
309 not os.listdir(self.checkout_path)):
maruel@chromium.org6c48a302011-10-20 23:44:20 +0000310 gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path))
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000311 self._Clone(revision, url, options)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000312 self.UpdateSubmoduleConfig()
thomasvl@chromium.org858d6452011-03-24 17:59:20 +0000313 files = self._Capture(['ls-files']).splitlines()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000314 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000315 if not verbose:
316 # Make the output a little prettier. It's nice to have some whitespace
317 # between projects when cloning.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000318 print('')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000319 return
320
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000321 if not managed:
mmoss@chromium.org96833fa2013-04-17 03:26:37 +0000322 self._UpdateBranchHeads(options, fetch=False)
szager@chromium.orgf5cc4272012-06-21 22:38:07 +0000323 self.UpdateSubmoduleConfig()
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000324 print ('________ unmanaged solution; skipping %s' % self.relpath)
325 return
326
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000327 if not os.path.exists(os.path.join(self.checkout_path, '.git')):
328 raise gclient_utils.Error('\n____ %s%s\n'
329 '\tPath is not a git repo. No .git dir.\n'
330 '\tTo resolve:\n'
331 '\t\trm -rf %s\n'
332 '\tAnd run gclient sync again\n'
333 % (self.relpath, rev_str, self.relpath))
334
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000335 # See if the url has changed (the unittests use git://foo for the url, let
336 # that through).
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000337 current_url = self._Capture(['config', 'remote.origin.url'])
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000338 # TODO(maruel): Delete url != 'git://foo' since it's just to make the
339 # unit test pass. (and update the comment above)
ilevy@chromium.orgf6f58402013-05-22 00:14:32 +0000340 # Skip url auto-correction if remote.origin.gclient-auto-fix-url is set.
341 # This allows devs to use experimental repos which have a different url
342 # but whose branch(s) are the same as official repos.
343 if (current_url != url and
344 url != 'git://foo' and
345 subprocess2.capture(
346 ['git', 'config', 'remote.origin.gclient-auto-fix-url'],
347 cwd=self.checkout_path).strip() != 'False'):
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000348 print('_____ switching %s to a new upstream' % self.relpath)
349 # Make sure it's clean
350 self._CheckClean(rev_str)
351 # Switch over to the new upstream
352 self._Run(['remote', 'set-url', 'origin', url], options)
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000353 self._FetchAndReset(revision, file_list, options)
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000354 return
355
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000356 if not self._IsValidGitRepo():
357 # .git directory is hosed for some reason, set it back up.
358 print('_____ %s/.git is corrupted, rebuilding' % self.relpath)
359 self._Run(['init'], options)
360 self._Run(['remote', 'set-url', 'origin', url], options)
361
362 if not self._HasHead():
363 # Previous checkout was aborted before branches could be created in repo,
364 # so we need to reconstruct them here.
365 self._Run(['pull', 'origin', 'master'], options)
366 self._FetchAndReset(revision, file_list, options)
367
msb@chromium.org5bde4852009-12-14 16:47:12 +0000368 cur_branch = self._GetCurrentBranch()
369
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000370 # Cases:
msb@chromium.org786fb682010-06-02 15:16:23 +0000371 # 0) HEAD is detached. Probably from our initial clone.
372 # - make sure HEAD is contained by a named ref, then update.
373 # Cases 1-4. HEAD is a branch.
374 # 1) current branch is not tracking a remote branch (could be git-svn)
375 # - try to rebase onto the new hash or branch
376 # 2) current branch is tracking a remote branch with local committed
377 # changes, but the DEPS file switched to point to a hash
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000378 # - rebase those changes on top of the hash
msb@chromium.org786fb682010-06-02 15:16:23 +0000379 # 3) current branch is tracking a remote branch w/or w/out changes,
380 # no switch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000381 # - see if we can FF, if not, prompt the user for rebase, merge, or stop
msb@chromium.org786fb682010-06-02 15:16:23 +0000382 # 4) current branch is tracking a remote branch, switches to a different
383 # remote branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000384 # - exit
385
maruel@chromium.org81e012c2010-04-29 16:07:24 +0000386 # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for
387 # a tracking branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000388 # or 'master' if not a tracking branch (it's based on a specific rev/hash)
389 # or it returns None if it couldn't find an upstream
msb@chromium.org786fb682010-06-02 15:16:23 +0000390 if cur_branch is None:
391 upstream_branch = None
392 current_type = "detached"
393 logging.debug("Detached HEAD")
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000394 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000395 upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
396 if not upstream_branch or not upstream_branch.startswith('refs/remotes'):
397 current_type = "hash"
398 logging.debug("Current branch is not tracking an upstream (remote)"
399 " branch.")
400 elif upstream_branch.startswith('refs/remotes'):
401 current_type = "branch"
402 else:
403 raise gclient_utils.Error('Invalid Upstream: %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000404
maruel@chromium.org92067382012-06-28 19:58:41 +0000405 if (not re.match(r'^[0-9a-fA-F]{40}$', revision) or
406 not scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=revision)):
bauerb@chromium.orgcbd20a42012-06-27 13:49:27 +0000407 # Update the remotes first so we have all the refs.
408 backoff_time = 5
409 for _ in range(10):
410 try:
411 remote_output = scm.GIT.Capture(
412 ['remote'] + verbose + ['update'],
413 cwd=self.checkout_path)
414 break
415 except subprocess2.CalledProcessError, e:
416 # Hackish but at that point, git is known to work so just checking for
417 # 502 in stderr should be fine.
418 if '502' in e.stderr:
419 print(str(e))
420 print('Sleeping %.1f seconds and retrying...' % backoff_time)
421 time.sleep(backoff_time)
422 backoff_time *= 1.3
423 continue
424 raise
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000425
bauerb@chromium.orgcbd20a42012-06-27 13:49:27 +0000426 if verbose:
427 print(remote_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000428
mmoss@chromium.orge409df62013-04-16 17:28:57 +0000429 self._UpdateBranchHeads(options, fetch=True)
430
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000431 # This is a big hammer, debatable if it should even be here...
davemoore@chromium.org793796d2010-02-19 17:27:41 +0000432 if options.force or options.reset:
maruel@chromium.org37e89872010-09-07 16:11:33 +0000433 self._Run(['reset', '--hard', 'HEAD'], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000434
msb@chromium.org786fb682010-06-02 15:16:23 +0000435 if current_type == 'detached':
436 # case 0
437 self._CheckClean(rev_str)
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000438 self._CheckDetachedHead(rev_str, options)
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000439 self._Capture(['checkout', '--quiet', '%s' % revision])
msb@chromium.org786fb682010-06-02 15:16:23 +0000440 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000441 print('\n_____ %s%s' % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000442 elif current_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000443 # case 1
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000444 if scm.GIT.IsGitSvn(self.checkout_path) and upstream_branch is not None:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000445 # Our git-svn branch (upstream_branch) is our upstream
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000446 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000447 newbase=revision, printed_path=printed_path)
448 printed_path = True
449 else:
450 # Can't find a merge-base since we don't know our upstream. That makes
451 # this command VERY likely to produce a rebase failure. For now we
452 # assume origin is our upstream since that's what the old behavior was.
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000453 upstream_branch = 'origin'
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000454 if options.revision or deps_revision:
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000455 upstream_branch = revision
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000456 self._AttemptRebase(upstream_branch, files, options,
457 printed_path=printed_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000458 printed_path = True
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000459 elif rev_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000460 # case 2
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000461 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000462 newbase=revision, printed_path=printed_path)
463 printed_path = True
464 elif revision.replace('heads', 'remotes/origin') != upstream_branch:
465 # case 4
466 new_base = revision.replace('heads', 'remotes/origin')
467 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000468 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000469 switch_error = ("Switching upstream branch from %s to %s\n"
470 % (upstream_branch, new_base) +
471 "Please merge or rebase manually:\n" +
472 "cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
473 "OR git checkout -b <some new branch> %s" % new_base)
474 raise gclient_utils.Error(switch_error)
475 else:
476 # case 3 - the default case
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000477 files = self._Capture(['diff', upstream_branch, '--name-only']).split()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000478 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000479 print('Trying fast-forward merge to branch : %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000480 try:
bauerb@chromium.org2aad1b22011-07-22 12:00:41 +0000481 merge_args = ['merge']
482 if not options.merge:
483 merge_args.append('--ff-only')
484 merge_args.append(upstream_branch)
485 merge_output = scm.GIT.Capture(merge_args, cwd=self.checkout_path)
bratell@opera.com18fa4542013-05-21 13:30:46 +0000486 except subprocess2.CalledProcessError as e:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000487 if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr):
488 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000489 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000490 printed_path = True
491 while True:
492 try:
maruel@chromium.org90541732011-04-01 17:54:18 +0000493 action = ask_for_data(
494 'Cannot fast-forward merge, attempt to rebase? '
bratell@opera.com18fa4542013-05-21 13:30:46 +0000495 '(y)es / (q)uit / (s)kip : ', options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000496 except ValueError:
maruel@chromium.org90541732011-04-01 17:54:18 +0000497 raise gclient_utils.Error('Invalid Character')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000498 if re.match(r'yes|y', action, re.I):
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000499 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000500 printed_path=printed_path)
501 printed_path = True
502 break
503 elif re.match(r'quit|q', action, re.I):
504 raise gclient_utils.Error("Can't fast-forward, please merge or "
505 "rebase manually.\n"
506 "cd %s && git " % self.checkout_path
507 + "rebase %s" % upstream_branch)
508 elif re.match(r'skip|s', action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000509 print('Skipping %s' % self.relpath)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000510 return
511 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000512 print('Input not recognized')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000513 elif re.match("error: Your local changes to '.*' would be "
514 "overwritten by merge. Aborting.\nPlease, commit your "
515 "changes or stash them before you can merge.\n",
516 e.stderr):
517 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000518 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000519 printed_path = True
520 raise gclient_utils.Error(e.stderr)
521 else:
522 # Some other problem happened with the merge
523 logging.error("Error during fast-forward merge in %s!" % self.relpath)
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000524 print(e.stderr)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000525 raise
526 else:
527 # Fast-forward merge was successful
528 if not re.match('Already up-to-date.', merge_output) or verbose:
529 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000530 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000531 printed_path = True
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000532 print(merge_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000533 if not verbose:
534 # Make the output a little prettier. It's nice to have some
535 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000536 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000537
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000538 self.UpdateSubmoduleConfig()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000539 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.org5bde4852009-12-14 16:47:12 +0000540
541 # If the rebase generated a conflict, abort and ask user to fix
msb@chromium.org786fb682010-06-02 15:16:23 +0000542 if self._IsRebasing():
msb@chromium.org5bde4852009-12-14 16:47:12 +0000543 raise gclient_utils.Error('\n____ %s%s\n'
544 '\nConflict while rebasing this branch.\n'
545 'Fix the conflict and run gclient again.\n'
546 'See man git-rebase for details.\n'
547 % (self.relpath, rev_str))
548
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000549 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000550 print('Checked out revision %s' % self.revinfo(options, (), None))
msb@chromium.orge28e4982009-09-25 20:51:45 +0000551
steveblock@chromium.org98e69452012-02-16 16:36:43 +0000552 # If --reset and --delete_unversioned_trees are specified, remove any
553 # untracked directories.
554 if options.reset and options.delete_unversioned_trees:
555 # GIT.CaptureStatus() uses 'dit diff' to compare to a specific SHA1 (the
556 # merge-base by default), so doesn't include untracked files. So we use
557 # 'git ls-files --directory --others --exclude-standard' here directly.
558 paths = scm.GIT.Capture(
559 ['ls-files', '--directory', '--others', '--exclude-standard'],
560 self.checkout_path)
561 for path in (p for p in paths.splitlines() if p.endswith('/')):
562 full_path = os.path.join(self.checkout_path, path)
563 if not os.path.islink(full_path):
564 print('\n_____ removing unversioned directory %s' % path)
digit@chromium.orgdc112ac2013-04-24 13:00:19 +0000565 gclient_utils.rmtree(full_path)
steveblock@chromium.org98e69452012-02-16 16:36:43 +0000566
567
msb@chromium.orge28e4982009-09-25 20:51:45 +0000568 def revert(self, options, args, file_list):
569 """Reverts local modifications.
570
571 All reverted files will be appended to file_list.
572 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000573 if not os.path.isdir(self.checkout_path):
msb@chromium.org260c6532009-10-28 03:22:35 +0000574 # revert won't work if the directory doesn't exist. It needs to
575 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000576 print('\n_____ %s is missing, synching instead' % self.relpath)
msb@chromium.org260c6532009-10-28 03:22:35 +0000577 # Don't reuse the args.
578 return self.update(options, [], file_list)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000579
580 default_rev = "refs/heads/master"
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000581 _, deps_revision = gclient_utils.SplitUrlRevision(self.url)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000582 if not deps_revision:
583 deps_revision = default_rev
584 if deps_revision.startswith('refs/heads/'):
585 deps_revision = deps_revision.replace('refs/heads/', 'origin/')
586
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000587 files = self._Capture(['diff', deps_revision, '--name-only']).split()
maruel@chromium.org37e89872010-09-07 16:11:33 +0000588 self._Run(['reset', '--hard', deps_revision], options)
lliabraa@chromium.orgade83db2012-09-27 14:06:49 +0000589 self._Run(['clean', '-f', '-d'], options)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000590 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
591
msb@chromium.org0f282062009-11-06 20:14:02 +0000592 def revinfo(self, options, args, file_list):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000593 """Returns revision"""
594 return self._Capture(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000595
msb@chromium.orge28e4982009-09-25 20:51:45 +0000596 def runhooks(self, options, args, file_list):
597 self.status(options, args, file_list)
598
599 def status(self, options, args, file_list):
600 """Display status information."""
601 if not os.path.isdir(self.checkout_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000602 print(('\n________ couldn\'t run status in %s:\n'
603 'The directory does not exist.') % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000604 else:
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000605 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org37e89872010-09-07 16:11:33 +0000606 self._Run(['diff', '--name-status', merge_base], options)
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000607 files = self._Capture(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000608 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
609
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000610 def GetUsableRev(self, rev, options):
611 """Finds a useful revision for this repository.
612
613 If SCM is git-svn and the head revision is less than |rev|, git svn fetch
614 will be called on the source."""
615 sha1 = None
iannucci@chromium.org3830a672013-02-19 20:15:14 +0000616 if not os.path.isdir(self.checkout_path):
617 raise gclient_utils.Error(
618 ( 'We could not find a valid hash for safesync_url response "%s".\n'
619 'Safesync URLs with a git checkout currently require the repo to\n'
620 'be cloned without a safesync_url before adding the safesync_url.\n'
621 'For more info, see: '
622 'http://code.google.com/p/chromium/wiki/UsingNewGit'
623 '#Initial_checkout' ) % rev)
624 elif rev.isdigit() and len(rev) < 7:
625 # Handles an SVN rev. As an optimization, only verify an SVN revision as
626 # [0-9]{1,6} for now to avoid making a network request.
627 if scm.GIT.IsGitSvn(cwd=self.checkout_path):
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000628 local_head = scm.GIT.GetGitSvnHeadRev(cwd=self.checkout_path)
629 if not local_head or local_head < int(rev):
dbeam@chromium.org2a75fdb2012-02-15 01:32:57 +0000630 try:
631 logging.debug('Looking for git-svn configuration optimizations.')
632 if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
633 cwd=self.checkout_path):
634 scm.GIT.Capture(['fetch'], cwd=self.checkout_path)
635 except subprocess2.CalledProcessError:
636 logging.debug('git config --get svn-remote.svn.fetch failed, '
637 'ignoring possible optimization.')
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000638 if options.verbose:
639 print('Running git svn fetch. This might take a while.\n')
640 scm.GIT.Capture(['svn', 'fetch'], cwd=self.checkout_path)
szager@google.com312a6a42012-10-11 21:19:42 +0000641 try:
szager@chromium.orgc51def32012-10-15 18:50:37 +0000642 sha1 = scm.GIT.GetBlessedSha1ForSvnRev(
643 cwd=self.checkout_path, rev=rev)
szager@google.com312a6a42012-10-11 21:19:42 +0000644 except gclient_utils.Error, e:
645 sha1 = e.message
646 print('\nWarning: Could not find a git revision with accurate\n'
647 '.DEPS.git that maps to SVN revision %s. Sync-ing to\n'
648 'the closest sane git revision, which is:\n'
649 ' %s\n' % (rev, e.message))
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000650 if not sha1:
651 raise gclient_utils.Error(
652 ( 'It appears that either your git-svn remote is incorrectly\n'
653 'configured or the revision in your safesync_url is\n'
654 'higher than git-svn remote\'s HEAD as we couldn\'t find a\n'
655 'corresponding git hash for SVN rev %s.' ) % rev)
iannucci@chromium.org3830a672013-02-19 20:15:14 +0000656 else:
657 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
658 sha1 = rev
659 else:
660 # May exist in origin, but we don't have it yet, so fetch and look
661 # again.
662 scm.GIT.Capture(['fetch', 'origin'], cwd=self.checkout_path)
663 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
664 sha1 = rev
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000665
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000666 if not sha1:
667 raise gclient_utils.Error(
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000668 ( 'We could not find a valid hash for safesync_url response "%s".\n'
669 'Safesync URLs with a git checkout currently require a git-svn\n'
670 'remote or a safesync_url that provides git sha1s. Please add a\n'
671 'git-svn remote or change your safesync_url. For more info, see:\n'
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000672 'http://code.google.com/p/chromium/wiki/UsingNewGit'
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000673 '#Initial_checkout' ) % rev)
674
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000675 return sha1
676
msb@chromium.orge6f78352010-01-13 17:05:33 +0000677 def FullUrlForRelativeUrl(self, url):
678 # Strip from last '/'
679 # Equivalent to unix basename
680 base_url = self.url
681 return base_url[:base_url.rfind('/')] + url
682
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000683 def _Clone(self, revision, url, options):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000684 """Clone a git repository from the given URL.
685
msb@chromium.org786fb682010-06-02 15:16:23 +0000686 Once we've cloned the repo, we checkout a working branch if the specified
687 revision is a branch head. If it is a tag or a specific commit, then we
688 leave HEAD detached as it makes future updates simpler -- in this case the
689 user should first create a new branch or switch to an existing branch before
690 making changes in the repo."""
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000691 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000692 # git clone doesn't seem to insert a newline properly before printing
693 # to stdout
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000694 print('')
szager@chromium.orgee10d7d2013-04-24 23:18:20 +0000695 clone_cmd = ['clone', '--progress']
msb@chromium.org786fb682010-06-02 15:16:23 +0000696 if revision.startswith('refs/heads/'):
697 clone_cmd.extend(['-b', revision.replace('refs/heads/', '')])
698 detach_head = False
699 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000700 detach_head = True
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000701 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000702 clone_cmd.append('--verbose')
703 clone_cmd.extend([url, self.checkout_path])
704
nsylvain@chromium.org328c3c72011-06-01 20:50:27 +0000705 # If the parent directory does not exist, Git clone on Windows will not
706 # create it, so we need to do it manually.
707 parent_dir = os.path.dirname(self.checkout_path)
708 if not os.path.exists(parent_dir):
maruel@chromium.org6c48a302011-10-20 23:44:20 +0000709 gclient_utils.safe_makedirs(parent_dir)
nsylvain@chromium.org328c3c72011-06-01 20:50:27 +0000710
szager@google.com85d3e3a2011-10-07 17:12:00 +0000711 percent_re = re.compile('.* ([0-9]{1,2})% .*')
712 def _GitFilter(line):
713 # git uses an escape sequence to clear the line; elide it.
714 esc = line.find(unichr(033))
715 if esc > -1:
716 line = line[:esc]
717 match = percent_re.match(line)
718 if not match or not int(match.group(1)) % 10:
719 print '%s' % line
720
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000721 for _ in range(3):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000722 try:
szager@google.com85d3e3a2011-10-07 17:12:00 +0000723 self._Run(clone_cmd, options, cwd=self._root_dir, filter_fn=_GitFilter,
724 print_stdout=False)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000725 break
maruel@chromium.org2a5b6a22011-09-09 14:03:12 +0000726 except subprocess2.CalledProcessError, e:
727 # Too bad we don't have access to the actual output yet.
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000728 # We should check for "transfer closed with NNN bytes remaining to
729 # read". In the meantime, just make sure .git exists.
maruel@chromium.org2a5b6a22011-09-09 14:03:12 +0000730 if (e.returncode == 128 and
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000731 os.path.exists(os.path.join(self.checkout_path, '.git'))):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000732 print(str(e))
733 print('Retrying...')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000734 continue
735 raise e
736
mmoss@chromium.orge409df62013-04-16 17:28:57 +0000737 # Update the "branch-heads" remote-tracking branches, since we might need it
738 # to checkout a specific revision below.
739 self._UpdateBranchHeads(options, fetch=True)
mmoss@chromium.org059cc452013-03-11 15:14:35 +0000740
msb@chromium.org786fb682010-06-02 15:16:23 +0000741 if detach_head:
742 # Squelch git's very verbose detached HEAD warning and use our own
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000743 self._Capture(['checkout', '--quiet', '%s' % revision])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000744 print(
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000745 ('Checked out %s to a detached HEAD. Before making any commits\n'
746 'in this repo, you should use \'git checkout <branch>\' to switch to\n'
747 'an existing branch or use \'git checkout origin -b <branch>\' to\n'
748 'create a new branch for your work.') % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000749
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000750 def _AttemptRebase(self, upstream, files, options, newbase=None,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000751 branch=None, printed_path=False):
752 """Attempt to rebase onto either upstream or, if specified, newbase."""
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000753 files.extend(self._Capture(['diff', upstream, '--name-only']).split())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000754 revision = upstream
755 if newbase:
756 revision = newbase
757 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000758 print('\n_____ %s : Attempting rebase onto %s...' % (
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000759 self.relpath, revision))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000760 printed_path = True
761 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000762 print('Attempting rebase onto %s...' % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000763
764 # Build the rebase command here using the args
765 # git rebase [options] [--onto <newbase>] <upstream> [<branch>]
766 rebase_cmd = ['rebase']
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000767 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000768 rebase_cmd.append('--verbose')
769 if newbase:
770 rebase_cmd.extend(['--onto', newbase])
771 rebase_cmd.append(upstream)
772 if branch:
773 rebase_cmd.append(branch)
774
775 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000776 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000777 except subprocess2.CalledProcessError, e:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000778 if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or
779 re.match(r'cannot rebase: your index contains uncommitted changes',
780 e.stderr)):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000781 while True:
maruel@chromium.org90541732011-04-01 17:54:18 +0000782 rebase_action = ask_for_data(
783 'Cannot rebase because of unstaged changes.\n'
784 '\'git reset --hard HEAD\' ?\n'
785 'WARNING: destroys any uncommitted work in your current branch!'
bratell@opera.com18fa4542013-05-21 13:30:46 +0000786 ' (y)es / (q)uit / (s)how : ', options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000787 if re.match(r'yes|y', rebase_action, re.I):
maruel@chromium.org37e89872010-09-07 16:11:33 +0000788 self._Run(['reset', '--hard', 'HEAD'], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000789 # Should this be recursive?
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000790 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000791 break
792 elif re.match(r'quit|q', rebase_action, re.I):
793 raise gclient_utils.Error("Please merge or rebase manually\n"
794 "cd %s && git " % self.checkout_path
795 + "%s" % ' '.join(rebase_cmd))
796 elif re.match(r'show|s', rebase_action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000797 print('\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000798 continue
799 else:
800 gclient_utils.Error("Input not recognized")
801 continue
802 elif re.search(r'^CONFLICT', e.stdout, re.M):
803 raise gclient_utils.Error("Conflict while rebasing this branch.\n"
804 "Fix the conflict and run gclient again.\n"
805 "See 'man git-rebase' for details.\n")
806 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000807 print(e.stdout.strip())
808 print('Rebase produced error output:\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000809 raise gclient_utils.Error("Unrecognized error, please merge or rebase "
810 "manually.\ncd %s && git " %
811 self.checkout_path
812 + "%s" % ' '.join(rebase_cmd))
813
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000814 print(rebase_output.strip())
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000815 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000816 # Make the output a little prettier. It's nice to have some
817 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000818 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000819
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000820 def _IsValidGitRepo(self):
821 """Returns if the directory is a valid git repository.
822
823 Checks if git status works.
824 """
825 try:
826 self._Capture(['status'])
827 return True
828 except subprocess2.CalledProcessError:
829 return False
830
831 def _HasHead(self):
832 """Returns True if any commit is checked out.
833
834 This is done by checking if rev-parse HEAD works in the current repository.
835 """
836 try:
837 self._GetCurrentBranch()
838 return True
839 except subprocess2.CalledProcessError:
840 return False
841
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000842 @staticmethod
843 def _CheckMinVersion(min_version):
maruel@chromium.orgd0f854a2010-03-11 19:35:53 +0000844 (ok, current_version) = scm.GIT.AssertVersion(min_version)
845 if not ok:
846 raise gclient_utils.Error('git version %s < minimum required %s' %
847 (current_version, min_version))
msb@chromium.org923a0372009-12-11 20:42:43 +0000848
msb@chromium.org786fb682010-06-02 15:16:23 +0000849 def _IsRebasing(self):
850 # Check for any of REBASE-i/REBASE-m/REBASE/AM. Unfortunately git doesn't
851 # have a plumbing command to determine whether a rebase is in progress, so
852 # for now emualate (more-or-less) git-rebase.sh / git-completion.bash
853 g = os.path.join(self.checkout_path, '.git')
854 return (
855 os.path.isdir(os.path.join(g, "rebase-merge")) or
856 os.path.isdir(os.path.join(g, "rebase-apply")))
857
858 def _CheckClean(self, rev_str):
859 # Make sure the tree is clean; see git-rebase.sh for reference
860 try:
861 scm.GIT.Capture(['update-index', '--ignore-submodules', '--refresh'],
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000862 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000863 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000864 raise gclient_utils.Error('\n____ %s%s\n'
865 '\tYou have unstaged changes.\n'
866 '\tPlease commit, stash, or reset.\n'
867 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000868 try:
869 scm.GIT.Capture(['diff-index', '--cached', '--name-status', '-r',
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000870 '--ignore-submodules', 'HEAD', '--'],
871 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000872 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000873 raise gclient_utils.Error('\n____ %s%s\n'
874 '\tYour index contains uncommitted changes\n'
875 '\tPlease commit, stash, or reset.\n'
876 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000877
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000878 def _CheckDetachedHead(self, rev_str, options):
msb@chromium.org786fb682010-06-02 15:16:23 +0000879 # HEAD is detached. Make sure it is safe to move away from (i.e., it is
880 # reference by a commit). If not, error out -- most likely a rebase is
881 # in progress, try to detect so we can give a better error.
882 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000883 scm.GIT.Capture(['name-rev', '--no-undefined', 'HEAD'],
884 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000885 except subprocess2.CalledProcessError:
msb@chromium.org786fb682010-06-02 15:16:23 +0000886 # Commit is not contained by any rev. See if the user is rebasing:
887 if self._IsRebasing():
888 # Punt to the user
889 raise gclient_utils.Error('\n____ %s%s\n'
890 '\tAlready in a conflict, i.e. (no branch).\n'
891 '\tFix the conflict and run gclient again.\n'
892 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
893 '\tSee man git-rebase for details.\n'
894 % (self.relpath, rev_str))
895 # Let's just save off the commit so we can proceed.
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000896 name = ('saved-by-gclient-' +
897 self._Capture(['rev-parse', '--short', 'HEAD']))
898 self._Capture(['branch', name])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000899 print('\n_____ found an unreferenced commit and saved it as \'%s\'' %
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000900 name)
msb@chromium.org786fb682010-06-02 15:16:23 +0000901
msb@chromium.org5bde4852009-12-14 16:47:12 +0000902 def _GetCurrentBranch(self):
msb@chromium.org786fb682010-06-02 15:16:23 +0000903 # Returns name of current branch or None for detached HEAD
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000904 branch = self._Capture(['rev-parse', '--abbrev-ref=strict', 'HEAD'])
msb@chromium.org786fb682010-06-02 15:16:23 +0000905 if branch == 'HEAD':
msb@chromium.org5bde4852009-12-14 16:47:12 +0000906 return None
907 return branch
908
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000909 def _Capture(self, args):
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000910 return subprocess2.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000911 ['git'] + args,
ilevy@chromium.orgf6f58402013-05-22 00:14:32 +0000912 stderr=subprocess2.VOID,
szager@chromium.org12b07e72013-05-03 22:06:34 +0000913 nag_timer=self.nag_timer,
914 nag_max=self.nag_max,
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000915 cwd=self.checkout_path).strip()
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000916
mmoss@chromium.orge409df62013-04-16 17:28:57 +0000917 def _UpdateBranchHeads(self, options, fetch=False):
918 """Adds, and optionally fetches, "branch-heads" refspecs if requested."""
919 if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
920 backoff_time = 5
921 for _ in range(3):
922 try:
923 config_cmd = ['config', 'remote.origin.fetch',
924 '+refs/branch-heads/*:refs/remotes/branch-heads/*',
925 '^\\+refs/branch-heads/\\*:.*$']
926 self._Run(config_cmd, options)
927 if fetch:
928 fetch_cmd = ['fetch', 'origin']
929 if options.verbose:
930 fetch_cmd.append('--verbose')
931 self._Run(fetch_cmd, options)
932 break
933 except subprocess2.CalledProcessError, e:
934 print(str(e))
935 print('Retrying in %.1f seconds...' % backoff_time)
936 time.sleep(backoff_time)
937 backoff_time *= 1.3
938
maruel@chromium.org37e89872010-09-07 16:11:33 +0000939 def _Run(self, args, options, **kwargs):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000940 kwargs.setdefault('cwd', self.checkout_path)
szager@google.com85d3e3a2011-10-07 17:12:00 +0000941 kwargs.setdefault('print_stdout', True)
szager@chromium.org12b07e72013-05-03 22:06:34 +0000942 kwargs.setdefault('nag_timer', self.nag_timer)
943 kwargs.setdefault('nag_max', self.nag_max)
szager@google.com85d3e3a2011-10-07 17:12:00 +0000944 stdout = kwargs.get('stdout', sys.stdout)
945 stdout.write('\n________ running \'git %s\' in \'%s\'\n' % (
946 ' '.join(args), kwargs['cwd']))
947 gclient_utils.CheckCallAndFilter(['git'] + args, **kwargs)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000948
949
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000950class SVNWrapper(SCMWrapper):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000951 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000952
mukai@chromium.org9e3e82c2012-04-18 12:55:43 +0000953 @staticmethod
954 def BinaryExists():
955 """Returns true if the command exists."""
956 try:
957 result, version = scm.SVN.AssertVersion('1.4')
958 if not result:
959 raise gclient_utils.Error('SVN version is older than 1.4: %s' % version)
960 return result
961 except OSError:
962 return False
963
xusydoc@chromium.org885a9602013-05-31 09:54:40 +0000964 def GetCheckoutRoot(self):
965 return scm.SVN.GetCheckoutRoot(self.checkout_path)
966
floitsch@google.comeaab7842011-04-28 09:07:58 +0000967 def GetRevisionDate(self, revision):
968 """Returns the given revision's date in ISO-8601 format (which contains the
969 time zone)."""
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +0000970 date = scm.SVN.Capture(
971 ['propget', '--revprop', 'svn:date', '-r', revision],
972 os.path.join(self.checkout_path, '.'))
floitsch@google.comeaab7842011-04-28 09:07:58 +0000973 return date.strip()
974
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000975 def cleanup(self, options, args, file_list):
976 """Cleanup working copy."""
maruel@chromium.org669600d2010-09-01 19:06:31 +0000977 self._Run(['cleanup'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000978
979 def diff(self, options, args, file_list):
980 # NOTE: This function does not currently modify file_list.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000981 if not os.path.isdir(self.checkout_path):
982 raise gclient_utils.Error('Directory %s is not present.' %
983 self.checkout_path)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000984 self._Run(['diff'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000985
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000986 def pack(self, options, args, file_list):
987 """Generates a patch file which can be applied to the root of the
988 repository."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000989 if not os.path.isdir(self.checkout_path):
990 raise gclient_utils.Error('Directory %s is not present.' %
991 self.checkout_path)
992 gclient_utils.CheckCallAndFilter(
993 ['svn', 'diff', '-x', '--ignore-eol-style'] + args,
994 cwd=self.checkout_path,
995 print_stdout=False,
szager@chromium.org12b07e72013-05-03 22:06:34 +0000996 nag_timer=self.nag_timer,
997 nag_max=self.nag_max,
haitao.feng@intel.com306080c2012-05-04 13:11:29 +0000998 filter_fn=SvnDiffFilterer(self.relpath).Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000999
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001000 def update(self, options, args, file_list):
msb@chromium.orgd6504212010-01-13 17:34:31 +00001001 """Runs svn to update or transparently checkout the working copy.
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001002
1003 All updated files will be appended to file_list.
1004
1005 Raises:
1006 Error: if can't get URL for relative path.
1007 """
morrita@chromium.org21dca0e2010-10-05 00:55:12 +00001008 # Only update if git or hg is not controlling the directory.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001009 git_path = os.path.join(self.checkout_path, '.git')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001010 if os.path.exists(git_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001011 print('________ found .git directory; skipping %s' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001012 return
1013
morrita@chromium.org21dca0e2010-10-05 00:55:12 +00001014 hg_path = os.path.join(self.checkout_path, '.hg')
1015 if os.path.exists(hg_path):
1016 print('________ found .hg directory; skipping %s' % self.relpath)
1017 return
1018
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001019 if args:
1020 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
1021
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001022 # revision is the revision to match. It is None if no revision is specified,
1023 # i.e. the 'deps ain't pinned'.
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001024 url, revision = gclient_utils.SplitUrlRevision(self.url)
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001025 # Keep the original unpinned url for reference in case the repo is switched.
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001026 base_url = url
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +00001027 managed = True
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001028 if options.revision:
1029 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001030 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001031 if revision:
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +00001032 if revision != 'unmanaged':
1033 forced_revision = True
1034 # Reconstruct the url.
1035 url = '%s@%s' % (url, revision)
1036 rev_str = ' at %s' % revision
1037 else:
1038 managed = False
1039 revision = None
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001040 else:
1041 forced_revision = False
1042 rev_str = ''
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001043
davidjames@chromium.org13349e22012-11-15 17:11:28 +00001044 # Get the existing scm url and the revision number of the current checkout.
1045 exists = os.path.exists(self.checkout_path)
1046 if exists and managed:
1047 try:
1048 from_info = scm.SVN.CaptureLocalInfo(
1049 [], os.path.join(self.checkout_path, '.'))
1050 except (gclient_utils.Error, subprocess2.CalledProcessError):
1051 if options.reset and options.delete_unversioned_trees:
1052 print 'Removing troublesome path %s' % self.checkout_path
1053 gclient_utils.rmtree(self.checkout_path)
1054 exists = False
1055 else:
1056 msg = ('Can\'t update/checkout %s if an unversioned directory is '
1057 'present. Delete the directory and try again.')
1058 raise gclient_utils.Error(msg % self.checkout_path)
1059
1060 if not exists:
maruel@chromium.org6c48a302011-10-20 23:44:20 +00001061 gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001062 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001063 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001064 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001065 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001066 return
1067
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +00001068 if not managed:
1069 print ('________ unmanaged solution; skipping %s' % self.relpath)
1070 return
1071
maruel@chromium.org49fcb0c2011-09-23 14:34:38 +00001072 if 'URL' not in from_info:
1073 raise gclient_utils.Error(
1074 ('gclient is confused. Couldn\'t get the url for %s.\n'
1075 'Try using @unmanaged.\n%s') % (
1076 self.checkout_path, from_info))
1077
maruel@chromium.orge407c9a2010-08-09 19:11:37 +00001078 # Look for locked directories.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001079 dir_info = scm.SVN.CaptureStatus(
1080 None, os.path.join(self.checkout_path, '.'))
phajdan.jr@chromium.orgd558c4b2011-09-22 18:56:24 +00001081 if any(d[0][2] == 'L' for d in dir_info):
1082 try:
1083 self._Run(['cleanup', self.checkout_path], options)
1084 except subprocess2.CalledProcessError, e:
1085 # Get the status again, svn cleanup may have cleaned up at least
1086 # something.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001087 dir_info = scm.SVN.CaptureStatus(
1088 None, os.path.join(self.checkout_path, '.'))
phajdan.jr@chromium.orgd558c4b2011-09-22 18:56:24 +00001089
1090 # Try to fix the failures by removing troublesome files.
1091 for d in dir_info:
1092 if d[0][2] == 'L':
1093 if d[0][0] == '!' and options.force:
1094 print 'Removing troublesome path %s' % d[1]
1095 gclient_utils.rmtree(d[1])
1096 else:
1097 print 'Not removing troublesome path %s automatically.' % d[1]
1098 if d[0][0] == '!':
1099 print 'You can pass --force to enable automatic removal.'
1100 raise e
maruel@chromium.orge407c9a2010-08-09 19:11:37 +00001101
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001102 # Retrieve the current HEAD version because svn is slow at null updates.
1103 if options.manually_grab_svn_rev and not revision:
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001104 from_info_live = scm.SVN.CaptureRemoteInfo(from_info['URL'])
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001105 revision = str(from_info_live['Revision'])
1106 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001107
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001108 if from_info['URL'] != base_url:
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001109 # The repository url changed, need to switch.
maruel@chromium.org54019f32010-09-09 13:50:11 +00001110 try:
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001111 to_info = scm.SVN.CaptureRemoteInfo(url)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +00001112 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +00001113 # The url is invalid or the server is not accessible, it's safer to bail
1114 # out right now.
1115 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001116 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
1117 and (from_info['UUID'] == to_info['UUID']))
1118 if can_switch:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001119 print('\n_____ relocating %s to a new checkout' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001120 # We have different roots, so check if we can switch --relocate.
1121 # Subversion only permits this if the repository UUIDs match.
1122 # Perform the switch --relocate, then rewrite the from_url
1123 # to reflect where we "are now." (This is the same way that
1124 # Subversion itself handles the metadata when switch --relocate
1125 # is used.) This makes the checks below for whether we
1126 # can update to a revision or have to switch to a different
1127 # branch work as expected.
1128 # TODO(maruel): TEST ME !
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001129 command = ['switch', '--relocate',
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001130 from_info['Repository Root'],
1131 to_info['Repository Root'],
1132 self.relpath]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001133 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001134 from_info['URL'] = from_info['URL'].replace(
1135 from_info['Repository Root'],
1136 to_info['Repository Root'])
1137 else:
maruel@chromium.org3294f522010-08-18 19:54:57 +00001138 if not options.force and not options.reset:
maruel@chromium.org86f0f952010-08-10 17:17:19 +00001139 # Look for local modifications but ignore unversioned files.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001140 for status in scm.SVN.CaptureStatus(None, self.checkout_path):
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001141 if status[0][0] != '?':
maruel@chromium.org86f0f952010-08-10 17:17:19 +00001142 raise gclient_utils.Error(
1143 ('Can\'t switch the checkout to %s; UUID don\'t match and '
1144 'there is local changes in %s. Delete the directory and '
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001145 'try again.') % (url, self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001146 # Ok delete it.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001147 print('\n_____ switching %s to a new checkout' % self.relpath)
digit@chromium.orgdc112ac2013-04-24 13:00:19 +00001148 gclient_utils.rmtree(self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001149 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001150 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001151 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001152 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001153 return
1154
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001155 # If the provided url has a revision number that matches the revision
1156 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +00001157 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001158 if options.verbose or not forced_revision:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001159 print('\n_____ %s%s' % (self.relpath, rev_str))
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001160 else:
1161 command = ['update', self.checkout_path]
1162 command = self._AddAdditionalUpdateFlags(command, options, revision)
1163 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001164
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001165 # If --reset and --delete_unversioned_trees are specified, remove any
1166 # untracked files and directories.
1167 if options.reset and options.delete_unversioned_trees:
1168 for status in scm.SVN.CaptureStatus(None, self.checkout_path):
1169 full_path = os.path.join(self.checkout_path, status[1])
1170 if (status[0][0] == '?'
1171 and os.path.isdir(full_path)
1172 and not os.path.islink(full_path)):
1173 print('\n_____ removing unversioned directory %s' % status[1])
digit@chromium.orgdc112ac2013-04-24 13:00:19 +00001174 gclient_utils.rmtree(full_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001175
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001176 def updatesingle(self, options, args, file_list):
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001177 filename = args.pop()
tony@chromium.org57564662010-04-14 02:35:12 +00001178 if scm.SVN.AssertVersion("1.5")[0]:
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001179 if not os.path.exists(os.path.join(self.checkout_path, '.svn')):
tony@chromium.org57564662010-04-14 02:35:12 +00001180 # Create an empty checkout and then update the one file we want. Future
1181 # operations will only apply to the one file we checked out.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001182 command = ["checkout", "--depth", "empty", self.url, self.checkout_path]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001183 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001184 if os.path.exists(os.path.join(self.checkout_path, filename)):
1185 os.remove(os.path.join(self.checkout_path, filename))
tony@chromium.org57564662010-04-14 02:35:12 +00001186 command = ["update", filename]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001187 self._RunAndGetFileList(command, options, file_list)
tony@chromium.org57564662010-04-14 02:35:12 +00001188 # After the initial checkout, we can use update as if it were any other
1189 # dep.
1190 self.update(options, args, file_list)
1191 else:
1192 # If the installed version of SVN doesn't support --depth, fallback to
1193 # just exporting the file. This has the downside that revision
1194 # information is not stored next to the file, so we will have to
1195 # re-export the file every time we sync.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001196 if not os.path.exists(self.checkout_path):
maruel@chromium.org6c48a302011-10-20 23:44:20 +00001197 gclient_utils.safe_makedirs(self.checkout_path)
tony@chromium.org57564662010-04-14 02:35:12 +00001198 command = ["export", os.path.join(self.url, filename),
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001199 os.path.join(self.checkout_path, filename)]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001200 command = self._AddAdditionalUpdateFlags(command, options,
1201 options.revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001202 self._Run(command, options, cwd=self._root_dir)
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001203
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001204 def revert(self, options, args, file_list):
1205 """Reverts local modifications. Subversion specific.
1206
1207 All reverted files will be appended to file_list, even if Subversion
1208 doesn't know about them.
1209 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001210 if not os.path.isdir(self.checkout_path):
maruel@chromium.orgc0cc0872011-10-12 17:02:41 +00001211 if os.path.exists(self.checkout_path):
1212 gclient_utils.rmtree(self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001213 # svn revert won't work if the directory doesn't exist. It needs to
1214 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001215 print('\n_____ %s is missing, synching instead' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001216 # Don't reuse the args.
1217 return self.update(options, [], file_list)
1218
maruel@chromium.orgc0cc0872011-10-12 17:02:41 +00001219 if not os.path.isdir(os.path.join(self.checkout_path, '.svn')):
1220 if os.path.isdir(os.path.join(self.checkout_path, '.git')):
1221 print('________ found .git directory; skipping %s' % self.relpath)
1222 return
1223 if os.path.isdir(os.path.join(self.checkout_path, '.hg')):
1224 print('________ found .hg directory; skipping %s' % self.relpath)
1225 return
1226 if not options.force:
1227 raise gclient_utils.Error('Invalid checkout path, aborting')
1228 print(
1229 '\n_____ %s is not a valid svn checkout, synching instead' %
1230 self.relpath)
1231 gclient_utils.rmtree(self.checkout_path)
1232 # Don't reuse the args.
1233 return self.update(options, [], file_list)
1234
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001235 def printcb(file_status):
1236 file_list.append(file_status[1])
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001237 if logging.getLogger().isEnabledFor(logging.INFO):
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001238 logging.info('%s%s' % (file_status[0], file_status[1]))
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001239 else:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001240 print(os.path.join(self.checkout_path, file_status[1]))
1241 scm.SVN.Revert(self.checkout_path, callback=printcb)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001242
maruel@chromium.org8b322b32011-11-01 19:05:50 +00001243 # Revert() may delete the directory altogether.
1244 if not os.path.isdir(self.checkout_path):
1245 # Don't reuse the args.
1246 return self.update(options, [], file_list)
1247
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001248 try:
1249 # svn revert is so broken we don't even use it. Using
1250 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001251 # file_list will contain duplicates.
maruel@chromium.org669600d2010-09-01 19:06:31 +00001252 self._RunAndGetFileList(['update', '--revision', 'BASE'], options,
1253 file_list)
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001254 except OSError, e:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001255 # Maybe the directory disapeared meanwhile. Do not throw an exception.
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001256 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001257
msb@chromium.org0f282062009-11-06 20:14:02 +00001258 def revinfo(self, options, args, file_list):
1259 """Display revision"""
maruel@chromium.org54019f32010-09-09 13:50:11 +00001260 try:
1261 return scm.SVN.CaptureRevision(self.checkout_path)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +00001262 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.org54019f32010-09-09 13:50:11 +00001263 return None
msb@chromium.org0f282062009-11-06 20:14:02 +00001264
msb@chromium.orgcb5442b2009-09-22 16:51:24 +00001265 def runhooks(self, options, args, file_list):
1266 self.status(options, args, file_list)
1267
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001268 def status(self, options, args, file_list):
1269 """Display status information."""
maruel@chromium.org669600d2010-09-01 19:06:31 +00001270 command = ['status'] + args
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001271 if not os.path.isdir(self.checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001272 # svn status won't work if the directory doesn't exist.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001273 print(('\n________ couldn\'t run \'%s\' in \'%s\':\n'
1274 'The directory does not exist.') %
1275 (' '.join(command), self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001276 # There's no file list to retrieve.
1277 else:
maruel@chromium.org669600d2010-09-01 19:06:31 +00001278 self._RunAndGetFileList(command, options, file_list)
msb@chromium.orge6f78352010-01-13 17:05:33 +00001279
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +00001280 def GetUsableRev(self, rev, options):
1281 """Verifies the validity of the revision for this repository."""
1282 if not scm.SVN.IsValidRevision(url='%s@%s' % (self.url, rev)):
1283 raise gclient_utils.Error(
1284 ( '%s isn\'t a valid revision. Please check that your safesync_url is\n'
1285 'correct.') % rev)
1286 return rev
1287
msb@chromium.orge6f78352010-01-13 17:05:33 +00001288 def FullUrlForRelativeUrl(self, url):
1289 # Find the forth '/' and strip from there. A bit hackish.
1290 return '/'.join(self.url.split('/')[:4]) + url
tony@chromium.org99828122010-06-04 01:41:02 +00001291
maruel@chromium.org669600d2010-09-01 19:06:31 +00001292 def _Run(self, args, options, **kwargs):
1293 """Runs a commands that goes to stdout."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001294 kwargs.setdefault('cwd', self.checkout_path)
szager@chromium.org12b07e72013-05-03 22:06:34 +00001295 kwargs.setdefault('nag_timer', self.nag_timer)
1296 kwargs.setdefault('nag_max', self.nag_max)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001297 gclient_utils.CheckCallAndFilterAndHeader(['svn'] + args,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001298 always=options.verbose, **kwargs)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001299
1300 def _RunAndGetFileList(self, args, options, file_list, cwd=None):
1301 """Runs a commands that goes to stdout and grabs the file listed."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001302 cwd = cwd or self.checkout_path
maruel@chromium.orgce117f62011-01-17 20:04:25 +00001303 scm.SVN.RunAndGetFileList(
1304 options.verbose,
1305 args + ['--ignore-externals'],
1306 cwd=cwd,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001307 file_list=file_list)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001308
maruel@chromium.org6e29d572010-06-04 17:32:20 +00001309 @staticmethod
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001310 def _AddAdditionalUpdateFlags(command, options, revision):
tony@chromium.org99828122010-06-04 01:41:02 +00001311 """Add additional flags to command depending on what options are set.
1312 command should be a list of strings that represents an svn command.
1313
1314 This method returns a new list to be used as a command."""
1315 new_command = command[:]
1316 if revision:
1317 new_command.extend(['--revision', str(revision).strip()])
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001318 # We don't want interaction when jobs are used.
1319 if options.jobs > 1:
1320 new_command.append('--non-interactive')
tony@chromium.org99828122010-06-04 01:41:02 +00001321 # --force was added to 'svn update' in svn 1.5.
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001322 # --accept was added to 'svn update' in svn 1.6.
1323 if not scm.SVN.AssertVersion('1.5')[0]:
1324 return new_command
1325
1326 # It's annoying to have it block in the middle of a sync, just sensible
1327 # defaults.
1328 if options.force:
tony@chromium.org99828122010-06-04 01:41:02 +00001329 new_command.append('--force')
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001330 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1331 new_command.extend(('--accept', 'theirs-conflict'))
1332 elif options.manually_grab_svn_rev:
1333 new_command.append('--force')
1334 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1335 new_command.extend(('--accept', 'postpone'))
1336 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1337 new_command.extend(('--accept', 'postpone'))
tony@chromium.org99828122010-06-04 01:41:02 +00001338 return new_command