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