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