[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
[email protected] | ebbf947 | 2009-11-10 20:26:30 | [diff] [blame] | 5 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 6 | """\ |
| 7 | Wrapper script around Rietveld's upload.py that simplifies working with groups |
| 8 | of files. |
| 9 | """ |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 10 | |
| 11 | import getpass |
| 12 | import os |
| 13 | import random |
| 14 | import re |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 15 | import string |
| 16 | import subprocess |
| 17 | import sys |
| 18 | import tempfile |
[email protected] | 2f6a0d8 | 2010-05-12 00:03:30 | [diff] [blame] | 19 | import time |
[email protected] | ba55177 | 2010-02-03 18:21:42 | [diff] [blame] | 20 | from third_party import upload |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 21 | import urllib2 |
| 22 | |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 23 | __pychecker__ = 'unusednames=breakpad' |
[email protected] | ada4c65 | 2009-12-03 15:32:01 | [diff] [blame] | 24 | import breakpad |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 25 | __pychecker__ = '' |
[email protected] | ada4c65 | 2009-12-03 15:32:01 | [diff] [blame] | 26 | |
[email protected] | 46a9410 | 2009-05-12 20:32:43 | [diff] [blame] | 27 | # gcl now depends on gclient. |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 28 | from scm import SVN |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 29 | import gclient_utils |
[email protected] | c1675e2 | 2009-04-27 20:30:48 | [diff] [blame] | 30 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 31 | __version__ = '1.2' |
[email protected] | c1675e2 | 2009-04-27 20:30:48 | [diff] [blame] | 32 | |
| 33 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 34 | CODEREVIEW_SETTINGS = { |
[email protected] | 172b6e7 | 2010-01-26 00:35:03 | [diff] [blame] | 35 | # Ideally, we want to set |CODE_REVIEW_SERVER| to a generic server like |
| 36 | # codereview.appspot.com and remove |CC_LIST| and |VIEW_VC|. In practice, we |
| 37 | # need these settings so developers making changes in directories such as |
| 38 | # Chromium's src/third_party/WebKit will send change lists to the correct |
| 39 | # server. |
| 40 | # |
| 41 | # To make gcl send reviews to a different server, check in a file named |
| 42 | # "codereview.settings" (see |CODEREVIEW_SETTINGS_FILE| below) to your |
| 43 | # project's base directory and add the following line to codereview.settings: |
| 44 | # CODE_REVIEW_SERVER: codereview.yourserver.org |
| 45 | # |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 46 | # Default values. |
[email protected] | 172b6e7 | 2010-01-26 00:35:03 | [diff] [blame] | 47 | "CODE_REVIEW_SERVER": "codereview.chromium.org", |
| 48 | "CC_LIST": "[email protected]", |
| 49 | "VIEW_VC": "http://src.chromium.org/viewvc/chrome?view=rev&revision=", |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 52 | # globals that store the root of the current repository and the directory where |
| 53 | # we store information about changelists. |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 54 | REPOSITORY_ROOT = "" |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 55 | |
| 56 | # Filename where we store repository specific information for gcl. |
| 57 | CODEREVIEW_SETTINGS_FILE = "codereview.settings" |
| 58 | |
| 59 | # Warning message when the change appears to be missing tests. |
| 60 | MISSING_TEST_MSG = "Change contains new or modified methods, but no new tests!" |
| 61 | |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 62 | # Global cache of files cached in GetCacheDir(). |
| 63 | FILES_CACHE = {} |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 64 | |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 65 | # Valid extensions for files we want to lint. |
| 66 | DEFAULT_LINT_REGEX = r"(.*\.cpp|.*\.cc|.*\.h)" |
| 67 | DEFAULT_LINT_IGNORE_REGEX = r"$^" |
| 68 | |
| 69 | |
[email protected] | e529901 | 2010-04-07 18:02:26 | [diff] [blame] | 70 | def CheckHomeForFile(filename): |
| 71 | """Checks the users home dir for the existence of the given file. Returns |
| 72 | the path to the file if it's there, or None if it is not. |
| 73 | """ |
| 74 | home_vars = ['HOME'] |
| 75 | if sys.platform in ('cygwin', 'win32'): |
| 76 | home_vars.append('USERPROFILE') |
| 77 | for home_var in home_vars: |
| 78 | home = os.getenv(home_var) |
| 79 | if home != None: |
| 80 | full_path = os.path.join(home, filename) |
| 81 | if os.path.exists(full_path): |
| 82 | return full_path |
| 83 | return None |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 84 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 85 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 86 | def UnknownFiles(): |
| 87 | """Runs svn status and returns unknown files.""" |
| 88 | return [item[1] for item in SVN.CaptureStatus([]) if item[0][0] == '?'] |
[email protected] | 207fdf3 | 2009-04-28 19:57:01 | [diff] [blame] | 89 | |
| 90 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 91 | def GetRepositoryRoot(): |
| 92 | """Returns the top level directory of the current repository. |
| 93 | |
| 94 | The directory is returned as an absolute path. |
| 95 | """ |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 96 | global REPOSITORY_ROOT |
| 97 | if not REPOSITORY_ROOT: |
[email protected] | 94b1ee9 | 2009-12-19 20:27:20 | [diff] [blame] | 98 | REPOSITORY_ROOT = SVN.GetCheckoutRoot(os.getcwd()) |
| 99 | if not REPOSITORY_ROOT: |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 100 | raise gclient_utils.Error("gcl run outside of repository") |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 101 | return REPOSITORY_ROOT |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def GetInfoDir(): |
| 105 | """Returns the directory where gcl info files are stored.""" |
[email protected] | 374d65e | 2009-05-21 14:00:52 | [diff] [blame] | 106 | return os.path.join(GetRepositoryRoot(), '.svn', 'gcl_info') |
| 107 | |
| 108 | |
| 109 | def GetChangesDir(): |
| 110 | """Returns the directory where gcl change files are stored.""" |
| 111 | return os.path.join(GetInfoDir(), 'changes') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 112 | |
| 113 | |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 114 | def GetCacheDir(): |
| 115 | """Returns the directory where gcl change files are stored.""" |
| 116 | return os.path.join(GetInfoDir(), 'cache') |
| 117 | |
| 118 | |
| 119 | def GetCachedFile(filename, max_age=60*60*24*3, use_root=False): |
| 120 | """Retrieves a file from the repository and caches it in GetCacheDir() for |
| 121 | max_age seconds. |
| 122 | |
| 123 | use_root: If False, look up the arborescence for the first match, otherwise go |
| 124 | directory to the root repository. |
| 125 | |
| 126 | Note: The cache will be inconsistent if the same file is retrieved with both |
[email protected] | bb81638 | 2009-10-29 01:38:02 | [diff] [blame] | 127 | use_root=True and use_root=False. Don't be stupid. |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 128 | """ |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 129 | if filename not in FILES_CACHE: |
| 130 | # Don't try to look up twice. |
| 131 | FILES_CACHE[filename] = None |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 132 | # First we check if we have a cached version. |
[email protected] | a05be0b | 2009-06-30 19:13:02 | [diff] [blame] | 133 | try: |
| 134 | cached_file = os.path.join(GetCacheDir(), filename) |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 135 | except gclient_utils.Error: |
[email protected] | a05be0b | 2009-06-30 19:13:02 | [diff] [blame] | 136 | return None |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 137 | if (not os.path.exists(cached_file) or |
[email protected] | 2f6a0d8 | 2010-05-12 00:03:30 | [diff] [blame] | 138 | (time.time() - os.stat(cached_file).st_mtime) > max_age): |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 139 | dir_info = SVN.CaptureInfo(".") |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 140 | repo_root = dir_info["Repository Root"] |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 141 | if use_root: |
| 142 | url_path = repo_root |
| 143 | else: |
| 144 | url_path = dir_info["URL"] |
| 145 | content = "" |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 146 | while True: |
[email protected] | fa44e4a | 2009-12-03 01:41:13 | [diff] [blame] | 147 | # Look in the repository at the current level for the file. |
| 148 | svn_path = url_path + "/" + filename |
| 149 | content, rc = RunShellWithReturnCode(["svn", "cat", svn_path]) |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 150 | if not rc: |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 151 | # Exit the loop if the file was found. Override content. |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 152 | break |
| 153 | # Make sure to mark settings as empty if not found. |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 154 | content = "" |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 155 | if url_path == repo_root: |
| 156 | # Reached the root. Abandoning search. |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 157 | break |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 158 | # Go up one level to try again. |
| 159 | url_path = os.path.dirname(url_path) |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 160 | # Write a cached version even if there isn't a file, so we don't try to |
| 161 | # fetch it each time. |
[email protected] | fc83c11 | 2009-12-18 15:14:10 | [diff] [blame] | 162 | gclient_utils.FileWrite(cached_file, content) |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 163 | else: |
[email protected] | 0fca4f3 | 2009-12-18 15:14:34 | [diff] [blame] | 164 | content = gclient_utils.FileRead(cached_file, 'r') |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 165 | # Keep the content cached in memory. |
| 166 | FILES_CACHE[filename] = content |
| 167 | return FILES_CACHE[filename] |
[email protected] | 9b61327 | 2009-04-24 01:28:28 | [diff] [blame] | 168 | |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 169 | |
| 170 | def GetCodeReviewSetting(key): |
| 171 | """Returns a value for the given key for this repository.""" |
| 172 | # Use '__just_initialized' as a flag to determine if the settings were |
| 173 | # already initialized. |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 174 | if '__just_initialized' not in CODEREVIEW_SETTINGS: |
[email protected] | b044218 | 2009-06-05 14:20:47 | [diff] [blame] | 175 | settings_file = GetCachedFile(CODEREVIEW_SETTINGS_FILE) |
| 176 | if settings_file: |
| 177 | for line in settings_file.splitlines(): |
[email protected] | 807c446 | 2010-07-10 00:45:28 | [diff] [blame] | 178 | if not line or line.startswith('#'): |
[email protected] | b044218 | 2009-06-05 14:20:47 | [diff] [blame] | 179 | continue |
[email protected] | 807c446 | 2010-07-10 00:45:28 | [diff] [blame] | 180 | if not ':' in line: |
| 181 | raise gclient_utils.Error( |
| 182 | '%s is invalid, please fix. It\'s content:\n\n%s' % |
| 183 | (CODEREVIEW_SETTINGS_FILE, settings_file)) |
| 184 | k, v = line.split(': ', 1) |
[email protected] | b044218 | 2009-06-05 14:20:47 | [diff] [blame] | 185 | CODEREVIEW_SETTINGS[k] = v |
[email protected] | 98fc2b9 | 2009-05-21 14:11:51 | [diff] [blame] | 186 | CODEREVIEW_SETTINGS.setdefault('__just_initialized', None) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 187 | return CODEREVIEW_SETTINGS.get(key, "") |
| 188 | |
| 189 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 190 | def Warn(msg): |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 191 | print >> sys.stderr, msg |
[email protected] | 223b719 | 2010-06-04 18:52:58 | [diff] [blame] | 192 | |
| 193 | |
| 194 | def ErrorExit(msg): |
| 195 | print >> sys.stderr, msg |
| 196 | sys.exit(1) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 197 | |
| 198 | |
| 199 | def RunShellWithReturnCode(command, print_output=False): |
| 200 | """Executes a command and returns the output and the return code.""" |
[email protected] | be0d1ca | 2009-05-12 19:23:02 | [diff] [blame] | 201 | # Use a shell for subcommands on Windows to get a PATH search, and because svn |
| 202 | # may be a batch file. |
| 203 | use_shell = sys.platform.startswith("win") |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 204 | p = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 205 | stderr=subprocess.STDOUT, shell=use_shell, |
| 206 | universal_newlines=True) |
| 207 | if print_output: |
| 208 | output_array = [] |
| 209 | while True: |
| 210 | line = p.stdout.readline() |
| 211 | if not line: |
| 212 | break |
| 213 | if print_output: |
| 214 | print line.strip('\n') |
| 215 | output_array.append(line) |
| 216 | output = "".join(output_array) |
| 217 | else: |
| 218 | output = p.stdout.read() |
| 219 | p.wait() |
| 220 | p.stdout.close() |
| 221 | return output, p.returncode |
| 222 | |
| 223 | |
| 224 | def RunShell(command, print_output=False): |
| 225 | """Executes a command and returns the output.""" |
| 226 | return RunShellWithReturnCode(command, print_output)[0] |
| 227 | |
| 228 | |
[email protected] | 51ee007 | 2009-06-08 19:20:05 | [diff] [blame] | 229 | def FilterFlag(args, flag): |
| 230 | """Returns True if the flag is present in args list. |
| 231 | |
| 232 | The flag is removed from args if present. |
| 233 | """ |
| 234 | if flag in args: |
| 235 | args.remove(flag) |
| 236 | return True |
| 237 | return False |
| 238 | |
| 239 | |
[email protected] | be0d1ca | 2009-05-12 19:23:02 | [diff] [blame] | 240 | class ChangeInfo(object): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 241 | """Holds information about a changelist. |
| 242 | |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 243 | name: change name. |
| 244 | issue: the Rietveld issue number or 0 if it hasn't been uploaded yet. |
| 245 | patchset: the Rietveld latest patchset number or 0. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 246 | description: the description. |
| 247 | files: a list of 2 tuple containing (status, filename) of changed files, |
| 248 | with paths being relative to the top repository directory. |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 249 | local_root: Local root directory |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 250 | """ |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 251 | |
| 252 | _SEPARATOR = "\n-----\n" |
| 253 | # The info files have the following format: |
| 254 | # issue_id, patchset\n (, patchset is optional) |
| 255 | # _SEPARATOR\n |
| 256 | # filepath1\n |
| 257 | # filepath2\n |
| 258 | # . |
| 259 | # . |
| 260 | # filepathn\n |
| 261 | # _SEPARATOR\n |
| 262 | # description |
| 263 | |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 264 | def __init__(self, name, issue, patchset, description, files, local_root, |
| 265 | needs_upload=False): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 266 | self.name = name |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 267 | self.issue = int(issue) |
| 268 | self.patchset = int(patchset) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 269 | self.description = description |
[email protected] | be0d1ca | 2009-05-12 19:23:02 | [diff] [blame] | 270 | if files is None: |
| 271 | files = [] |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 272 | self._files = files |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 273 | self.patch = None |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 274 | self._local_root = local_root |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 275 | self.needs_upload = needs_upload |
| 276 | |
| 277 | def NeedsUpload(self): |
| 278 | return self.needs_upload |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 279 | |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 280 | def GetFileNames(self): |
| 281 | """Returns the list of file names included in this change.""" |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 282 | return [f[1] for f in self._files] |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 283 | |
| 284 | def GetFiles(self): |
| 285 | """Returns the list of files included in this change with their status.""" |
| 286 | return self._files |
| 287 | |
| 288 | def GetLocalRoot(self): |
| 289 | """Returns the local repository checkout root directory.""" |
| 290 | return self._local_root |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 291 | |
[email protected] | f0dfba3 | 2009-08-07 22:03:37 | [diff] [blame] | 292 | def Exists(self): |
| 293 | """Returns True if this change already exists (i.e., is not new).""" |
| 294 | return (self.issue or self.description or self._files) |
| 295 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 296 | def _NonDeletedFileList(self): |
| 297 | """Returns a list of files in this change, not including deleted files.""" |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 298 | return [f[1] for f in self.GetFiles() |
| 299 | if not f[0].startswith("D")] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 300 | |
| 301 | def _AddedFileList(self): |
| 302 | """Returns a list of files added in this change.""" |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 303 | return [f[1] for f in self.GetFiles() if f[0].startswith("A")] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 304 | |
| 305 | def Save(self): |
| 306 | """Writes the changelist information to disk.""" |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 307 | if self.NeedsUpload(): |
| 308 | needs_upload = "dirty" |
| 309 | else: |
| 310 | needs_upload = "clean" |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 311 | data = ChangeInfo._SEPARATOR.join([ |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 312 | "%d, %d, %s" % (self.issue, self.patchset, needs_upload), |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 313 | "\n".join([f[0] + f[1] for f in self.GetFiles()]), |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 314 | self.description]) |
[email protected] | fc83c11 | 2009-12-18 15:14:10 | [diff] [blame] | 315 | gclient_utils.FileWrite(GetChangelistInfoFile(self.name), data) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 316 | |
| 317 | def Delete(self): |
| 318 | """Removes the changelist information from disk.""" |
| 319 | os.remove(GetChangelistInfoFile(self.name)) |
| 320 | |
| 321 | def CloseIssue(self): |
| 322 | """Closes the Rietveld issue for this changelist.""" |
| 323 | data = [("description", self.description),] |
| 324 | ctype, body = upload.EncodeMultipartFormData(data, []) |
[email protected] | 2c8d4b2 | 2009-06-06 21:03:10 | [diff] [blame] | 325 | SendToRietveld("/%d/close" % self.issue, body, ctype) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 326 | |
| 327 | def UpdateRietveldDescription(self): |
| 328 | """Sets the description for an issue on Rietveld.""" |
| 329 | data = [("description", self.description),] |
| 330 | ctype, body = upload.EncodeMultipartFormData(data, []) |
[email protected] | 2c8d4b2 | 2009-06-06 21:03:10 | [diff] [blame] | 331 | SendToRietveld("/%d/description" % self.issue, body, ctype) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 332 | |
| 333 | def MissingTests(self): |
| 334 | """Returns True if the change looks like it needs unit tests but has none. |
| 335 | |
| 336 | A change needs unit tests if it contains any new source files or methods. |
| 337 | """ |
| 338 | SOURCE_SUFFIXES = [".cc", ".cpp", ".c", ".m", ".mm"] |
| 339 | # Ignore third_party entirely. |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 340 | files = [f for f in self._NonDeletedFileList() |
| 341 | if f.find("third_party") == -1] |
| 342 | added_files = [f for f in self._AddedFileList() |
| 343 | if f.find("third_party") == -1] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 344 | |
| 345 | # If the change is entirely in third_party, we're done. |
| 346 | if len(files) == 0: |
| 347 | return False |
| 348 | |
| 349 | # Any new or modified test files? |
| 350 | # A test file's name ends with "test.*" or "tests.*". |
| 351 | test_files = [test for test in files |
| 352 | if os.path.splitext(test)[0].rstrip("s").endswith("test")] |
| 353 | if len(test_files) > 0: |
| 354 | return False |
| 355 | |
| 356 | # Any new source files? |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 357 | source_files = [item for item in added_files |
| 358 | if os.path.splitext(item)[1] in SOURCE_SUFFIXES] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 359 | if len(source_files) > 0: |
| 360 | return True |
| 361 | |
| 362 | # Do the long test, checking the files for new methods. |
| 363 | return self._HasNewMethod() |
| 364 | |
| 365 | def _HasNewMethod(self): |
| 366 | """Returns True if the changeset contains any new functions, or if a |
| 367 | function signature has been changed. |
| 368 | |
| 369 | A function is identified by starting flush left, containing a "(" before |
| 370 | the next flush-left line, and either ending with "{" before the next |
| 371 | flush-left line or being followed by an unindented "{". |
| 372 | |
| 373 | Currently this returns True for new methods, new static functions, and |
| 374 | methods or functions whose signatures have been changed. |
| 375 | |
| 376 | Inline methods added to header files won't be detected by this. That's |
| 377 | acceptable for purposes of determining if a unit test is needed, since |
| 378 | inline methods should be trivial. |
| 379 | """ |
| 380 | # To check for methods added to source or header files, we need the diffs. |
| 381 | # We'll generate them all, since there aren't likely to be many files |
| 382 | # apart from source and headers; besides, we'll want them all if we're |
| 383 | # uploading anyway. |
| 384 | if self.patch is None: |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 385 | self.patch = GenerateDiff(self.GetFileNames()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 386 | |
| 387 | definition = "" |
| 388 | for line in self.patch.splitlines(): |
| 389 | if not line.startswith("+"): |
| 390 | continue |
| 391 | line = line.strip("+").rstrip(" \t") |
| 392 | # Skip empty lines, comments, and preprocessor directives. |
| 393 | # TODO(pamg): Handle multiline comments if it turns out to be a problem. |
| 394 | if line == "" or line.startswith("/") or line.startswith("#"): |
| 395 | continue |
| 396 | |
| 397 | # A possible definition ending with "{" is complete, so check it. |
| 398 | if definition.endswith("{"): |
| 399 | if definition.find("(") != -1: |
| 400 | return True |
| 401 | definition = "" |
| 402 | |
| 403 | # A { or an indented line, when we're in a definition, continues it. |
| 404 | if (definition != "" and |
| 405 | (line == "{" or line.startswith(" ") or line.startswith("\t"))): |
| 406 | definition += line |
| 407 | |
| 408 | # A flush-left line starts a new possible function definition. |
| 409 | elif not line.startswith(" ") and not line.startswith("\t"): |
| 410 | definition = line |
| 411 | |
| 412 | return False |
| 413 | |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 414 | @staticmethod |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 415 | def Load(changename, local_root, fail_on_not_found, update_status): |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 416 | """Gets information about a changelist. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 417 | |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 418 | Args: |
| 419 | fail_on_not_found: if True, this function will quit the program if the |
| 420 | changelist doesn't exist. |
| 421 | update_status: if True, the svn status will be updated for all the files |
| 422 | and unchanged files will be removed. |
| 423 | |
| 424 | Returns: a ChangeInfo object. |
| 425 | """ |
| 426 | info_file = GetChangelistInfoFile(changename) |
| 427 | if not os.path.exists(info_file): |
| 428 | if fail_on_not_found: |
| 429 | ErrorExit("Changelist " + changename + " not found.") |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 430 | return ChangeInfo(changename, 0, 0, '', None, local_root, |
| 431 | needs_upload=False) |
[email protected] | 0fca4f3 | 2009-12-18 15:14:34 | [diff] [blame] | 432 | split_data = gclient_utils.FileRead(info_file, 'r').split( |
| 433 | ChangeInfo._SEPARATOR, 2) |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 434 | if len(split_data) != 3: |
| 435 | ErrorExit("Changelist file %s is corrupt" % info_file) |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 436 | items = split_data[0].split(', ') |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 437 | issue = 0 |
| 438 | patchset = 0 |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 439 | needs_upload = False |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 440 | if items[0]: |
| 441 | issue = int(items[0]) |
| 442 | if len(items) > 1: |
| 443 | patchset = int(items[1]) |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 444 | if len(items) > 2: |
| 445 | needs_upload = (items[2] == "dirty") |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 446 | files = [] |
| 447 | for line in split_data[1].splitlines(): |
| 448 | status = line[:7] |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 449 | filename = line[7:] |
| 450 | files.append((status, filename)) |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 451 | description = split_data[2] |
| 452 | save = False |
| 453 | if update_status: |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 454 | for item in files: |
| 455 | filename = os.path.join(local_root, item[1]) |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 456 | status_result = SVN.CaptureStatus(filename) |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 457 | if not status_result or not status_result[0][0]: |
| 458 | # File has been reverted. |
| 459 | save = True |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 460 | files.remove(item) |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 461 | continue |
| 462 | status = status_result[0][0] |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 463 | if status != item[0]: |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 464 | save = True |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 465 | files[files.index(item)] = (status, item[1]) |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 466 | change_info = ChangeInfo(changename, issue, patchset, description, files, |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 467 | local_root, needs_upload) |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 468 | if save: |
| 469 | change_info.Save() |
| 470 | return change_info |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 471 | |
| 472 | |
| 473 | def GetChangelistInfoFile(changename): |
| 474 | """Returns the file that stores information about a changelist.""" |
| 475 | if not changename or re.search(r'[^\w-]', changename): |
| 476 | ErrorExit("Invalid changelist name: " + changename) |
[email protected] | 374d65e | 2009-05-21 14:00:52 | [diff] [blame] | 477 | return os.path.join(GetChangesDir(), changename) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 478 | |
| 479 | |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 480 | def LoadChangelistInfoForMultiple(changenames, local_root, fail_on_not_found, |
| 481 | update_status): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 482 | """Loads many changes and merge their files list into one pseudo change. |
| 483 | |
| 484 | This is mainly usefull to concatenate many changes into one for a 'gcl try'. |
| 485 | """ |
| 486 | changes = changenames.split(',') |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 487 | aggregate_change_info = ChangeInfo(changenames, 0, 0, '', None, local_root, |
| 488 | needs_upload=False) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 489 | for change in changes: |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 490 | aggregate_change_info._files += ChangeInfo.Load(change, |
| 491 | local_root, |
| 492 | fail_on_not_found, |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 493 | update_status).GetFiles() |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 494 | return aggregate_change_info |
| 495 | |
| 496 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 497 | def GetCLs(): |
| 498 | """Returns a list of all the changelists in this repository.""" |
[email protected] | 374d65e | 2009-05-21 14:00:52 | [diff] [blame] | 499 | cls = os.listdir(GetChangesDir()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 500 | if CODEREVIEW_SETTINGS_FILE in cls: |
| 501 | cls.remove(CODEREVIEW_SETTINGS_FILE) |
| 502 | return cls |
| 503 | |
| 504 | |
| 505 | def GenerateChangeName(): |
| 506 | """Generate a random changelist name.""" |
| 507 | random.seed() |
| 508 | current_cl_names = GetCLs() |
| 509 | while True: |
| 510 | cl_name = (random.choice(string.ascii_lowercase) + |
| 511 | random.choice(string.digits) + |
| 512 | random.choice(string.ascii_lowercase) + |
| 513 | random.choice(string.digits)) |
| 514 | if cl_name not in current_cl_names: |
| 515 | return cl_name |
| 516 | |
| 517 | |
| 518 | def GetModifiedFiles(): |
| 519 | """Returns a set that maps from changelist name to (status,filename) tuples. |
| 520 | |
| 521 | Files not in a changelist have an empty changelist name. Filenames are in |
| 522 | relation to the top level directory of the current repository. Note that |
| 523 | only the current directory and subdirectories are scanned, in order to |
| 524 | improve performance while still being flexible. |
| 525 | """ |
| 526 | files = {} |
| 527 | |
| 528 | # Since the files are normalized to the root folder of the repositary, figure |
| 529 | # out what we need to add to the paths. |
| 530 | dir_prefix = os.getcwd()[len(GetRepositoryRoot()):].strip(os.sep) |
| 531 | |
| 532 | # Get a list of all files in changelists. |
| 533 | files_in_cl = {} |
| 534 | for cl in GetCLs(): |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 535 | change_info = ChangeInfo.Load(cl, GetRepositoryRoot(), |
| 536 | fail_on_not_found=True, update_status=False) |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 537 | for status, filename in change_info.GetFiles(): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 538 | files_in_cl[filename] = change_info.name |
| 539 | |
| 540 | # Get all the modified files. |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 541 | status_result = SVN.CaptureStatus(None) |
[email protected] | 207fdf3 | 2009-04-28 19:57:01 | [diff] [blame] | 542 | for line in status_result: |
| 543 | status = line[0] |
| 544 | filename = line[1] |
| 545 | if status[0] == "?": |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 546 | continue |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 547 | if dir_prefix: |
| 548 | filename = os.path.join(dir_prefix, filename) |
| 549 | change_list_name = "" |
| 550 | if filename in files_in_cl: |
| 551 | change_list_name = files_in_cl[filename] |
| 552 | files.setdefault(change_list_name, []).append((status, filename)) |
| 553 | |
| 554 | return files |
| 555 | |
| 556 | |
| 557 | def GetFilesNotInCL(): |
| 558 | """Returns a list of tuples (status,filename) that aren't in any changelists. |
| 559 | |
| 560 | See docstring of GetModifiedFiles for information about path of files and |
| 561 | which directories are scanned. |
| 562 | """ |
| 563 | modified_files = GetModifiedFiles() |
| 564 | if "" not in modified_files: |
| 565 | return [] |
| 566 | return modified_files[""] |
| 567 | |
| 568 | |
| 569 | def SendToRietveld(request_path, payload=None, |
| 570 | content_type="application/octet-stream", timeout=None): |
| 571 | """Send a POST/GET to Rietveld. Returns the response body.""" |
[email protected] | 3884d76 | 2009-11-25 00:01:22 | [diff] [blame] | 572 | server = GetCodeReviewSetting("CODE_REVIEW_SERVER") |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 573 | def GetUserCredentials(): |
| 574 | """Prompts the user for a username and password.""" |
[email protected] | 3884d76 | 2009-11-25 00:01:22 | [diff] [blame] | 575 | email = upload.GetEmail("Email (login for uploading to %s)" % server) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 576 | password = getpass.getpass("Password for %s: " % email) |
| 577 | return email, password |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 578 | rpc_server = upload.HttpRpcServer(server, |
| 579 | GetUserCredentials, |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 580 | save_cookies=True) |
| 581 | try: |
| 582 | return rpc_server.Send(request_path, payload, content_type, timeout) |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 583 | except urllib2.URLError: |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 584 | if timeout is None: |
| 585 | ErrorExit("Error accessing url %s" % request_path) |
| 586 | else: |
| 587 | return None |
| 588 | |
| 589 | |
| 590 | def GetIssueDescription(issue): |
| 591 | """Returns the issue description from Rietveld.""" |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 592 | return SendToRietveld("/%d/description" % issue) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 593 | |
| 594 | |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 595 | def ListFiles(show_unknown_files): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 596 | files = GetModifiedFiles() |
| 597 | cl_keys = files.keys() |
| 598 | cl_keys.sort() |
| 599 | for cl_name in cl_keys: |
[email protected] | 88c32d8 | 2009-10-12 18:24:05 | [diff] [blame] | 600 | if not cl_name: |
| 601 | continue |
| 602 | note = "" |
| 603 | change_info = ChangeInfo.Load(cl_name, GetRepositoryRoot(), |
| 604 | fail_on_not_found=True, update_status=False) |
| 605 | if len(change_info.GetFiles()) != len(files[cl_name]): |
| 606 | note = " (Note: this changelist contains files outside this directory)" |
| 607 | print "\n--- Changelist " + cl_name + note + ":" |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 608 | for filename in files[cl_name]: |
| 609 | print "".join(filename) |
[email protected] | 88c32d8 | 2009-10-12 18:24:05 | [diff] [blame] | 610 | if show_unknown_files: |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 611 | unknown_files = UnknownFiles() |
[email protected] | 88c32d8 | 2009-10-12 18:24:05 | [diff] [blame] | 612 | if (files.get('') or (show_unknown_files and len(unknown_files))): |
| 613 | print "\n--- Not in any changelist:" |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 614 | for item in files.get('', []): |
| 615 | print "".join(item) |
[email protected] | 88c32d8 | 2009-10-12 18:24:05 | [diff] [blame] | 616 | if show_unknown_files: |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 617 | for filename in unknown_files: |
| 618 | print "? %s" % filename |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 619 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 620 | |
| 621 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 622 | def GetEditor(): |
| 623 | editor = os.environ.get("SVN_EDITOR") |
| 624 | if not editor: |
| 625 | editor = os.environ.get("EDITOR") |
| 626 | |
| 627 | if not editor: |
| 628 | if sys.platform.startswith("win"): |
| 629 | editor = "notepad" |
| 630 | else: |
| 631 | editor = "vi" |
| 632 | |
| 633 | return editor |
| 634 | |
| 635 | |
| 636 | def GenerateDiff(files, root=None): |
[email protected] | f2f9d55 | 2009-12-22 00:12:57 | [diff] [blame] | 637 | return SVN.GenerateDiff(files, root=root) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 638 | |
[email protected] | 51ee007 | 2009-06-08 19:20:05 | [diff] [blame] | 639 | |
| 640 | def OptionallyDoPresubmitChecks(change_info, committing, args): |
| 641 | if FilterFlag(args, "--no_presubmit") or FilterFlag(args, "--force"): |
| 642 | return True |
[email protected] | b0dfd35 | 2009-06-10 14:12:54 | [diff] [blame] | 643 | return DoPresubmitChecks(change_info, committing, True) |
[email protected] | 51ee007 | 2009-06-08 19:20:05 | [diff] [blame] | 644 | |
| 645 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 646 | def defer_attributes(a, b): |
| 647 | """Copy attributes from an object (like a function) to another.""" |
| 648 | for x in dir(a): |
| 649 | if not getattr(b, x, None): |
| 650 | setattr(b, x, getattr(a, x)) |
| 651 | |
| 652 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 653 | def need_change(function): |
| 654 | """Converts args -> change_info.""" |
| 655 | def hook(args): |
| 656 | if not len(args) == 1: |
| 657 | ErrorExit("You need to pass a change list name") |
| 658 | change_info = ChangeInfo.Load(args[0], GetRepositoryRoot(), True, True) |
| 659 | return function(change_info) |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 660 | defer_attributes(function, hook) |
| 661 | hook.need_change = True |
| 662 | hook.no_args = True |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 663 | return hook |
| 664 | |
| 665 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 666 | def need_change_and_args(function): |
| 667 | """Converts args -> change_info.""" |
| 668 | def hook(args): |
[email protected] | e56fe82 | 2010-05-28 20:36:57 | [diff] [blame] | 669 | if not args: |
| 670 | ErrorExit("You need to pass a change list name") |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 671 | change_info = ChangeInfo.Load(args.pop(0), GetRepositoryRoot(), True, True) |
| 672 | return function(change_info, args) |
| 673 | defer_attributes(function, hook) |
| 674 | hook.need_change = True |
| 675 | return hook |
| 676 | |
| 677 | |
| 678 | def no_args(function): |
| 679 | """Make sure no args are passed.""" |
| 680 | def hook(args): |
| 681 | if args: |
| 682 | ErrorExit("Doesn't support arguments") |
| 683 | return function() |
| 684 | defer_attributes(function, hook) |
| 685 | hook.no_args = True |
| 686 | return hook |
| 687 | |
| 688 | |
| 689 | def attrs(**kwargs): |
| 690 | """Decorate a function with new attributes.""" |
| 691 | def decorate(function): |
| 692 | for k in kwargs: |
| 693 | setattr(function, k, kwargs[k]) |
| 694 | return function |
| 695 | return decorate |
| 696 | |
| 697 | |
| 698 | @no_args |
| 699 | def CMDopened(): |
| 700 | """Lists modified files in the current directory down.""" |
| 701 | return ListFiles(False) |
| 702 | |
| 703 | |
| 704 | @no_args |
| 705 | def CMDstatus(): |
| 706 | """Lists modified and unknown files in the current directory down.""" |
| 707 | return ListFiles(True) |
| 708 | |
| 709 | |
| 710 | @need_change_and_args |
[email protected] | 5db5ba5 | 2010-07-20 15:50:47 | [diff] [blame^] | 711 | @attrs(usage='[--no_presubmit] [--clobber] [--no_watchlists]') |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 712 | def CMDupload(change_info, args): |
| 713 | """Uploads the changelist to the server for review. |
| 714 | |
[email protected] | 5db5ba5 | 2010-07-20 15:50:47 | [diff] [blame^] | 715 | This does not submit a try job; use gcl try to submit a try job. |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 716 | """ |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 717 | if not change_info.GetFiles(): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 718 | print "Nothing to upload, changelist is empty." |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 719 | return 0 |
[email protected] | 51ee007 | 2009-06-08 19:20:05 | [diff] [blame] | 720 | if not OptionallyDoPresubmitChecks(change_info, False, args): |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 721 | return 1 |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 722 | no_watchlists = (FilterFlag(args, "--no_watchlists") or |
| 723 | FilterFlag(args, "--no-watchlists")) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 724 | |
| 725 | # Map --send-mail to --send_mail |
[email protected] | 51ee007 | 2009-06-08 19:20:05 | [diff] [blame] | 726 | if FilterFlag(args, "--send-mail"): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 727 | args.append("--send_mail") |
| 728 | |
| 729 | # Supports --clobber for the try server. |
[email protected] | 51ee007 | 2009-06-08 19:20:05 | [diff] [blame] | 730 | clobber = FilterFlag(args, "--clobber") |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 731 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 732 | upload_arg = ["upload.py", "-y"] |
| 733 | upload_arg.append("--server=" + GetCodeReviewSetting("CODE_REVIEW_SERVER")) |
| 734 | upload_arg.extend(args) |
| 735 | |
| 736 | desc_file = "" |
| 737 | if change_info.issue: # Uploading a new patchset. |
| 738 | found_message = False |
| 739 | for arg in args: |
| 740 | if arg.startswith("--message") or arg.startswith("-m"): |
| 741 | found_message = True |
| 742 | break |
| 743 | |
| 744 | if not found_message: |
| 745 | upload_arg.append("--message=''") |
| 746 | |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 747 | upload_arg.append("--issue=%d" % change_info.issue) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 748 | else: # First time we upload. |
| 749 | handle, desc_file = tempfile.mkstemp(text=True) |
| 750 | os.write(handle, change_info.description) |
| 751 | os.close(handle) |
| 752 | |
[email protected] | b2ab494 | 2009-06-11 21:39:19 | [diff] [blame] | 753 | # Watchlist processing -- CC people interested in this changeset |
| 754 | # http://dev.chromium.org/developers/contributing-code/watchlists |
| 755 | if not no_watchlists: |
| 756 | import watchlists |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 757 | watchlist = watchlists.Watchlists(change_info.GetLocalRoot()) |
[email protected] | 07f0186 | 2009-06-12 16:51:08 | [diff] [blame] | 758 | watchers = watchlist.GetWatchersForPaths(change_info.GetFileNames()) |
[email protected] | b2ab494 | 2009-06-11 21:39:19 | [diff] [blame] | 759 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 760 | cc_list = GetCodeReviewSetting("CC_LIST") |
[email protected] | b2ab494 | 2009-06-11 21:39:19 | [diff] [blame] | 761 | if not no_watchlists and watchers: |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 762 | # Filter out all empty elements and join by ',' |
| 763 | cc_list = ','.join(filter(None, [cc_list] + watchers)) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 764 | if cc_list: |
| 765 | upload_arg.append("--cc=" + cc_list) |
| 766 | upload_arg.append("--description_file=" + desc_file + "") |
| 767 | if change_info.description: |
| 768 | subject = change_info.description[:77] |
| 769 | if subject.find("\r\n") != -1: |
| 770 | subject = subject[:subject.find("\r\n")] |
| 771 | if subject.find("\n") != -1: |
| 772 | subject = subject[:subject.find("\n")] |
| 773 | if len(change_info.description) > 77: |
| 774 | subject = subject + "..." |
| 775 | upload_arg.append("--message=" + subject) |
| 776 | |
[email protected] | 83b6e4b | 2010-03-09 03:16:14 | [diff] [blame] | 777 | if GetCodeReviewSetting("PRIVATE") == "True": |
| 778 | upload_arg.append("--private") |
| 779 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 780 | # Change the current working directory before calling upload.py so that it |
| 781 | # shows the correct base. |
| 782 | previous_cwd = os.getcwd() |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 783 | os.chdir(change_info.GetLocalRoot()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 784 | |
| 785 | # If we have a lot of files with long paths, then we won't be able to fit |
| 786 | # the command to "svn diff". Instead, we generate the diff manually for |
| 787 | # each file and concatenate them before passing it to upload.py. |
| 788 | if change_info.patch is None: |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 789 | change_info.patch = GenerateDiff(change_info.GetFileNames()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 790 | issue, patchset = upload.RealMain(upload_arg, change_info.patch) |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 791 | if issue and patchset: |
| 792 | change_info.issue = int(issue) |
| 793 | change_info.patchset = int(patchset) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 794 | change_info.Save() |
| 795 | |
| 796 | if desc_file: |
| 797 | os.remove(desc_file) |
| 798 | |
| 799 | # Do background work on Rietveld to lint the file so that the results are |
| 800 | # ready when the issue is viewed. |
| 801 | SendToRietveld("/lint/issue%s_%s" % (issue, patchset), timeout=0.5) |
| 802 | |
[email protected] | 57e7855 | 2009-09-11 23:04:30 | [diff] [blame] | 803 | # Move back before considering try, so GetCodeReviewSettings is |
| 804 | # consistent. |
| 805 | os.chdir(previous_cwd) |
| 806 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 807 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 808 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 809 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 810 | @need_change |
| 811 | def CMDpresubmit(change_info): |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 812 | """Runs presubmit checks on the change. |
| 813 | |
| 814 | The actual presubmit code is implemented in presubmit_support.py and looks |
| 815 | for PRESUBMIT.py files.""" |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 816 | if not change_info.GetFiles(): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 817 | print "Nothing to presubmit check, changelist is empty." |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 818 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 819 | |
| 820 | print "*** Presubmit checks for UPLOAD would report: ***" |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 821 | result = DoPresubmitChecks(change_info, False, False) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 822 | |
[email protected] | b0dfd35 | 2009-06-10 14:12:54 | [diff] [blame] | 823 | print "\n*** Presubmit checks for COMMIT would report: ***" |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 824 | result &= DoPresubmitChecks(change_info, True, False) |
| 825 | return not result |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 826 | |
| 827 | |
| 828 | def TryChange(change_info, args, swallow_exception): |
| 829 | """Create a diff file of change_info and send it to the try server.""" |
| 830 | try: |
| 831 | import trychange |
| 832 | except ImportError: |
| 833 | if swallow_exception: |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 834 | return 1 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 835 | ErrorExit("You need to install trychange.py to use the try server.") |
| 836 | |
[email protected] | 1811135 | 2009-12-20 17:21:28 | [diff] [blame] | 837 | trychange_args = [] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 838 | if change_info: |
[email protected] | 1811135 | 2009-12-20 17:21:28 | [diff] [blame] | 839 | trychange_args.extend(['--name', change_info.name]) |
[email protected] | 32ba260 | 2009-06-06 18:44:48 | [diff] [blame] | 840 | if change_info.issue: |
| 841 | trychange_args.extend(["--issue", str(change_info.issue)]) |
| 842 | if change_info.patchset: |
| 843 | trychange_args.extend(["--patchset", str(change_info.patchset)]) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 844 | trychange_args.extend(args) |
[email protected] | 1227c7d | 2009-12-22 00:54:27 | [diff] [blame] | 845 | file_list = change_info.GetFileNames() |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 846 | else: |
[email protected] | 1811135 | 2009-12-20 17:21:28 | [diff] [blame] | 847 | trychange_args.extend(args) |
[email protected] | 1227c7d | 2009-12-22 00:54:27 | [diff] [blame] | 848 | file_list = None |
[email protected] | d089192 | 2010-05-31 18:33:16 | [diff] [blame] | 849 | return trychange.TryChange( |
| 850 | trychange_args, |
| 851 | file_list=file_list, |
| 852 | swallow_exception=swallow_exception, |
| 853 | prog='gcl try', |
| 854 | extra_epilog='\n' |
| 855 | 'When called from gcl, use the format gcl try <change_name>.\n') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 856 | |
| 857 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 858 | @need_change_and_args |
| 859 | @attrs(usage='[--no_presubmit]') |
[email protected] | 4357af2 | 2010-05-27 15:42:34 | [diff] [blame] | 860 | def CMDcommit(change_info, args): |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 861 | """Commits the changelist to the repository.""" |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 862 | if not change_info.GetFiles(): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 863 | print "Nothing to commit, changelist is empty." |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 864 | return 1 |
[email protected] | 51ee007 | 2009-06-08 19:20:05 | [diff] [blame] | 865 | if not OptionallyDoPresubmitChecks(change_info, True, args): |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 866 | return 1 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 867 | |
[email protected] | 1bb04aa | 2009-06-01 17:52:11 | [diff] [blame] | 868 | # We face a problem with svn here: Let's say change 'bleh' modifies |
| 869 | # svn:ignore on dir1\. but another unrelated change 'pouet' modifies |
| 870 | # dir1\foo.cc. When the user `gcl commit bleh`, foo.cc is *also committed*. |
| 871 | # The only fix is to use --non-recursive but that has its issues too: |
| 872 | # Let's say if dir1 is deleted, --non-recursive must *not* be used otherwise |
| 873 | # you'll get "svn: Cannot non-recursively commit a directory deletion of a |
| 874 | # directory with child nodes". Yay... |
| 875 | commit_cmd = ["svn", "commit"] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 876 | if change_info.issue: |
| 877 | # Get the latest description from Rietveld. |
| 878 | change_info.description = GetIssueDescription(change_info.issue) |
| 879 | |
| 880 | commit_message = change_info.description.replace('\r\n', '\n') |
| 881 | if change_info.issue: |
[email protected] | fcff927 | 2010-04-29 23:56:19 | [diff] [blame] | 882 | server = GetCodeReviewSetting("CODE_REVIEW_SERVER") |
| 883 | if not server.startswith("http://") and not server.startswith("https://"): |
| 884 | server = "http://" + server |
| 885 | commit_message += ('\nReview URL: %s/%d' % (server, change_info.issue)) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 886 | |
| 887 | handle, commit_filename = tempfile.mkstemp(text=True) |
| 888 | os.write(handle, commit_message) |
| 889 | os.close(handle) |
| 890 | |
| 891 | handle, targets_filename = tempfile.mkstemp(text=True) |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 892 | os.write(handle, "\n".join(change_info.GetFileNames())) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 893 | os.close(handle) |
| 894 | |
| 895 | commit_cmd += ['--file=' + commit_filename] |
| 896 | commit_cmd += ['--targets=' + targets_filename] |
| 897 | # Change the current working directory before calling commit. |
| 898 | previous_cwd = os.getcwd() |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 899 | os.chdir(change_info.GetLocalRoot()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 900 | output = RunShell(commit_cmd, True) |
| 901 | os.remove(commit_filename) |
| 902 | os.remove(targets_filename) |
| 903 | if output.find("Committed revision") != -1: |
| 904 | change_info.Delete() |
| 905 | |
| 906 | if change_info.issue: |
| 907 | revision = re.compile(".*?\nCommitted revision (\d+)", |
| 908 | re.DOTALL).match(output).group(1) |
| 909 | viewvc_url = GetCodeReviewSetting("VIEW_VC") |
| 910 | change_info.description = change_info.description + '\n' |
| 911 | if viewvc_url: |
| 912 | change_info.description += "\nCommitted: " + viewvc_url + revision |
| 913 | change_info.CloseIssue() |
| 914 | os.chdir(previous_cwd) |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 915 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 916 | |
[email protected] | 2c8d4b2 | 2009-06-06 21:03:10 | [diff] [blame] | 917 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 918 | def CMDchange(args): |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 919 | """Creates or edits a changelist. |
| 920 | |
| 921 | Only scans the current directory and subdirectories.""" |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 922 | if len(args) == 0: |
| 923 | # Generate a random changelist name. |
| 924 | changename = GenerateChangeName() |
| 925 | elif args[0] == '--force': |
| 926 | changename = GenerateChangeName() |
| 927 | else: |
| 928 | changename = args[0] |
| 929 | change_info = ChangeInfo.Load(changename, GetRepositoryRoot(), False, True) |
[email protected] | 9ce9822 | 2009-10-19 20:24:17 | [diff] [blame] | 930 | silent = FilterFlag(args, "--silent") |
[email protected] | d36b3ed | 2009-11-09 18:51:42 | [diff] [blame] | 931 | |
| 932 | # Verify the user is running the change command from a read-write checkout. |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 933 | svn_info = SVN.CaptureInfo('.') |
[email protected] | d36b3ed | 2009-11-09 18:51:42 | [diff] [blame] | 934 | if not svn_info: |
| 935 | ErrorExit("Current checkout is unversioned. Please retry with a versioned " |
| 936 | "directory.") |
[email protected] | d36b3ed | 2009-11-09 18:51:42 | [diff] [blame] | 937 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 938 | if len(args) == 2: |
| 939 | f = open(args[1], 'rU') |
[email protected] | 9ce9822 | 2009-10-19 20:24:17 | [diff] [blame] | 940 | override_description = f.read() |
| 941 | f.close() |
| 942 | else: |
| 943 | override_description = None |
[email protected] | 5aeb7dd | 2009-11-17 18:09:01 | [diff] [blame] | 944 | |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 945 | if change_info.issue and not change_info.NeedsUpload(): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 946 | try: |
| 947 | description = GetIssueDescription(change_info.issue) |
| 948 | except urllib2.HTTPError, err: |
| 949 | if err.code == 404: |
| 950 | # The user deleted the issue in Rietveld, so forget the old issue id. |
| 951 | description = change_info.description |
[email protected] | 2c8d4b2 | 2009-06-06 21:03:10 | [diff] [blame] | 952 | change_info.issue = 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 953 | change_info.Save() |
| 954 | else: |
| 955 | ErrorExit("Error getting the description from Rietveld: " + err) |
| 956 | else: |
[email protected] | 85532fc | 2009-06-04 22:36:53 | [diff] [blame] | 957 | if override_description: |
| 958 | description = override_description |
| 959 | else: |
| 960 | description = change_info.description |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 961 | |
| 962 | other_files = GetFilesNotInCL() |
[email protected] | bfd09ce | 2009-08-05 21:17:23 | [diff] [blame] | 963 | |
[email protected] | f0dfba3 | 2009-08-07 22:03:37 | [diff] [blame] | 964 | # Edited files (as opposed to files with only changed properties) will have |
| 965 | # a letter for the first character in the status string. |
[email protected] | 85532fc | 2009-06-04 22:36:53 | [diff] [blame] | 966 | file_re = re.compile(r"^[a-z].+\Z", re.IGNORECASE) |
[email protected] | f0dfba3 | 2009-08-07 22:03:37 | [diff] [blame] | 967 | affected_files = [x for x in other_files if file_re.match(x[0])] |
| 968 | unaffected_files = [x for x in other_files if not file_re.match(x[0])] |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 969 | |
| 970 | separator1 = ("\n---All lines above this line become the description.\n" |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 971 | "---Repository Root: " + change_info.GetLocalRoot() + "\n" |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 972 | "---Paths in this changelist (" + change_info.name + "):\n") |
| 973 | separator2 = "\n\n---Paths modified but not in any changelist:\n\n" |
| 974 | text = (description + separator1 + '\n' + |
[email protected] | f0dfba3 | 2009-08-07 22:03:37 | [diff] [blame] | 975 | '\n'.join([f[0] + f[1] for f in change_info.GetFiles()])) |
| 976 | |
| 977 | if change_info.Exists(): |
| 978 | text += (separator2 + |
| 979 | '\n'.join([f[0] + f[1] for f in affected_files]) + '\n') |
| 980 | else: |
| 981 | text += ('\n'.join([f[0] + f[1] for f in affected_files]) + '\n' + |
| 982 | separator2) |
| 983 | text += '\n'.join([f[0] + f[1] for f in unaffected_files]) + '\n' |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 984 | |
| 985 | handle, filename = tempfile.mkstemp(text=True) |
| 986 | os.write(handle, text) |
| 987 | os.close(handle) |
| 988 | |
[email protected] | 9ce9822 | 2009-10-19 20:24:17 | [diff] [blame] | 989 | if not silent: |
| 990 | os.system(GetEditor() + " " + filename) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 991 | |
[email protected] | 0fca4f3 | 2009-12-18 15:14:34 | [diff] [blame] | 992 | result = gclient_utils.FileRead(filename, 'r') |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 993 | os.remove(filename) |
| 994 | |
| 995 | if not result: |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 996 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 997 | |
| 998 | split_result = result.split(separator1, 1) |
| 999 | if len(split_result) != 2: |
| 1000 | ErrorExit("Don't modify the text starting with ---!\n\n" + result) |
| 1001 | |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 1002 | # Update the CL description if it has changed. |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1003 | new_description = split_result[0] |
| 1004 | cl_files_text = split_result[1] |
[email protected] | 85532fc | 2009-06-04 22:36:53 | [diff] [blame] | 1005 | if new_description != description or override_description: |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1006 | change_info.description = new_description |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 1007 | change_info.needs_upload = True |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1008 | |
| 1009 | new_cl_files = [] |
| 1010 | for line in cl_files_text.splitlines(): |
| 1011 | if not len(line): |
| 1012 | continue |
| 1013 | if line.startswith("---"): |
| 1014 | break |
| 1015 | status = line[:7] |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 1016 | filename = line[7:] |
| 1017 | new_cl_files.append((status, filename)) |
[email protected] | bfd09ce | 2009-08-05 21:17:23 | [diff] [blame] | 1018 | |
| 1019 | if (not len(change_info._files)) and (not change_info.issue) and \ |
| 1020 | (not len(new_description) and (not new_cl_files)): |
| 1021 | ErrorExit("Empty changelist not saved") |
| 1022 | |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 1023 | change_info._files = new_cl_files |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1024 | change_info.Save() |
[email protected] | 53bcf15 | 2009-11-13 21:04:10 | [diff] [blame] | 1025 | if svn_info.get('URL', '').startswith('http:'): |
| 1026 | Warn("WARNING: Creating CL in a read-only checkout. You will not be " |
| 1027 | "able to commit it!") |
| 1028 | |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1029 | print change_info.name + " changelist saved." |
| 1030 | if change_info.MissingTests(): |
| 1031 | Warn("WARNING: " + MISSING_TEST_MSG) |
| 1032 | |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 1033 | # Update the Rietveld issue. |
| 1034 | if change_info.issue and change_info.NeedsUpload(): |
| 1035 | change_info.UpdateRietveldDescription() |
| 1036 | change_info.needs_upload = False |
| 1037 | change_info.Save() |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1038 | return 0 |
[email protected] | ea452b3 | 2009-11-22 20:04:31 | [diff] [blame] | 1039 | |
| 1040 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1041 | @need_change_and_args |
| 1042 | def CMDlint(change_info, args): |
| 1043 | """Runs cpplint.py on all the files in the change list. |
| 1044 | |
| 1045 | Checks all the files in the changelist for possible style violations. |
| 1046 | """ |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1047 | try: |
| 1048 | import cpplint |
| 1049 | except ImportError: |
| 1050 | ErrorExit("You need to install cpplint.py to lint C++ files.") |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1051 | # Change the current working directory before calling lint so that it |
| 1052 | # shows the correct base. |
| 1053 | previous_cwd = os.getcwd() |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 1054 | os.chdir(change_info.GetLocalRoot()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1055 | |
| 1056 | # Process cpplints arguments if any. |
[email protected] | 17f59f2 | 2009-06-12 13:27:24 | [diff] [blame] | 1057 | filenames = cpplint.ParseArguments(args + change_info.GetFileNames()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1058 | |
[email protected] | bb81638 | 2009-10-29 01:38:02 | [diff] [blame] | 1059 | white_list = GetCodeReviewSetting("LINT_REGEX") |
| 1060 | if not white_list: |
[email protected] | e72bb63 | 2009-10-29 20:15:48 | [diff] [blame] | 1061 | white_list = DEFAULT_LINT_REGEX |
[email protected] | bb81638 | 2009-10-29 01:38:02 | [diff] [blame] | 1062 | white_regex = re.compile(white_list) |
| 1063 | black_list = GetCodeReviewSetting("LINT_IGNORE_REGEX") |
| 1064 | if not black_list: |
[email protected] | e72bb63 | 2009-10-29 20:15:48 | [diff] [blame] | 1065 | black_list = DEFAULT_LINT_IGNORE_REGEX |
[email protected] | bb81638 | 2009-10-29 01:38:02 | [diff] [blame] | 1066 | black_regex = re.compile(black_list) |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 1067 | for filename in filenames: |
| 1068 | if white_regex.match(filename): |
| 1069 | if black_regex.match(filename): |
| 1070 | print "Ignoring file %s" % filename |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1071 | else: |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 1072 | cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level) |
[email protected] | bb81638 | 2009-10-29 01:38:02 | [diff] [blame] | 1073 | else: |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 1074 | print "Skipping file %s" % filename |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1075 | |
| 1076 | print "Total errors found: %d\n" % cpplint._cpplint_state.error_count |
| 1077 | os.chdir(previous_cwd) |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1078 | return 1 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1079 | |
| 1080 | |
[email protected] | b0dfd35 | 2009-06-10 14:12:54 | [diff] [blame] | 1081 | def DoPresubmitChecks(change_info, committing, may_prompt): |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1082 | """Imports presubmit, then calls presubmit.DoPresubmitChecks.""" |
| 1083 | # Need to import here to avoid circular dependency. |
[email protected] | 1033acc | 2009-05-13 14:36:48 | [diff] [blame] | 1084 | import presubmit_support |
[email protected] | 0ff1fab | 2009-05-22 13:08:15 | [diff] [blame] | 1085 | root_presubmit = GetCachedFile('PRESUBMIT.py', use_root=True) |
[email protected] | 2e50180 | 2009-06-12 22:00:41 | [diff] [blame] | 1086 | change = presubmit_support.SvnChange(change_info.name, |
| 1087 | change_info.description, |
| 1088 | change_info.GetLocalRoot(), |
| 1089 | change_info.GetFiles(), |
| 1090 | change_info.issue, |
| 1091 | change_info.patchset) |
| 1092 | result = presubmit_support.DoPresubmitChecks(change=change, |
[email protected] | b0dfd35 | 2009-06-10 14:12:54 | [diff] [blame] | 1093 | committing=committing, |
[email protected] | 1033acc | 2009-05-13 14:36:48 | [diff] [blame] | 1094 | verbose=False, |
| 1095 | output_stream=sys.stdout, |
[email protected] | 0ff1fab | 2009-05-22 13:08:15 | [diff] [blame] | 1096 | input_stream=sys.stdin, |
[email protected] | b0dfd35 | 2009-06-10 14:12:54 | [diff] [blame] | 1097 | default_presubmit=root_presubmit, |
| 1098 | may_prompt=may_prompt) |
[email protected] | 21b893b | 2009-06-10 18:56:55 | [diff] [blame] | 1099 | if not result and may_prompt: |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1100 | print "\nPresubmit errors, can't continue (use --no_presubmit to bypass)" |
| 1101 | return result |
| 1102 | |
| 1103 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1104 | @no_args |
| 1105 | def CMDchanges(): |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1106 | """Lists all the changelists and their files.""" |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1107 | for cl in GetCLs(): |
[email protected] | 8d5c9a5 | 2009-06-12 15:59:08 | [diff] [blame] | 1108 | change_info = ChangeInfo.Load(cl, GetRepositoryRoot(), True, True) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1109 | print "\n--- Changelist " + change_info.name + ":" |
[email protected] | e3608df | 2009-11-10 20:22:57 | [diff] [blame] | 1110 | for filename in change_info.GetFiles(): |
| 1111 | print "".join(filename) |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1112 | return 0 |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1113 | |
| 1114 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1115 | @no_args |
| 1116 | def CMDdeleteempties(): |
[email protected] | bfd09ce | 2009-08-05 21:17:23 | [diff] [blame] | 1117 | """Delete all changelists that have no files.""" |
| 1118 | print "\n--- Deleting:" |
| 1119 | for cl in GetCLs(): |
| 1120 | change_info = ChangeInfo.Load(cl, GetRepositoryRoot(), True, True) |
| 1121 | if not len(change_info._files): |
| 1122 | print change_info.name |
| 1123 | change_info.Delete() |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1124 | return 0 |
| 1125 | |
| 1126 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1127 | @no_args |
| 1128 | def CMDnothave(): |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1129 | """Lists files unknown to Subversion.""" |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1130 | for filename in UnknownFiles(): |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1131 | print "? " + "".join(filename) |
| 1132 | return 0 |
| 1133 | |
| 1134 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1135 | @attrs(usage='<svn options>') |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1136 | def CMDdiff(args): |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1137 | """Diffs all files in the changelist or all files that aren't in a CL.""" |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1138 | files = None |
| 1139 | if args: |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1140 | change_info = ChangeInfo.Load(args.pop(0), GetRepositoryRoot(), True, True) |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1141 | files = change_info.GetFileNames() |
| 1142 | else: |
[email protected] | 707c148 | 2010-06-02 19:52:42 | [diff] [blame] | 1143 | files = [f[1] for f in GetFilesNotInCL()] |
[email protected] | 3872970 | 2010-06-01 23:42:03 | [diff] [blame] | 1144 | |
| 1145 | root = GetRepositoryRoot() |
| 1146 | cmd = ['svn', 'diff'] |
| 1147 | cmd.extend([os.path.join(root, x) for x in files]) |
| 1148 | cmd.extend(args) |
| 1149 | return RunShellWithReturnCode(cmd, print_output=True)[1] |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1150 | |
| 1151 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1152 | @no_args |
| 1153 | def CMDsettings(): |
| 1154 | """Prints code review settings for this checkout.""" |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1155 | # Force load settings |
[email protected] | 6e29d57 | 2010-06-04 17:32:20 | [diff] [blame] | 1156 | GetCodeReviewSetting("UNKNOWN") |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1157 | del CODEREVIEW_SETTINGS['__just_initialized'] |
| 1158 | print '\n'.join(("%s: %s" % (str(k), str(v)) |
| 1159 | for (k,v) in CODEREVIEW_SETTINGS.iteritems())) |
| 1160 | return 0 |
| 1161 | |
| 1162 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1163 | @need_change |
| 1164 | def CMDdescription(change_info): |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1165 | """Prints the description of the specified change to stdout.""" |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1166 | print change_info.description |
| 1167 | return 0 |
| 1168 | |
| 1169 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1170 | @need_change |
| 1171 | def CMDdelete(change_info): |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1172 | """Deletes a changelist.""" |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1173 | change_info.Delete() |
| 1174 | return 0 |
| 1175 | |
| 1176 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1177 | def CMDtry(args): |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1178 | """Sends the change to the tryserver to do a test run on your code. |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1179 | |
| 1180 | To send multiple changes as one path, use a comma-separated list of |
| 1181 | changenames. Use 'gcl help try' for more information!""" |
| 1182 | # When the change contains no file, send the "changename" positional |
| 1183 | # argument to trychange.py. |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1184 | # When the command is 'try' and --patchset is used, the patch to try |
| 1185 | # is on the Rietveld server. |
| 1186 | if not args: |
| 1187 | ErrorExit("You need to pass a change list name") |
| 1188 | if args[0].find(',') != -1: |
| 1189 | change_info = LoadChangelistInfoForMultiple(args[0], GetRepositoryRoot(), |
| 1190 | True, True) |
| 1191 | else: |
| 1192 | change_info = ChangeInfo.Load(args[0], GetRepositoryRoot(), |
| 1193 | False, True) |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1194 | if change_info.GetFiles(): |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1195 | args = args[1:] |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1196 | else: |
| 1197 | change_info = None |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1198 | return TryChange(change_info, args, swallow_exception=False) |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1199 | |
| 1200 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1201 | @attrs(usage='<old-name> <new-name>') |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1202 | def CMDrename(args): |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1203 | """Renames an existing change.""" |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1204 | if len(args) != 2: |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1205 | ErrorExit("Usage: gcl rename <old-name> <new-name>.") |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1206 | src, dst = args |
[email protected] | 4c22d72 | 2010-05-14 19:01:22 | [diff] [blame] | 1207 | src_file = GetChangelistInfoFile(src) |
| 1208 | if not os.path.isfile(src_file): |
| 1209 | ErrorExit("Change '%s' does not exist." % src) |
| 1210 | dst_file = GetChangelistInfoFile(dst) |
| 1211 | if os.path.isfile(dst_file): |
| 1212 | ErrorExit("Change '%s' already exists; pick a new name." % dst) |
| 1213 | os.rename(src_file, dst_file) |
| 1214 | print "Change '%s' renamed '%s'." % (src, dst) |
| 1215 | return 0 |
[email protected] | bfd09ce | 2009-08-05 21:17:23 | [diff] [blame] | 1216 | |
| 1217 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1218 | def CMDpassthru(args): |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1219 | """Everything else that is passed into gcl we redirect to svn. |
| 1220 | |
| 1221 | It assumes a change list name is passed and is converted with the files names. |
| 1222 | """ |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1223 | args = ["svn", args[0]] |
| 1224 | if len(args) > 1: |
| 1225 | root = GetRepositoryRoot() |
| 1226 | change_info = ChangeInfo.Load(args[1], root, True, True) |
| 1227 | args.extend([os.path.join(root, x) for x in change_info.GetFileNames()]) |
| 1228 | return RunShellWithReturnCode(args, print_output=True)[1] |
| 1229 | |
| 1230 | |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1231 | def Command(name): |
| 1232 | return getattr(sys.modules[__name__], 'CMD' + name, None) |
| 1233 | |
| 1234 | |
| 1235 | def GenUsage(command): |
| 1236 | """Modify an OptParse object with the function's documentation.""" |
| 1237 | obj = Command(command) |
| 1238 | display = command |
| 1239 | more = getattr(obj, 'usage', '') |
| 1240 | if command == 'help': |
| 1241 | display = '<command>' |
| 1242 | need_change = '' |
| 1243 | if getattr(obj, 'need_change', None): |
| 1244 | need_change = ' <change_list>' |
| 1245 | options = ' [options]' |
| 1246 | if getattr(obj, 'no_args', None): |
| 1247 | options = '' |
| 1248 | res = 'Usage: gcl %s%s%s %s\n\n' % (display, need_change, options, more) |
| 1249 | res += re.sub('\n ', '\n', obj.__doc__) |
| 1250 | return res |
| 1251 | |
| 1252 | |
| 1253 | def CMDhelp(args): |
| 1254 | """Prints this help or help for the given command.""" |
| 1255 | if args and 'CMD' + args[0] in dir(sys.modules[__name__]): |
| 1256 | print GenUsage(args[0]) |
| 1257 | |
| 1258 | # These commands defer to external tools so give this info too. |
| 1259 | if args[0] == 'try': |
| 1260 | TryChange(None, ['--help'], swallow_exception=False) |
| 1261 | if args[0] == 'upload': |
| 1262 | upload.RealMain(['upload.py', '--help']) |
| 1263 | return 0 |
| 1264 | |
| 1265 | print GenUsage('help') |
| 1266 | print sys.modules[__name__].__doc__ |
| 1267 | print 'version ' + __version__ + '\n' |
| 1268 | |
| 1269 | print('Commands are:\n' + '\n'.join([ |
| 1270 | ' %-12s %s' % (fn[3:], Command(fn[3:]).__doc__.split('\n')[0].strip()) |
| 1271 | for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')])) |
| 1272 | return 0 |
| 1273 | |
| 1274 | |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1275 | def main(argv): |
[email protected] | c68f9cb | 2010-06-17 20:34:18 | [diff] [blame] | 1276 | if not argv: |
| 1277 | argv = ['help'] |
| 1278 | command = Command(argv[0]) |
| 1279 | # Help can be run from anywhere. |
| 1280 | if command == CMDhelp: |
| 1281 | return command(argv[1:]) |
| 1282 | |
[email protected] | a05be0b | 2009-06-30 19:13:02 | [diff] [blame] | 1283 | try: |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1284 | GetRepositoryRoot() |
[email protected] | 5f3eee3 | 2009-09-17 00:34:30 | [diff] [blame] | 1285 | except gclient_utils.Error: |
[email protected] | 62fd693 | 2010-05-27 13:13:23 | [diff] [blame] | 1286 | print('To use gcl, you need to be in a subversion checkout.') |
| 1287 | return 1 |
| 1288 | |
| 1289 | # Create the directories where we store information about changelists if it |
| 1290 | # doesn't exist. |
[email protected] | 807c446 | 2010-07-10 00:45:28 | [diff] [blame] | 1291 | try: |
| 1292 | if not os.path.exists(GetInfoDir()): |
| 1293 | os.mkdir(GetInfoDir()) |
| 1294 | if not os.path.exists(GetChangesDir()): |
| 1295 | os.mkdir(GetChangesDir()) |
| 1296 | if not os.path.exists(GetCacheDir()): |
| 1297 | os.mkdir(GetCacheDir()) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1298 | |
[email protected] | 807c446 | 2010-07-10 00:45:28 | [diff] [blame] | 1299 | if command: |
| 1300 | return command(argv[1:]) |
| 1301 | # Unknown command, try to pass that to svn |
| 1302 | return CMDpassthru(argv) |
| 1303 | except gclient_utils.Error, e: |
| 1304 | print('Got an exception') |
| 1305 | print(str(e)) |
[email protected] | fb2b8eb | 2009-04-23 21:03:42 | [diff] [blame] | 1306 | |
| 1307 | if __name__ == "__main__": |
[email protected] | 35fe9ad | 2010-05-25 23:59:54 | [diff] [blame] | 1308 | sys.exit(main(sys.argv[1:])) |