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