blob: 5c3929a9639792e358a350e311b65a152664735e [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)
iannucci@chromium.orgb8376882013-07-01 20:08:10 +0000246
247 fetch_cmd = [
248 '-c', 'core.deltaBaseCacheLimit=2g', 'fetch', 'origin', '--prune']
249 self._Run(fetch_cmd + quiet, options)
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000250 self._Run(['reset', '--hard', revision] + quiet, options)
251 self.UpdateSubmoduleConfig()
252 files = self._Capture(['ls-files']).splitlines()
253 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
254
msb@chromium.orge28e4982009-09-25 20:51:45 +0000255 def update(self, options, args, file_list):
256 """Runs git to update or transparently checkout the working copy.
257
258 All updated files will be appended to file_list.
259
260 Raises:
261 Error: if can't get URL for relative path.
262 """
msb@chromium.orge28e4982009-09-25 20:51:45 +0000263 if args:
264 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
265
nasser@codeaurora.orgece406f2010-02-23 17:29:15 +0000266 self._CheckMinVersion("1.6.6")
msb@chromium.org923a0372009-12-11 20:42:43 +0000267
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000268 default_rev = "refs/heads/master"
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000269 url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000270 rev_str = ""
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000271 revision = deps_revision
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000272 managed = True
msb@chromium.orge28e4982009-09-25 20:51:45 +0000273 if options.revision:
msb@chromium.orgac915bb2009-11-13 17:03:01 +0000274 # Override the revision number.
275 revision = str(options.revision)
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000276 if revision == 'unmanaged':
277 revision = None
278 managed = False
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000279 if not revision:
280 revision = default_rev
msb@chromium.orge28e4982009-09-25 20:51:45 +0000281
floitsch@google.comeaab7842011-04-28 09:07:58 +0000282 if gclient_utils.IsDateRevision(revision):
283 # Date-revisions only work on git-repositories if the reflog hasn't
284 # expired yet. Use rev-list to get the corresponding revision.
285 # git rev-list -n 1 --before='time-stamp' branchname
286 if options.transitive:
287 print('Warning: --transitive only works for SVN repositories.')
288 revision = default_rev
289
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000290 rev_str = ' at %s' % revision
291 files = []
292
293 printed_path = False
294 verbose = []
msb@chromium.orgb1a22bf2009-11-07 02:33:50 +0000295 if options.verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000296 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000297 verbose = ['--verbose']
298 printed_path = True
299
300 if revision.startswith('refs/heads/'):
301 rev_type = "branch"
302 elif revision.startswith('origin/'):
303 # For compatability with old naming, translate 'origin' to 'refs/heads'
304 revision = revision.replace('origin/', 'refs/heads/')
305 rev_type = "branch"
306 else:
307 # hash is also a tag, only make a distinction at checkout
308 rev_type = "hash"
309
szager@google.com873e6672012-03-13 18:53:36 +0000310 if not os.path.exists(self.checkout_path) or (
311 os.path.isdir(self.checkout_path) and
312 not os.listdir(self.checkout_path)):
maruel@chromium.org6c48a302011-10-20 23:44:20 +0000313 gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path))
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000314 self._Clone(revision, url, options)
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000315 self.UpdateSubmoduleConfig()
thomasvl@chromium.org858d6452011-03-24 17:59:20 +0000316 files = self._Capture(['ls-files']).splitlines()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000317 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000318 if not verbose:
319 # Make the output a little prettier. It's nice to have some whitespace
320 # between projects when cloning.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000321 print('')
msb@chromium.orge28e4982009-09-25 20:51:45 +0000322 return
323
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000324 if not managed:
mmoss@chromium.org96833fa2013-04-17 03:26:37 +0000325 self._UpdateBranchHeads(options, fetch=False)
szager@chromium.orgf5cc4272012-06-21 22:38:07 +0000326 self.UpdateSubmoduleConfig()
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +0000327 print ('________ unmanaged solution; skipping %s' % self.relpath)
328 return
329
msb@chromium.orge4af1ab2010-01-13 21:26:09 +0000330 if not os.path.exists(os.path.join(self.checkout_path, '.git')):
331 raise gclient_utils.Error('\n____ %s%s\n'
332 '\tPath is not a git repo. No .git dir.\n'
333 '\tTo resolve:\n'
334 '\t\trm -rf %s\n'
335 '\tAnd run gclient sync again\n'
336 % (self.relpath, rev_str, self.relpath))
337
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000338 # See if the url has changed (the unittests use git://foo for the url, let
339 # that through).
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000340 current_url = self._Capture(['config', 'remote.origin.url'])
thomasvl@chromium.orgd6f89d82011-03-25 20:41:58 +0000341 # TODO(maruel): Delete url != 'git://foo' since it's just to make the
342 # unit test pass. (and update the comment above)
ilevy@chromium.orgf6f58402013-05-22 00:14:32 +0000343 # Skip url auto-correction if remote.origin.gclient-auto-fix-url is set.
344 # This allows devs to use experimental repos which have a different url
345 # but whose branch(s) are the same as official repos.
346 if (current_url != url and
347 url != 'git://foo' and
348 subprocess2.capture(
349 ['git', 'config', 'remote.origin.gclient-auto-fix-url'],
350 cwd=self.checkout_path).strip() != 'False'):
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000351 print('_____ switching %s to a new upstream' % self.relpath)
352 # Make sure it's clean
353 self._CheckClean(rev_str)
354 # Switch over to the new upstream
355 self._Run(['remote', 'set-url', 'origin', url], options)
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000356 self._FetchAndReset(revision, file_list, options)
thomasvl@chromium.org668667c2011-03-24 18:27:24 +0000357 return
358
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000359 if not self._IsValidGitRepo():
360 # .git directory is hosed for some reason, set it back up.
361 print('_____ %s/.git is corrupted, rebuilding' % self.relpath)
362 self._Run(['init'], options)
363 self._Run(['remote', 'set-url', 'origin', url], options)
364
365 if not self._HasHead():
366 # Previous checkout was aborted before branches could be created in repo,
367 # so we need to reconstruct them here.
iannucci@chromium.orgb8376882013-07-01 20:08:10 +0000368 self._Run(['-c', 'core.deltaBaseCacheLimit=2g', 'pull', 'origin',
369 'master'], options)
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000370 self._FetchAndReset(revision, file_list, options)
371
msb@chromium.org5bde4852009-12-14 16:47:12 +0000372 cur_branch = self._GetCurrentBranch()
373
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000374 # Cases:
msb@chromium.org786fb682010-06-02 15:16:23 +0000375 # 0) HEAD is detached. Probably from our initial clone.
376 # - make sure HEAD is contained by a named ref, then update.
377 # Cases 1-4. HEAD is a branch.
378 # 1) current branch is not tracking a remote branch (could be git-svn)
379 # - try to rebase onto the new hash or branch
380 # 2) current branch is tracking a remote branch with local committed
381 # changes, but the DEPS file switched to point to a hash
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000382 # - rebase those changes on top of the hash
msb@chromium.org786fb682010-06-02 15:16:23 +0000383 # 3) current branch is tracking a remote branch w/or w/out changes,
384 # no switch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000385 # - see if we can FF, if not, prompt the user for rebase, merge, or stop
msb@chromium.org786fb682010-06-02 15:16:23 +0000386 # 4) current branch is tracking a remote branch, switches to a different
387 # remote branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000388 # - exit
389
maruel@chromium.org81e012c2010-04-29 16:07:24 +0000390 # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for
391 # a tracking branch
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000392 # or 'master' if not a tracking branch (it's based on a specific rev/hash)
393 # or it returns None if it couldn't find an upstream
msb@chromium.org786fb682010-06-02 15:16:23 +0000394 if cur_branch is None:
395 upstream_branch = None
396 current_type = "detached"
397 logging.debug("Detached HEAD")
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000398 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000399 upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
400 if not upstream_branch or not upstream_branch.startswith('refs/remotes'):
401 current_type = "hash"
402 logging.debug("Current branch is not tracking an upstream (remote)"
403 " branch.")
404 elif upstream_branch.startswith('refs/remotes'):
405 current_type = "branch"
406 else:
407 raise gclient_utils.Error('Invalid Upstream: %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000408
maruel@chromium.org92067382012-06-28 19:58:41 +0000409 if (not re.match(r'^[0-9a-fA-F]{40}$', revision) or
410 not scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=revision)):
bauerb@chromium.orgcbd20a42012-06-27 13:49:27 +0000411 # Update the remotes first so we have all the refs.
412 backoff_time = 5
413 for _ in range(10):
414 try:
415 remote_output = scm.GIT.Capture(
416 ['remote'] + verbose + ['update'],
417 cwd=self.checkout_path)
418 break
419 except subprocess2.CalledProcessError, e:
420 # Hackish but at that point, git is known to work so just checking for
421 # 502 in stderr should be fine.
422 if '502' in e.stderr:
423 print(str(e))
424 print('Sleeping %.1f seconds and retrying...' % backoff_time)
425 time.sleep(backoff_time)
426 backoff_time *= 1.3
427 continue
428 raise
maruel@chromium.org0b1c2462010-03-02 00:48:14 +0000429
bauerb@chromium.orgcbd20a42012-06-27 13:49:27 +0000430 if verbose:
431 print(remote_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000432
mmoss@chromium.orge409df62013-04-16 17:28:57 +0000433 self._UpdateBranchHeads(options, fetch=True)
434
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000435 # This is a big hammer, debatable if it should even be here...
davemoore@chromium.org793796d2010-02-19 17:27:41 +0000436 if options.force or options.reset:
iannucci@chromium.orgd4fffee2013-06-28 00:35:26 +0000437 target = 'HEAD'
438 if options.upstream and upstream_branch:
439 target = upstream_branch
440 self._Run(['reset', '--hard', target], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000441
msb@chromium.org786fb682010-06-02 15:16:23 +0000442 if current_type == 'detached':
443 # case 0
444 self._CheckClean(rev_str)
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000445 self._CheckDetachedHead(rev_str, options)
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000446 self._Capture(['checkout', '--quiet', '%s' % revision])
msb@chromium.org786fb682010-06-02 15:16:23 +0000447 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000448 print('\n_____ %s%s' % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000449 elif current_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000450 # case 1
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000451 if scm.GIT.IsGitSvn(self.checkout_path) and upstream_branch is not None:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000452 # Our git-svn branch (upstream_branch) is our upstream
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000453 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000454 newbase=revision, printed_path=printed_path)
455 printed_path = True
456 else:
457 # Can't find a merge-base since we don't know our upstream. That makes
458 # this command VERY likely to produce a rebase failure. For now we
459 # assume origin is our upstream since that's what the old behavior was.
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000460 upstream_branch = 'origin'
nasser@codeaurora.org7080e942010-03-15 15:06:16 +0000461 if options.revision or deps_revision:
nasser@codeaurora.org3b29de12010-03-08 18:34:28 +0000462 upstream_branch = revision
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000463 self._AttemptRebase(upstream_branch, files, options,
464 printed_path=printed_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000465 printed_path = True
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000466 elif rev_type == 'hash':
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000467 # case 2
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000468 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000469 newbase=revision, printed_path=printed_path)
470 printed_path = True
471 elif revision.replace('heads', 'remotes/origin') != upstream_branch:
472 # case 4
473 new_base = revision.replace('heads', 'remotes/origin')
474 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000475 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000476 switch_error = ("Switching upstream branch from %s to %s\n"
477 % (upstream_branch, new_base) +
478 "Please merge or rebase manually:\n" +
479 "cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
480 "OR git checkout -b <some new branch> %s" % new_base)
481 raise gclient_utils.Error(switch_error)
482 else:
483 # case 3 - the default case
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000484 files = self._Capture(['diff', upstream_branch, '--name-only']).split()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000485 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000486 print('Trying fast-forward merge to branch : %s' % upstream_branch)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000487 try:
bauerb@chromium.org2aad1b22011-07-22 12:00:41 +0000488 merge_args = ['merge']
489 if not options.merge:
490 merge_args.append('--ff-only')
491 merge_args.append(upstream_branch)
492 merge_output = scm.GIT.Capture(merge_args, cwd=self.checkout_path)
bratell@opera.com18fa4542013-05-21 13:30:46 +0000493 except subprocess2.CalledProcessError as e:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000494 if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr):
495 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000496 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000497 printed_path = True
498 while True:
499 try:
maruel@chromium.org90541732011-04-01 17:54:18 +0000500 action = ask_for_data(
501 'Cannot fast-forward merge, attempt to rebase? '
bratell@opera.com18fa4542013-05-21 13:30:46 +0000502 '(y)es / (q)uit / (s)kip : ', options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000503 except ValueError:
maruel@chromium.org90541732011-04-01 17:54:18 +0000504 raise gclient_utils.Error('Invalid Character')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000505 if re.match(r'yes|y', action, re.I):
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000506 self._AttemptRebase(upstream_branch, files, options,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000507 printed_path=printed_path)
508 printed_path = True
509 break
510 elif re.match(r'quit|q', action, re.I):
511 raise gclient_utils.Error("Can't fast-forward, please merge or "
512 "rebase manually.\n"
513 "cd %s && git " % self.checkout_path
514 + "rebase %s" % upstream_branch)
515 elif re.match(r'skip|s', action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000516 print('Skipping %s' % self.relpath)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000517 return
518 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000519 print('Input not recognized')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000520 elif re.match("error: Your local changes to '.*' would be "
521 "overwritten by merge. Aborting.\nPlease, commit your "
522 "changes or stash them before you can merge.\n",
523 e.stderr):
524 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000525 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000526 printed_path = True
527 raise gclient_utils.Error(e.stderr)
528 else:
529 # Some other problem happened with the merge
530 logging.error("Error during fast-forward merge in %s!" % self.relpath)
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000531 print(e.stderr)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000532 raise
533 else:
534 # Fast-forward merge was successful
535 if not re.match('Already up-to-date.', merge_output) or verbose:
536 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000537 print('\n_____ %s%s' % (self.relpath, rev_str))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000538 printed_path = True
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000539 print(merge_output.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000540 if not verbose:
541 # Make the output a little prettier. It's nice to have some
542 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000543 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000544
szager@chromium.orgd4af6622012-06-04 22:13:55 +0000545 self.UpdateSubmoduleConfig()
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000546 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
msb@chromium.org5bde4852009-12-14 16:47:12 +0000547
548 # If the rebase generated a conflict, abort and ask user to fix
msb@chromium.org786fb682010-06-02 15:16:23 +0000549 if self._IsRebasing():
msb@chromium.org5bde4852009-12-14 16:47:12 +0000550 raise gclient_utils.Error('\n____ %s%s\n'
551 '\nConflict while rebasing this branch.\n'
552 'Fix the conflict and run gclient again.\n'
553 'See man git-rebase for details.\n'
554 % (self.relpath, rev_str))
555
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000556 if verbose:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000557 print('Checked out revision %s' % self.revinfo(options, (), None))
msb@chromium.orge28e4982009-09-25 20:51:45 +0000558
steveblock@chromium.org98e69452012-02-16 16:36:43 +0000559 # If --reset and --delete_unversioned_trees are specified, remove any
560 # untracked directories.
561 if options.reset and options.delete_unversioned_trees:
562 # GIT.CaptureStatus() uses 'dit diff' to compare to a specific SHA1 (the
563 # merge-base by default), so doesn't include untracked files. So we use
564 # 'git ls-files --directory --others --exclude-standard' here directly.
565 paths = scm.GIT.Capture(
566 ['ls-files', '--directory', '--others', '--exclude-standard'],
567 self.checkout_path)
568 for path in (p for p in paths.splitlines() if p.endswith('/')):
569 full_path = os.path.join(self.checkout_path, path)
570 if not os.path.islink(full_path):
571 print('\n_____ removing unversioned directory %s' % path)
digit@chromium.orgdc112ac2013-04-24 13:00:19 +0000572 gclient_utils.rmtree(full_path)
steveblock@chromium.org98e69452012-02-16 16:36:43 +0000573
574
msb@chromium.orge28e4982009-09-25 20:51:45 +0000575 def revert(self, options, args, file_list):
576 """Reverts local modifications.
577
578 All reverted files will be appended to file_list.
579 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000580 if not os.path.isdir(self.checkout_path):
msb@chromium.org260c6532009-10-28 03:22:35 +0000581 # revert won't work if the directory doesn't exist. It needs to
582 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000583 print('\n_____ %s is missing, synching instead' % self.relpath)
msb@chromium.org260c6532009-10-28 03:22:35 +0000584 # Don't reuse the args.
585 return self.update(options, [], file_list)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000586
587 default_rev = "refs/heads/master"
iannucci@chromium.orgd4fffee2013-06-28 00:35:26 +0000588 if options.upstream:
589 if self._GetCurrentBranch():
590 upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
591 default_rev = upstream_branch or default_rev
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000592 _, deps_revision = gclient_utils.SplitUrlRevision(self.url)
nasser@codeaurora.orgb2b46312010-04-30 20:58:03 +0000593 if not deps_revision:
594 deps_revision = default_rev
595 if deps_revision.startswith('refs/heads/'):
596 deps_revision = deps_revision.replace('refs/heads/', 'origin/')
597
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000598 files = self._Capture(['diff', deps_revision, '--name-only']).split()
maruel@chromium.org37e89872010-09-07 16:11:33 +0000599 self._Run(['reset', '--hard', deps_revision], options)
lliabraa@chromium.orgade83db2012-09-27 14:06:49 +0000600 self._Run(['clean', '-f', '-d'], options)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000601 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
602
msb@chromium.org0f282062009-11-06 20:14:02 +0000603 def revinfo(self, options, args, file_list):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000604 """Returns revision"""
605 return self._Capture(['rev-parse', 'HEAD'])
msb@chromium.org0f282062009-11-06 20:14:02 +0000606
msb@chromium.orge28e4982009-09-25 20:51:45 +0000607 def runhooks(self, options, args, file_list):
608 self.status(options, args, file_list)
609
610 def status(self, options, args, file_list):
611 """Display status information."""
612 if not os.path.isdir(self.checkout_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000613 print(('\n________ couldn\'t run status in %s:\n'
614 'The directory does not exist.') % self.checkout_path)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000615 else:
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000616 merge_base = self._Capture(['merge-base', 'HEAD', 'origin'])
maruel@chromium.org37e89872010-09-07 16:11:33 +0000617 self._Run(['diff', '--name-status', merge_base], options)
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000618 files = self._Capture(['diff', '--name-only', merge_base]).split()
msb@chromium.orge28e4982009-09-25 20:51:45 +0000619 file_list.extend([os.path.join(self.checkout_path, f) for f in files])
620
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000621 def GetUsableRev(self, rev, options):
622 """Finds a useful revision for this repository.
623
624 If SCM is git-svn and the head revision is less than |rev|, git svn fetch
625 will be called on the source."""
626 sha1 = None
iannucci@chromium.org3830a672013-02-19 20:15:14 +0000627 if not os.path.isdir(self.checkout_path):
628 raise gclient_utils.Error(
629 ( 'We could not find a valid hash for safesync_url response "%s".\n'
630 'Safesync URLs with a git checkout currently require the repo to\n'
631 'be cloned without a safesync_url before adding the safesync_url.\n'
632 'For more info, see: '
633 'http://code.google.com/p/chromium/wiki/UsingNewGit'
634 '#Initial_checkout' ) % rev)
635 elif rev.isdigit() and len(rev) < 7:
636 # Handles an SVN rev. As an optimization, only verify an SVN revision as
637 # [0-9]{1,6} for now to avoid making a network request.
638 if scm.GIT.IsGitSvn(cwd=self.checkout_path):
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000639 local_head = scm.GIT.GetGitSvnHeadRev(cwd=self.checkout_path)
640 if not local_head or local_head < int(rev):
dbeam@chromium.org2a75fdb2012-02-15 01:32:57 +0000641 try:
642 logging.debug('Looking for git-svn configuration optimizations.')
643 if scm.GIT.Capture(['config', '--get', 'svn-remote.svn.fetch'],
644 cwd=self.checkout_path):
645 scm.GIT.Capture(['fetch'], cwd=self.checkout_path)
646 except subprocess2.CalledProcessError:
647 logging.debug('git config --get svn-remote.svn.fetch failed, '
648 'ignoring possible optimization.')
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000649 if options.verbose:
650 print('Running git svn fetch. This might take a while.\n')
651 scm.GIT.Capture(['svn', 'fetch'], cwd=self.checkout_path)
szager@google.com312a6a42012-10-11 21:19:42 +0000652 try:
szager@chromium.orgc51def32012-10-15 18:50:37 +0000653 sha1 = scm.GIT.GetBlessedSha1ForSvnRev(
654 cwd=self.checkout_path, rev=rev)
szager@google.com312a6a42012-10-11 21:19:42 +0000655 except gclient_utils.Error, e:
656 sha1 = e.message
657 print('\nWarning: Could not find a git revision with accurate\n'
658 '.DEPS.git that maps to SVN revision %s. Sync-ing to\n'
659 'the closest sane git revision, which is:\n'
660 ' %s\n' % (rev, e.message))
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000661 if not sha1:
662 raise gclient_utils.Error(
663 ( 'It appears that either your git-svn remote is incorrectly\n'
664 'configured or the revision in your safesync_url is\n'
665 'higher than git-svn remote\'s HEAD as we couldn\'t find a\n'
666 'corresponding git hash for SVN rev %s.' ) % rev)
iannucci@chromium.org3830a672013-02-19 20:15:14 +0000667 else:
668 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
669 sha1 = rev
670 else:
671 # May exist in origin, but we don't have it yet, so fetch and look
672 # again.
673 scm.GIT.Capture(['fetch', 'origin'], cwd=self.checkout_path)
674 if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
675 sha1 = rev
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000676
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000677 if not sha1:
678 raise gclient_utils.Error(
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000679 ( 'We could not find a valid hash for safesync_url response "%s".\n'
680 'Safesync URLs with a git checkout currently require a git-svn\n'
681 'remote or a safesync_url that provides git sha1s. Please add a\n'
682 'git-svn remote or change your safesync_url. For more info, see:\n'
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000683 'http://code.google.com/p/chromium/wiki/UsingNewGit'
dbeam@chromium.org051c88b2011-12-22 00:23:03 +0000684 '#Initial_checkout' ) % rev)
685
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +0000686 return sha1
687
msb@chromium.orge6f78352010-01-13 17:05:33 +0000688 def FullUrlForRelativeUrl(self, url):
689 # Strip from last '/'
690 # Equivalent to unix basename
691 base_url = self.url
692 return base_url[:base_url.rfind('/')] + url
693
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000694 def _Clone(self, revision, url, options):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000695 """Clone a git repository from the given URL.
696
msb@chromium.org786fb682010-06-02 15:16:23 +0000697 Once we've cloned the repo, we checkout a working branch if the specified
698 revision is a branch head. If it is a tag or a specific commit, then we
699 leave HEAD detached as it makes future updates simpler -- in this case the
700 user should first create a new branch or switch to an existing branch before
701 making changes in the repo."""
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000702 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000703 # git clone doesn't seem to insert a newline properly before printing
704 # to stdout
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000705 print('')
iannucci@chromium.orgb8376882013-07-01 20:08:10 +0000706 clone_cmd = ['-c', 'core.deltaBaseCacheLimit=2g', 'clone', '--progress']
msb@chromium.org786fb682010-06-02 15:16:23 +0000707 if revision.startswith('refs/heads/'):
708 clone_cmd.extend(['-b', revision.replace('refs/heads/', '')])
709 detach_head = False
710 else:
msb@chromium.org786fb682010-06-02 15:16:23 +0000711 detach_head = True
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000712 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000713 clone_cmd.append('--verbose')
714 clone_cmd.extend([url, self.checkout_path])
715
nsylvain@chromium.org328c3c72011-06-01 20:50:27 +0000716 # If the parent directory does not exist, Git clone on Windows will not
717 # create it, so we need to do it manually.
718 parent_dir = os.path.dirname(self.checkout_path)
719 if not os.path.exists(parent_dir):
maruel@chromium.org6c48a302011-10-20 23:44:20 +0000720 gclient_utils.safe_makedirs(parent_dir)
nsylvain@chromium.org328c3c72011-06-01 20:50:27 +0000721
szager@google.com85d3e3a2011-10-07 17:12:00 +0000722 percent_re = re.compile('.* ([0-9]{1,2})% .*')
723 def _GitFilter(line):
724 # git uses an escape sequence to clear the line; elide it.
725 esc = line.find(unichr(033))
726 if esc > -1:
727 line = line[:esc]
728 match = percent_re.match(line)
729 if not match or not int(match.group(1)) % 10:
730 print '%s' % line
731
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000732 for _ in range(3):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000733 try:
szager@google.com85d3e3a2011-10-07 17:12:00 +0000734 self._Run(clone_cmd, options, cwd=self._root_dir, filter_fn=_GitFilter,
735 print_stdout=False)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000736 break
maruel@chromium.org2a5b6a22011-09-09 14:03:12 +0000737 except subprocess2.CalledProcessError, e:
738 # Too bad we don't have access to the actual output yet.
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000739 # We should check for "transfer closed with NNN bytes remaining to
740 # read". In the meantime, just make sure .git exists.
maruel@chromium.org2a5b6a22011-09-09 14:03:12 +0000741 if (e.returncode == 128 and
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000742 os.path.exists(os.path.join(self.checkout_path, '.git'))):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000743 print(str(e))
744 print('Retrying...')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000745 continue
746 raise e
747
mmoss@chromium.orge409df62013-04-16 17:28:57 +0000748 # Update the "branch-heads" remote-tracking branches, since we might need it
749 # to checkout a specific revision below.
750 self._UpdateBranchHeads(options, fetch=True)
mmoss@chromium.org059cc452013-03-11 15:14:35 +0000751
msb@chromium.org786fb682010-06-02 15:16:23 +0000752 if detach_head:
753 # Squelch git's very verbose detached HEAD warning and use our own
nsylvain@chromium.orgf7826d72011-06-02 18:20:14 +0000754 self._Capture(['checkout', '--quiet', '%s' % revision])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000755 print(
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000756 ('Checked out %s to a detached HEAD. Before making any commits\n'
757 'in this repo, you should use \'git checkout <branch>\' to switch to\n'
758 'an existing branch or use \'git checkout origin -b <branch>\' to\n'
759 'create a new branch for your work.') % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000760
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000761 def _AttemptRebase(self, upstream, files, options, newbase=None,
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000762 branch=None, printed_path=False):
763 """Attempt to rebase onto either upstream or, if specified, newbase."""
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000764 files.extend(self._Capture(['diff', upstream, '--name-only']).split())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000765 revision = upstream
766 if newbase:
767 revision = newbase
768 if not printed_path:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000769 print('\n_____ %s : Attempting rebase onto %s...' % (
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000770 self.relpath, revision))
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000771 printed_path = True
772 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000773 print('Attempting rebase onto %s...' % revision)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000774
775 # Build the rebase command here using the args
776 # git rebase [options] [--onto <newbase>] <upstream> [<branch>]
777 rebase_cmd = ['rebase']
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000778 if options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000779 rebase_cmd.append('--verbose')
780 if newbase:
781 rebase_cmd.extend(['--onto', newbase])
782 rebase_cmd.append(upstream)
783 if branch:
784 rebase_cmd.append(branch)
785
786 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000787 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000788 except subprocess2.CalledProcessError, e:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000789 if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or
790 re.match(r'cannot rebase: your index contains uncommitted changes',
791 e.stderr)):
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000792 while True:
maruel@chromium.org90541732011-04-01 17:54:18 +0000793 rebase_action = ask_for_data(
794 'Cannot rebase because of unstaged changes.\n'
795 '\'git reset --hard HEAD\' ?\n'
796 'WARNING: destroys any uncommitted work in your current branch!'
bratell@opera.com18fa4542013-05-21 13:30:46 +0000797 ' (y)es / (q)uit / (s)how : ', options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000798 if re.match(r'yes|y', rebase_action, re.I):
maruel@chromium.org37e89872010-09-07 16:11:33 +0000799 self._Run(['reset', '--hard', 'HEAD'], options)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000800 # Should this be recursive?
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000801 rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000802 break
803 elif re.match(r'quit|q', rebase_action, re.I):
804 raise gclient_utils.Error("Please merge or rebase manually\n"
805 "cd %s && git " % self.checkout_path
806 + "%s" % ' '.join(rebase_cmd))
807 elif re.match(r'show|s', rebase_action, re.I):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000808 print('\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000809 continue
810 else:
811 gclient_utils.Error("Input not recognized")
812 continue
813 elif re.search(r'^CONFLICT', e.stdout, re.M):
814 raise gclient_utils.Error("Conflict while rebasing this branch.\n"
815 "Fix the conflict and run gclient again.\n"
816 "See 'man git-rebase' for details.\n")
817 else:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000818 print(e.stdout.strip())
819 print('Rebase produced error output:\n%s' % e.stderr.strip())
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000820 raise gclient_utils.Error("Unrecognized error, please merge or rebase "
821 "manually.\ncd %s && git " %
822 self.checkout_path
823 + "%s" % ' '.join(rebase_cmd))
824
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000825 print(rebase_output.strip())
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000826 if not options.verbose:
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000827 # Make the output a little prettier. It's nice to have some
828 # whitespace between projects when syncing.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000829 print('')
nasser@codeaurora.orgd90ba3f2010-02-23 14:42:57 +0000830
xusydoc@chromium.orgc3761712013-06-12 00:53:36 +0000831 def _IsValidGitRepo(self):
832 """Returns if the directory is a valid git repository.
833
834 Checks if git status works.
835 """
836 try:
837 self._Capture(['status'])
838 return True
839 except subprocess2.CalledProcessError:
840 return False
841
842 def _HasHead(self):
843 """Returns True if any commit is checked out.
844
845 This is done by checking if rev-parse HEAD works in the current repository.
846 """
847 try:
848 self._GetCurrentBranch()
849 return True
850 except subprocess2.CalledProcessError:
851 return False
852
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000853 @staticmethod
854 def _CheckMinVersion(min_version):
maruel@chromium.orgd0f854a2010-03-11 19:35:53 +0000855 (ok, current_version) = scm.GIT.AssertVersion(min_version)
856 if not ok:
857 raise gclient_utils.Error('git version %s < minimum required %s' %
858 (current_version, min_version))
msb@chromium.org923a0372009-12-11 20:42:43 +0000859
msb@chromium.org786fb682010-06-02 15:16:23 +0000860 def _IsRebasing(self):
861 # Check for any of REBASE-i/REBASE-m/REBASE/AM. Unfortunately git doesn't
862 # have a plumbing command to determine whether a rebase is in progress, so
863 # for now emualate (more-or-less) git-rebase.sh / git-completion.bash
864 g = os.path.join(self.checkout_path, '.git')
865 return (
866 os.path.isdir(os.path.join(g, "rebase-merge")) or
867 os.path.isdir(os.path.join(g, "rebase-apply")))
868
869 def _CheckClean(self, rev_str):
870 # Make sure the tree is clean; see git-rebase.sh for reference
871 try:
872 scm.GIT.Capture(['update-index', '--ignore-submodules', '--refresh'],
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000873 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000874 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000875 raise gclient_utils.Error('\n____ %s%s\n'
876 '\tYou have unstaged changes.\n'
877 '\tPlease commit, stash, or reset.\n'
878 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000879 try:
880 scm.GIT.Capture(['diff-index', '--cached', '--name-status', '-r',
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000881 '--ignore-submodules', 'HEAD', '--'],
882 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000883 except subprocess2.CalledProcessError:
maruel@chromium.org6e29d572010-06-04 17:32:20 +0000884 raise gclient_utils.Error('\n____ %s%s\n'
885 '\tYour index contains uncommitted changes\n'
886 '\tPlease commit, stash, or reset.\n'
887 % (self.relpath, rev_str))
msb@chromium.org786fb682010-06-02 15:16:23 +0000888
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000889 def _CheckDetachedHead(self, rev_str, options):
msb@chromium.org786fb682010-06-02 15:16:23 +0000890 # HEAD is detached. Make sure it is safe to move away from (i.e., it is
891 # reference by a commit). If not, error out -- most likely a rebase is
892 # in progress, try to detect so we can give a better error.
893 try:
maruel@chromium.orgad80e3b2010-09-09 14:18:28 +0000894 scm.GIT.Capture(['name-rev', '--no-undefined', 'HEAD'],
895 cwd=self.checkout_path)
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000896 except subprocess2.CalledProcessError:
msb@chromium.org786fb682010-06-02 15:16:23 +0000897 # Commit is not contained by any rev. See if the user is rebasing:
898 if self._IsRebasing():
899 # Punt to the user
900 raise gclient_utils.Error('\n____ %s%s\n'
901 '\tAlready in a conflict, i.e. (no branch).\n'
902 '\tFix the conflict and run gclient again.\n'
903 '\tOr to abort run:\n\t\tgit-rebase --abort\n'
904 '\tSee man git-rebase for details.\n'
905 % (self.relpath, rev_str))
906 # Let's just save off the commit so we can proceed.
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000907 name = ('saved-by-gclient-' +
908 self._Capture(['rev-parse', '--short', 'HEAD']))
909 self._Capture(['branch', name])
maruel@chromium.org77e4eca2010-09-21 13:23:07 +0000910 print('\n_____ found an unreferenced commit and saved it as \'%s\'' %
maruel@chromium.orgf5d37bf2010-09-02 00:50:34 +0000911 name)
msb@chromium.org786fb682010-06-02 15:16:23 +0000912
msb@chromium.org5bde4852009-12-14 16:47:12 +0000913 def _GetCurrentBranch(self):
msb@chromium.org786fb682010-06-02 15:16:23 +0000914 # Returns name of current branch or None for detached HEAD
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000915 branch = self._Capture(['rev-parse', '--abbrev-ref=strict', 'HEAD'])
msb@chromium.org786fb682010-06-02 15:16:23 +0000916 if branch == 'HEAD':
msb@chromium.org5bde4852009-12-14 16:47:12 +0000917 return None
918 return branch
919
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000920 def _Capture(self, args):
maruel@chromium.orgbffad372011-09-08 17:54:22 +0000921 return subprocess2.check_output(
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000922 ['git'] + args,
ilevy@chromium.orgf6f58402013-05-22 00:14:32 +0000923 stderr=subprocess2.VOID,
szager@chromium.org12b07e72013-05-03 22:06:34 +0000924 nag_timer=self.nag_timer,
925 nag_max=self.nag_max,
maruel@chromium.org87e6d332011-09-09 19:01:28 +0000926 cwd=self.checkout_path).strip()
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000927
mmoss@chromium.orge409df62013-04-16 17:28:57 +0000928 def _UpdateBranchHeads(self, options, fetch=False):
929 """Adds, and optionally fetches, "branch-heads" refspecs if requested."""
930 if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
931 backoff_time = 5
932 for _ in range(3):
933 try:
934 config_cmd = ['config', 'remote.origin.fetch',
935 '+refs/branch-heads/*:refs/remotes/branch-heads/*',
936 '^\\+refs/branch-heads/\\*:.*$']
937 self._Run(config_cmd, options)
938 if fetch:
iannucci@chromium.orgb8376882013-07-01 20:08:10 +0000939 fetch_cmd = ['-c', 'core.deltaBaseCacheLimit=2g', 'fetch', 'origin']
mmoss@chromium.orge409df62013-04-16 17:28:57 +0000940 if options.verbose:
941 fetch_cmd.append('--verbose')
942 self._Run(fetch_cmd, options)
943 break
944 except subprocess2.CalledProcessError, e:
945 print(str(e))
946 print('Retrying in %.1f seconds...' % backoff_time)
947 time.sleep(backoff_time)
948 backoff_time *= 1.3
949
maruel@chromium.org37e89872010-09-07 16:11:33 +0000950 def _Run(self, args, options, **kwargs):
maruel@chromium.org6cafa132010-09-07 14:17:26 +0000951 kwargs.setdefault('cwd', self.checkout_path)
szager@google.com85d3e3a2011-10-07 17:12:00 +0000952 kwargs.setdefault('print_stdout', True)
szager@chromium.org12b07e72013-05-03 22:06:34 +0000953 kwargs.setdefault('nag_timer', self.nag_timer)
954 kwargs.setdefault('nag_max', self.nag_max)
szager@google.com85d3e3a2011-10-07 17:12:00 +0000955 stdout = kwargs.get('stdout', sys.stdout)
956 stdout.write('\n________ running \'git %s\' in \'%s\'\n' % (
957 ' '.join(args), kwargs['cwd']))
958 gclient_utils.CheckCallAndFilter(['git'] + args, **kwargs)
msb@chromium.orge28e4982009-09-25 20:51:45 +0000959
960
maruel@chromium.org55e724e2010-03-11 19:36:49 +0000961class SVNWrapper(SCMWrapper):
msb@chromium.orgcb5442b2009-09-22 16:51:24 +0000962 """ Wrapper for SVN """
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000963
mukai@chromium.org9e3e82c2012-04-18 12:55:43 +0000964 @staticmethod
965 def BinaryExists():
966 """Returns true if the command exists."""
967 try:
968 result, version = scm.SVN.AssertVersion('1.4')
969 if not result:
970 raise gclient_utils.Error('SVN version is older than 1.4: %s' % version)
971 return result
972 except OSError:
973 return False
974
xusydoc@chromium.org885a9602013-05-31 09:54:40 +0000975 def GetCheckoutRoot(self):
976 return scm.SVN.GetCheckoutRoot(self.checkout_path)
977
floitsch@google.comeaab7842011-04-28 09:07:58 +0000978 def GetRevisionDate(self, revision):
979 """Returns the given revision's date in ISO-8601 format (which contains the
980 time zone)."""
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +0000981 date = scm.SVN.Capture(
982 ['propget', '--revprop', 'svn:date', '-r', revision],
983 os.path.join(self.checkout_path, '.'))
floitsch@google.comeaab7842011-04-28 09:07:58 +0000984 return date.strip()
985
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000986 def cleanup(self, options, args, file_list):
987 """Cleanup working copy."""
maruel@chromium.org669600d2010-09-01 19:06:31 +0000988 self._Run(['cleanup'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000989
990 def diff(self, options, args, file_list):
991 # NOTE: This function does not currently modify file_list.
maruel@chromium.org8469bf92010-09-03 19:03:15 +0000992 if not os.path.isdir(self.checkout_path):
993 raise gclient_utils.Error('Directory %s is not present.' %
994 self.checkout_path)
maruel@chromium.org669600d2010-09-01 19:06:31 +0000995 self._Run(['diff'] + args, options)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +0000996
maruel@chromium.orgee4071d2009-12-22 22:25:37 +0000997 def pack(self, options, args, file_list):
998 """Generates a patch file which can be applied to the root of the
999 repository."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001000 if not os.path.isdir(self.checkout_path):
1001 raise gclient_utils.Error('Directory %s is not present.' %
1002 self.checkout_path)
1003 gclient_utils.CheckCallAndFilter(
1004 ['svn', 'diff', '-x', '--ignore-eol-style'] + args,
1005 cwd=self.checkout_path,
1006 print_stdout=False,
szager@chromium.org12b07e72013-05-03 22:06:34 +00001007 nag_timer=self.nag_timer,
1008 nag_max=self.nag_max,
haitao.feng@intel.com306080c2012-05-04 13:11:29 +00001009 filter_fn=SvnDiffFilterer(self.relpath).Filter)
maruel@chromium.orgee4071d2009-12-22 22:25:37 +00001010
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001011 def update(self, options, args, file_list):
msb@chromium.orgd6504212010-01-13 17:34:31 +00001012 """Runs svn to update or transparently checkout the working copy.
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001013
1014 All updated files will be appended to file_list.
1015
1016 Raises:
1017 Error: if can't get URL for relative path.
1018 """
morrita@chromium.org21dca0e2010-10-05 00:55:12 +00001019 # Only update if git or hg is not controlling the directory.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001020 git_path = os.path.join(self.checkout_path, '.git')
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001021 if os.path.exists(git_path):
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001022 print('________ found .git directory; skipping %s' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001023 return
1024
morrita@chromium.org21dca0e2010-10-05 00:55:12 +00001025 hg_path = os.path.join(self.checkout_path, '.hg')
1026 if os.path.exists(hg_path):
1027 print('________ found .hg directory; skipping %s' % self.relpath)
1028 return
1029
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001030 if args:
1031 raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args))
1032
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001033 # revision is the revision to match. It is None if no revision is specified,
1034 # i.e. the 'deps ain't pinned'.
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001035 url, revision = gclient_utils.SplitUrlRevision(self.url)
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001036 # Keep the original unpinned url for reference in case the repo is switched.
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001037 base_url = url
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +00001038 managed = True
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001039 if options.revision:
1040 # Override the revision number.
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001041 revision = str(options.revision)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001042 if revision:
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +00001043 if revision != 'unmanaged':
1044 forced_revision = True
1045 # Reconstruct the url.
1046 url = '%s@%s' % (url, revision)
1047 rev_str = ' at %s' % revision
1048 else:
1049 managed = False
1050 revision = None
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001051 else:
1052 forced_revision = False
1053 rev_str = ''
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001054
davidjames@chromium.org13349e22012-11-15 17:11:28 +00001055 # Get the existing scm url and the revision number of the current checkout.
1056 exists = os.path.exists(self.checkout_path)
1057 if exists and managed:
1058 try:
1059 from_info = scm.SVN.CaptureLocalInfo(
1060 [], os.path.join(self.checkout_path, '.'))
1061 except (gclient_utils.Error, subprocess2.CalledProcessError):
1062 if options.reset and options.delete_unversioned_trees:
1063 print 'Removing troublesome path %s' % self.checkout_path
1064 gclient_utils.rmtree(self.checkout_path)
1065 exists = False
1066 else:
1067 msg = ('Can\'t update/checkout %s if an unversioned directory is '
1068 'present. Delete the directory and try again.')
1069 raise gclient_utils.Error(msg % self.checkout_path)
1070
1071 if not exists:
maruel@chromium.org6c48a302011-10-20 23:44:20 +00001072 gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001073 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001074 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001075 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001076 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001077 return
1078
cmp@chromium.orgeb2756d2011-09-20 20:17:51 +00001079 if not managed:
1080 print ('________ unmanaged solution; skipping %s' % self.relpath)
1081 return
1082
maruel@chromium.org49fcb0c2011-09-23 14:34:38 +00001083 if 'URL' not in from_info:
1084 raise gclient_utils.Error(
1085 ('gclient is confused. Couldn\'t get the url for %s.\n'
1086 'Try using @unmanaged.\n%s') % (
1087 self.checkout_path, from_info))
1088
maruel@chromium.orge407c9a2010-08-09 19:11:37 +00001089 # Look for locked directories.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001090 dir_info = scm.SVN.CaptureStatus(
1091 None, os.path.join(self.checkout_path, '.'))
phajdan.jr@chromium.orgd558c4b2011-09-22 18:56:24 +00001092 if any(d[0][2] == 'L' for d in dir_info):
1093 try:
1094 self._Run(['cleanup', self.checkout_path], options)
1095 except subprocess2.CalledProcessError, e:
1096 # Get the status again, svn cleanup may have cleaned up at least
1097 # something.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001098 dir_info = scm.SVN.CaptureStatus(
1099 None, os.path.join(self.checkout_path, '.'))
phajdan.jr@chromium.orgd558c4b2011-09-22 18:56:24 +00001100
1101 # Try to fix the failures by removing troublesome files.
1102 for d in dir_info:
1103 if d[0][2] == 'L':
1104 if d[0][0] == '!' and options.force:
1105 print 'Removing troublesome path %s' % d[1]
1106 gclient_utils.rmtree(d[1])
1107 else:
1108 print 'Not removing troublesome path %s automatically.' % d[1]
1109 if d[0][0] == '!':
1110 print 'You can pass --force to enable automatic removal.'
1111 raise e
maruel@chromium.orge407c9a2010-08-09 19:11:37 +00001112
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001113 # Retrieve the current HEAD version because svn is slow at null updates.
1114 if options.manually_grab_svn_rev and not revision:
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001115 from_info_live = scm.SVN.CaptureRemoteInfo(from_info['URL'])
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001116 revision = str(from_info_live['Revision'])
1117 rev_str = ' at %s' % revision
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001118
msb@chromium.orgac915bb2009-11-13 17:03:01 +00001119 if from_info['URL'] != base_url:
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001120 # The repository url changed, need to switch.
maruel@chromium.org54019f32010-09-09 13:50:11 +00001121 try:
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001122 to_info = scm.SVN.CaptureRemoteInfo(url)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +00001123 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.orge2ce0c72009-09-23 16:14:18 +00001124 # The url is invalid or the server is not accessible, it's safer to bail
1125 # out right now.
1126 raise gclient_utils.Error('This url is unreachable: %s' % url)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001127 can_switch = ((from_info['Repository Root'] != to_info['Repository Root'])
1128 and (from_info['UUID'] == to_info['UUID']))
1129 if can_switch:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001130 print('\n_____ relocating %s to a new checkout' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001131 # We have different roots, so check if we can switch --relocate.
1132 # Subversion only permits this if the repository UUIDs match.
1133 # Perform the switch --relocate, then rewrite the from_url
1134 # to reflect where we "are now." (This is the same way that
1135 # Subversion itself handles the metadata when switch --relocate
1136 # is used.) This makes the checks below for whether we
1137 # can update to a revision or have to switch to a different
1138 # branch work as expected.
1139 # TODO(maruel): TEST ME !
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001140 command = ['switch', '--relocate',
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001141 from_info['Repository Root'],
1142 to_info['Repository Root'],
1143 self.relpath]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001144 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001145 from_info['URL'] = from_info['URL'].replace(
1146 from_info['Repository Root'],
1147 to_info['Repository Root'])
1148 else:
maruel@chromium.org3294f522010-08-18 19:54:57 +00001149 if not options.force and not options.reset:
maruel@chromium.org86f0f952010-08-10 17:17:19 +00001150 # Look for local modifications but ignore unversioned files.
maruel@chromium.orgd579fcf2011-12-13 20:36:03 +00001151 for status in scm.SVN.CaptureStatus(None, self.checkout_path):
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001152 if status[0][0] != '?':
maruel@chromium.org86f0f952010-08-10 17:17:19 +00001153 raise gclient_utils.Error(
1154 ('Can\'t switch the checkout to %s; UUID don\'t match and '
1155 'there is local changes in %s. Delete the directory and '
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001156 'try again.') % (url, self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001157 # Ok delete it.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001158 print('\n_____ switching %s to a new checkout' % self.relpath)
digit@chromium.orgdc112ac2013-04-24 13:00:19 +00001159 gclient_utils.rmtree(self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001160 # We need to checkout.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001161 command = ['checkout', url, self.checkout_path]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001162 command = self._AddAdditionalUpdateFlags(command, options, revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001163 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001164 return
1165
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001166 # If the provided url has a revision number that matches the revision
1167 # number of the existing directory, then we don't need to bother updating.
maruel@chromium.org2e0c6852009-09-24 00:02:07 +00001168 if not options.force and str(from_info['Revision']) == revision:
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001169 if options.verbose or not forced_revision:
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001170 print('\n_____ %s%s' % (self.relpath, rev_str))
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001171 else:
1172 command = ['update', self.checkout_path]
1173 command = self._AddAdditionalUpdateFlags(command, options, revision)
1174 self._RunAndGetFileList(command, options, file_list, self._root_dir)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001175
steveblock@chromium.org98e69452012-02-16 16:36:43 +00001176 # If --reset and --delete_unversioned_trees are specified, remove any
1177 # untracked files and directories.
1178 if options.reset and options.delete_unversioned_trees:
1179 for status in scm.SVN.CaptureStatus(None, self.checkout_path):
1180 full_path = os.path.join(self.checkout_path, status[1])
1181 if (status[0][0] == '?'
1182 and os.path.isdir(full_path)
1183 and not os.path.islink(full_path)):
1184 print('\n_____ removing unversioned directory %s' % status[1])
digit@chromium.orgdc112ac2013-04-24 13:00:19 +00001185 gclient_utils.rmtree(full_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001186
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001187 def updatesingle(self, options, args, file_list):
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001188 filename = args.pop()
tony@chromium.org57564662010-04-14 02:35:12 +00001189 if scm.SVN.AssertVersion("1.5")[0]:
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001190 if not os.path.exists(os.path.join(self.checkout_path, '.svn')):
tony@chromium.org57564662010-04-14 02:35:12 +00001191 # Create an empty checkout and then update the one file we want. Future
1192 # operations will only apply to the one file we checked out.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001193 command = ["checkout", "--depth", "empty", self.url, self.checkout_path]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001194 self._Run(command, options, cwd=self._root_dir)
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001195 if os.path.exists(os.path.join(self.checkout_path, filename)):
1196 os.remove(os.path.join(self.checkout_path, filename))
tony@chromium.org57564662010-04-14 02:35:12 +00001197 command = ["update", filename]
maruel@chromium.org669600d2010-09-01 19:06:31 +00001198 self._RunAndGetFileList(command, options, file_list)
tony@chromium.org57564662010-04-14 02:35:12 +00001199 # After the initial checkout, we can use update as if it were any other
1200 # dep.
1201 self.update(options, args, file_list)
1202 else:
1203 # If the installed version of SVN doesn't support --depth, fallback to
1204 # just exporting the file. This has the downside that revision
1205 # information is not stored next to the file, so we will have to
1206 # re-export the file every time we sync.
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001207 if not os.path.exists(self.checkout_path):
maruel@chromium.org6c48a302011-10-20 23:44:20 +00001208 gclient_utils.safe_makedirs(self.checkout_path)
tony@chromium.org57564662010-04-14 02:35:12 +00001209 command = ["export", os.path.join(self.url, filename),
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001210 os.path.join(self.checkout_path, filename)]
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001211 command = self._AddAdditionalUpdateFlags(command, options,
1212 options.revision)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001213 self._Run(command, options, cwd=self._root_dir)
tony@chromium.org4b5b1772010-04-08 01:52:56 +00001214
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001215 def revert(self, options, args, file_list):
1216 """Reverts local modifications. Subversion specific.
1217
1218 All reverted files will be appended to file_list, even if Subversion
1219 doesn't know about them.
1220 """
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001221 if not os.path.isdir(self.checkout_path):
maruel@chromium.orgc0cc0872011-10-12 17:02:41 +00001222 if os.path.exists(self.checkout_path):
1223 gclient_utils.rmtree(self.checkout_path)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001224 # svn revert won't work if the directory doesn't exist. It needs to
1225 # checkout instead.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001226 print('\n_____ %s is missing, synching instead' % self.relpath)
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001227 # Don't reuse the args.
1228 return self.update(options, [], file_list)
1229
maruel@chromium.orgc0cc0872011-10-12 17:02:41 +00001230 if not os.path.isdir(os.path.join(self.checkout_path, '.svn')):
1231 if os.path.isdir(os.path.join(self.checkout_path, '.git')):
1232 print('________ found .git directory; skipping %s' % self.relpath)
1233 return
1234 if os.path.isdir(os.path.join(self.checkout_path, '.hg')):
1235 print('________ found .hg directory; skipping %s' % self.relpath)
1236 return
1237 if not options.force:
1238 raise gclient_utils.Error('Invalid checkout path, aborting')
1239 print(
1240 '\n_____ %s is not a valid svn checkout, synching instead' %
1241 self.relpath)
1242 gclient_utils.rmtree(self.checkout_path)
1243 # Don't reuse the args.
1244 return self.update(options, [], file_list)
1245
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001246 def printcb(file_status):
1247 file_list.append(file_status[1])
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001248 if logging.getLogger().isEnabledFor(logging.INFO):
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001249 logging.info('%s%s' % (file_status[0], file_status[1]))
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001250 else:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001251 print(os.path.join(self.checkout_path, file_status[1]))
1252 scm.SVN.Revert(self.checkout_path, callback=printcb)
maruel@chromium.orgaa3dd472009-09-21 19:02:48 +00001253
maruel@chromium.org8b322b32011-11-01 19:05:50 +00001254 # Revert() may delete the directory altogether.
1255 if not os.path.isdir(self.checkout_path):
1256 # Don't reuse the args.
1257 return self.update(options, [], file_list)
1258
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001259 try:
1260 # svn revert is so broken we don't even use it. Using
1261 # "svn up --revision BASE" achieve the same effect.
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001262 # file_list will contain duplicates.
maruel@chromium.org669600d2010-09-01 19:06:31 +00001263 self._RunAndGetFileList(['update', '--revision', 'BASE'], options,
1264 file_list)
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001265 except OSError, e:
maruel@chromium.org07ab60e2011-02-08 21:54:00 +00001266 # Maybe the directory disapeared meanwhile. Do not throw an exception.
maruel@chromium.org810a50b2009-10-05 23:03:18 +00001267 logging.error('Failed to update:\n%s' % str(e))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001268
msb@chromium.org0f282062009-11-06 20:14:02 +00001269 def revinfo(self, options, args, file_list):
1270 """Display revision"""
maruel@chromium.org54019f32010-09-09 13:50:11 +00001271 try:
1272 return scm.SVN.CaptureRevision(self.checkout_path)
maruel@chromium.org31cb48a2011-04-04 18:01:36 +00001273 except (gclient_utils.Error, subprocess2.CalledProcessError):
maruel@chromium.org54019f32010-09-09 13:50:11 +00001274 return None
msb@chromium.org0f282062009-11-06 20:14:02 +00001275
msb@chromium.orgcb5442b2009-09-22 16:51:24 +00001276 def runhooks(self, options, args, file_list):
1277 self.status(options, args, file_list)
1278
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001279 def status(self, options, args, file_list):
1280 """Display status information."""
maruel@chromium.org669600d2010-09-01 19:06:31 +00001281 command = ['status'] + args
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001282 if not os.path.isdir(self.checkout_path):
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001283 # svn status won't work if the directory doesn't exist.
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001284 print(('\n________ couldn\'t run \'%s\' in \'%s\':\n'
1285 'The directory does not exist.') %
1286 (' '.join(command), self.checkout_path))
maruel@chromium.org5f3eee32009-09-17 00:34:30 +00001287 # There's no file list to retrieve.
1288 else:
maruel@chromium.org669600d2010-09-01 19:06:31 +00001289 self._RunAndGetFileList(command, options, file_list)
msb@chromium.orge6f78352010-01-13 17:05:33 +00001290
dbeam@chromium.orge5d1e612011-12-19 19:49:19 +00001291 def GetUsableRev(self, rev, options):
1292 """Verifies the validity of the revision for this repository."""
1293 if not scm.SVN.IsValidRevision(url='%s@%s' % (self.url, rev)):
1294 raise gclient_utils.Error(
1295 ( '%s isn\'t a valid revision. Please check that your safesync_url is\n'
1296 'correct.') % rev)
1297 return rev
1298
msb@chromium.orge6f78352010-01-13 17:05:33 +00001299 def FullUrlForRelativeUrl(self, url):
1300 # Find the forth '/' and strip from there. A bit hackish.
1301 return '/'.join(self.url.split('/')[:4]) + url
tony@chromium.org99828122010-06-04 01:41:02 +00001302
maruel@chromium.org669600d2010-09-01 19:06:31 +00001303 def _Run(self, args, options, **kwargs):
1304 """Runs a commands that goes to stdout."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001305 kwargs.setdefault('cwd', self.checkout_path)
szager@chromium.org12b07e72013-05-03 22:06:34 +00001306 kwargs.setdefault('nag_timer', self.nag_timer)
1307 kwargs.setdefault('nag_max', self.nag_max)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001308 gclient_utils.CheckCallAndFilterAndHeader(['svn'] + args,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001309 always=options.verbose, **kwargs)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001310
1311 def _RunAndGetFileList(self, args, options, file_list, cwd=None):
1312 """Runs a commands that goes to stdout and grabs the file listed."""
maruel@chromium.org8469bf92010-09-03 19:03:15 +00001313 cwd = cwd or self.checkout_path
maruel@chromium.orgce117f62011-01-17 20:04:25 +00001314 scm.SVN.RunAndGetFileList(
1315 options.verbose,
1316 args + ['--ignore-externals'],
1317 cwd=cwd,
maruel@chromium.org77e4eca2010-09-21 13:23:07 +00001318 file_list=file_list)
maruel@chromium.org669600d2010-09-01 19:06:31 +00001319
maruel@chromium.org6e29d572010-06-04 17:32:20 +00001320 @staticmethod
maruel@chromium.org8e0e9262010-08-17 19:20:27 +00001321 def _AddAdditionalUpdateFlags(command, options, revision):
tony@chromium.org99828122010-06-04 01:41:02 +00001322 """Add additional flags to command depending on what options are set.
1323 command should be a list of strings that represents an svn command.
1324
1325 This method returns a new list to be used as a command."""
1326 new_command = command[:]
1327 if revision:
1328 new_command.extend(['--revision', str(revision).strip()])
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001329 # We don't want interaction when jobs are used.
1330 if options.jobs > 1:
1331 new_command.append('--non-interactive')
tony@chromium.org99828122010-06-04 01:41:02 +00001332 # --force was added to 'svn update' in svn 1.5.
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001333 # --accept was added to 'svn update' in svn 1.6.
1334 if not scm.SVN.AssertVersion('1.5')[0]:
1335 return new_command
1336
1337 # It's annoying to have it block in the middle of a sync, just sensible
1338 # defaults.
1339 if options.force:
tony@chromium.org99828122010-06-04 01:41:02 +00001340 new_command.append('--force')
maruel@chromium.org36ac2392011-10-12 16:36:11 +00001341 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1342 new_command.extend(('--accept', 'theirs-conflict'))
1343 elif options.manually_grab_svn_rev:
1344 new_command.append('--force')
1345 if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1346 new_command.extend(('--accept', 'postpone'))
1347 elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]:
1348 new_command.extend(('--accept', 'postpone'))
tony@chromium.org99828122010-06-04 01:41:02 +00001349 return new_command