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