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