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