[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 4 | |
[email protected] | d5800f1 | 2009-11-12 20:03:43 | [diff] [blame] | 5 | """Gclient-specific SCM-specific operations.""" |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 6 | |
[email protected] | 754960e | 2009-09-21 12:31:05 | [diff] [blame] | 7 | import logging |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 8 | import os |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 9 | import posixpath |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 10 | import re |
[email protected] | 9054173 | 2011-04-01 17:54:18 | [diff] [blame] | 11 | import sys |
[email protected] | fd87617 | 2010-04-30 14:01:05 | [diff] [blame] | 12 | import time |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 13 | |
| 14 | import gclient_utils |
[email protected] | 31cb48a | 2011-04-04 18:01:36 | [diff] [blame] | 15 | import scm |
| 16 | import subprocess2 |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 17 | |
| 18 | |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 19 | class DiffFilterer(object): |
| 20 | """Simple class which tracks which file is being diffed and |
| 21 | replaces instances of its file name in the original and |
[email protected] | d650421 | 2010-01-13 17:34:31 | [diff] [blame] | 22 | working copy lines of the svn/git diff output.""" |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 23 | index_string = "Index: " |
| 24 | original_prefix = "--- " |
| 25 | working_prefix = "+++ " |
| 26 | |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 27 | def __init__(self, relpath): |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 28 | # Note that we always use '/' as the path separator to be |
| 29 | # consistent with svn's cygwin-style output on Windows |
| 30 | self._relpath = relpath.replace("\\", "/") |
| 31 | self._current_file = "" |
| 32 | self._replacement_file = "" |
| 33 | |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 34 | def SetCurrentFile(self, current_file): |
| 35 | self._current_file = current_file |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 36 | # Note that we always use '/' as the path separator to be |
| 37 | # consistent with svn's cygwin-style output on Windows |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 38 | self._replacement_file = posixpath.join(self._relpath, current_file) |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 39 | |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 40 | def _Replace(self, line): |
| 41 | return line.replace(self._current_file, self._replacement_file) |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 42 | |
| 43 | def Filter(self, line): |
| 44 | if (line.startswith(self.index_string)): |
| 45 | self.SetCurrentFile(line[len(self.index_string):]) |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 46 | line = self._Replace(line) |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 47 | else: |
| 48 | if (line.startswith(self.original_prefix) or |
| 49 | line.startswith(self.working_prefix)): |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 50 | line = self._Replace(line) |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 51 | print(line) |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 52 | |
| 53 | |
[email protected] | 9054173 | 2011-04-01 17:54:18 | [diff] [blame] | 54 | def ask_for_data(prompt): |
| 55 | try: |
| 56 | return raw_input(prompt) |
| 57 | except KeyboardInterrupt: |
| 58 | # Hide the exception. |
| 59 | sys.exit(1) |
| 60 | |
| 61 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 62 | ### SCM abstraction layer |
| 63 | |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 64 | # Factory Method for SCM wrapper creation |
| 65 | |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 66 | def GetScmName(url): |
| 67 | if url: |
| 68 | url, _ = gclient_utils.SplitUrlRevision(url) |
| 69 | if (url.startswith('git://') or url.startswith('ssh://') or |
| 70 | url.endswith('.git')): |
| 71 | return 'git' |
[email protected] | b74dca2 | 2010-06-11 20:10:40 | [diff] [blame] | 72 | elif (url.startswith('http://') or url.startswith('https://') or |
[email protected] | 54a07a2 | 2010-06-14 19:07:39 | [diff] [blame] | 73 | url.startswith('svn://') or url.startswith('svn+ssh://')): |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 74 | return 'svn' |
| 75 | return None |
| 76 | |
| 77 | |
| 78 | def CreateSCM(url, root_dir=None, relpath=None): |
| 79 | SCM_MAP = { |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 80 | 'svn' : SVNWrapper, |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 81 | 'git' : GitWrapper, |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 82 | } |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 83 | |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 84 | scm_name = GetScmName(url) |
| 85 | if not scm_name in SCM_MAP: |
| 86 | raise gclient_utils.Error('No SCM found for url %s' % url) |
| 87 | return SCM_MAP[scm_name](url, root_dir, relpath) |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 88 | |
| 89 | |
| 90 | # SCMWrapper base class |
| 91 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 92 | class SCMWrapper(object): |
| 93 | """Add necessary glue between all the supported SCM. |
| 94 | |
[email protected] | d650421 | 2010-01-13 17:34:31 | [diff] [blame] | 95 | This is the abstraction layer to bind to different SCM. |
| 96 | """ |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 97 | def __init__(self, url=None, root_dir=None, relpath=None): |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 98 | self.url = url |
[email protected] | 5e73b0c | 2009-09-18 19:47:48 | [diff] [blame] | 99 | self._root_dir = root_dir |
| 100 | if self._root_dir: |
| 101 | self._root_dir = self._root_dir.replace('/', os.sep) |
| 102 | self.relpath = relpath |
| 103 | if self.relpath: |
| 104 | self.relpath = self.relpath.replace('/', os.sep) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 105 | if self.relpath and self._root_dir: |
| 106 | self.checkout_path = os.path.join(self._root_dir, self.relpath) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 107 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 108 | def RunCommand(self, command, options, args, file_list=None): |
| 109 | # file_list will have all files that are modified appended to it. |
[email protected] | de754ac | 2009-09-17 18:04:50 | [diff] [blame] | 110 | if file_list is None: |
| 111 | file_list = [] |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 112 | |
[email protected] | 6e043f7 | 2011-05-02 07:24:32 | [diff] [blame] | 113 | commands = ['cleanup', 'update', 'updatesingle', 'revert', |
[email protected] | 4b5b177 | 2010-04-08 01:52:56 | [diff] [blame] | 114 | 'revinfo', 'status', 'diff', 'pack', 'runhooks'] |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 115 | |
| 116 | if not command in commands: |
| 117 | raise gclient_utils.Error('Unknown command %s' % command) |
| 118 | |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 119 | if not command in dir(self): |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 120 | raise gclient_utils.Error('Command %s not implemented in %s wrapper' % ( |
[email protected] | 9eda411 | 2010-06-11 18:56:10 | [diff] [blame] | 121 | command, self.__class__.__name__)) |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 122 | |
| 123 | return getattr(self, command)(options, args, file_list) |
| 124 | |
| 125 | |
[email protected] | 55e724e | 2010-03-11 19:36:49 | [diff] [blame] | 126 | class GitWrapper(SCMWrapper): |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 127 | """Wrapper for Git""" |
| 128 | |
[email protected] | eaab784 | 2011-04-28 09:07:58 | [diff] [blame] | 129 | def GetRevisionDate(self, revision): |
| 130 | """Returns the given revision's date in ISO-8601 format (which contains the |
| 131 | time zone).""" |
| 132 | # TODO(floitsch): get the time-stamp of the given revision and not just the |
| 133 | # time-stamp of the currently checked out revision. |
| 134 | return self._Capture(['log', '-n', '1', '--format=%ai']) |
| 135 | |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 136 | @staticmethod |
| 137 | def cleanup(options, args, file_list): |
[email protected] | d8a6378 | 2010-01-25 17:47:05 | [diff] [blame] | 138 | """'Cleanup' the repo. |
| 139 | |
| 140 | There's no real git equivalent for the svn cleanup command, do a no-op. |
| 141 | """ |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 142 | |
| 143 | def diff(self, options, args, file_list): |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 144 | merge_base = self._Capture(['merge-base', 'HEAD', 'origin']) |
[email protected] | 37e8987 | 2010-09-07 16:11:33 | [diff] [blame] | 145 | self._Run(['diff', merge_base], options) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 146 | |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 147 | def pack(self, options, args, file_list): |
| 148 | """Generates a patch file which can be applied to the root of the |
[email protected] | d650421 | 2010-01-13 17:34:31 | [diff] [blame] | 149 | repository. |
| 150 | |
| 151 | The patch file is generated from a diff of the merge base of HEAD and |
| 152 | its upstream branch. |
| 153 | """ |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 154 | merge_base = self._Capture(['merge-base', 'HEAD', 'origin']) |
[email protected] | 17d0179 | 2010-09-01 18:07:10 | [diff] [blame] | 155 | gclient_utils.CheckCallAndFilter( |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 156 | ['git', 'diff', merge_base], |
| 157 | cwd=self.checkout_path, |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 158 | filter_fn=DiffFilterer(self.relpath).Filter) |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 159 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 160 | def update(self, options, args, file_list): |
| 161 | """Runs git to update or transparently checkout the working copy. |
| 162 | |
| 163 | All updated files will be appended to file_list. |
| 164 | |
| 165 | Raises: |
| 166 | Error: if can't get URL for relative path. |
| 167 | """ |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 168 | if args: |
| 169 | raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args)) |
| 170 | |
[email protected] | ece406f | 2010-02-23 17:29:15 | [diff] [blame] | 171 | self._CheckMinVersion("1.6.6") |
[email protected] | 923a037 | 2009-12-11 20:42:43 | [diff] [blame] | 172 | |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 173 | default_rev = "refs/heads/master" |
[email protected] | 7080e94 | 2010-03-15 15:06:16 | [diff] [blame] | 174 | url, deps_revision = gclient_utils.SplitUrlRevision(self.url) |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 175 | rev_str = "" |
[email protected] | 7080e94 | 2010-03-15 15:06:16 | [diff] [blame] | 176 | revision = deps_revision |
[email protected] | eb2756d | 2011-09-20 20:17:51 | [diff] [blame] | 177 | managed = True |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 178 | if options.revision: |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 179 | # Override the revision number. |
| 180 | revision = str(options.revision) |
[email protected] | eb2756d | 2011-09-20 20:17:51 | [diff] [blame] | 181 | if revision == 'unmanaged': |
| 182 | revision = None |
| 183 | managed = False |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 184 | if not revision: |
| 185 | revision = default_rev |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 186 | |
[email protected] | eaab784 | 2011-04-28 09:07:58 | [diff] [blame] | 187 | if gclient_utils.IsDateRevision(revision): |
| 188 | # Date-revisions only work on git-repositories if the reflog hasn't |
| 189 | # expired yet. Use rev-list to get the corresponding revision. |
| 190 | # git rev-list -n 1 --before='time-stamp' branchname |
| 191 | if options.transitive: |
| 192 | print('Warning: --transitive only works for SVN repositories.') |
| 193 | revision = default_rev |
| 194 | |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 195 | rev_str = ' at %s' % revision |
| 196 | files = [] |
| 197 | |
| 198 | printed_path = False |
| 199 | verbose = [] |
[email protected] | b1a22bf | 2009-11-07 02:33:50 | [diff] [blame] | 200 | if options.verbose: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 201 | print('\n_____ %s%s' % (self.relpath, rev_str)) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 202 | verbose = ['--verbose'] |
| 203 | printed_path = True |
| 204 | |
| 205 | if revision.startswith('refs/heads/'): |
| 206 | rev_type = "branch" |
| 207 | elif revision.startswith('origin/'): |
| 208 | # For compatability with old naming, translate 'origin' to 'refs/heads' |
| 209 | revision = revision.replace('origin/', 'refs/heads/') |
| 210 | rev_type = "branch" |
| 211 | else: |
| 212 | # hash is also a tag, only make a distinction at checkout |
| 213 | rev_type = "hash" |
| 214 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 215 | if not os.path.exists(self.checkout_path): |
[email protected] | 6c48a30 | 2011-10-20 23:44:20 | [diff] [blame^] | 216 | gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path)) |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 217 | self._Clone(revision, url, options) |
[email protected] | 858d645 | 2011-03-24 17:59:20 | [diff] [blame] | 218 | files = self._Capture(['ls-files']).splitlines() |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 219 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 220 | if not verbose: |
| 221 | # Make the output a little prettier. It's nice to have some whitespace |
| 222 | # between projects when cloning. |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 223 | print('') |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 224 | return |
| 225 | |
[email protected] | eb2756d | 2011-09-20 20:17:51 | [diff] [blame] | 226 | if not managed: |
| 227 | print ('________ unmanaged solution; skipping %s' % self.relpath) |
| 228 | return |
| 229 | |
[email protected] | e4af1ab | 2010-01-13 21:26:09 | [diff] [blame] | 230 | if not os.path.exists(os.path.join(self.checkout_path, '.git')): |
| 231 | raise gclient_utils.Error('\n____ %s%s\n' |
| 232 | '\tPath is not a git repo. No .git dir.\n' |
| 233 | '\tTo resolve:\n' |
| 234 | '\t\trm -rf %s\n' |
| 235 | '\tAnd run gclient sync again\n' |
| 236 | % (self.relpath, rev_str, self.relpath)) |
| 237 | |
[email protected] | d6f89d8 | 2011-03-25 20:41:58 | [diff] [blame] | 238 | # See if the url has changed (the unittests use git://foo for the url, let |
| 239 | # that through). |
[email protected] | 668667c | 2011-03-24 18:27:24 | [diff] [blame] | 240 | current_url = self._Capture(['config', 'remote.origin.url']) |
[email protected] | d6f89d8 | 2011-03-25 20:41:58 | [diff] [blame] | 241 | # TODO(maruel): Delete url != 'git://foo' since it's just to make the |
| 242 | # unit test pass. (and update the comment above) |
| 243 | if current_url != url and url != 'git://foo': |
[email protected] | 668667c | 2011-03-24 18:27:24 | [diff] [blame] | 244 | print('_____ switching %s to a new upstream' % self.relpath) |
| 245 | # Make sure it's clean |
| 246 | self._CheckClean(rev_str) |
| 247 | # Switch over to the new upstream |
| 248 | self._Run(['remote', 'set-url', 'origin', url], options) |
| 249 | quiet = [] |
| 250 | if not options.verbose: |
| 251 | quiet = ['--quiet'] |
| 252 | self._Run(['fetch', 'origin', '--prune'] + quiet, options) |
| 253 | self._Run(['reset', '--hard', 'origin/master'] + quiet, options) |
| 254 | files = self._Capture(['ls-files']).splitlines() |
| 255 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 256 | return |
| 257 | |
[email protected] | 5bde485 | 2009-12-14 16:47:12 | [diff] [blame] | 258 | cur_branch = self._GetCurrentBranch() |
| 259 | |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 260 | # Cases: |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 261 | # 0) HEAD is detached. Probably from our initial clone. |
| 262 | # - make sure HEAD is contained by a named ref, then update. |
| 263 | # Cases 1-4. HEAD is a branch. |
| 264 | # 1) current branch is not tracking a remote branch (could be git-svn) |
| 265 | # - try to rebase onto the new hash or branch |
| 266 | # 2) current branch is tracking a remote branch with local committed |
| 267 | # changes, but the DEPS file switched to point to a hash |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 268 | # - rebase those changes on top of the hash |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 269 | # 3) current branch is tracking a remote branch w/or w/out changes, |
| 270 | # no switch |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 271 | # - see if we can FF, if not, prompt the user for rebase, merge, or stop |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 272 | # 4) current branch is tracking a remote branch, switches to a different |
| 273 | # remote branch |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 274 | # - exit |
| 275 | |
[email protected] | 81e012c | 2010-04-29 16:07:24 | [diff] [blame] | 276 | # GetUpstreamBranch returns something like 'refs/remotes/origin/master' for |
| 277 | # a tracking branch |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 278 | # or 'master' if not a tracking branch (it's based on a specific rev/hash) |
| 279 | # or it returns None if it couldn't find an upstream |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 280 | if cur_branch is None: |
| 281 | upstream_branch = None |
| 282 | current_type = "detached" |
| 283 | logging.debug("Detached HEAD") |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 284 | else: |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 285 | upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path) |
| 286 | if not upstream_branch or not upstream_branch.startswith('refs/remotes'): |
| 287 | current_type = "hash" |
| 288 | logging.debug("Current branch is not tracking an upstream (remote)" |
| 289 | " branch.") |
| 290 | elif upstream_branch.startswith('refs/remotes'): |
| 291 | current_type = "branch" |
| 292 | else: |
| 293 | raise gclient_utils.Error('Invalid Upstream: %s' % upstream_branch) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 294 | |
[email protected] | 0b1c246 | 2010-03-02 00:48:14 | [diff] [blame] | 295 | # Update the remotes first so we have all the refs. |
[email protected] | 2aee2298 | 2010-09-03 14:15:25 | [diff] [blame] | 296 | backoff_time = 5 |
[email protected] | fd87617 | 2010-04-30 14:01:05 | [diff] [blame] | 297 | for _ in range(10): |
[email protected] | 0b1c246 | 2010-03-02 00:48:14 | [diff] [blame] | 298 | try: |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 299 | remote_output = scm.GIT.Capture( |
[email protected] | 0b1c246 | 2010-03-02 00:48:14 | [diff] [blame] | 300 | ['remote'] + verbose + ['update'], |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 301 | cwd=self.checkout_path) |
[email protected] | 0b1c246 | 2010-03-02 00:48:14 | [diff] [blame] | 302 | break |
[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 303 | except subprocess2.CalledProcessError, e: |
[email protected] | 0b1c246 | 2010-03-02 00:48:14 | [diff] [blame] | 304 | # Hackish but at that point, git is known to work so just checking for |
| 305 | # 502 in stderr should be fine. |
| 306 | if '502' in e.stderr: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 307 | print(str(e)) |
| 308 | print('Sleeping %.1f seconds and retrying...' % backoff_time) |
[email protected] | 2aee2298 | 2010-09-03 14:15:25 | [diff] [blame] | 309 | time.sleep(backoff_time) |
| 310 | backoff_time *= 1.3 |
[email protected] | 0b1c246 | 2010-03-02 00:48:14 | [diff] [blame] | 311 | continue |
[email protected] | fd87617 | 2010-04-30 14:01:05 | [diff] [blame] | 312 | raise |
[email protected] | 0b1c246 | 2010-03-02 00:48:14 | [diff] [blame] | 313 | |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 314 | if verbose: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 315 | print(remote_output.strip()) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 316 | |
| 317 | # This is a big hammer, debatable if it should even be here... |
[email protected] | 793796d | 2010-02-19 17:27:41 | [diff] [blame] | 318 | if options.force or options.reset: |
[email protected] | 37e8987 | 2010-09-07 16:11:33 | [diff] [blame] | 319 | self._Run(['reset', '--hard', 'HEAD'], options) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 320 | |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 321 | if current_type == 'detached': |
| 322 | # case 0 |
| 323 | self._CheckClean(rev_str) |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 324 | self._CheckDetachedHead(rev_str, options) |
[email protected] | f7826d7 | 2011-06-02 18:20:14 | [diff] [blame] | 325 | self._Capture(['checkout', '--quiet', '%s' % revision]) |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 326 | if not printed_path: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 327 | print('\n_____ %s%s' % (self.relpath, rev_str)) |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 328 | elif current_type == 'hash': |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 329 | # case 1 |
[email protected] | 55e724e | 2010-03-11 19:36:49 | [diff] [blame] | 330 | if scm.GIT.IsGitSvn(self.checkout_path) and upstream_branch is not None: |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 331 | # Our git-svn branch (upstream_branch) is our upstream |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 332 | self._AttemptRebase(upstream_branch, files, options, |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 333 | newbase=revision, printed_path=printed_path) |
| 334 | printed_path = True |
| 335 | else: |
| 336 | # Can't find a merge-base since we don't know our upstream. That makes |
| 337 | # this command VERY likely to produce a rebase failure. For now we |
| 338 | # assume origin is our upstream since that's what the old behavior was. |
[email protected] | 3b29de1 | 2010-03-08 18:34:28 | [diff] [blame] | 339 | upstream_branch = 'origin' |
[email protected] | 7080e94 | 2010-03-15 15:06:16 | [diff] [blame] | 340 | if options.revision or deps_revision: |
[email protected] | 3b29de1 | 2010-03-08 18:34:28 | [diff] [blame] | 341 | upstream_branch = revision |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 342 | self._AttemptRebase(upstream_branch, files, options, |
| 343 | printed_path=printed_path) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 344 | printed_path = True |
[email protected] | 55e724e | 2010-03-11 19:36:49 | [diff] [blame] | 345 | elif rev_type == 'hash': |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 346 | # case 2 |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 347 | self._AttemptRebase(upstream_branch, files, options, |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 348 | newbase=revision, printed_path=printed_path) |
| 349 | printed_path = True |
| 350 | elif revision.replace('heads', 'remotes/origin') != upstream_branch: |
| 351 | # case 4 |
| 352 | new_base = revision.replace('heads', 'remotes/origin') |
| 353 | if not printed_path: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 354 | print('\n_____ %s%s' % (self.relpath, rev_str)) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 355 | switch_error = ("Switching upstream branch from %s to %s\n" |
| 356 | % (upstream_branch, new_base) + |
| 357 | "Please merge or rebase manually:\n" + |
| 358 | "cd %s; git rebase %s\n" % (self.checkout_path, new_base) + |
| 359 | "OR git checkout -b <some new branch> %s" % new_base) |
| 360 | raise gclient_utils.Error(switch_error) |
| 361 | else: |
| 362 | # case 3 - the default case |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 363 | files = self._Capture(['diff', upstream_branch, '--name-only']).split() |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 364 | if verbose: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 365 | print('Trying fast-forward merge to branch : %s' % upstream_branch) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 366 | try: |
[email protected] | 2aad1b2 | 2011-07-22 12:00:41 | [diff] [blame] | 367 | merge_args = ['merge'] |
| 368 | if not options.merge: |
| 369 | merge_args.append('--ff-only') |
| 370 | merge_args.append(upstream_branch) |
| 371 | merge_output = scm.GIT.Capture(merge_args, cwd=self.checkout_path) |
[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 372 | except subprocess2.CalledProcessError, e: |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 373 | if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr): |
| 374 | if not printed_path: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 375 | print('\n_____ %s%s' % (self.relpath, rev_str)) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 376 | printed_path = True |
| 377 | while True: |
| 378 | try: |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 379 | # TODO(maruel): That can't work with --jobs. |
[email protected] | 9054173 | 2011-04-01 17:54:18 | [diff] [blame] | 380 | action = ask_for_data( |
| 381 | 'Cannot fast-forward merge, attempt to rebase? ' |
| 382 | '(y)es / (q)uit / (s)kip : ') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 383 | except ValueError: |
[email protected] | 9054173 | 2011-04-01 17:54:18 | [diff] [blame] | 384 | raise gclient_utils.Error('Invalid Character') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 385 | if re.match(r'yes|y', action, re.I): |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 386 | self._AttemptRebase(upstream_branch, files, options, |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 387 | printed_path=printed_path) |
| 388 | printed_path = True |
| 389 | break |
| 390 | elif re.match(r'quit|q', action, re.I): |
| 391 | raise gclient_utils.Error("Can't fast-forward, please merge or " |
| 392 | "rebase manually.\n" |
| 393 | "cd %s && git " % self.checkout_path |
| 394 | + "rebase %s" % upstream_branch) |
| 395 | elif re.match(r'skip|s', action, re.I): |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 396 | print('Skipping %s' % self.relpath) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 397 | return |
| 398 | else: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 399 | print('Input not recognized') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 400 | elif re.match("error: Your local changes to '.*' would be " |
| 401 | "overwritten by merge. Aborting.\nPlease, commit your " |
| 402 | "changes or stash them before you can merge.\n", |
| 403 | e.stderr): |
| 404 | if not printed_path: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 405 | print('\n_____ %s%s' % (self.relpath, rev_str)) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 406 | printed_path = True |
| 407 | raise gclient_utils.Error(e.stderr) |
| 408 | else: |
| 409 | # Some other problem happened with the merge |
| 410 | logging.error("Error during fast-forward merge in %s!" % self.relpath) |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 411 | print(e.stderr) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 412 | raise |
| 413 | else: |
| 414 | # Fast-forward merge was successful |
| 415 | if not re.match('Already up-to-date.', merge_output) or verbose: |
| 416 | if not printed_path: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 417 | print('\n_____ %s%s' % (self.relpath, rev_str)) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 418 | printed_path = True |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 419 | print(merge_output.strip()) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 420 | if not verbose: |
| 421 | # Make the output a little prettier. It's nice to have some |
| 422 | # whitespace between projects when syncing. |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 423 | print('') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 424 | |
| 425 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
[email protected] | 5bde485 | 2009-12-14 16:47:12 | [diff] [blame] | 426 | |
| 427 | # If the rebase generated a conflict, abort and ask user to fix |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 428 | if self._IsRebasing(): |
[email protected] | 5bde485 | 2009-12-14 16:47:12 | [diff] [blame] | 429 | raise gclient_utils.Error('\n____ %s%s\n' |
| 430 | '\nConflict while rebasing this branch.\n' |
| 431 | 'Fix the conflict and run gclient again.\n' |
| 432 | 'See man git-rebase for details.\n' |
| 433 | % (self.relpath, rev_str)) |
| 434 | |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 435 | if verbose: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 436 | print('Checked out revision %s' % self.revinfo(options, (), None)) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 437 | |
| 438 | def revert(self, options, args, file_list): |
| 439 | """Reverts local modifications. |
| 440 | |
| 441 | All reverted files will be appended to file_list. |
| 442 | """ |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 443 | if not os.path.isdir(self.checkout_path): |
[email protected] | 260c653 | 2009-10-28 03:22:35 | [diff] [blame] | 444 | # revert won't work if the directory doesn't exist. It needs to |
| 445 | # checkout instead. |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 446 | print('\n_____ %s is missing, synching instead' % self.relpath) |
[email protected] | 260c653 | 2009-10-28 03:22:35 | [diff] [blame] | 447 | # Don't reuse the args. |
| 448 | return self.update(options, [], file_list) |
[email protected] | b2b4631 | 2010-04-30 20:58:03 | [diff] [blame] | 449 | |
| 450 | default_rev = "refs/heads/master" |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 451 | _, deps_revision = gclient_utils.SplitUrlRevision(self.url) |
[email protected] | b2b4631 | 2010-04-30 20:58:03 | [diff] [blame] | 452 | if not deps_revision: |
| 453 | deps_revision = default_rev |
| 454 | if deps_revision.startswith('refs/heads/'): |
| 455 | deps_revision = deps_revision.replace('refs/heads/', 'origin/') |
| 456 | |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 457 | files = self._Capture(['diff', deps_revision, '--name-only']).split() |
[email protected] | 37e8987 | 2010-09-07 16:11:33 | [diff] [blame] | 458 | self._Run(['reset', '--hard', deps_revision], options) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 459 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 460 | |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 461 | def revinfo(self, options, args, file_list): |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 462 | """Returns revision""" |
| 463 | return self._Capture(['rev-parse', 'HEAD']) |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 464 | |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 465 | def runhooks(self, options, args, file_list): |
| 466 | self.status(options, args, file_list) |
| 467 | |
| 468 | def status(self, options, args, file_list): |
| 469 | """Display status information.""" |
| 470 | if not os.path.isdir(self.checkout_path): |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 471 | print(('\n________ couldn\'t run status in %s:\n' |
| 472 | 'The directory does not exist.') % self.checkout_path) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 473 | else: |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 474 | merge_base = self._Capture(['merge-base', 'HEAD', 'origin']) |
[email protected] | 37e8987 | 2010-09-07 16:11:33 | [diff] [blame] | 475 | self._Run(['diff', '--name-status', merge_base], options) |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 476 | files = self._Capture(['diff', '--name-only', merge_base]).split() |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 477 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 478 | |
[email protected] | e6f7835 | 2010-01-13 17:05:33 | [diff] [blame] | 479 | def FullUrlForRelativeUrl(self, url): |
| 480 | # Strip from last '/' |
| 481 | # Equivalent to unix basename |
| 482 | base_url = self.url |
| 483 | return base_url[:base_url.rfind('/')] + url |
| 484 | |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 485 | def _Clone(self, revision, url, options): |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 486 | """Clone a git repository from the given URL. |
| 487 | |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 488 | Once we've cloned the repo, we checkout a working branch if the specified |
| 489 | revision is a branch head. If it is a tag or a specific commit, then we |
| 490 | leave HEAD detached as it makes future updates simpler -- in this case the |
| 491 | user should first create a new branch or switch to an existing branch before |
| 492 | making changes in the repo.""" |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 493 | if not options.verbose: |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 494 | # git clone doesn't seem to insert a newline properly before printing |
| 495 | # to stdout |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 496 | print('') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 497 | |
[email protected] | 85d3e3a | 2011-10-07 17:12:00 | [diff] [blame] | 498 | clone_cmd = ['clone', '--progress'] |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 499 | if revision.startswith('refs/heads/'): |
| 500 | clone_cmd.extend(['-b', revision.replace('refs/heads/', '')]) |
| 501 | detach_head = False |
| 502 | else: |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 503 | detach_head = True |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 504 | if options.verbose: |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 505 | clone_cmd.append('--verbose') |
| 506 | clone_cmd.extend([url, self.checkout_path]) |
| 507 | |
[email protected] | 328c3c7 | 2011-06-01 20:50:27 | [diff] [blame] | 508 | # If the parent directory does not exist, Git clone on Windows will not |
| 509 | # create it, so we need to do it manually. |
| 510 | parent_dir = os.path.dirname(self.checkout_path) |
| 511 | if not os.path.exists(parent_dir): |
[email protected] | 6c48a30 | 2011-10-20 23:44:20 | [diff] [blame^] | 512 | gclient_utils.safe_makedirs(parent_dir) |
[email protected] | 328c3c7 | 2011-06-01 20:50:27 | [diff] [blame] | 513 | |
[email protected] | 85d3e3a | 2011-10-07 17:12:00 | [diff] [blame] | 514 | percent_re = re.compile('.* ([0-9]{1,2})% .*') |
| 515 | def _GitFilter(line): |
| 516 | # git uses an escape sequence to clear the line; elide it. |
| 517 | esc = line.find(unichr(033)) |
| 518 | if esc > -1: |
| 519 | line = line[:esc] |
| 520 | match = percent_re.match(line) |
| 521 | if not match or not int(match.group(1)) % 10: |
| 522 | print '%s' % line |
| 523 | |
[email protected] | 55e724e | 2010-03-11 19:36:49 | [diff] [blame] | 524 | for _ in range(3): |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 525 | try: |
[email protected] | 85d3e3a | 2011-10-07 17:12:00 | [diff] [blame] | 526 | self._Run(clone_cmd, options, cwd=self._root_dir, filter_fn=_GitFilter, |
| 527 | print_stdout=False) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 528 | break |
[email protected] | 2a5b6a2 | 2011-09-09 14:03:12 | [diff] [blame] | 529 | except subprocess2.CalledProcessError, e: |
| 530 | # Too bad we don't have access to the actual output yet. |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 531 | # We should check for "transfer closed with NNN bytes remaining to |
| 532 | # read". In the meantime, just make sure .git exists. |
[email protected] | 2a5b6a2 | 2011-09-09 14:03:12 | [diff] [blame] | 533 | if (e.returncode == 128 and |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 534 | os.path.exists(os.path.join(self.checkout_path, '.git'))): |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 535 | print(str(e)) |
| 536 | print('Retrying...') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 537 | continue |
| 538 | raise e |
| 539 | |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 540 | if detach_head: |
| 541 | # Squelch git's very verbose detached HEAD warning and use our own |
[email protected] | f7826d7 | 2011-06-02 18:20:14 | [diff] [blame] | 542 | self._Capture(['checkout', '--quiet', '%s' % revision]) |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 543 | print( |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 544 | ('Checked out %s to a detached HEAD. Before making any commits\n' |
| 545 | 'in this repo, you should use \'git checkout <branch>\' to switch to\n' |
| 546 | 'an existing branch or use \'git checkout origin -b <branch>\' to\n' |
| 547 | 'create a new branch for your work.') % revision) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 548 | |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 549 | def _AttemptRebase(self, upstream, files, options, newbase=None, |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 550 | branch=None, printed_path=False): |
| 551 | """Attempt to rebase onto either upstream or, if specified, newbase.""" |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 552 | files.extend(self._Capture(['diff', upstream, '--name-only']).split()) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 553 | revision = upstream |
| 554 | if newbase: |
| 555 | revision = newbase |
| 556 | if not printed_path: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 557 | print('\n_____ %s : Attempting rebase onto %s...' % ( |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 558 | self.relpath, revision)) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 559 | printed_path = True |
| 560 | else: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 561 | print('Attempting rebase onto %s...' % revision) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 562 | |
| 563 | # Build the rebase command here using the args |
| 564 | # git rebase [options] [--onto <newbase>] <upstream> [<branch>] |
| 565 | rebase_cmd = ['rebase'] |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 566 | if options.verbose: |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 567 | rebase_cmd.append('--verbose') |
| 568 | if newbase: |
| 569 | rebase_cmd.extend(['--onto', newbase]) |
| 570 | rebase_cmd.append(upstream) |
| 571 | if branch: |
| 572 | rebase_cmd.append(branch) |
| 573 | |
| 574 | try: |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 575 | rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path) |
[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 576 | except subprocess2.CalledProcessError, e: |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 577 | if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or |
| 578 | re.match(r'cannot rebase: your index contains uncommitted changes', |
| 579 | e.stderr)): |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 580 | while True: |
[email protected] | 9054173 | 2011-04-01 17:54:18 | [diff] [blame] | 581 | rebase_action = ask_for_data( |
| 582 | 'Cannot rebase because of unstaged changes.\n' |
| 583 | '\'git reset --hard HEAD\' ?\n' |
| 584 | 'WARNING: destroys any uncommitted work in your current branch!' |
| 585 | ' (y)es / (q)uit / (s)how : ') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 586 | if re.match(r'yes|y', rebase_action, re.I): |
[email protected] | 37e8987 | 2010-09-07 16:11:33 | [diff] [blame] | 587 | self._Run(['reset', '--hard', 'HEAD'], options) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 588 | # Should this be recursive? |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 589 | rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 590 | break |
| 591 | elif re.match(r'quit|q', rebase_action, re.I): |
| 592 | raise gclient_utils.Error("Please merge or rebase manually\n" |
| 593 | "cd %s && git " % self.checkout_path |
| 594 | + "%s" % ' '.join(rebase_cmd)) |
| 595 | elif re.match(r'show|s', rebase_action, re.I): |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 596 | print('\n%s' % e.stderr.strip()) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 597 | continue |
| 598 | else: |
| 599 | gclient_utils.Error("Input not recognized") |
| 600 | continue |
| 601 | elif re.search(r'^CONFLICT', e.stdout, re.M): |
| 602 | raise gclient_utils.Error("Conflict while rebasing this branch.\n" |
| 603 | "Fix the conflict and run gclient again.\n" |
| 604 | "See 'man git-rebase' for details.\n") |
| 605 | else: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 606 | print(e.stdout.strip()) |
| 607 | print('Rebase produced error output:\n%s' % e.stderr.strip()) |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 608 | raise gclient_utils.Error("Unrecognized error, please merge or rebase " |
| 609 | "manually.\ncd %s && git " % |
| 610 | self.checkout_path |
| 611 | + "%s" % ' '.join(rebase_cmd)) |
| 612 | |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 613 | print(rebase_output.strip()) |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 614 | if not options.verbose: |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 615 | # Make the output a little prettier. It's nice to have some |
| 616 | # whitespace between projects when syncing. |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 617 | print('') |
[email protected] | d90ba3f | 2010-02-23 14:42:57 | [diff] [blame] | 618 | |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 619 | @staticmethod |
| 620 | def _CheckMinVersion(min_version): |
[email protected] | d0f854a | 2010-03-11 19:35:53 | [diff] [blame] | 621 | (ok, current_version) = scm.GIT.AssertVersion(min_version) |
| 622 | if not ok: |
| 623 | raise gclient_utils.Error('git version %s < minimum required %s' % |
| 624 | (current_version, min_version)) |
[email protected] | 923a037 | 2009-12-11 20:42:43 | [diff] [blame] | 625 | |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 626 | def _IsRebasing(self): |
| 627 | # Check for any of REBASE-i/REBASE-m/REBASE/AM. Unfortunately git doesn't |
| 628 | # have a plumbing command to determine whether a rebase is in progress, so |
| 629 | # for now emualate (more-or-less) git-rebase.sh / git-completion.bash |
| 630 | g = os.path.join(self.checkout_path, '.git') |
| 631 | return ( |
| 632 | os.path.isdir(os.path.join(g, "rebase-merge")) or |
| 633 | os.path.isdir(os.path.join(g, "rebase-apply"))) |
| 634 | |
| 635 | def _CheckClean(self, rev_str): |
| 636 | # Make sure the tree is clean; see git-rebase.sh for reference |
| 637 | try: |
| 638 | scm.GIT.Capture(['update-index', '--ignore-submodules', '--refresh'], |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 639 | cwd=self.checkout_path) |
[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 640 | except subprocess2.CalledProcessError: |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 641 | raise gclient_utils.Error('\n____ %s%s\n' |
| 642 | '\tYou have unstaged changes.\n' |
| 643 | '\tPlease commit, stash, or reset.\n' |
| 644 | % (self.relpath, rev_str)) |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 645 | try: |
| 646 | scm.GIT.Capture(['diff-index', '--cached', '--name-status', '-r', |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 647 | '--ignore-submodules', 'HEAD', '--'], |
| 648 | cwd=self.checkout_path) |
[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 649 | except subprocess2.CalledProcessError: |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 650 | raise gclient_utils.Error('\n____ %s%s\n' |
| 651 | '\tYour index contains uncommitted changes\n' |
| 652 | '\tPlease commit, stash, or reset.\n' |
| 653 | % (self.relpath, rev_str)) |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 654 | |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 655 | def _CheckDetachedHead(self, rev_str, options): |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 656 | # HEAD is detached. Make sure it is safe to move away from (i.e., it is |
| 657 | # reference by a commit). If not, error out -- most likely a rebase is |
| 658 | # in progress, try to detect so we can give a better error. |
| 659 | try: |
[email protected] | ad80e3b | 2010-09-09 14:18:28 | [diff] [blame] | 660 | scm.GIT.Capture(['name-rev', '--no-undefined', 'HEAD'], |
| 661 | cwd=self.checkout_path) |
[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 662 | except subprocess2.CalledProcessError: |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 663 | # Commit is not contained by any rev. See if the user is rebasing: |
| 664 | if self._IsRebasing(): |
| 665 | # Punt to the user |
| 666 | raise gclient_utils.Error('\n____ %s%s\n' |
| 667 | '\tAlready in a conflict, i.e. (no branch).\n' |
| 668 | '\tFix the conflict and run gclient again.\n' |
| 669 | '\tOr to abort run:\n\t\tgit-rebase --abort\n' |
| 670 | '\tSee man git-rebase for details.\n' |
| 671 | % (self.relpath, rev_str)) |
| 672 | # Let's just save off the commit so we can proceed. |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 673 | name = ('saved-by-gclient-' + |
| 674 | self._Capture(['rev-parse', '--short', 'HEAD'])) |
| 675 | self._Capture(['branch', name]) |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 676 | print('\n_____ found an unreferenced commit and saved it as \'%s\'' % |
[email protected] | f5d37bf | 2010-09-02 00:50:34 | [diff] [blame] | 677 | name) |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 678 | |
[email protected] | 5bde485 | 2009-12-14 16:47:12 | [diff] [blame] | 679 | def _GetCurrentBranch(self): |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 680 | # Returns name of current branch or None for detached HEAD |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 681 | branch = self._Capture(['rev-parse', '--abbrev-ref=strict', 'HEAD']) |
[email protected] | 786fb68 | 2010-06-02 15:16:23 | [diff] [blame] | 682 | if branch == 'HEAD': |
[email protected] | 5bde485 | 2009-12-14 16:47:12 | [diff] [blame] | 683 | return None |
| 684 | return branch |
| 685 | |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 686 | def _Capture(self, args): |
[email protected] | bffad37 | 2011-09-08 17:54:22 | [diff] [blame] | 687 | return subprocess2.check_output( |
[email protected] | 87e6d33 | 2011-09-09 19:01:28 | [diff] [blame] | 688 | ['git'] + args, |
| 689 | stderr=subprocess2.PIPE, |
| 690 | cwd=self.checkout_path).strip() |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 691 | |
[email protected] | 37e8987 | 2010-09-07 16:11:33 | [diff] [blame] | 692 | def _Run(self, args, options, **kwargs): |
[email protected] | 6cafa13 | 2010-09-07 14:17:26 | [diff] [blame] | 693 | kwargs.setdefault('cwd', self.checkout_path) |
[email protected] | 85d3e3a | 2011-10-07 17:12:00 | [diff] [blame] | 694 | kwargs.setdefault('print_stdout', True) |
| 695 | stdout = kwargs.get('stdout', sys.stdout) |
| 696 | stdout.write('\n________ running \'git %s\' in \'%s\'\n' % ( |
| 697 | ' '.join(args), kwargs['cwd'])) |
| 698 | gclient_utils.CheckCallAndFilter(['git'] + args, **kwargs) |
[email protected] | e28e498 | 2009-09-25 20:51:45 | [diff] [blame] | 699 | |
| 700 | |
[email protected] | 55e724e | 2010-03-11 19:36:49 | [diff] [blame] | 701 | class SVNWrapper(SCMWrapper): |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 702 | """ Wrapper for SVN """ |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 703 | |
[email protected] | eaab784 | 2011-04-28 09:07:58 | [diff] [blame] | 704 | def GetRevisionDate(self, revision): |
| 705 | """Returns the given revision's date in ISO-8601 format (which contains the |
| 706 | time zone).""" |
| 707 | date = scm.SVN.Capture(['propget', '--revprop', 'svn:date', '-r', revision, |
| 708 | os.path.join(self.checkout_path, '.')]) |
| 709 | return date.strip() |
| 710 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 711 | def cleanup(self, options, args, file_list): |
| 712 | """Cleanup working copy.""" |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 713 | self._Run(['cleanup'] + args, options) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 714 | |
| 715 | def diff(self, options, args, file_list): |
| 716 | # NOTE: This function does not currently modify file_list. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 717 | if not os.path.isdir(self.checkout_path): |
| 718 | raise gclient_utils.Error('Directory %s is not present.' % |
| 719 | self.checkout_path) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 720 | self._Run(['diff'] + args, options) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 721 | |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 722 | def pack(self, options, args, file_list): |
| 723 | """Generates a patch file which can be applied to the root of the |
| 724 | repository.""" |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 725 | if not os.path.isdir(self.checkout_path): |
| 726 | raise gclient_utils.Error('Directory %s is not present.' % |
| 727 | self.checkout_path) |
| 728 | gclient_utils.CheckCallAndFilter( |
| 729 | ['svn', 'diff', '-x', '--ignore-eol-style'] + args, |
| 730 | cwd=self.checkout_path, |
| 731 | print_stdout=False, |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 732 | filter_fn=DiffFilterer(self.relpath).Filter) |
[email protected] | ee4071d | 2009-12-22 22:25:37 | [diff] [blame] | 733 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 734 | def update(self, options, args, file_list): |
[email protected] | d650421 | 2010-01-13 17:34:31 | [diff] [blame] | 735 | """Runs svn to update or transparently checkout the working copy. |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 736 | |
| 737 | All updated files will be appended to file_list. |
| 738 | |
| 739 | Raises: |
| 740 | Error: if can't get URL for relative path. |
| 741 | """ |
[email protected] | 21dca0e | 2010-10-05 00:55:12 | [diff] [blame] | 742 | # Only update if git or hg is not controlling the directory. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 743 | git_path = os.path.join(self.checkout_path, '.git') |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 744 | if os.path.exists(git_path): |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 745 | print('________ found .git directory; skipping %s' % self.relpath) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 746 | return |
| 747 | |
[email protected] | 21dca0e | 2010-10-05 00:55:12 | [diff] [blame] | 748 | hg_path = os.path.join(self.checkout_path, '.hg') |
| 749 | if os.path.exists(hg_path): |
| 750 | print('________ found .hg directory; skipping %s' % self.relpath) |
| 751 | return |
| 752 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 753 | if args: |
| 754 | raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args)) |
| 755 | |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 756 | # revision is the revision to match. It is None if no revision is specified, |
| 757 | # i.e. the 'deps ain't pinned'. |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 758 | url, revision = gclient_utils.SplitUrlRevision(self.url) |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 759 | # Keep the original unpinned url for reference in case the repo is switched. |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 760 | base_url = url |
[email protected] | eb2756d | 2011-09-20 20:17:51 | [diff] [blame] | 761 | managed = True |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 762 | if options.revision: |
| 763 | # Override the revision number. |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 764 | revision = str(options.revision) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 765 | if revision: |
[email protected] | eb2756d | 2011-09-20 20:17:51 | [diff] [blame] | 766 | if revision != 'unmanaged': |
| 767 | forced_revision = True |
| 768 | # Reconstruct the url. |
| 769 | url = '%s@%s' % (url, revision) |
| 770 | rev_str = ' at %s' % revision |
| 771 | else: |
| 772 | managed = False |
| 773 | revision = None |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 774 | else: |
| 775 | forced_revision = False |
| 776 | rev_str = '' |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 777 | |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 778 | if not os.path.exists(self.checkout_path): |
[email protected] | 6c48a30 | 2011-10-20 23:44:20 | [diff] [blame^] | 779 | gclient_utils.safe_makedirs(os.path.dirname(self.checkout_path)) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 780 | # We need to checkout. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 781 | command = ['checkout', url, self.checkout_path] |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 782 | command = self._AddAdditionalUpdateFlags(command, options, revision) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 783 | self._RunAndGetFileList(command, options, file_list, self._root_dir) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 784 | return |
| 785 | |
[email protected] | eb2756d | 2011-09-20 20:17:51 | [diff] [blame] | 786 | if not managed: |
| 787 | print ('________ unmanaged solution; skipping %s' % self.relpath) |
| 788 | return |
| 789 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 790 | # Get the existing scm url and the revision number of the current checkout. |
[email protected] | 54019f3 | 2010-09-09 13:50:11 | [diff] [blame] | 791 | try: |
| 792 | from_info = scm.SVN.CaptureInfo(os.path.join(self.checkout_path, '.')) |
[email protected] | 31cb48a | 2011-04-04 18:01:36 | [diff] [blame] | 793 | except (gclient_utils.Error, subprocess2.CalledProcessError): |
[email protected] | 54019f3 | 2010-09-09 13:50:11 | [diff] [blame] | 794 | raise gclient_utils.Error( |
| 795 | ('Can\'t update/checkout %s if an unversioned directory is present. ' |
| 796 | 'Delete the directory and try again.') % self.checkout_path) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 797 | |
[email protected] | 49fcb0c | 2011-09-23 14:34:38 | [diff] [blame] | 798 | if 'URL' not in from_info: |
| 799 | raise gclient_utils.Error( |
| 800 | ('gclient is confused. Couldn\'t get the url for %s.\n' |
| 801 | 'Try using @unmanaged.\n%s') % ( |
| 802 | self.checkout_path, from_info)) |
| 803 | |
[email protected] | e407c9a | 2010-08-09 19:11:37 | [diff] [blame] | 804 | # Look for locked directories. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 805 | dir_info = scm.SVN.CaptureStatus(os.path.join(self.checkout_path, '.')) |
[email protected] | d558c4b | 2011-09-22 18:56:24 | [diff] [blame] | 806 | if any(d[0][2] == 'L' for d in dir_info): |
| 807 | try: |
| 808 | self._Run(['cleanup', self.checkout_path], options) |
| 809 | except subprocess2.CalledProcessError, e: |
| 810 | # Get the status again, svn cleanup may have cleaned up at least |
| 811 | # something. |
| 812 | dir_info = scm.SVN.CaptureStatus(os.path.join(self.checkout_path, '.')) |
| 813 | |
| 814 | # Try to fix the failures by removing troublesome files. |
| 815 | for d in dir_info: |
| 816 | if d[0][2] == 'L': |
| 817 | if d[0][0] == '!' and options.force: |
| 818 | print 'Removing troublesome path %s' % d[1] |
| 819 | gclient_utils.rmtree(d[1]) |
| 820 | else: |
| 821 | print 'Not removing troublesome path %s automatically.' % d[1] |
| 822 | if d[0][0] == '!': |
| 823 | print 'You can pass --force to enable automatic removal.' |
| 824 | raise e |
[email protected] | e407c9a | 2010-08-09 19:11:37 | [diff] [blame] | 825 | |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 826 | # Retrieve the current HEAD version because svn is slow at null updates. |
| 827 | if options.manually_grab_svn_rev and not revision: |
[email protected] | 54019f3 | 2010-09-09 13:50:11 | [diff] [blame] | 828 | from_info_live = scm.SVN.CaptureInfo(from_info['URL']) |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 829 | revision = str(from_info_live['Revision']) |
| 830 | rev_str = ' at %s' % revision |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 831 | |
[email protected] | ac915bb | 2009-11-13 17:03:01 | [diff] [blame] | 832 | if from_info['URL'] != base_url: |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 833 | # The repository url changed, need to switch. |
[email protected] | 54019f3 | 2010-09-09 13:50:11 | [diff] [blame] | 834 | try: |
| 835 | to_info = scm.SVN.CaptureInfo(url) |
[email protected] | 31cb48a | 2011-04-04 18:01:36 | [diff] [blame] | 836 | except (gclient_utils.Error, subprocess2.CalledProcessError): |
[email protected] | e2ce0c7 | 2009-09-23 16:14:18 | [diff] [blame] | 837 | # The url is invalid or the server is not accessible, it's safer to bail |
| 838 | # out right now. |
| 839 | raise gclient_utils.Error('This url is unreachable: %s' % url) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 840 | can_switch = ((from_info['Repository Root'] != to_info['Repository Root']) |
| 841 | and (from_info['UUID'] == to_info['UUID'])) |
| 842 | if can_switch: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 843 | print('\n_____ relocating %s to a new checkout' % self.relpath) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 844 | # We have different roots, so check if we can switch --relocate. |
| 845 | # Subversion only permits this if the repository UUIDs match. |
| 846 | # Perform the switch --relocate, then rewrite the from_url |
| 847 | # to reflect where we "are now." (This is the same way that |
| 848 | # Subversion itself handles the metadata when switch --relocate |
| 849 | # is used.) This makes the checks below for whether we |
| 850 | # can update to a revision or have to switch to a different |
| 851 | # branch work as expected. |
| 852 | # TODO(maruel): TEST ME ! |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 853 | command = ['switch', '--relocate', |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 854 | from_info['Repository Root'], |
| 855 | to_info['Repository Root'], |
| 856 | self.relpath] |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 857 | self._Run(command, options, cwd=self._root_dir) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 858 | from_info['URL'] = from_info['URL'].replace( |
| 859 | from_info['Repository Root'], |
| 860 | to_info['Repository Root']) |
| 861 | else: |
[email protected] | 3294f52 | 2010-08-18 19:54:57 | [diff] [blame] | 862 | if not options.force and not options.reset: |
[email protected] | 86f0f95 | 2010-08-10 17:17:19 | [diff] [blame] | 863 | # Look for local modifications but ignore unversioned files. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 864 | for status in scm.SVN.CaptureStatus(self.checkout_path): |
[email protected] | 86f0f95 | 2010-08-10 17:17:19 | [diff] [blame] | 865 | if status[0] != '?': |
| 866 | raise gclient_utils.Error( |
| 867 | ('Can\'t switch the checkout to %s; UUID don\'t match and ' |
| 868 | 'there is local changes in %s. Delete the directory and ' |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 869 | 'try again.') % (url, self.checkout_path)) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 870 | # Ok delete it. |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 871 | print('\n_____ switching %s to a new checkout' % self.relpath) |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 872 | gclient_utils.RemoveDirectory(self.checkout_path) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 873 | # We need to checkout. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 874 | command = ['checkout', url, self.checkout_path] |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 875 | command = self._AddAdditionalUpdateFlags(command, options, revision) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 876 | self._RunAndGetFileList(command, options, file_list, self._root_dir) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 877 | return |
| 878 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 879 | # If the provided url has a revision number that matches the revision |
| 880 | # number of the existing directory, then we don't need to bother updating. |
[email protected] | 2e0c685 | 2009-09-24 00:02:07 | [diff] [blame] | 881 | if not options.force and str(from_info['Revision']) == revision: |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 882 | if options.verbose or not forced_revision: |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 883 | print('\n_____ %s%s' % (self.relpath, rev_str)) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 884 | return |
| 885 | |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 886 | command = ['update', self.checkout_path] |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 887 | command = self._AddAdditionalUpdateFlags(command, options, revision) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 888 | self._RunAndGetFileList(command, options, file_list, self._root_dir) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 889 | |
[email protected] | 4b5b177 | 2010-04-08 01:52:56 | [diff] [blame] | 890 | def updatesingle(self, options, args, file_list): |
[email protected] | 4b5b177 | 2010-04-08 01:52:56 | [diff] [blame] | 891 | filename = args.pop() |
[email protected] | 5756466 | 2010-04-14 02:35:12 | [diff] [blame] | 892 | if scm.SVN.AssertVersion("1.5")[0]: |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 893 | if not os.path.exists(os.path.join(self.checkout_path, '.svn')): |
[email protected] | 5756466 | 2010-04-14 02:35:12 | [diff] [blame] | 894 | # Create an empty checkout and then update the one file we want. Future |
| 895 | # operations will only apply to the one file we checked out. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 896 | command = ["checkout", "--depth", "empty", self.url, self.checkout_path] |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 897 | self._Run(command, options, cwd=self._root_dir) |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 898 | if os.path.exists(os.path.join(self.checkout_path, filename)): |
| 899 | os.remove(os.path.join(self.checkout_path, filename)) |
[email protected] | 5756466 | 2010-04-14 02:35:12 | [diff] [blame] | 900 | command = ["update", filename] |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 901 | self._RunAndGetFileList(command, options, file_list) |
[email protected] | 5756466 | 2010-04-14 02:35:12 | [diff] [blame] | 902 | # After the initial checkout, we can use update as if it were any other |
| 903 | # dep. |
| 904 | self.update(options, args, file_list) |
| 905 | else: |
| 906 | # If the installed version of SVN doesn't support --depth, fallback to |
| 907 | # just exporting the file. This has the downside that revision |
| 908 | # information is not stored next to the file, so we will have to |
| 909 | # re-export the file every time we sync. |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 910 | if not os.path.exists(self.checkout_path): |
[email protected] | 6c48a30 | 2011-10-20 23:44:20 | [diff] [blame^] | 911 | gclient_utils.safe_makedirs(self.checkout_path) |
[email protected] | 5756466 | 2010-04-14 02:35:12 | [diff] [blame] | 912 | command = ["export", os.path.join(self.url, filename), |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 913 | os.path.join(self.checkout_path, filename)] |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 914 | command = self._AddAdditionalUpdateFlags(command, options, |
| 915 | options.revision) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 916 | self._Run(command, options, cwd=self._root_dir) |
[email protected] | 4b5b177 | 2010-04-08 01:52:56 | [diff] [blame] | 917 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 918 | def revert(self, options, args, file_list): |
| 919 | """Reverts local modifications. Subversion specific. |
| 920 | |
| 921 | All reverted files will be appended to file_list, even if Subversion |
| 922 | doesn't know about them. |
| 923 | """ |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 924 | if not os.path.isdir(self.checkout_path): |
[email protected] | c0cc087 | 2011-10-12 17:02:41 | [diff] [blame] | 925 | if os.path.exists(self.checkout_path): |
| 926 | gclient_utils.rmtree(self.checkout_path) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 927 | # svn revert won't work if the directory doesn't exist. It needs to |
| 928 | # checkout instead. |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 929 | print('\n_____ %s is missing, synching instead' % self.relpath) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 930 | # Don't reuse the args. |
| 931 | return self.update(options, [], file_list) |
| 932 | |
[email protected] | c0cc087 | 2011-10-12 17:02:41 | [diff] [blame] | 933 | if not os.path.isdir(os.path.join(self.checkout_path, '.svn')): |
| 934 | if os.path.isdir(os.path.join(self.checkout_path, '.git')): |
| 935 | print('________ found .git directory; skipping %s' % self.relpath) |
| 936 | return |
| 937 | if os.path.isdir(os.path.join(self.checkout_path, '.hg')): |
| 938 | print('________ found .hg directory; skipping %s' % self.relpath) |
| 939 | return |
| 940 | if not options.force: |
| 941 | raise gclient_utils.Error('Invalid checkout path, aborting') |
| 942 | print( |
| 943 | '\n_____ %s is not a valid svn checkout, synching instead' % |
| 944 | self.relpath) |
| 945 | gclient_utils.rmtree(self.checkout_path) |
| 946 | # Don't reuse the args. |
| 947 | return self.update(options, [], file_list) |
| 948 | |
[email protected] | 07ab60e | 2011-02-08 21:54:00 | [diff] [blame] | 949 | def printcb(file_status): |
| 950 | file_list.append(file_status[1]) |
[email protected] | aa3dd47 | 2009-09-21 19:02:48 | [diff] [blame] | 951 | if logging.getLogger().isEnabledFor(logging.INFO): |
[email protected] | 07ab60e | 2011-02-08 21:54:00 | [diff] [blame] | 952 | logging.info('%s%s' % (file_status[0], file_status[1])) |
[email protected] | aa3dd47 | 2009-09-21 19:02:48 | [diff] [blame] | 953 | else: |
[email protected] | 07ab60e | 2011-02-08 21:54:00 | [diff] [blame] | 954 | print(os.path.join(self.checkout_path, file_status[1])) |
| 955 | scm.SVN.Revert(self.checkout_path, callback=printcb) |
[email protected] | aa3dd47 | 2009-09-21 19:02:48 | [diff] [blame] | 956 | |
[email protected] | 810a50b | 2009-10-05 23:03:18 | [diff] [blame] | 957 | try: |
| 958 | # svn revert is so broken we don't even use it. Using |
| 959 | # "svn up --revision BASE" achieve the same effect. |
[email protected] | 07ab60e | 2011-02-08 21:54:00 | [diff] [blame] | 960 | # file_list will contain duplicates. |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 961 | self._RunAndGetFileList(['update', '--revision', 'BASE'], options, |
| 962 | file_list) |
[email protected] | 810a50b | 2009-10-05 23:03:18 | [diff] [blame] | 963 | except OSError, e: |
[email protected] | 07ab60e | 2011-02-08 21:54:00 | [diff] [blame] | 964 | # Maybe the directory disapeared meanwhile. Do not throw an exception. |
[email protected] | 810a50b | 2009-10-05 23:03:18 | [diff] [blame] | 965 | logging.error('Failed to update:\n%s' % str(e)) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 966 | |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 967 | def revinfo(self, options, args, file_list): |
| 968 | """Display revision""" |
[email protected] | 54019f3 | 2010-09-09 13:50:11 | [diff] [blame] | 969 | try: |
| 970 | return scm.SVN.CaptureRevision(self.checkout_path) |
[email protected] | 31cb48a | 2011-04-04 18:01:36 | [diff] [blame] | 971 | except (gclient_utils.Error, subprocess2.CalledProcessError): |
[email protected] | 54019f3 | 2010-09-09 13:50:11 | [diff] [blame] | 972 | return None |
[email protected] | 0f28206 | 2009-11-06 20:14:02 | [diff] [blame] | 973 | |
[email protected] | cb5442b | 2009-09-22 16:51:24 | [diff] [blame] | 974 | def runhooks(self, options, args, file_list): |
| 975 | self.status(options, args, file_list) |
| 976 | |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 977 | def status(self, options, args, file_list): |
| 978 | """Display status information.""" |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 979 | command = ['status'] + args |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 980 | if not os.path.isdir(self.checkout_path): |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 981 | # svn status won't work if the directory doesn't exist. |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 982 | print(('\n________ couldn\'t run \'%s\' in \'%s\':\n' |
| 983 | 'The directory does not exist.') % |
| 984 | (' '.join(command), self.checkout_path)) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 985 | # There's no file list to retrieve. |
| 986 | else: |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 987 | self._RunAndGetFileList(command, options, file_list) |
[email protected] | e6f7835 | 2010-01-13 17:05:33 | [diff] [blame] | 988 | |
| 989 | def FullUrlForRelativeUrl(self, url): |
| 990 | # Find the forth '/' and strip from there. A bit hackish. |
| 991 | return '/'.join(self.url.split('/')[:4]) + url |
[email protected] | 9982812 | 2010-06-04 01:41:02 | [diff] [blame] | 992 | |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 993 | def _Run(self, args, options, **kwargs): |
| 994 | """Runs a commands that goes to stdout.""" |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 995 | kwargs.setdefault('cwd', self.checkout_path) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 996 | gclient_utils.CheckCallAndFilterAndHeader(['svn'] + args, |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 997 | always=options.verbose, **kwargs) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 998 | |
| 999 | def _RunAndGetFileList(self, args, options, file_list, cwd=None): |
| 1000 | """Runs a commands that goes to stdout and grabs the file listed.""" |
[email protected] | 8469bf9 | 2010-09-03 19:03:15 | [diff] [blame] | 1001 | cwd = cwd or self.checkout_path |
[email protected] | ce117f6 | 2011-01-17 20:04:25 | [diff] [blame] | 1002 | scm.SVN.RunAndGetFileList( |
| 1003 | options.verbose, |
| 1004 | args + ['--ignore-externals'], |
| 1005 | cwd=cwd, |
[email protected] | 77e4eca | 2010-09-21 13:23:07 | [diff] [blame] | 1006 | file_list=file_list) |
[email protected] | 669600d | 2010-09-01 19:06:31 | [diff] [blame] | 1007 | |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1008 | @staticmethod |
[email protected] | 8e0e926 | 2010-08-17 19:20:27 | [diff] [blame] | 1009 | def _AddAdditionalUpdateFlags(command, options, revision): |
[email protected] | 9982812 | 2010-06-04 01:41:02 | [diff] [blame] | 1010 | """Add additional flags to command depending on what options are set. |
| 1011 | command should be a list of strings that represents an svn command. |
| 1012 | |
| 1013 | This method returns a new list to be used as a command.""" |
| 1014 | new_command = command[:] |
| 1015 | if revision: |
| 1016 | new_command.extend(['--revision', str(revision).strip()]) |
[email protected] | 36ac239 | 2011-10-12 16:36:11 | [diff] [blame] | 1017 | # We don't want interaction when jobs are used. |
| 1018 | if options.jobs > 1: |
| 1019 | new_command.append('--non-interactive') |
[email protected] | 9982812 | 2010-06-04 01:41:02 | [diff] [blame] | 1020 | # --force was added to 'svn update' in svn 1.5. |
[email protected] | 36ac239 | 2011-10-12 16:36:11 | [diff] [blame] | 1021 | # --accept was added to 'svn update' in svn 1.6. |
| 1022 | if not scm.SVN.AssertVersion('1.5')[0]: |
| 1023 | return new_command |
| 1024 | |
| 1025 | # It's annoying to have it block in the middle of a sync, just sensible |
| 1026 | # defaults. |
| 1027 | if options.force: |
[email protected] | 9982812 | 2010-06-04 01:41:02 | [diff] [blame] | 1028 | new_command.append('--force') |
[email protected] | 36ac239 | 2011-10-12 16:36:11 | [diff] [blame] | 1029 | if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1030 | new_command.extend(('--accept', 'theirs-conflict')) |
| 1031 | elif options.manually_grab_svn_rev: |
| 1032 | new_command.append('--force') |
| 1033 | if command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1034 | new_command.extend(('--accept', 'postpone')) |
| 1035 | elif command[0] != 'checkout' and scm.SVN.AssertVersion('1.6')[0]: |
| 1036 | new_command.extend(('--accept', 'postpone')) |
[email protected] | 9982812 | 2010-06-04 01:41:02 | [diff] [blame] | 1037 | return new_command |