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