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