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