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