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