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