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