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 |
| 20 | working copy lines of the svn diff output.""" |
| 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'): |
| 58 | # TODO(maruel): Deduce the SCM from the url. |
| 59 | scm_map = { |
| 60 | 'svn' : SVNWrapper, |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 61 | 'git' : GitWrapper, |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 62 | } |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 63 | |
msb@chromium.org | 1b8779a | 2009-11-19 18:11:39 +0000 | [diff] [blame] | 64 | orig_url = url |
| 65 | |
| 66 | if url: |
| 67 | url, _ = gclient_utils.SplitUrlRevision(url) |
| 68 | if url.startswith('git:') or url.startswith('ssh:') or url.endswith('.git'): |
| 69 | scm_name = 'git' |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 70 | |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 71 | if not scm_name in scm_map: |
| 72 | raise gclient_utils.Error('Unsupported scm %s' % scm_name) |
msb@chromium.org | 1b8779a | 2009-11-19 18:11:39 +0000 | [diff] [blame] | 73 | 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] | 74 | |
| 75 | |
| 76 | # SCMWrapper base class |
| 77 | |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 78 | class SCMWrapper(object): |
| 79 | """Add necessary glue between all the supported SCM. |
| 80 | |
| 81 | This is the abstraction layer to bind to different SCM. Since currently only |
| 82 | subversion is supported, a lot of subersionism remains. This can be sorted out |
| 83 | once another SCM is supported.""" |
maruel@chromium.org | 5e73b0c | 2009-09-18 19:47:48 +0000 | [diff] [blame] | 84 | def __init__(self, url=None, root_dir=None, relpath=None, |
| 85 | scm_name='svn'): |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 86 | self.scm_name = scm_name |
| 87 | self.url = url |
maruel@chromium.org | 5e73b0c | 2009-09-18 19:47:48 +0000 | [diff] [blame] | 88 | self._root_dir = root_dir |
| 89 | if self._root_dir: |
| 90 | self._root_dir = self._root_dir.replace('/', os.sep) |
| 91 | self.relpath = relpath |
| 92 | if self.relpath: |
| 93 | self.relpath = self.relpath.replace('/', os.sep) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 94 | if self.relpath and self._root_dir: |
| 95 | self.checkout_path = os.path.join(self._root_dir, self.relpath) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 96 | |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 97 | def RunCommand(self, command, options, args, file_list=None): |
| 98 | # 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] | 99 | if file_list is None: |
| 100 | file_list = [] |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 101 | |
msb@chromium.org | 0f28206 | 2009-11-06 20:14:02 +0000 | [diff] [blame] | 102 | commands = ['cleanup', 'export', 'update', 'revert', 'revinfo', |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 103 | 'status', 'diff', 'pack', 'runhooks'] |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 104 | |
| 105 | if not command in commands: |
| 106 | raise gclient_utils.Error('Unknown command %s' % command) |
| 107 | |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 108 | if not command in dir(self): |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 109 | raise gclient_utils.Error('Command %s not implemented in %s wrapper' % ( |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 110 | command, self.scm_name)) |
| 111 | |
| 112 | return getattr(self, command)(options, args, file_list) |
| 113 | |
| 114 | |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 115 | class GitWrapper(SCMWrapper, scm.GIT): |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 116 | """Wrapper for Git""" |
| 117 | |
| 118 | def cleanup(self, options, args, file_list): |
| 119 | """Cleanup working copy.""" |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 120 | __pychecker__ = 'unusednames=args,file_list,options' |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 121 | self._Run(['prune'], redirect_stdout=False) |
| 122 | self._Run(['fsck'], redirect_stdout=False) |
| 123 | self._Run(['gc'], redirect_stdout=False) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 124 | |
| 125 | def diff(self, options, args, file_list): |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 126 | __pychecker__ = 'unusednames=args,file_list,options' |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 127 | merge_base = self._Run(['merge-base', 'HEAD', 'origin']) |
| 128 | self._Run(['diff', merge_base], redirect_stdout=False) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 129 | |
| 130 | def export(self, options, args, file_list): |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 131 | __pychecker__ = 'unusednames=file_list,options' |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 132 | assert len(args) == 1 |
| 133 | export_path = os.path.abspath(os.path.join(args[0], self.relpath)) |
| 134 | if not os.path.exists(export_path): |
| 135 | os.makedirs(export_path) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 136 | self._Run(['checkout-index', '-a', '--prefix=%s/' % export_path], |
| 137 | redirect_stdout=False) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 138 | |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 139 | def pack(self, options, args, file_list): |
| 140 | """Generates a patch file which can be applied to the root of the |
| 141 | repository.""" |
| 142 | __pychecker__ = 'unusednames=file_list,options' |
| 143 | path = os.path.join(self._root_dir, self.relpath) |
| 144 | merge_base = self._Run(['merge-base', 'HEAD', 'origin']) |
| 145 | command = ['diff', merge_base] |
| 146 | filterer = DiffFilterer(self.relpath) |
| 147 | self.RunAndFilterOutput(command, path, False, False, filterer.Filter) |
| 148 | |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 149 | def update(self, options, args, file_list): |
| 150 | """Runs git to update or transparently checkout the working copy. |
| 151 | |
| 152 | All updated files will be appended to file_list. |
| 153 | |
| 154 | Raises: |
| 155 | Error: if can't get URL for relative path. |
| 156 | """ |
| 157 | |
| 158 | if args: |
| 159 | raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args)) |
| 160 | |
msb@chromium.org | 83376f2 | 2009-12-11 22:25:31 +0000 | [diff] [blame] | 161 | self._CheckMinVersion("1.6") |
msb@chromium.org | 923a037 | 2009-12-11 20:42:43 +0000 | [diff] [blame] | 162 | |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 163 | url, revision = gclient_utils.SplitUrlRevision(self.url) |
| 164 | rev_str = "" |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 165 | if options.revision: |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 166 | # Override the revision number. |
| 167 | revision = str(options.revision) |
| 168 | if revision: |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 169 | rev_str = ' at %s' % revision |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 170 | |
msb@chromium.org | b1a22bf | 2009-11-07 02:33:50 +0000 | [diff] [blame] | 171 | if options.verbose: |
msb@chromium.org | b1a22bf | 2009-11-07 02:33:50 +0000 | [diff] [blame] | 172 | print("\n_____ %s%s" % (self.relpath, rev_str)) |
| 173 | |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 174 | if not os.path.exists(self.checkout_path): |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 175 | self._Run(['clone', url, self.checkout_path], |
| 176 | cwd=self._root_dir, redirect_stdout=False) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 177 | if revision: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 178 | self._Run(['reset', '--hard', revision], redirect_stdout=False) |
| 179 | files = self._Run(['ls-files']).split() |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 180 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 181 | return |
| 182 | |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 183 | new_base = 'origin' |
| 184 | if revision: |
| 185 | new_base = revision |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 186 | cur_branch = self._GetCurrentBranch() |
| 187 | |
| 188 | # Check if we are in a rebase conflict |
| 189 | if cur_branch is None: |
| 190 | raise gclient_utils.Error('\n____ %s%s\n' |
| 191 | '\tAlready in a conflict, i.e. (no branch).\n' |
| 192 | '\tFix the conflict and run gclient again.\n' |
| 193 | '\tOr to abort run:\n\t\tgit-rebase --abort\n' |
| 194 | '\tSee man git-rebase for details.\n' |
| 195 | % (self.relpath, rev_str)) |
| 196 | |
msb@chromium.org | f237063 | 2009-11-25 00:22:11 +0000 | [diff] [blame] | 197 | merge_base = self._Run(['merge-base', 'HEAD', new_base]) |
| 198 | self._Run(['remote', 'update'], redirect_stdout=False) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 199 | files = self._Run(['diff', new_base, '--name-only']).split() |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 200 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
msb@chromium.org | f237063 | 2009-11-25 00:22:11 +0000 | [diff] [blame] | 201 | self._Run(['rebase', '-v', '--onto', new_base, merge_base, cur_branch], |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 202 | redirect_stdout=False, checkrc=False) |
| 203 | |
| 204 | # If the rebase generated a conflict, abort and ask user to fix |
| 205 | if self._GetCurrentBranch() is None: |
| 206 | raise gclient_utils.Error('\n____ %s%s\n' |
| 207 | '\nConflict while rebasing this branch.\n' |
| 208 | 'Fix the conflict and run gclient again.\n' |
| 209 | 'See man git-rebase for details.\n' |
| 210 | % (self.relpath, rev_str)) |
| 211 | |
msb@chromium.org | b1a22bf | 2009-11-07 02:33:50 +0000 | [diff] [blame] | 212 | print "Checked out revision %s." % self.revinfo(options, (), None) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 213 | |
| 214 | def revert(self, options, args, file_list): |
| 215 | """Reverts local modifications. |
| 216 | |
| 217 | All reverted files will be appended to file_list. |
| 218 | """ |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 219 | __pychecker__ = 'unusednames=args' |
msb@chromium.org | 260c653 | 2009-10-28 03:22:35 +0000 | [diff] [blame] | 220 | path = os.path.join(self._root_dir, self.relpath) |
| 221 | if not os.path.isdir(path): |
| 222 | # revert won't work if the directory doesn't exist. It needs to |
| 223 | # checkout instead. |
| 224 | print("\n_____ %s is missing, synching instead" % self.relpath) |
| 225 | # Don't reuse the args. |
| 226 | return self.update(options, [], file_list) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 227 | merge_base = self._Run(['merge-base', 'HEAD', 'origin']) |
| 228 | files = self._Run(['diff', merge_base, '--name-only']).split() |
| 229 | self._Run(['reset', '--hard', merge_base], redirect_stdout=False) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 230 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 231 | |
msb@chromium.org | 0f28206 | 2009-11-06 20:14:02 +0000 | [diff] [blame] | 232 | def revinfo(self, options, args, file_list): |
| 233 | """Display revision""" |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 234 | __pychecker__ = 'unusednames=args,file_list,options' |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 235 | return self._Run(['rev-parse', 'HEAD']) |
msb@chromium.org | 0f28206 | 2009-11-06 20:14:02 +0000 | [diff] [blame] | 236 | |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 237 | def runhooks(self, options, args, file_list): |
| 238 | self.status(options, args, file_list) |
| 239 | |
| 240 | def status(self, options, args, file_list): |
| 241 | """Display status information.""" |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 242 | __pychecker__ = 'unusednames=args,options' |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 243 | if not os.path.isdir(self.checkout_path): |
| 244 | print('\n________ couldn\'t run status in %s:\nThe directory ' |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 245 | 'does not exist.' % self.checkout_path) |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 246 | else: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 247 | merge_base = self._Run(['merge-base', 'HEAD', 'origin']) |
| 248 | self._Run(['diff', '--name-status', merge_base], redirect_stdout=False) |
| 249 | files = self._Run(['diff', '--name-only', merge_base]).split() |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 250 | file_list.extend([os.path.join(self.checkout_path, f) for f in files]) |
| 251 | |
msb@chromium.org | e6f7835 | 2010-01-13 17:05:33 +0000 | [diff] [blame^] | 252 | def FullUrlForRelativeUrl(self, url): |
| 253 | # Strip from last '/' |
| 254 | # Equivalent to unix basename |
| 255 | base_url = self.url |
| 256 | return base_url[:base_url.rfind('/')] + url |
| 257 | |
msb@chromium.org | 923a037 | 2009-12-11 20:42:43 +0000 | [diff] [blame] | 258 | def _CheckMinVersion(self, min_version): |
msb@chromium.org | 83376f2 | 2009-12-11 22:25:31 +0000 | [diff] [blame] | 259 | def only_int(val): |
| 260 | if val.isdigit(): |
| 261 | return int(val) |
| 262 | else: |
| 263 | return 0 |
msb@chromium.org | ba9b239 | 2009-12-11 23:30:13 +0000 | [diff] [blame] | 264 | version = self._Run(['--version'], cwd='.').split()[-1] |
msb@chromium.org | 83376f2 | 2009-12-11 22:25:31 +0000 | [diff] [blame] | 265 | version_list = map(only_int, version.split('.')) |
msb@chromium.org | 923a037 | 2009-12-11 20:42:43 +0000 | [diff] [blame] | 266 | min_version_list = map(int, min_version.split('.')) |
| 267 | for min_ver in min_version_list: |
| 268 | ver = version_list.pop(0) |
| 269 | if min_ver > ver: |
| 270 | raise gclient_utils.Error('git version %s < minimum required %s' % |
| 271 | (version, min_version)) |
| 272 | elif min_ver < ver: |
| 273 | return |
| 274 | |
msb@chromium.org | 5bde485 | 2009-12-14 16:47:12 +0000 | [diff] [blame] | 275 | def _GetCurrentBranch(self): |
| 276 | # Returns name of current branch |
| 277 | # Returns None if inside a (no branch) |
| 278 | tokens = self._Run(['branch']).split() |
| 279 | branch = tokens[tokens.index('*') + 1] |
| 280 | if branch == '(no': |
| 281 | return None |
| 282 | return branch |
| 283 | |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 284 | def _Run(self, args, cwd=None, checkrc=True, redirect_stdout=True): |
| 285 | # TODO(maruel): Merge with Capture? |
maruel@chromium.org | ffe96f0 | 2009-12-09 18:39:15 +0000 | [diff] [blame] | 286 | if cwd is None: |
| 287 | cwd = self.checkout_path |
msb@chromium.org | e8e60e5 | 2009-11-02 21:50:56 +0000 | [diff] [blame] | 288 | stdout=None |
| 289 | if redirect_stdout: |
| 290 | stdout=subprocess.PIPE |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 291 | if cwd == None: |
| 292 | cwd = self.checkout_path |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 293 | cmd = [self.COMMAND] |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 294 | cmd.extend(args) |
maruel@chromium.org | f3909bf | 2010-01-08 01:14:51 +0000 | [diff] [blame] | 295 | logging.debug(cmd) |
| 296 | try: |
| 297 | sp = subprocess.Popen(cmd, cwd=cwd, stdout=stdout) |
| 298 | output = sp.communicate()[0] |
| 299 | except OSError: |
| 300 | raise gclient_utils.Error("git command '%s' failed to run." % |
| 301 | ' '.join(cmd) + "\nCheck that you have git installed.") |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 302 | if checkrc and sp.returncode: |
| 303 | raise gclient_utils.Error('git command %s returned %d' % |
| 304 | (args[0], sp.returncode)) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 305 | if output is not None: |
msb@chromium.org | e8e60e5 | 2009-11-02 21:50:56 +0000 | [diff] [blame] | 306 | return output.strip() |
msb@chromium.org | e28e498 | 2009-09-25 20:51:45 +0000 | [diff] [blame] | 307 | |
| 308 | |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 309 | class SVNWrapper(SCMWrapper, scm.SVN): |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 310 | """ Wrapper for SVN """ |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 311 | |
| 312 | def cleanup(self, options, args, file_list): |
| 313 | """Cleanup working copy.""" |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 314 | __pychecker__ = 'unusednames=file_list,options' |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 315 | command = ['cleanup'] |
| 316 | command.extend(args) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 317 | self.Run(command, os.path.join(self._root_dir, self.relpath)) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 318 | |
| 319 | def diff(self, options, args, file_list): |
| 320 | # NOTE: This function does not currently modify file_list. |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 321 | __pychecker__ = 'unusednames=file_list,options' |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 322 | command = ['diff'] |
| 323 | command.extend(args) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 324 | self.Run(command, os.path.join(self._root_dir, self.relpath)) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 325 | |
| 326 | def export(self, options, args, file_list): |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 327 | __pychecker__ = 'unusednames=file_list,options' |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 328 | assert len(args) == 1 |
| 329 | export_path = os.path.abspath(os.path.join(args[0], self.relpath)) |
| 330 | try: |
| 331 | os.makedirs(export_path) |
| 332 | except OSError: |
| 333 | pass |
| 334 | assert os.path.exists(export_path) |
| 335 | command = ['export', '--force', '.'] |
| 336 | command.append(export_path) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 337 | self.Run(command, os.path.join(self._root_dir, self.relpath)) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 338 | |
maruel@chromium.org | ee4071d | 2009-12-22 22:25:37 +0000 | [diff] [blame] | 339 | def pack(self, options, args, file_list): |
| 340 | """Generates a patch file which can be applied to the root of the |
| 341 | repository.""" |
| 342 | __pychecker__ = 'unusednames=file_list,options' |
| 343 | path = os.path.join(self._root_dir, self.relpath) |
| 344 | command = ['diff'] |
| 345 | command.extend(args) |
| 346 | |
| 347 | filterer = DiffFilterer(self.relpath) |
| 348 | self.RunAndFilterOutput(command, path, False, False, filterer.Filter) |
| 349 | |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 350 | def update(self, options, args, file_list): |
| 351 | """Runs SCM to update or transparently checkout the working copy. |
| 352 | |
| 353 | All updated files will be appended to file_list. |
| 354 | |
| 355 | Raises: |
| 356 | Error: if can't get URL for relative path. |
| 357 | """ |
| 358 | # Only update if git is not controlling the directory. |
| 359 | checkout_path = os.path.join(self._root_dir, self.relpath) |
| 360 | git_path = os.path.join(self._root_dir, self.relpath, '.git') |
| 361 | if os.path.exists(git_path): |
| 362 | print("________ found .git directory; skipping %s" % self.relpath) |
| 363 | return |
| 364 | |
| 365 | if args: |
| 366 | raise gclient_utils.Error("Unsupported argument(s): %s" % ",".join(args)) |
| 367 | |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 368 | url, revision = gclient_utils.SplitUrlRevision(self.url) |
| 369 | base_url = url |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 370 | forced_revision = False |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 371 | rev_str = "" |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 372 | if options.revision: |
| 373 | # Override the revision number. |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 374 | revision = str(options.revision) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 375 | if revision: |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 376 | forced_revision = True |
| 377 | url = '%s@%s' % (url, revision) |
msb@chromium.org | 770ff9e | 2009-09-23 17:18:18 +0000 | [diff] [blame] | 378 | rev_str = ' at %s' % revision |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 379 | |
| 380 | if not os.path.exists(checkout_path): |
| 381 | # We need to checkout. |
| 382 | command = ['checkout', url, checkout_path] |
| 383 | if revision: |
| 384 | command.extend(['--revision', str(revision)]) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 385 | self.RunAndGetFileList(options, command, self._root_dir, file_list) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 386 | return |
| 387 | |
| 388 | # Get the existing scm url and the revision number of the current checkout. |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 389 | from_info = self.CaptureInfo(os.path.join(checkout_path, '.'), '.') |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 390 | if not from_info: |
| 391 | raise gclient_utils.Error("Can't update/checkout %r if an unversioned " |
| 392 | "directory is present. Delete the directory " |
| 393 | "and try again." % |
| 394 | checkout_path) |
| 395 | |
maruel@chromium.org | 7753d24 | 2009-10-07 17:40:24 +0000 | [diff] [blame] | 396 | if options.manually_grab_svn_rev: |
| 397 | # Retrieve the current HEAD version because svn is slow at null updates. |
| 398 | if not revision: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 399 | from_info_live = self.CaptureInfo(from_info['URL'], '.') |
maruel@chromium.org | 7753d24 | 2009-10-07 17:40:24 +0000 | [diff] [blame] | 400 | revision = str(from_info_live['Revision']) |
| 401 | rev_str = ' at %s' % revision |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 402 | |
msb@chromium.org | ac915bb | 2009-11-13 17:03:01 +0000 | [diff] [blame] | 403 | if from_info['URL'] != base_url: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 404 | to_info = self.CaptureInfo(url, '.') |
maruel@chromium.org | e2ce0c7 | 2009-09-23 16:14:18 +0000 | [diff] [blame] | 405 | if not to_info.get('Repository Root') or not to_info.get('UUID'): |
| 406 | # The url is invalid or the server is not accessible, it's safer to bail |
| 407 | # out right now. |
| 408 | raise gclient_utils.Error('This url is unreachable: %s' % url) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 409 | can_switch = ((from_info['Repository Root'] != to_info['Repository Root']) |
| 410 | and (from_info['UUID'] == to_info['UUID'])) |
| 411 | if can_switch: |
| 412 | print("\n_____ relocating %s to a new checkout" % self.relpath) |
| 413 | # We have different roots, so check if we can switch --relocate. |
| 414 | # Subversion only permits this if the repository UUIDs match. |
| 415 | # Perform the switch --relocate, then rewrite the from_url |
| 416 | # to reflect where we "are now." (This is the same way that |
| 417 | # Subversion itself handles the metadata when switch --relocate |
| 418 | # is used.) This makes the checks below for whether we |
| 419 | # can update to a revision or have to switch to a different |
| 420 | # branch work as expected. |
| 421 | # TODO(maruel): TEST ME ! |
| 422 | command = ["switch", "--relocate", |
| 423 | from_info['Repository Root'], |
| 424 | to_info['Repository Root'], |
| 425 | self.relpath] |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 426 | self.Run(command, self._root_dir) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 427 | from_info['URL'] = from_info['URL'].replace( |
| 428 | from_info['Repository Root'], |
| 429 | to_info['Repository Root']) |
| 430 | else: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 431 | if self.CaptureStatus(checkout_path): |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 432 | raise gclient_utils.Error("Can't switch the checkout to %s; UUID " |
| 433 | "don't match and there is local changes " |
| 434 | "in %s. Delete the directory and " |
| 435 | "try again." % (url, checkout_path)) |
| 436 | # Ok delete it. |
| 437 | print("\n_____ switching %s to a new checkout" % self.relpath) |
bradnelson@google.com | 8f9c69f | 2009-09-17 00:48:28 +0000 | [diff] [blame] | 438 | gclient_utils.RemoveDirectory(checkout_path) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 439 | # We need to checkout. |
| 440 | command = ['checkout', url, checkout_path] |
| 441 | if revision: |
| 442 | command.extend(['--revision', str(revision)]) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 443 | self.RunAndGetFileList(options, command, self._root_dir, file_list) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 444 | return |
| 445 | |
| 446 | |
| 447 | # If the provided url has a revision number that matches the revision |
| 448 | # 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] | 449 | if not options.force and str(from_info['Revision']) == revision: |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 450 | if options.verbose or not forced_revision: |
| 451 | print("\n_____ %s%s" % (self.relpath, rev_str)) |
| 452 | return |
| 453 | |
| 454 | command = ["update", checkout_path] |
| 455 | if revision: |
| 456 | command.extend(['--revision', str(revision)]) |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 457 | self.RunAndGetFileList(options, command, self._root_dir, file_list) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 458 | |
| 459 | def revert(self, options, args, file_list): |
| 460 | """Reverts local modifications. Subversion specific. |
| 461 | |
| 462 | All reverted files will be appended to file_list, even if Subversion |
| 463 | doesn't know about them. |
| 464 | """ |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 465 | __pychecker__ = 'unusednames=args' |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 466 | path = os.path.join(self._root_dir, self.relpath) |
| 467 | if not os.path.isdir(path): |
| 468 | # svn revert won't work if the directory doesn't exist. It needs to |
| 469 | # checkout instead. |
| 470 | print("\n_____ %s is missing, synching instead" % self.relpath) |
| 471 | # Don't reuse the args. |
| 472 | return self.update(options, [], file_list) |
| 473 | |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 474 | for file_status in self.CaptureStatus(path): |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 475 | file_path = os.path.join(path, file_status[1]) |
| 476 | if file_status[0][0] == 'X': |
maruel@chromium.org | 754960e | 2009-09-21 12:31:05 +0000 | [diff] [blame] | 477 | # Ignore externals. |
maruel@chromium.org | aa3dd47 | 2009-09-21 19:02:48 +0000 | [diff] [blame] | 478 | logging.info('Ignoring external %s' % file_path) |
maruel@chromium.org | 754960e | 2009-09-21 12:31:05 +0000 | [diff] [blame] | 479 | continue |
| 480 | |
maruel@chromium.org | aa3dd47 | 2009-09-21 19:02:48 +0000 | [diff] [blame] | 481 | if logging.getLogger().isEnabledFor(logging.INFO): |
| 482 | logging.info('%s%s' % (file[0], file[1])) |
| 483 | else: |
| 484 | print(file_path) |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 485 | if file_status[0].isspace(): |
maruel@chromium.org | aa3dd47 | 2009-09-21 19:02:48 +0000 | [diff] [blame] | 486 | logging.error('No idea what is the status of %s.\n' |
| 487 | 'You just found a bug in gclient, please ping ' |
| 488 | 'maruel@chromium.org ASAP!' % file_path) |
| 489 | # svn revert is really stupid. It fails on inconsistent line-endings, |
| 490 | # on switched directories, etc. So take no chance and delete everything! |
| 491 | try: |
| 492 | if not os.path.exists(file_path): |
| 493 | pass |
maruel@chromium.org | d2e78ff | 2010-01-11 20:37:19 +0000 | [diff] [blame] | 494 | 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] | 495 | logging.info('os.remove(%s)' % file_path) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 496 | os.remove(file_path) |
maruel@chromium.org | aa3dd47 | 2009-09-21 19:02:48 +0000 | [diff] [blame] | 497 | elif os.path.isdir(file_path): |
maruel@chromium.org | 754960e | 2009-09-21 12:31:05 +0000 | [diff] [blame] | 498 | logging.info('gclient_utils.RemoveDirectory(%s)' % file_path) |
bradnelson@google.com | 8f9c69f | 2009-09-17 00:48:28 +0000 | [diff] [blame] | 499 | gclient_utils.RemoveDirectory(file_path) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 500 | else: |
maruel@chromium.org | aa3dd47 | 2009-09-21 19:02:48 +0000 | [diff] [blame] | 501 | logging.error('no idea what is %s.\nYou just found a bug in gclient' |
| 502 | ', please ping maruel@chromium.org ASAP!' % file_path) |
| 503 | except EnvironmentError: |
| 504 | logging.error('Failed to remove %s.' % file_path) |
| 505 | |
maruel@chromium.org | 810a50b | 2009-10-05 23:03:18 +0000 | [diff] [blame] | 506 | try: |
| 507 | # svn revert is so broken we don't even use it. Using |
| 508 | # "svn up --revision BASE" achieve the same effect. |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 509 | self.RunAndGetFileList(options, ['update', '--revision', 'BASE'], path, |
maruel@chromium.org | 810a50b | 2009-10-05 23:03:18 +0000 | [diff] [blame] | 510 | file_list) |
| 511 | except OSError, e: |
| 512 | # Maybe the directory disapeared meanwhile. We don't want it to throw an |
| 513 | # exception. |
| 514 | logging.error('Failed to update:\n%s' % str(e)) |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 515 | |
msb@chromium.org | 0f28206 | 2009-11-06 20:14:02 +0000 | [diff] [blame] | 516 | def revinfo(self, options, args, file_list): |
| 517 | """Display revision""" |
maruel@chromium.org | e3608df | 2009-11-10 20:22:57 +0000 | [diff] [blame] | 518 | __pychecker__ = 'unusednames=args,file_list,options' |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 519 | return self.CaptureHeadRevision(self.url) |
msb@chromium.org | 0f28206 | 2009-11-06 20:14:02 +0000 | [diff] [blame] | 520 | |
msb@chromium.org | cb5442b | 2009-09-22 16:51:24 +0000 | [diff] [blame] | 521 | def runhooks(self, options, args, file_list): |
| 522 | self.status(options, args, file_list) |
| 523 | |
maruel@chromium.org | 5f3eee3 | 2009-09-17 00:34:30 +0000 | [diff] [blame] | 524 | def status(self, options, args, file_list): |
| 525 | """Display status information.""" |
| 526 | path = os.path.join(self._root_dir, self.relpath) |
| 527 | command = ['status'] |
| 528 | command.extend(args) |
| 529 | if not os.path.isdir(path): |
| 530 | # svn status won't work if the directory doesn't exist. |
| 531 | print("\n________ couldn't run \'%s\' in \'%s\':\nThe directory " |
| 532 | "does not exist." |
| 533 | % (' '.join(command), path)) |
| 534 | # There's no file list to retrieve. |
| 535 | else: |
maruel@chromium.org | 5aeb7dd | 2009-11-17 18:09:01 +0000 | [diff] [blame] | 536 | self.RunAndGetFileList(options, command, path, file_list) |
msb@chromium.org | e6f7835 | 2010-01-13 17:05:33 +0000 | [diff] [blame^] | 537 | |
| 538 | def FullUrlForRelativeUrl(self, url): |
| 539 | # Find the forth '/' and strip from there. A bit hackish. |
| 540 | return '/'.join(self.url.split('/')[:4]) + url |