maruel@chromium.org | 725f1c3 | 2011-04-01 20:24:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
miket@chromium.org | 183df1a | 2012-01-04 19:44:55 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | 725f1c3 | 2011-04-01 20:24:54 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 6 | # Copyright (C) 2008 Evan Martin <martine@danga.com> |
| 7 | |
maruel@chromium.org | 725f1c3 | 2011-04-01 20:24:54 +0000 | [diff] [blame] | 8 | """A git-command for integrating reviews on Rietveld.""" |
| 9 | |
maruel@chromium.org | 4f6852c | 2012-04-20 20:39:20 +0000 | [diff] [blame] | 10 | import json |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 11 | import logging |
| 12 | import optparse |
| 13 | import os |
| 14 | import re |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 15 | import stat |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 16 | import sys |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 17 | import textwrap |
msb@chromium.org | bf1a7ba | 2011-02-01 16:21:46 +0000 | [diff] [blame] | 18 | import urlparse |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 19 | import urllib2 |
| 20 | |
| 21 | try: |
maruel@chromium.org | c98c0c5 | 2011-04-06 13:39:43 +0000 | [diff] [blame] | 22 | import readline # pylint: disable=F0401,W0611 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 23 | except ImportError: |
| 24 | pass |
| 25 | |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 26 | |
| 27 | from third_party import upload |
| 28 | import breakpad # pylint: disable=W0611 |
maruel@chromium.org | 6f09cd9 | 2011-04-01 16:38:12 +0000 | [diff] [blame] | 29 | import fix_encoding |
maruel@chromium.org | 0e0436a | 2011-10-25 13:32:41 +0000 | [diff] [blame] | 30 | import gclient_utils |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 31 | import presubmit_support |
maruel@chromium.org | cab38e9 | 2011-04-09 00:30:51 +0000 | [diff] [blame] | 32 | import rietveld |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 33 | import scm |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 34 | import subprocess2 |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 35 | import watchlists |
| 36 | |
| 37 | |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 38 | DEFAULT_SERVER = 'https://codereview.appspot.com' |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 39 | POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 40 | DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 41 | GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingNewGit' |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 42 | CHANGE_ID = 'Change-Id:' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 43 | |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 44 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 45 | # Initialized in main() |
| 46 | settings = None |
| 47 | |
| 48 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 49 | def DieWithError(message): |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 50 | print >> sys.stderr, message |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 51 | sys.exit(1) |
| 52 | |
| 53 | |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 54 | def RunCommand(args, error_ok=False, error_message=None, **kwargs): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 55 | try: |
maruel@chromium.org | 373af80 | 2012-05-25 21:07:33 +0000 | [diff] [blame] | 56 | return subprocess2.check_output(args, shell=False, **kwargs) |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 57 | except subprocess2.CalledProcessError, e: |
| 58 | if not error_ok: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 59 | DieWithError( |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 60 | 'Command "%s" failed.\n%s' % ( |
| 61 | ' '.join(args), error_message or e.stdout or '')) |
| 62 | return e.stdout |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def RunGit(args, **kwargs): |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 66 | """Returns stdout.""" |
| 67 | return RunCommand(['git'] + args, **kwargs) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def RunGitWithCode(args): |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 71 | """Returns return code and stdout.""" |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 72 | try: |
| 73 | out, code = subprocess2.communicate(['git'] + args, stdout=subprocess2.PIPE) |
| 74 | return code, out[0] |
| 75 | except ValueError: |
| 76 | # When the subprocess fails, it returns None. That triggers a ValueError |
| 77 | # when trying to unpack the return value into (out, code). |
| 78 | return 1, '' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | def usage(more): |
| 82 | def hook(fn): |
| 83 | fn.usage_more = more |
| 84 | return fn |
| 85 | return hook |
| 86 | |
| 87 | |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 88 | def ask_for_data(prompt): |
| 89 | try: |
| 90 | return raw_input(prompt) |
| 91 | except KeyboardInterrupt: |
| 92 | # Hide the exception. |
| 93 | sys.exit(1) |
| 94 | |
| 95 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 96 | def git_set_branch_value(key, value): |
| 97 | branch = Changelist().GetBranch() |
rogerta@chromium.org | caa1655 | 2013-03-18 20:45:05 +0000 | [diff] [blame] | 98 | if not branch: |
| 99 | return |
| 100 | |
| 101 | cmd = ['config'] |
| 102 | if isinstance(value, int): |
| 103 | cmd.append('--int') |
| 104 | git_key = 'branch.%s.%s' % (branch, key) |
| 105 | RunGit(cmd + [git_key, str(value)]) |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def git_get_branch_default(key, default): |
| 109 | branch = Changelist().GetBranch() |
| 110 | if branch: |
| 111 | git_key = 'branch.%s.%s' % (branch, key) |
| 112 | (_, stdout) = RunGitWithCode(['config', '--int', '--get', git_key]) |
| 113 | try: |
| 114 | return int(stdout.strip()) |
| 115 | except ValueError: |
| 116 | pass |
| 117 | return default |
| 118 | |
| 119 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 120 | def add_git_similarity(parser): |
| 121 | parser.add_option( |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 122 | '--similarity', metavar='SIM', type='int', action='store', |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 123 | help='Sets the percentage that a pair of files need to match in order to' |
| 124 | ' be considered copies (default 50)') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 125 | parser.add_option( |
| 126 | '--find-copies', action='store_true', |
| 127 | help='Allows git to look for copies.') |
| 128 | parser.add_option( |
| 129 | '--no-find-copies', action='store_false', dest='find_copies', |
| 130 | help='Disallows git from looking for copies.') |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 131 | |
| 132 | old_parser_args = parser.parse_args |
| 133 | def Parse(args): |
| 134 | options, args = old_parser_args(args) |
| 135 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 136 | if options.similarity is None: |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 137 | options.similarity = git_get_branch_default('git-cl-similarity', 50) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 138 | else: |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 139 | print('Note: Saving similarity of %d%% in git config.' |
| 140 | % options.similarity) |
| 141 | git_set_branch_value('git-cl-similarity', options.similarity) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 142 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 143 | options.similarity = max(0, min(options.similarity, 100)) |
| 144 | |
| 145 | if options.find_copies is None: |
| 146 | options.find_copies = bool( |
| 147 | git_get_branch_default('git-find-copies', True)) |
| 148 | else: |
| 149 | git_set_branch_value('git-find-copies', int(options.find_copies)) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 150 | |
| 151 | print('Using %d%% similarity for rename/copy detection. ' |
| 152 | 'Override with --similarity.' % options.similarity) |
| 153 | |
| 154 | return options, args |
| 155 | parser.parse_args = Parse |
| 156 | |
| 157 | |
ukai@chromium.org | 259e468 | 2012-10-25 07:36:33 +0000 | [diff] [blame] | 158 | def is_dirty_git_tree(cmd): |
| 159 | # Make sure index is up-to-date before running diff-index. |
| 160 | RunGit(['update-index', '--refresh', '-q'], error_ok=True) |
| 161 | dirty = RunGit(['diff-index', '--name-status', 'HEAD']) |
| 162 | if dirty: |
| 163 | print 'Cannot %s with a dirty tree. You must commit locally first.' % cmd |
| 164 | print 'Uncommitted files: (git diff-index --name-status HEAD)' |
| 165 | print dirty[:4096] |
| 166 | if len(dirty) > 4096: |
| 167 | print '... (run "git diff-index --name-status HEAD" to see full output).' |
| 168 | return True |
| 169 | return False |
| 170 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 171 | |
bauerb@chromium.org | 866276c | 2011-03-18 20:09:31 +0000 | [diff] [blame] | 172 | def MatchSvnGlob(url, base_url, glob_spec, allow_wildcards): |
| 173 | """Return the corresponding git ref if |base_url| together with |glob_spec| |
| 174 | matches the full |url|. |
| 175 | |
| 176 | If |allow_wildcards| is true, |glob_spec| can contain wildcards (see below). |
| 177 | """ |
| 178 | fetch_suburl, as_ref = glob_spec.split(':') |
| 179 | if allow_wildcards: |
| 180 | glob_match = re.match('(.+/)?(\*|{[^/]*})(/.+)?', fetch_suburl) |
| 181 | if glob_match: |
| 182 | # Parse specs like "branches/*/src:refs/remotes/svn/*" or |
| 183 | # "branches/{472,597,648}/src:refs/remotes/svn/*". |
| 184 | branch_re = re.escape(base_url) |
| 185 | if glob_match.group(1): |
| 186 | branch_re += '/' + re.escape(glob_match.group(1)) |
| 187 | wildcard = glob_match.group(2) |
| 188 | if wildcard == '*': |
| 189 | branch_re += '([^/]*)' |
| 190 | else: |
| 191 | # Escape and replace surrounding braces with parentheses and commas |
| 192 | # with pipe symbols. |
| 193 | wildcard = re.escape(wildcard) |
| 194 | wildcard = re.sub('^\\\\{', '(', wildcard) |
| 195 | wildcard = re.sub('\\\\,', '|', wildcard) |
| 196 | wildcard = re.sub('\\\\}$', ')', wildcard) |
| 197 | branch_re += wildcard |
| 198 | if glob_match.group(3): |
| 199 | branch_re += re.escape(glob_match.group(3)) |
| 200 | match = re.match(branch_re, url) |
| 201 | if match: |
| 202 | return re.sub('\*$', match.group(1), as_ref) |
| 203 | |
| 204 | # Parse specs like "trunk/src:refs/remotes/origin/trunk". |
| 205 | if fetch_suburl: |
| 206 | full_url = base_url + '/' + fetch_suburl |
| 207 | else: |
| 208 | full_url = base_url |
| 209 | if full_url == url: |
| 210 | return as_ref |
| 211 | return None |
| 212 | |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 213 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 214 | def print_stats(similarity, find_copies, args): |
maruel@chromium.org | 49e3d80 | 2012-07-18 23:54:45 +0000 | [diff] [blame] | 215 | """Prints statistics about the change to the user.""" |
| 216 | # --no-ext-diff is broken in some versions of Git, so try to work around |
| 217 | # this by overriding the environment (but there is still a problem if the |
| 218 | # git config key "diff.external" is used). |
| 219 | env = os.environ.copy() |
| 220 | if 'GIT_EXTERNAL_DIFF' in env: |
| 221 | del env['GIT_EXTERNAL_DIFF'] |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 222 | |
| 223 | if find_copies: |
| 224 | similarity_options = ['--find-copies-harder', '-l100000', |
| 225 | '-C%s' % similarity] |
| 226 | else: |
| 227 | similarity_options = ['-M%s' % similarity] |
| 228 | |
maruel@chromium.org | 49e3d80 | 2012-07-18 23:54:45 +0000 | [diff] [blame] | 229 | return subprocess2.call( |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 230 | ['git', 'diff', '--no-ext-diff', '--stat'] + similarity_options + args, |
| 231 | env=env) |
maruel@chromium.org | 49e3d80 | 2012-07-18 23:54:45 +0000 | [diff] [blame] | 232 | |
| 233 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 234 | class Settings(object): |
| 235 | def __init__(self): |
| 236 | self.default_server = None |
| 237 | self.cc = None |
| 238 | self.root = None |
| 239 | self.is_git_svn = None |
| 240 | self.svn_branch = None |
| 241 | self.tree_status_url = None |
| 242 | self.viewvc_url = None |
| 243 | self.updated = False |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 244 | self.is_gerrit = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 245 | |
| 246 | def LazyUpdateIfNeeded(self): |
| 247 | """Updates the settings from a codereview.settings file, if available.""" |
| 248 | if not self.updated: |
| 249 | cr_settings_file = FindCodereviewSettingsFile() |
| 250 | if cr_settings_file: |
| 251 | LoadCodereviewSettingsFromFile(cr_settings_file) |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 252 | self.updated = True |
| 253 | DownloadHooks(False) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 254 | self.updated = True |
| 255 | |
| 256 | def GetDefaultServerUrl(self, error_ok=False): |
| 257 | if not self.default_server: |
| 258 | self.LazyUpdateIfNeeded() |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 259 | self.default_server = gclient_utils.UpgradeToHttps( |
| 260 | self._GetConfig('rietveld.server', error_ok=True)) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 261 | if error_ok: |
| 262 | return self.default_server |
| 263 | if not self.default_server: |
| 264 | error_message = ('Could not find settings file. You must configure ' |
| 265 | 'your review setup by running "git cl config".') |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 266 | self.default_server = gclient_utils.UpgradeToHttps( |
| 267 | self._GetConfig('rietveld.server', error_message=error_message)) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 268 | return self.default_server |
| 269 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 270 | def GetRoot(self): |
| 271 | if not self.root: |
| 272 | self.root = os.path.abspath(RunGit(['rev-parse', '--show-cdup']).strip()) |
| 273 | return self.root |
| 274 | |
| 275 | def GetIsGitSvn(self): |
| 276 | """Return true if this repo looks like it's using git-svn.""" |
| 277 | if self.is_git_svn is None: |
| 278 | # If you have any "svn-remote.*" config keys, we think you're using svn. |
| 279 | self.is_git_svn = RunGitWithCode( |
| 280 | ['config', '--get-regexp', r'^svn-remote\.'])[0] == 0 |
| 281 | return self.is_git_svn |
| 282 | |
| 283 | def GetSVNBranch(self): |
| 284 | if self.svn_branch is None: |
| 285 | if not self.GetIsGitSvn(): |
| 286 | DieWithError('Repo doesn\'t appear to be a git-svn repo.') |
| 287 | |
| 288 | # Try to figure out which remote branch we're based on. |
| 289 | # Strategy: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 290 | # 1) iterate through our branch history and find the svn URL. |
| 291 | # 2) find the svn-remote that fetches from the URL. |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 292 | |
| 293 | # regexp matching the git-svn line that contains the URL. |
| 294 | git_svn_re = re.compile(r'^\s*git-svn-id: (\S+)@', re.MULTILINE) |
| 295 | |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 296 | # We don't want to go through all of history, so read a line from the |
| 297 | # pipe at a time. |
| 298 | # The -100 is an arbitrary limit so we don't search forever. |
| 299 | cmd = ['git', 'log', '-100', '--pretty=medium'] |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 300 | proc = subprocess2.Popen(cmd, stdout=subprocess2.PIPE) |
maruel@chromium.org | 740f9d7 | 2011-06-10 18:33:10 +0000 | [diff] [blame] | 301 | url = None |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 302 | for line in proc.stdout: |
| 303 | match = git_svn_re.match(line) |
| 304 | if match: |
| 305 | url = match.group(1) |
| 306 | proc.stdout.close() # Cut pipe. |
| 307 | break |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 308 | |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 309 | if url: |
| 310 | svn_remote_re = re.compile(r'^svn-remote\.([^.]+)\.url (.*)$') |
| 311 | remotes = RunGit(['config', '--get-regexp', |
| 312 | r'^svn-remote\..*\.url']).splitlines() |
| 313 | for remote in remotes: |
| 314 | match = svn_remote_re.match(remote) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 315 | if match: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 316 | remote = match.group(1) |
| 317 | base_url = match.group(2) |
| 318 | fetch_spec = RunGit( |
bauerb@chromium.org | 866276c | 2011-03-18 20:09:31 +0000 | [diff] [blame] | 319 | ['config', 'svn-remote.%s.fetch' % remote], |
| 320 | error_ok=True).strip() |
| 321 | if fetch_spec: |
| 322 | self.svn_branch = MatchSvnGlob(url, base_url, fetch_spec, False) |
| 323 | if self.svn_branch: |
| 324 | break |
| 325 | branch_spec = RunGit( |
| 326 | ['config', 'svn-remote.%s.branches' % remote], |
| 327 | error_ok=True).strip() |
| 328 | if branch_spec: |
| 329 | self.svn_branch = MatchSvnGlob(url, base_url, branch_spec, True) |
| 330 | if self.svn_branch: |
| 331 | break |
| 332 | tag_spec = RunGit( |
| 333 | ['config', 'svn-remote.%s.tags' % remote], |
| 334 | error_ok=True).strip() |
| 335 | if tag_spec: |
| 336 | self.svn_branch = MatchSvnGlob(url, base_url, tag_spec, True) |
| 337 | if self.svn_branch: |
| 338 | break |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 339 | |
| 340 | if not self.svn_branch: |
| 341 | DieWithError('Can\'t guess svn branch -- try specifying it on the ' |
| 342 | 'command line') |
| 343 | |
| 344 | return self.svn_branch |
| 345 | |
| 346 | def GetTreeStatusUrl(self, error_ok=False): |
| 347 | if not self.tree_status_url: |
| 348 | error_message = ('You must configure your tree status URL by running ' |
| 349 | '"git cl config".') |
| 350 | self.tree_status_url = self._GetConfig('rietveld.tree-status-url', |
| 351 | error_ok=error_ok, |
| 352 | error_message=error_message) |
| 353 | return self.tree_status_url |
| 354 | |
| 355 | def GetViewVCUrl(self): |
| 356 | if not self.viewvc_url: |
ilevy@chromium.org | a78f7c0 | 2012-11-28 02:06:45 +0000 | [diff] [blame] | 357 | self.viewvc_url = self._GetConfig('rietveld.viewvc-url', error_ok=True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 358 | return self.viewvc_url |
| 359 | |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 360 | def GetDefaultCCList(self): |
| 361 | return self._GetConfig('rietveld.cc', error_ok=True) |
| 362 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 363 | def GetIsGerrit(self): |
| 364 | """Return true if this repo is assosiated with gerrit code review system.""" |
| 365 | if self.is_gerrit is None: |
| 366 | self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True) |
| 367 | return self.is_gerrit |
| 368 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 369 | def _GetConfig(self, param, **kwargs): |
| 370 | self.LazyUpdateIfNeeded() |
| 371 | return RunGit(['config', param], **kwargs).strip() |
| 372 | |
| 373 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 374 | def ShortBranchName(branch): |
| 375 | """Convert a name like 'refs/heads/foo' to just 'foo'.""" |
| 376 | return branch.replace('refs/heads/', '') |
| 377 | |
| 378 | |
| 379 | class Changelist(object): |
| 380 | def __init__(self, branchref=None): |
| 381 | # Poke settings so we get the "configure your server" message if necessary. |
maruel@chromium.org | 379d07a | 2011-11-30 14:58:10 +0000 | [diff] [blame] | 382 | global settings |
| 383 | if not settings: |
| 384 | # Happens when git_cl.py is used as a utility library. |
| 385 | settings = Settings() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 386 | settings.GetDefaultServerUrl() |
| 387 | self.branchref = branchref |
| 388 | if self.branchref: |
| 389 | self.branch = ShortBranchName(self.branchref) |
| 390 | else: |
| 391 | self.branch = None |
| 392 | self.rietveld_server = None |
| 393 | self.upstream_branch = None |
| 394 | self.has_issue = False |
| 395 | self.issue = None |
| 396 | self.has_description = False |
| 397 | self.description = None |
| 398 | self.has_patchset = False |
| 399 | self.patchset = None |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 400 | self._rpc_server = None |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 401 | self.cc = None |
| 402 | self.watchers = () |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 403 | self._remote = None |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 404 | |
| 405 | def GetCCList(self): |
| 406 | """Return the users cc'd on this CL. |
| 407 | |
| 408 | Return is a string suitable for passing to gcl with the --cc flag. |
| 409 | """ |
| 410 | if self.cc is None: |
| 411 | base_cc = settings .GetDefaultCCList() |
| 412 | more_cc = ','.join(self.watchers) |
| 413 | self.cc = ','.join(filter(None, (base_cc, more_cc))) or '' |
| 414 | return self.cc |
| 415 | |
| 416 | def SetWatchers(self, watchers): |
| 417 | """Set the list of email addresses that should be cc'd based on the changed |
| 418 | files in this CL. |
| 419 | """ |
| 420 | self.watchers = watchers |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 421 | |
| 422 | def GetBranch(self): |
| 423 | """Returns the short branch name, e.g. 'master'.""" |
| 424 | if not self.branch: |
| 425 | self.branchref = RunGit(['symbolic-ref', 'HEAD']).strip() |
| 426 | self.branch = ShortBranchName(self.branchref) |
| 427 | return self.branch |
| 428 | |
| 429 | def GetBranchRef(self): |
| 430 | """Returns the full branch name, e.g. 'refs/heads/master'.""" |
| 431 | self.GetBranch() # Poke the lazy loader. |
| 432 | return self.branchref |
| 433 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 434 | @staticmethod |
| 435 | def FetchUpstreamTuple(branch): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 436 | """Returns a tuple containg remote and remote ref, |
| 437 | e.g. 'origin', 'refs/heads/master' |
| 438 | """ |
| 439 | remote = '.' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 440 | upstream_branch = RunGit(['config', 'branch.%s.merge' % branch], |
| 441 | error_ok=True).strip() |
| 442 | if upstream_branch: |
| 443 | remote = RunGit(['config', 'branch.%s.remote' % branch]).strip() |
| 444 | else: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 445 | upstream_branch = RunGit(['config', 'rietveld.upstream-branch'], |
| 446 | error_ok=True).strip() |
| 447 | if upstream_branch: |
| 448 | remote = RunGit(['config', 'rietveld.upstream-remote']).strip() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 449 | else: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 450 | # Fall back on trying a git-svn upstream branch. |
| 451 | if settings.GetIsGitSvn(): |
| 452 | upstream_branch = settings.GetSVNBranch() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 453 | else: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 454 | # Else, try to guess the origin remote. |
| 455 | remote_branches = RunGit(['branch', '-r']).split() |
| 456 | if 'origin/master' in remote_branches: |
| 457 | # Fall back on origin/master if it exits. |
| 458 | remote = 'origin' |
| 459 | upstream_branch = 'refs/heads/master' |
| 460 | elif 'origin/trunk' in remote_branches: |
| 461 | # Fall back on origin/trunk if it exists. Generally a shared |
| 462 | # git-svn clone |
| 463 | remote = 'origin' |
| 464 | upstream_branch = 'refs/heads/trunk' |
| 465 | else: |
| 466 | DieWithError("""Unable to determine default branch to diff against. |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 467 | Either pass complete "git diff"-style arguments, like |
| 468 | git cl upload origin/master |
| 469 | or verify this branch is set up to track another (via the --track argument to |
| 470 | "git checkout -b ...").""") |
| 471 | |
| 472 | return remote, upstream_branch |
| 473 | |
| 474 | def GetUpstreamBranch(self): |
| 475 | if self.upstream_branch is None: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 476 | remote, upstream_branch = self.FetchUpstreamTuple(self.GetBranch()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 477 | if remote is not '.': |
| 478 | upstream_branch = upstream_branch.replace('heads', 'remotes/' + remote) |
| 479 | self.upstream_branch = upstream_branch |
| 480 | return self.upstream_branch |
| 481 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 482 | def GetRemoteBranch(self): |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 483 | if not self._remote: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 484 | remote, branch = None, self.GetBranch() |
| 485 | seen_branches = set() |
| 486 | while branch not in seen_branches: |
| 487 | seen_branches.add(branch) |
| 488 | remote, branch = self.FetchUpstreamTuple(branch) |
| 489 | branch = ShortBranchName(branch) |
| 490 | if remote != '.' or branch.startswith('refs/remotes'): |
| 491 | break |
| 492 | else: |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 493 | remotes = RunGit(['remote'], error_ok=True).split() |
| 494 | if len(remotes) == 1: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 495 | remote, = remotes |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 496 | elif 'origin' in remotes: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 497 | remote = 'origin' |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 498 | logging.warning('Could not determine which remote this change is ' |
| 499 | 'associated with, so defaulting to "%s". This may ' |
| 500 | 'not be what you want. You may prevent this message ' |
| 501 | 'by running "git svn info" as documented here: %s', |
| 502 | self._remote, |
| 503 | GIT_INSTRUCTIONS_URL) |
| 504 | else: |
| 505 | logging.warn('Could not determine which remote this change is ' |
| 506 | 'associated with. You may prevent this message by ' |
| 507 | 'running "git svn info" as documented here: %s', |
| 508 | GIT_INSTRUCTIONS_URL) |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 509 | branch = 'HEAD' |
| 510 | if branch.startswith('refs/remotes'): |
| 511 | self._remote = (remote, branch) |
| 512 | else: |
| 513 | self._remote = (remote, 'refs/remotes/%s/%s' % (remote, branch)) |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 514 | return self._remote |
| 515 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 516 | def GitSanityChecks(self, upstream_git_obj): |
| 517 | """Checks git repo status and ensures diff is from local commits.""" |
| 518 | |
| 519 | # Verify the commit we're diffing against is in our current branch. |
| 520 | upstream_sha = RunGit(['rev-parse', '--verify', upstream_git_obj]).strip() |
| 521 | common_ancestor = RunGit(['merge-base', upstream_sha, 'HEAD']).strip() |
| 522 | if upstream_sha != common_ancestor: |
| 523 | print >> sys.stderr, ( |
| 524 | 'ERROR: %s is not in the current branch. You may need to rebase ' |
| 525 | 'your tracking branch' % upstream_sha) |
| 526 | return False |
| 527 | |
| 528 | # List the commits inside the diff, and verify they are all local. |
| 529 | commits_in_diff = RunGit( |
| 530 | ['rev-list', '^%s' % upstream_sha, 'HEAD']).splitlines() |
| 531 | code, remote_branch = RunGitWithCode(['config', 'gitcl.remotebranch']) |
| 532 | remote_branch = remote_branch.strip() |
| 533 | if code != 0: |
| 534 | _, remote_branch = self.GetRemoteBranch() |
| 535 | |
| 536 | commits_in_remote = RunGit( |
| 537 | ['rev-list', '^%s' % upstream_sha, remote_branch]).splitlines() |
| 538 | |
| 539 | common_commits = set(commits_in_diff) & set(commits_in_remote) |
| 540 | if common_commits: |
| 541 | print >> sys.stderr, ( |
| 542 | 'ERROR: Your diff contains %d commits already in %s.\n' |
| 543 | 'Run "git log --oneline %s..HEAD" to get a list of commits in ' |
| 544 | 'the diff. If you are using a custom git flow, you can override' |
| 545 | ' the reference used for this check with "git config ' |
| 546 | 'gitcl.remotebranch <git-ref>".' % ( |
| 547 | len(common_commits), remote_branch, upstream_git_obj)) |
| 548 | return False |
| 549 | return True |
| 550 | |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 551 | def GetGitBaseUrlFromConfig(self): |
| 552 | """Return the configured base URL from branch.<branchname>.baseurl. |
| 553 | |
| 554 | Returns None if it is not set. |
| 555 | """ |
| 556 | return RunGit(['config', 'branch.%s.base-url' % self.GetBranch()], |
| 557 | error_ok=True).strip() |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 558 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 559 | def GetRemoteUrl(self): |
| 560 | """Return the configured remote URL, e.g. 'git://example.org/foo.git/'. |
| 561 | |
| 562 | Returns None if there is no remote. |
| 563 | """ |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 564 | remote, _ = self.GetRemoteBranch() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 565 | return RunGit(['config', 'remote.%s.url' % remote], error_ok=True).strip() |
| 566 | |
| 567 | def GetIssue(self): |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 568 | """Returns the issue number as a int or None if not set.""" |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 569 | if not self.has_issue: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 570 | issue = RunGit(['config', self._IssueSetting()], error_ok=True).strip() |
| 571 | if issue: |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 572 | self.issue = int(issue) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 573 | else: |
| 574 | self.issue = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 575 | self.has_issue = True |
| 576 | return self.issue |
| 577 | |
| 578 | def GetRietveldServer(self): |
evan@chromium.org | 0af9b70 | 2012-02-11 00:42:16 +0000 | [diff] [blame] | 579 | if not self.rietveld_server: |
| 580 | # If we're on a branch then get the server potentially associated |
| 581 | # with that branch. |
| 582 | if self.GetIssue(): |
| 583 | self.rietveld_server = gclient_utils.UpgradeToHttps(RunGit( |
| 584 | ['config', self._RietveldServer()], error_ok=True).strip()) |
| 585 | if not self.rietveld_server: |
| 586 | self.rietveld_server = settings.GetDefaultServerUrl() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 587 | return self.rietveld_server |
| 588 | |
| 589 | def GetIssueURL(self): |
| 590 | """Get the URL for a particular issue.""" |
| 591 | return '%s/%s' % (self.GetRietveldServer(), self.GetIssue()) |
| 592 | |
| 593 | def GetDescription(self, pretty=False): |
| 594 | if not self.has_description: |
| 595 | if self.GetIssue(): |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 596 | issue = self.GetIssue() |
miket@chromium.org | 183df1a | 2012-01-04 19:44:55 +0000 | [diff] [blame] | 597 | try: |
| 598 | self.description = self.RpcServer().get_description(issue).strip() |
| 599 | except urllib2.HTTPError, e: |
| 600 | if e.code == 404: |
| 601 | DieWithError( |
| 602 | ('\nWhile fetching the description for issue %d, received a ' |
| 603 | '404 (not found)\n' |
| 604 | 'error. It is likely that you deleted this ' |
| 605 | 'issue on the server. If this is the\n' |
| 606 | 'case, please run\n\n' |
| 607 | ' git cl issue 0\n\n' |
| 608 | 'to clear the association with the deleted issue. Then run ' |
| 609 | 'this command again.') % issue) |
| 610 | else: |
| 611 | DieWithError( |
| 612 | '\nFailed to fetch issue description. HTTP error ' + e.code) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 613 | self.has_description = True |
| 614 | if pretty: |
| 615 | wrapper = textwrap.TextWrapper() |
| 616 | wrapper.initial_indent = wrapper.subsequent_indent = ' ' |
| 617 | return wrapper.fill(self.description) |
| 618 | return self.description |
| 619 | |
| 620 | def GetPatchset(self): |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 621 | """Returns the patchset number as a int or None if not set.""" |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 622 | if not self.has_patchset: |
| 623 | patchset = RunGit(['config', self._PatchsetSetting()], |
| 624 | error_ok=True).strip() |
| 625 | if patchset: |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 626 | self.patchset = int(patchset) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 627 | else: |
| 628 | self.patchset = None |
| 629 | self.has_patchset = True |
| 630 | return self.patchset |
| 631 | |
| 632 | def SetPatchset(self, patchset): |
| 633 | """Set this branch's patchset. If patchset=0, clears the patchset.""" |
| 634 | if patchset: |
| 635 | RunGit(['config', self._PatchsetSetting(), str(patchset)]) |
| 636 | else: |
| 637 | RunGit(['config', '--unset', self._PatchsetSetting()], |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 638 | stderr=subprocess2.PIPE, error_ok=True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 639 | self.has_patchset = False |
| 640 | |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 641 | def GetMostRecentPatchset(self, issue): |
| 642 | return self.RpcServer().get_issue_properties( |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 643 | int(issue), False)['patchsets'][-1] |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 644 | |
| 645 | def GetPatchSetDiff(self, issue, patchset): |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 646 | return self.RpcServer().get( |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 647 | '/download/issue%s_%s.diff' % (issue, patchset)) |
| 648 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 649 | def SetIssue(self, issue): |
| 650 | """Set this branch's issue. If issue=0, clears the issue.""" |
| 651 | if issue: |
| 652 | RunGit(['config', self._IssueSetting(), str(issue)]) |
| 653 | if self.rietveld_server: |
| 654 | RunGit(['config', self._RietveldServer(), self.rietveld_server]) |
| 655 | else: |
| 656 | RunGit(['config', '--unset', self._IssueSetting()]) |
| 657 | self.SetPatchset(0) |
| 658 | self.has_issue = False |
| 659 | |
asvitkine@chromium.org | 1516995 | 2011-09-27 14:30:53 +0000 | [diff] [blame] | 660 | def GetChange(self, upstream_branch, author): |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 661 | if not self.GitSanityChecks(upstream_branch): |
| 662 | DieWithError('\nGit sanity check failure') |
| 663 | |
bauerb@chromium.org | 512f1ef | 2011-04-20 15:17:57 +0000 | [diff] [blame] | 664 | root = RunCommand(['git', 'rev-parse', '--show-cdup']).strip() or '.' |
| 665 | absroot = os.path.abspath(root) |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 666 | |
| 667 | # We use the sha1 of HEAD as a name of this change. |
| 668 | name = RunCommand(['git', 'rev-parse', 'HEAD']).strip() |
bauerb@chromium.org | 512f1ef | 2011-04-20 15:17:57 +0000 | [diff] [blame] | 669 | # Need to pass a relative path for msysgit. |
maruel@chromium.org | 2b38e9c | 2011-10-19 00:04:35 +0000 | [diff] [blame] | 670 | try: |
maruel@chromium.org | 80a9ef1 | 2011-12-13 20:44:10 +0000 | [diff] [blame] | 671 | files = scm.GIT.CaptureStatus([root], '.', upstream_branch) |
maruel@chromium.org | 2b38e9c | 2011-10-19 00:04:35 +0000 | [diff] [blame] | 672 | except subprocess2.CalledProcessError: |
| 673 | DieWithError( |
| 674 | ('\nFailed to diff against upstream branch %s!\n\n' |
| 675 | 'This branch probably doesn\'t exist anymore. To reset the\n' |
| 676 | 'tracking branch, please run\n' |
| 677 | ' git branch --set-upstream %s trunk\n' |
| 678 | 'replacing trunk with origin/master or the relevant branch') % |
| 679 | (upstream_branch, self.GetBranch())) |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 680 | |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 681 | issue = self.GetIssue() |
| 682 | patchset = self.GetPatchset() |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 683 | if issue: |
| 684 | description = self.GetDescription() |
| 685 | else: |
| 686 | # If the change was never uploaded, use the log messages of all commits |
| 687 | # up to the branch point, as git cl upload will prefill the description |
| 688 | # with these log messages. |
maruel@chromium.org | 373af80 | 2012-05-25 21:07:33 +0000 | [diff] [blame] | 689 | description = RunCommand(['git', 'log', '--pretty=format:%s%n%n%b', |
| 690 | '%s...' % (upstream_branch)]).strip() |
maruel@chromium.org | 03b3bdc | 2011-06-14 13:04:12 +0000 | [diff] [blame] | 691 | |
| 692 | if not author: |
maruel@chromium.org | 13f623c | 2011-07-22 16:02:23 +0000 | [diff] [blame] | 693 | author = RunGit(['config', 'user.email']).strip() or None |
asvitkine@chromium.org | 1516995 | 2011-09-27 14:30:53 +0000 | [diff] [blame] | 694 | return presubmit_support.GitChange( |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 695 | name, |
| 696 | description, |
| 697 | absroot, |
| 698 | files, |
| 699 | issue, |
| 700 | patchset, |
maruel@chromium.org | 03b3bdc | 2011-06-14 13:04:12 +0000 | [diff] [blame] | 701 | author) |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 702 | |
ilevy@chromium.org | 051ad0e | 2013-03-04 21:57:34 +0000 | [diff] [blame] | 703 | def RunHook(self, committing, may_prompt, verbose, change): |
asvitkine@chromium.org | 1516995 | 2011-09-27 14:30:53 +0000 | [diff] [blame] | 704 | """Calls sys.exit() if the hook fails; returns a HookResults otherwise.""" |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 705 | |
| 706 | try: |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 707 | return presubmit_support.DoPresubmitChecks(change, committing, |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 708 | verbose=verbose, output_stream=sys.stdout, input_stream=sys.stdin, |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 709 | default_presubmit=None, may_prompt=may_prompt, |
maruel@chromium.org | 239f411 | 2011-06-03 20:08:23 +0000 | [diff] [blame] | 710 | rietveld_obj=self.RpcServer()) |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 711 | except presubmit_support.PresubmitFailure, e: |
| 712 | DieWithError( |
| 713 | ('%s\nMaybe your depot_tools is out of date?\n' |
| 714 | 'If all fails, contact maruel@') % e) |
| 715 | |
maruel@chromium.org | b021b32 | 2013-04-08 17:57:29 +0000 | [diff] [blame^] | 716 | def UpdateDescription(self, description): |
| 717 | self.description = description |
| 718 | return self.RpcServer().update_description( |
| 719 | self.GetIssue(), self.description) |
| 720 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 721 | def CloseIssue(self): |
maruel@chromium.org | 607bb1b | 2011-06-01 23:43:11 +0000 | [diff] [blame] | 722 | """Updates the description and closes the issue.""" |
maruel@chromium.org | b021b32 | 2013-04-08 17:57:29 +0000 | [diff] [blame^] | 723 | return self.RpcServer().close_issue(self.GetIssue()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 724 | |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 725 | def SetFlag(self, flag, value): |
| 726 | """Patchset must match.""" |
| 727 | if not self.GetPatchset(): |
| 728 | DieWithError('The patchset needs to match. Send another patchset.') |
| 729 | try: |
| 730 | return self.RpcServer().set_flag( |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 731 | self.GetIssue(), self.GetPatchset(), flag, value) |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 732 | except urllib2.HTTPError, e: |
| 733 | if e.code == 404: |
| 734 | DieWithError('The issue %s doesn\'t exist.' % self.GetIssue()) |
| 735 | if e.code == 403: |
| 736 | DieWithError( |
| 737 | ('Access denied to issue %s. Maybe the patchset %s doesn\'t ' |
| 738 | 'match?') % (self.GetIssue(), self.GetPatchset())) |
| 739 | raise |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 740 | |
maruel@chromium.org | cab38e9 | 2011-04-09 00:30:51 +0000 | [diff] [blame] | 741 | def RpcServer(self): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 742 | """Returns an upload.RpcServer() to access this review's rietveld instance. |
| 743 | """ |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 744 | if not self._rpc_server: |
maruel@chromium.org | 4bac4b5 | 2012-11-27 20:33:52 +0000 | [diff] [blame] | 745 | self._rpc_server = rietveld.CachingRietveld( |
| 746 | self.GetRietveldServer(), None, None) |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 747 | return self._rpc_server |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 748 | |
| 749 | def _IssueSetting(self): |
| 750 | """Return the git setting that stores this change's issue.""" |
| 751 | return 'branch.%s.rietveldissue' % self.GetBranch() |
| 752 | |
| 753 | def _PatchsetSetting(self): |
| 754 | """Return the git setting that stores this change's most recent patchset.""" |
| 755 | return 'branch.%s.rietveldpatchset' % self.GetBranch() |
| 756 | |
| 757 | def _RietveldServer(self): |
| 758 | """Returns the git setting that stores this change's rietveld server.""" |
| 759 | return 'branch.%s.rietveldserver' % self.GetBranch() |
| 760 | |
| 761 | |
| 762 | def GetCodereviewSettingsInteractively(): |
| 763 | """Prompt the user for settings.""" |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 764 | # TODO(ukai): ask code review system is rietveld or gerrit? |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 765 | server = settings.GetDefaultServerUrl(error_ok=True) |
| 766 | prompt = 'Rietveld server (host[:port])' |
| 767 | prompt += ' [%s]' % (server or DEFAULT_SERVER) |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 768 | newserver = ask_for_data(prompt + ':') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 769 | if not server and not newserver: |
| 770 | newserver = DEFAULT_SERVER |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 771 | if newserver: |
| 772 | newserver = gclient_utils.UpgradeToHttps(newserver) |
| 773 | if newserver != server: |
| 774 | RunGit(['config', 'rietveld.server', newserver]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 775 | |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 776 | def SetProperty(initial, caption, name, is_url): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 777 | prompt = caption |
| 778 | if initial: |
| 779 | prompt += ' ("x" to clear) [%s]' % initial |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 780 | new_val = ask_for_data(prompt + ':') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 781 | if new_val == 'x': |
| 782 | RunGit(['config', '--unset-all', 'rietveld.' + name], error_ok=True) |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 783 | elif new_val: |
| 784 | if is_url: |
| 785 | new_val = gclient_utils.UpgradeToHttps(new_val) |
| 786 | if new_val != initial: |
| 787 | RunGit(['config', 'rietveld.' + name, new_val]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 788 | |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 789 | SetProperty(settings.GetDefaultCCList(), 'CC list', 'cc', False) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 790 | SetProperty(settings.GetTreeStatusUrl(error_ok=True), 'Tree status URL', |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 791 | 'tree-status-url', False) |
| 792 | SetProperty(settings.GetViewVCUrl(), 'ViewVC URL', 'viewvc-url', True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 793 | |
| 794 | # TODO: configure a default branch to diff against, rather than this |
| 795 | # svn-based hackery. |
| 796 | |
| 797 | |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 798 | class ChangeDescription(object): |
| 799 | """Contains a parsed form of the change description.""" |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 800 | def __init__(self, log_desc, reviewers): |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 801 | self.log_desc = log_desc |
| 802 | self.reviewers = reviewers |
| 803 | self.description = self.log_desc |
| 804 | |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 805 | def Prompt(self): |
| 806 | content = """# Enter a description of the change. |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 807 | # This will displayed on the codereview site. |
| 808 | # The first line will also be used as the subject of the review. |
| 809 | """ |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 810 | content += self.description |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 811 | if ('\nR=' not in self.description and |
| 812 | '\nTBR=' not in self.description and |
| 813 | self.reviewers): |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 814 | content += '\nR=' + self.reviewers |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 815 | if '\nBUG=' not in self.description: |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 816 | content += '\nBUG=' |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 817 | content = content.rstrip('\n') + '\n' |
| 818 | content = gclient_utils.RunEditor(content, True) |
maruel@chromium.org | 0e0436a | 2011-10-25 13:32:41 +0000 | [diff] [blame] | 819 | if not content: |
| 820 | DieWithError('Running editor failed') |
| 821 | content = re.compile(r'^#.*$', re.MULTILINE).sub('', content).strip() |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 822 | if not content.strip(): |
maruel@chromium.org | 0e0436a | 2011-10-25 13:32:41 +0000 | [diff] [blame] | 823 | DieWithError('No CL description, aborting') |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 824 | self.description = content |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 825 | |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 826 | def ParseDescription(self): |
jam@chromium.org | 3108364 | 2012-01-27 03:14:45 +0000 | [diff] [blame] | 827 | """Updates the list of reviewers and subject from the description.""" |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 828 | self.description = self.description.strip('\n') + '\n' |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 829 | # Retrieves all reviewer lines |
| 830 | regexp = re.compile(r'^\s*(TBR|R)=(.+)$', re.MULTILINE) |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 831 | reviewers = ','.join( |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 832 | i.group(2).strip() for i in regexp.finditer(self.description)) |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 833 | if reviewers: |
| 834 | self.reviewers = reviewers |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 835 | |
| 836 | def IsEmpty(self): |
| 837 | return not self.description |
| 838 | |
| 839 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 840 | def FindCodereviewSettingsFile(filename='codereview.settings'): |
| 841 | """Finds the given file starting in the cwd and going up. |
| 842 | |
| 843 | Only looks up to the top of the repository unless an |
| 844 | 'inherit-review-settings-ok' file exists in the root of the repository. |
| 845 | """ |
| 846 | inherit_ok_file = 'inherit-review-settings-ok' |
| 847 | cwd = os.getcwd() |
| 848 | root = os.path.abspath(RunGit(['rev-parse', '--show-cdup']).strip()) |
| 849 | if os.path.isfile(os.path.join(root, inherit_ok_file)): |
| 850 | root = '/' |
| 851 | while True: |
| 852 | if filename in os.listdir(cwd): |
| 853 | if os.path.isfile(os.path.join(cwd, filename)): |
| 854 | return open(os.path.join(cwd, filename)) |
| 855 | if cwd == root: |
| 856 | break |
| 857 | cwd = os.path.dirname(cwd) |
| 858 | |
| 859 | |
| 860 | def LoadCodereviewSettingsFromFile(fileobj): |
| 861 | """Parse a codereview.settings file and updates hooks.""" |
maruel@chromium.org | 99ac1c5 | 2012-01-16 14:52:12 +0000 | [diff] [blame] | 862 | keyvals = gclient_utils.ParseCodereviewSettingsContent(fileobj.read()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 863 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 864 | def SetProperty(name, setting, unset_error_ok=False): |
| 865 | fullname = 'rietveld.' + name |
| 866 | if setting in keyvals: |
| 867 | RunGit(['config', fullname, keyvals[setting]]) |
| 868 | else: |
| 869 | RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok) |
| 870 | |
| 871 | SetProperty('server', 'CODE_REVIEW_SERVER') |
| 872 | # Only server setting is required. Other settings can be absent. |
| 873 | # In that case, we ignore errors raised during option deletion attempt. |
| 874 | SetProperty('cc', 'CC_LIST', unset_error_ok=True) |
| 875 | SetProperty('tree-status-url', 'STATUS', unset_error_ok=True) |
| 876 | SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True) |
| 877 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 878 | if 'GERRIT_HOST' in keyvals and 'GERRIT_PORT' in keyvals: |
| 879 | RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']]) |
| 880 | RunGit(['config', 'gerrit.port', keyvals['GERRIT_PORT']]) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 881 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 882 | if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals: |
| 883 | #should be of the form |
| 884 | #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof |
| 885 | #ORIGIN_URL_CONFIG: http://src.chromium.org/git |
| 886 | RunGit(['config', keyvals['PUSH_URL_CONFIG'], |
| 887 | keyvals['ORIGIN_URL_CONFIG']]) |
| 888 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 889 | |
joshua.lock@intel.com | 426f69b | 2012-08-02 23:41:49 +0000 | [diff] [blame] | 890 | def urlretrieve(source, destination): |
| 891 | """urllib is broken for SSL connections via a proxy therefore we |
| 892 | can't use urllib.urlretrieve().""" |
| 893 | with open(destination, 'w') as f: |
| 894 | f.write(urllib2.urlopen(source).read()) |
| 895 | |
| 896 | |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 897 | def DownloadHooks(force): |
| 898 | """downloads hooks |
| 899 | |
| 900 | Args: |
| 901 | force: True to update hooks. False to install hooks if not present. |
| 902 | """ |
| 903 | if not settings.GetIsGerrit(): |
| 904 | return |
| 905 | server_url = settings.GetDefaultServerUrl() |
| 906 | src = '%s/tools/hooks/commit-msg' % server_url |
| 907 | dst = os.path.join(settings.GetRoot(), '.git', 'hooks', 'commit-msg') |
| 908 | if not os.access(dst, os.X_OK): |
| 909 | if os.path.exists(dst): |
| 910 | if not force: |
| 911 | return |
| 912 | os.remove(dst) |
| 913 | try: |
joshua.lock@intel.com | 426f69b | 2012-08-02 23:41:49 +0000 | [diff] [blame] | 914 | urlretrieve(src, dst) |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 915 | os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) |
| 916 | except Exception: |
| 917 | if os.path.exists(dst): |
| 918 | os.remove(dst) |
| 919 | DieWithError('\nFailed to download hooks from %s' % src) |
| 920 | |
| 921 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 922 | @usage('[repo root containing codereview.settings]') |
| 923 | def CMDconfig(parser, args): |
| 924 | """edit configuration for this tree""" |
| 925 | |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 926 | _, args = parser.parse_args(args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 927 | if len(args) == 0: |
| 928 | GetCodereviewSettingsInteractively() |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 929 | DownloadHooks(True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 930 | return 0 |
| 931 | |
| 932 | url = args[0] |
| 933 | if not url.endswith('codereview.settings'): |
| 934 | url = os.path.join(url, 'codereview.settings') |
| 935 | |
| 936 | # Load code review settings and download hooks (if available). |
| 937 | LoadCodereviewSettingsFromFile(urllib2.urlopen(url)) |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 938 | DownloadHooks(True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 939 | return 0 |
| 940 | |
| 941 | |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 942 | def CMDbaseurl(parser, args): |
| 943 | """get or set base-url for this branch""" |
| 944 | branchref = RunGit(['symbolic-ref', 'HEAD']).strip() |
| 945 | branch = ShortBranchName(branchref) |
| 946 | _, args = parser.parse_args(args) |
| 947 | if not args: |
| 948 | print("Current base-url:") |
| 949 | return RunGit(['config', 'branch.%s.base-url' % branch], |
| 950 | error_ok=False).strip() |
| 951 | else: |
| 952 | print("Setting base-url to %s" % args[0]) |
| 953 | return RunGit(['config', 'branch.%s.base-url' % branch, args[0]], |
| 954 | error_ok=False).strip() |
| 955 | |
| 956 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 957 | def CMDstatus(parser, args): |
| 958 | """show status of changelists""" |
| 959 | parser.add_option('--field', |
| 960 | help='print only specific field (desc|id|patch|url)') |
| 961 | (options, args) = parser.parse_args(args) |
| 962 | |
| 963 | # TODO: maybe make show_branches a flag if necessary. |
| 964 | show_branches = not options.field |
| 965 | |
| 966 | if show_branches: |
| 967 | branches = RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads']) |
| 968 | if branches: |
| 969 | print 'Branches associated with reviews:' |
rch@chromium.org | 92d6716 | 2012-04-02 20:10:35 +0000 | [diff] [blame] | 970 | changes = (Changelist(branchref=b) for b in branches.splitlines()) |
| 971 | branches = dict((cl.GetBranch(), cl.GetIssue()) for cl in changes) |
| 972 | alignment = max(5, max(len(b) for b in branches)) |
| 973 | for branch in sorted(branches): |
| 974 | print " %*s: %s" % (alignment, branch, branches[branch]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 975 | |
| 976 | cl = Changelist() |
| 977 | if options.field: |
| 978 | if options.field.startswith('desc'): |
| 979 | print cl.GetDescription() |
| 980 | elif options.field == 'id': |
| 981 | issueid = cl.GetIssue() |
| 982 | if issueid: |
| 983 | print issueid |
| 984 | elif options.field == 'patch': |
| 985 | patchset = cl.GetPatchset() |
| 986 | if patchset: |
| 987 | print patchset |
| 988 | elif options.field == 'url': |
| 989 | url = cl.GetIssueURL() |
| 990 | if url: |
| 991 | print url |
| 992 | else: |
| 993 | print |
| 994 | print 'Current branch:', |
| 995 | if not cl.GetIssue(): |
| 996 | print 'no issue assigned.' |
| 997 | return 0 |
| 998 | print cl.GetBranch() |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 999 | print 'Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1000 | print 'Issue description:' |
| 1001 | print cl.GetDescription(pretty=True) |
| 1002 | return 0 |
| 1003 | |
| 1004 | |
| 1005 | @usage('[issue_number]') |
| 1006 | def CMDissue(parser, args): |
| 1007 | """Set or display the current code review issue number. |
| 1008 | |
| 1009 | Pass issue number 0 to clear the current issue. |
| 1010 | """ |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 1011 | _, args = parser.parse_args(args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1012 | |
| 1013 | cl = Changelist() |
| 1014 | if len(args) > 0: |
| 1015 | try: |
| 1016 | issue = int(args[0]) |
| 1017 | except ValueError: |
| 1018 | DieWithError('Pass a number to set the issue or none to list it.\n' |
| 1019 | 'Maybe you want to run git cl status?') |
| 1020 | cl.SetIssue(issue) |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1021 | print 'Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1022 | return 0 |
| 1023 | |
| 1024 | |
maruel@chromium.org | 9977a2e | 2012-06-06 22:30:56 +0000 | [diff] [blame] | 1025 | def CMDcomments(parser, args): |
| 1026 | """show review comments of the current changelist""" |
| 1027 | (_, args) = parser.parse_args(args) |
| 1028 | if args: |
| 1029 | parser.error('Unsupported argument: %s' % args) |
| 1030 | |
| 1031 | cl = Changelist() |
| 1032 | if cl.GetIssue(): |
| 1033 | data = cl.RpcServer().get_issue_properties(cl.GetIssue(), True) |
| 1034 | for message in sorted(data['messages'], key=lambda x: x['date']): |
| 1035 | print '\n%s %s' % (message['date'].split('.', 1)[0], message['sender']) |
| 1036 | if message['text'].strip(): |
| 1037 | print '\n'.join(' ' + l for l in message['text'].splitlines()) |
| 1038 | return 0 |
| 1039 | |
| 1040 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1041 | def CreateDescriptionFromLog(args): |
| 1042 | """Pulls out the commit log to use as a base for the CL description.""" |
| 1043 | log_args = [] |
| 1044 | if len(args) == 1 and not args[0].endswith('.'): |
| 1045 | log_args = [args[0] + '..'] |
| 1046 | elif len(args) == 1 and args[0].endswith('...'): |
| 1047 | log_args = [args[0][:-1]] |
| 1048 | elif len(args) == 2: |
| 1049 | log_args = [args[0] + '..' + args[1]] |
| 1050 | else: |
| 1051 | log_args = args[:] # Hope for the best! |
maruel@chromium.org | 373af80 | 2012-05-25 21:07:33 +0000 | [diff] [blame] | 1052 | return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1053 | |
| 1054 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1055 | def CMDpresubmit(parser, args): |
| 1056 | """run presubmit tests on the current changelist""" |
ilevy@chromium.org | 375a902 | 2013-01-07 01:12:05 +0000 | [diff] [blame] | 1057 | parser.add_option('-u', '--upload', action='store_true', |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1058 | help='Run upload hook instead of the push/dcommit hook') |
ilevy@chromium.org | 375a902 | 2013-01-07 01:12:05 +0000 | [diff] [blame] | 1059 | parser.add_option('-f', '--force', action='store_true', |
sbc@chromium.org | 495ad15 | 2012-09-04 23:07:42 +0000 | [diff] [blame] | 1060 | help='Run checks even if tree is dirty') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1061 | (options, args) = parser.parse_args(args) |
| 1062 | |
ukai@chromium.org | 259e468 | 2012-10-25 07:36:33 +0000 | [diff] [blame] | 1063 | if not options.force and is_dirty_git_tree('presubmit'): |
| 1064 | print 'use --force to check even if tree is dirty.' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1065 | return 1 |
| 1066 | |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1067 | cl = Changelist() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1068 | if args: |
| 1069 | base_branch = args[0] |
| 1070 | else: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1071 | # Default to diffing against the common ancestor of the upstream branch. |
| 1072 | base_branch = RunGit(['merge-base', cl.GetUpstreamBranch(), 'HEAD']).strip() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1073 | |
ilevy@chromium.org | 051ad0e | 2013-03-04 21:57:34 +0000 | [diff] [blame] | 1074 | cl.RunHook( |
| 1075 | committing=not options.upload, |
| 1076 | may_prompt=False, |
| 1077 | verbose=options.verbose, |
| 1078 | change=cl.GetChange(base_branch, None)) |
dpranke@chromium.org | 0a2bb37 | 2011-03-25 01:16:22 +0000 | [diff] [blame] | 1079 | return 0 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1080 | |
| 1081 | |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1082 | def AddChangeIdToCommitMessage(options, args): |
| 1083 | """Re-commits using the current message, assumes the commit hook is in |
| 1084 | place. |
| 1085 | """ |
| 1086 | log_desc = options.message or CreateDescriptionFromLog(args) |
| 1087 | git_command = ['commit', '--amend', '-m', log_desc] |
| 1088 | RunGit(git_command) |
| 1089 | new_log_desc = CreateDescriptionFromLog(args) |
| 1090 | if CHANGE_ID in new_log_desc: |
| 1091 | print 'git-cl: Added Change-Id to commit message.' |
| 1092 | else: |
| 1093 | print >> sys.stderr, 'ERROR: Gerrit commit-msg hook not available.' |
| 1094 | |
| 1095 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1096 | def GerritUpload(options, args, cl): |
| 1097 | """upload the current branch to gerrit.""" |
| 1098 | # We assume the remote called "origin" is the one we want. |
| 1099 | # It is probably not worthwhile to support different workflows. |
| 1100 | remote = 'origin' |
| 1101 | branch = 'master' |
| 1102 | if options.target_branch: |
| 1103 | branch = options.target_branch |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1104 | |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 1105 | log_desc = options.message or CreateDescriptionFromLog(args) |
sivachandra@chromium.org | aebe87f | 2012-10-22 20:34:21 +0000 | [diff] [blame] | 1106 | if CHANGE_ID not in log_desc: |
| 1107 | AddChangeIdToCommitMessage(options, args) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1108 | if options.reviewers: |
| 1109 | log_desc += '\nR=' + options.reviewers |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 1110 | change_desc = ChangeDescription(log_desc, options.reviewers) |
| 1111 | change_desc.ParseDescription() |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1112 | if change_desc.IsEmpty(): |
| 1113 | print "Description is empty; aborting." |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1114 | return 1 |
| 1115 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1116 | receive_options = [] |
| 1117 | cc = cl.GetCCList().split(',') |
| 1118 | if options.cc: |
| 1119 | cc += options.cc.split(',') |
| 1120 | cc = filter(None, cc) |
| 1121 | if cc: |
| 1122 | receive_options += ['--cc=' + email for email in cc] |
| 1123 | if change_desc.reviewers: |
| 1124 | reviewers = filter(None, change_desc.reviewers.split(',')) |
| 1125 | if reviewers: |
| 1126 | receive_options += ['--reviewer=' + email for email in reviewers] |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1127 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1128 | git_command = ['push'] |
| 1129 | if receive_options: |
ukai@chromium.org | 19bbfa2 | 2012-02-03 16:18:11 +0000 | [diff] [blame] | 1130 | git_command.append('--receive-pack=git receive-pack %s' % |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1131 | ' '.join(receive_options)) |
| 1132 | git_command += [remote, 'HEAD:refs/for/' + branch] |
| 1133 | RunGit(git_command) |
| 1134 | # TODO(ukai): parse Change-Id: and set issue number? |
| 1135 | return 0 |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1136 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1137 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1138 | def RietveldUpload(options, args, cl): |
| 1139 | """upload the patch to rietveld.""" |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1140 | upload_args = ['--assume_yes'] # Don't ask about untracked files. |
| 1141 | upload_args.extend(['--server', cl.GetRietveldServer()]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1142 | if options.emulate_svn_auto_props: |
| 1143 | upload_args.append('--emulate_svn_auto_props') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1144 | |
| 1145 | change_desc = None |
| 1146 | |
| 1147 | if cl.GetIssue(): |
rogerta@chromium.org | 420d3b8 | 2012-05-14 18:41:38 +0000 | [diff] [blame] | 1148 | if options.title: |
| 1149 | upload_args.extend(['--title', options.title]) |
| 1150 | elif options.message: |
| 1151 | # TODO(rogerta): for now, the -m option will also set the --title option |
| 1152 | # for upload.py. Soon this will be changed to set the --message option. |
| 1153 | # Will wait until people are used to typing -t instead of -m. |
| 1154 | upload_args.extend(['--title', options.message]) |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1155 | upload_args.extend(['--issue', str(cl.GetIssue())]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1156 | print ("This branch is associated with issue %s. " |
| 1157 | "Adding patch to that issue." % cl.GetIssue()) |
| 1158 | else: |
rogerta@chromium.org | 420d3b8 | 2012-05-14 18:41:38 +0000 | [diff] [blame] | 1159 | if options.title: |
| 1160 | upload_args.extend(['--title', options.title]) |
rogerta@chromium.org | 43e34f0 | 2013-03-25 14:52:48 +0000 | [diff] [blame] | 1161 | message = options.title or options.message or CreateDescriptionFromLog(args) |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 1162 | change_desc = ChangeDescription(message, options.reviewers) |
| 1163 | if not options.force: |
| 1164 | change_desc.Prompt() |
| 1165 | change_desc.ParseDescription() |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1166 | |
| 1167 | if change_desc.IsEmpty(): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1168 | print "Description is empty; aborting." |
| 1169 | return 1 |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1170 | |
maruel@chromium.org | 71e12a9 | 2012-02-14 02:34:15 +0000 | [diff] [blame] | 1171 | upload_args.extend(['--message', change_desc.description]) |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1172 | if change_desc.reviewers: |
| 1173 | upload_args.extend(['--reviewers', change_desc.reviewers]) |
maruel@chromium.org | a335365 | 2011-11-30 14:26:57 +0000 | [diff] [blame] | 1174 | if options.send_mail: |
| 1175 | if not change_desc.reviewers: |
| 1176 | DieWithError("Must specify reviewers to send email.") |
| 1177 | upload_args.append('--send_mail') |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 1178 | cc = ','.join(filter(None, (cl.GetCCList(), options.cc))) |
maruel@chromium.org | b2a7c33 | 2011-02-25 20:30:37 +0000 | [diff] [blame] | 1179 | if cc: |
| 1180 | upload_args.extend(['--cc', cc]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1181 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 1182 | upload_args.extend(['--git_similarity', str(options.similarity)]) |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 1183 | if not options.find_copies: |
| 1184 | upload_args.extend(['--git_no_find_copies']) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 1185 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1186 | # Include the upstream repo's URL in the change -- this is useful for |
| 1187 | # projects that have their source spread across multiple repos. |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 1188 | remote_url = cl.GetGitBaseUrlFromConfig() |
| 1189 | if not remote_url: |
| 1190 | if settings.GetIsGitSvn(): |
| 1191 | # URL is dependent on the current directory. |
| 1192 | data = RunGit(['svn', 'info'], cwd=settings.GetRoot()) |
| 1193 | if data: |
| 1194 | keys = dict(line.split(': ', 1) for line in data.splitlines() |
| 1195 | if ': ' in line) |
| 1196 | remote_url = keys.get('URL', None) |
| 1197 | else: |
| 1198 | if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch(): |
| 1199 | remote_url = (cl.GetRemoteUrl() + '@' |
| 1200 | + cl.GetUpstreamBranch().split('/')[-1]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1201 | if remote_url: |
| 1202 | upload_args.extend(['--base_url', remote_url]) |
| 1203 | |
| 1204 | try: |
ilevy@chromium.org | 8288019 | 2012-11-26 15:41:57 +0000 | [diff] [blame] | 1205 | upload_args = ['upload'] + upload_args + args |
| 1206 | logging.info('upload.RealMain(%s)', upload_args) |
| 1207 | issue, patchset = upload.RealMain(upload_args) |
maruel@chromium.org | 9ce0dff | 2011-04-04 17:56:50 +0000 | [diff] [blame] | 1208 | except KeyboardInterrupt: |
| 1209 | sys.exit(1) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1210 | except: |
| 1211 | # If we got an exception after the user typed a description for their |
| 1212 | # change, back up the description before re-raising. |
| 1213 | if change_desc: |
| 1214 | backup_path = os.path.expanduser(DESCRIPTION_BACKUP_FILE) |
| 1215 | print '\nGot exception while uploading -- saving description to %s\n' \ |
| 1216 | % backup_path |
| 1217 | backup_file = open(backup_path, 'w') |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1218 | backup_file.write(change_desc.description) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1219 | backup_file.close() |
| 1220 | raise |
| 1221 | |
| 1222 | if not cl.GetIssue(): |
| 1223 | cl.SetIssue(issue) |
| 1224 | cl.SetPatchset(patchset) |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1225 | |
| 1226 | if options.use_commit_queue: |
| 1227 | cl.SetFlag('commit', '1') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1228 | return 0 |
| 1229 | |
| 1230 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1231 | @usage('[args to "git diff"]') |
| 1232 | def CMDupload(parser, args): |
| 1233 | """upload the current changelist to codereview""" |
| 1234 | parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks', |
| 1235 | help='bypass upload presubmit hook') |
| 1236 | parser.add_option('-f', action='store_true', dest='force', |
| 1237 | help="force yes to questions (don't prompt)") |
rogerta@chromium.org | 420d3b8 | 2012-05-14 18:41:38 +0000 | [diff] [blame] | 1238 | parser.add_option('-m', dest='message', help='message for patchset') |
| 1239 | parser.add_option('-t', dest='title', help='title for patchset') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1240 | parser.add_option('-r', '--reviewers', |
| 1241 | help='reviewer email addresses') |
| 1242 | parser.add_option('--cc', |
| 1243 | help='cc email addresses') |
adamk@chromium.org | 36f4730 | 2013-04-05 01:08:31 +0000 | [diff] [blame] | 1244 | parser.add_option('-s', '--send-mail', action='store_true', |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1245 | help='send email to reviewer immediately') |
| 1246 | parser.add_option("--emulate_svn_auto_props", action="store_true", |
| 1247 | dest="emulate_svn_auto_props", |
| 1248 | help="Emulate Subversion's auto properties feature.") |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1249 | parser.add_option('-c', '--use-commit-queue', action='store_true', |
| 1250 | help='tell the commit queue to commit this patchset') |
ukai@chromium.org | 8ef7ab2 | 2012-11-28 04:24:52 +0000 | [diff] [blame] | 1251 | parser.add_option('--target_branch', |
| 1252 | help='When uploading to gerrit, remote branch to ' |
| 1253 | 'use for CL. Default: master') |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 1254 | add_git_similarity(parser) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1255 | (options, args) = parser.parse_args(args) |
| 1256 | |
ukai@chromium.org | 8ef7ab2 | 2012-11-28 04:24:52 +0000 | [diff] [blame] | 1257 | if options.target_branch and not settings.GetIsGerrit(): |
| 1258 | parser.error('Use --target_branch for non gerrit repository.') |
| 1259 | |
rogerta@chromium.org | 420d3b8 | 2012-05-14 18:41:38 +0000 | [diff] [blame] | 1260 | # Print warning if the user used the -m/--message argument. This will soon |
| 1261 | # change to -t/--title. |
| 1262 | if options.message: |
| 1263 | print >> sys.stderr, ( |
| 1264 | '\nWARNING: Use -t or --title to set the title of the patchset.\n' |
| 1265 | 'In the near future, -m or --message will send a message instead.\n' |
| 1266 | 'See http://goo.gl/JGg0Z for details.\n') |
maruel@chromium.org | 9977a2e | 2012-06-06 22:30:56 +0000 | [diff] [blame] | 1267 | |
ukai@chromium.org | 259e468 | 2012-10-25 07:36:33 +0000 | [diff] [blame] | 1268 | if is_dirty_git_tree('upload'): |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1269 | return 1 |
| 1270 | |
| 1271 | cl = Changelist() |
| 1272 | if args: |
| 1273 | # TODO(ukai): is it ok for gerrit case? |
| 1274 | base_branch = args[0] |
| 1275 | else: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1276 | # Default to diffing against common ancestor of upstream branch |
| 1277 | base_branch = RunGit(['merge-base', cl.GetUpstreamBranch(), 'HEAD']).strip() |
sbc@chromium.org | 5e07e06 | 2013-02-28 23:55:44 +0000 | [diff] [blame] | 1278 | args = [base_branch, 'HEAD'] |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1279 | |
ilevy@chromium.org | 051ad0e | 2013-03-04 21:57:34 +0000 | [diff] [blame] | 1280 | # Apply watchlists on upload. |
| 1281 | change = cl.GetChange(base_branch, None) |
| 1282 | watchlist = watchlists.Watchlists(change.RepositoryRoot()) |
| 1283 | files = [f.LocalPath() for f in change.AffectedFiles()] |
| 1284 | cl.SetWatchers(watchlist.GetWatchersForPaths(files)) |
| 1285 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1286 | if not options.bypass_hooks: |
ilevy@chromium.org | 051ad0e | 2013-03-04 21:57:34 +0000 | [diff] [blame] | 1287 | hook_results = cl.RunHook(committing=False, |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1288 | may_prompt=not options.force, |
| 1289 | verbose=options.verbose, |
ilevy@chromium.org | 051ad0e | 2013-03-04 21:57:34 +0000 | [diff] [blame] | 1290 | change=change) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1291 | if not hook_results.should_continue(): |
| 1292 | return 1 |
| 1293 | if not options.reviewers and hook_results.reviewers: |
| 1294 | options.reviewers = hook_results.reviewers |
| 1295 | |
koz@chromium.org | 5974d7a | 2013-04-02 20:50:37 +0000 | [diff] [blame] | 1296 | if cl.GetIssue(): |
| 1297 | latest_patchset = cl.GetMostRecentPatchset(cl.GetIssue()) |
| 1298 | local_patchset = cl.GetPatchset() |
dmikurube@chromium.org | 07d149f | 2013-04-03 11:40:23 +0000 | [diff] [blame] | 1299 | if latest_patchset and local_patchset and local_patchset != latest_patchset: |
koz@chromium.org | 5974d7a | 2013-04-02 20:50:37 +0000 | [diff] [blame] | 1300 | print ('The last upload made from this repository was patchset #%d but ' |
| 1301 | 'the most recent patchset on the server is #%d.' |
| 1302 | % (local_patchset, latest_patchset)) |
| 1303 | ask_for_data('About to upload; enter to confirm.') |
| 1304 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 1305 | print_stats(options.similarity, options.find_copies, args) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1306 | if settings.GetIsGerrit(): |
| 1307 | return GerritUpload(options, args, cl) |
rogerta@chromium.org | caa1655 | 2013-03-18 20:45:05 +0000 | [diff] [blame] | 1308 | ret = RietveldUpload(options, args, cl) |
| 1309 | if not ret: |
| 1310 | git_set_branch_value('last-upload-hash', RunGit(['rev-parse', 'HEAD'])) |
| 1311 | |
| 1312 | return ret |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1313 | |
| 1314 | |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1315 | def IsSubmoduleMergeCommit(ref): |
| 1316 | # When submodules are added to the repo, we expect there to be a single |
| 1317 | # non-git-svn merge commit at remote HEAD with a signature comment. |
| 1318 | pattern = '^SVN changes up to revision [0-9]*$' |
szager@chromium.org | e84b754 | 2012-06-15 21:26:58 +0000 | [diff] [blame] | 1319 | cmd = ['rev-list', '--merges', '--grep=%s' % pattern, '%s^!' % ref] |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1320 | return RunGit(cmd) != '' |
| 1321 | |
| 1322 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1323 | def SendUpstream(parser, args, cmd): |
| 1324 | """Common code for CmdPush and CmdDCommit |
| 1325 | |
| 1326 | Squashed commit into a single. |
| 1327 | Updates changelog with metadata (e.g. pointer to review). |
| 1328 | Pushes/dcommits the code upstream. |
| 1329 | Updates review and closes. |
| 1330 | """ |
| 1331 | parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks', |
| 1332 | help='bypass upload presubmit hook') |
| 1333 | parser.add_option('-m', dest='message', |
| 1334 | help="override review description") |
| 1335 | parser.add_option('-f', action='store_true', dest='force', |
| 1336 | help="force yes to questions (don't prompt)") |
| 1337 | parser.add_option('-c', dest='contributor', |
| 1338 | help="external contributor for patch (appended to " + |
| 1339 | "description and used as author for git). Should be " + |
| 1340 | "formatted as 'First Last <email@example.com>'") |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 1341 | add_git_similarity(parser) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1342 | (options, args) = parser.parse_args(args) |
| 1343 | cl = Changelist() |
| 1344 | |
| 1345 | if not args or cmd == 'push': |
| 1346 | # Default to merging against our best guess of the upstream branch. |
| 1347 | args = [cl.GetUpstreamBranch()] |
| 1348 | |
maruel@chromium.org | 13f623c | 2011-07-22 16:02:23 +0000 | [diff] [blame] | 1349 | if options.contributor: |
| 1350 | if not re.match('^.*\s<\S+@\S+>$', options.contributor): |
| 1351 | print "Please provide contibutor as 'First Last <email@example.com>'" |
| 1352 | return 1 |
| 1353 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1354 | base_branch = args[0] |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1355 | base_has_submodules = IsSubmoduleMergeCommit(base_branch) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1356 | |
ukai@chromium.org | 259e468 | 2012-10-25 07:36:33 +0000 | [diff] [blame] | 1357 | if is_dirty_git_tree(cmd): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1358 | return 1 |
| 1359 | |
| 1360 | # This rev-list syntax means "show all commits not in my branch that |
| 1361 | # are in base_branch". |
| 1362 | upstream_commits = RunGit(['rev-list', '^' + cl.GetBranchRef(), |
| 1363 | base_branch]).splitlines() |
| 1364 | if upstream_commits: |
| 1365 | print ('Base branch "%s" has %d commits ' |
| 1366 | 'not in this branch.' % (base_branch, len(upstream_commits))) |
| 1367 | print 'Run "git merge %s" before attempting to %s.' % (base_branch, cmd) |
| 1368 | return 1 |
| 1369 | |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1370 | # This is the revision `svn dcommit` will commit on top of. |
| 1371 | svn_head = RunGit(['log', '--grep=^git-svn-id:', '-1', |
| 1372 | '--pretty=format:%H']) |
| 1373 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1374 | if cmd == 'dcommit': |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1375 | # If the base_head is a submodule merge commit, the first parent of the |
| 1376 | # base_head should be a git-svn commit, which is what we're interested in. |
| 1377 | base_svn_head = base_branch |
| 1378 | if base_has_submodules: |
| 1379 | base_svn_head += '^1' |
| 1380 | |
| 1381 | extra_commits = RunGit(['rev-list', '^' + svn_head, base_svn_head]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1382 | if extra_commits: |
| 1383 | print ('This branch has %d additional commits not upstreamed yet.' |
| 1384 | % len(extra_commits.splitlines())) |
| 1385 | print ('Upstream "%s" or rebase this branch on top of the upstream trunk ' |
| 1386 | 'before attempting to %s.' % (base_branch, cmd)) |
| 1387 | return 1 |
| 1388 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1389 | base_branch = RunGit(['merge-base', base_branch, 'HEAD']).strip() |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 1390 | if not options.bypass_hooks: |
maruel@chromium.org | 13f623c | 2011-07-22 16:02:23 +0000 | [diff] [blame] | 1391 | author = None |
| 1392 | if options.contributor: |
| 1393 | author = re.search(r'\<(.*)\>', options.contributor).group(1) |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 1394 | hook_results = cl.RunHook( |
| 1395 | committing=True, |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 1396 | may_prompt=not options.force, |
| 1397 | verbose=options.verbose, |
ilevy@chromium.org | 051ad0e | 2013-03-04 21:57:34 +0000 | [diff] [blame] | 1398 | change=cl.GetChange(base_branch, author)) |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 1399 | if not hook_results.should_continue(): |
| 1400 | return 1 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1401 | |
| 1402 | if cmd == 'dcommit': |
| 1403 | # Check the tree status if the tree status URL is set. |
| 1404 | status = GetTreeStatus() |
| 1405 | if 'closed' == status: |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 1406 | print('The tree is closed. Please wait for it to reopen. Use ' |
| 1407 | '"git cl dcommit --bypass-hooks" to commit on a closed tree.') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1408 | return 1 |
| 1409 | elif 'unknown' == status: |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 1410 | print('Unable to determine tree status. Please verify manually and ' |
| 1411 | 'use "git cl dcommit --bypass-hooks" to commit on a closed tree.') |
maruel@chromium.org | ac63715 | 2012-01-16 14:19:54 +0000 | [diff] [blame] | 1412 | else: |
| 1413 | breakpad.SendStack( |
| 1414 | 'GitClHooksBypassedCommit', |
| 1415 | 'Issue %s/%s bypassed hook when committing' % |
maruel@chromium.org | 2e72bb1 | 2012-01-17 15:18:35 +0000 | [diff] [blame] | 1416 | (cl.GetRietveldServer(), cl.GetIssue()), |
| 1417 | verbose=False) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1418 | |
| 1419 | description = options.message |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 1420 | if not description and cl.GetIssue(): |
| 1421 | description = cl.GetDescription() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1422 | |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 1423 | if not description: |
erg@chromium.org | 1a17398 | 2012-08-29 20:43:05 +0000 | [diff] [blame] | 1424 | if not cl.GetIssue() and options.bypass_hooks: |
| 1425 | description = CreateDescriptionFromLog([base_branch]) |
| 1426 | else: |
| 1427 | print 'No description set.' |
| 1428 | print 'Visit %s/edit to set it.' % (cl.GetIssueURL()) |
| 1429 | return 1 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1430 | |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 1431 | if cl.GetIssue(): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1432 | description += "\n\nReview URL: %s" % cl.GetIssueURL() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1433 | |
| 1434 | if options.contributor: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1435 | description += "\nPatch from %s." % options.contributor |
| 1436 | print 'Description:', repr(description) |
| 1437 | |
| 1438 | branches = [base_branch, cl.GetBranchRef()] |
| 1439 | if not options.force: |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 1440 | print_stats(options.similarity, options.find_copies, branches) |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 1441 | ask_for_data('About to commit; enter to confirm.') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1442 | |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1443 | # We want to squash all this branch's commits into one commit with the proper |
| 1444 | # description. We do this by doing a "reset --soft" to the base branch (which |
| 1445 | # keeps the working copy the same), then dcommitting that. If origin/master |
| 1446 | # has a submodule merge commit, we'll also need to cherry-pick the squashed |
| 1447 | # commit onto a branch based on the git-svn head. |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1448 | MERGE_BRANCH = 'git-cl-commit' |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1449 | CHERRY_PICK_BRANCH = 'git-cl-cherry-pick' |
| 1450 | # Delete the branches if they exist. |
| 1451 | for branch in [MERGE_BRANCH, CHERRY_PICK_BRANCH]: |
| 1452 | showref_cmd = ['show-ref', '--quiet', '--verify', 'refs/heads/%s' % branch] |
| 1453 | result = RunGitWithCode(showref_cmd) |
| 1454 | if result[0] == 0: |
| 1455 | RunGit(['branch', '-D', branch]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1456 | |
| 1457 | # We might be in a directory that's present in this branch but not in the |
| 1458 | # trunk. Move up to the top of the tree so that git commands that expect a |
| 1459 | # valid CWD won't fail after we check out the merge branch. |
| 1460 | rel_base_path = RunGit(['rev-parse', '--show-cdup']).strip() |
| 1461 | if rel_base_path: |
| 1462 | os.chdir(rel_base_path) |
| 1463 | |
| 1464 | # Stuff our change into the merge branch. |
| 1465 | # We wrap in a try...finally block so if anything goes wrong, |
| 1466 | # we clean up the branches. |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 1467 | retcode = -1 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1468 | try: |
bauerb@chromium.org | b4a75c4 | 2011-03-08 08:35:38 +0000 | [diff] [blame] | 1469 | RunGit(['checkout', '-q', '-b', MERGE_BRANCH]) |
| 1470 | RunGit(['reset', '--soft', base_branch]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1471 | if options.contributor: |
| 1472 | RunGit(['commit', '--author', options.contributor, '-m', description]) |
| 1473 | else: |
| 1474 | RunGit(['commit', '-m', description]) |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1475 | if base_has_submodules: |
| 1476 | cherry_pick_commit = RunGit(['rev-list', 'HEAD^!']).rstrip() |
| 1477 | RunGit(['branch', CHERRY_PICK_BRANCH, svn_head]) |
| 1478 | RunGit(['checkout', CHERRY_PICK_BRANCH]) |
| 1479 | RunGit(['cherry-pick', cherry_pick_commit]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1480 | if cmd == 'push': |
| 1481 | # push the merge branch. |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1482 | remote, branch = cl.FetchUpstreamTuple(cl.GetBranch()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1483 | retcode, output = RunGitWithCode( |
| 1484 | ['push', '--porcelain', remote, 'HEAD:%s' % branch]) |
| 1485 | logging.debug(output) |
| 1486 | else: |
| 1487 | # dcommit the merge branch. |
bauerb@chromium.org | 2e64fa1 | 2011-05-05 11:13:44 +0000 | [diff] [blame] | 1488 | retcode, output = RunGitWithCode(['svn', 'dcommit', |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 1489 | '-C%s' % options.similarity, |
bauerb@chromium.org | 2e64fa1 | 2011-05-05 11:13:44 +0000 | [diff] [blame] | 1490 | '--no-rebase', '--rmdir']) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1491 | finally: |
| 1492 | # And then swap back to the original branch and clean up. |
| 1493 | RunGit(['checkout', '-q', cl.GetBranch()]) |
| 1494 | RunGit(['branch', '-D', MERGE_BRANCH]) |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 1495 | if base_has_submodules: |
| 1496 | RunGit(['branch', '-D', CHERRY_PICK_BRANCH]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1497 | |
| 1498 | if cl.GetIssue(): |
| 1499 | if cmd == 'dcommit' and 'Committed r' in output: |
| 1500 | revision = re.match('.*?\nCommitted r(\\d+)', output, re.DOTALL).group(1) |
| 1501 | elif cmd == 'push' and retcode == 0: |
maruel@chromium.org | df947ea | 2011-01-12 20:44:54 +0000 | [diff] [blame] | 1502 | match = (re.match(r'.*?([a-f0-9]{7})\.\.([a-f0-9]{7})$', l) |
| 1503 | for l in output.splitlines(False)) |
| 1504 | match = filter(None, match) |
| 1505 | if len(match) != 1: |
| 1506 | DieWithError("Couldn't parse ouput to extract the committed hash:\n%s" % |
| 1507 | output) |
| 1508 | revision = match[0].group(2) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1509 | else: |
| 1510 | return 1 |
| 1511 | viewvc_url = settings.GetViewVCUrl() |
| 1512 | if viewvc_url and revision: |
| 1513 | cl.description += ('\n\nCommitted: ' + viewvc_url + revision) |
cmp@chromium.org | c22ea4b | 2012-10-09 22:42:00 +0000 | [diff] [blame] | 1514 | elif revision: |
| 1515 | cl.description += ('\n\nCommitted: ' + revision) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1516 | print ('Closing issue ' |
| 1517 | '(you may be prompted for your codereview password)...') |
maruel@chromium.org | b021b32 | 2013-04-08 17:57:29 +0000 | [diff] [blame^] | 1518 | cl.UpdateDescription(cl.description) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1519 | cl.CloseIssue() |
iannucci@chromium.org | 16b5140 | 2013-02-17 05:33:36 +0000 | [diff] [blame] | 1520 | props = cl.RpcServer().get_issue_properties(cl.GetIssue(), False) |
sadrul@chromium.org | 34b5d82 | 2013-02-18 01:39:24 +0000 | [diff] [blame] | 1521 | patch_num = len(props['patchsets']) |
iannucci@chromium.org | 25a4ab4 | 2013-02-15 23:22:05 +0000 | [diff] [blame] | 1522 | comment = "Committed patchset #%d manually as r%s" % (patch_num, revision) |
iannucci@chromium.org | b85a316 | 2013-01-26 01:11:13 +0000 | [diff] [blame] | 1523 | comment += ' (presubmit successful).' if not options.bypass_hooks else '.' |
| 1524 | cl.RpcServer().add_comment(cl.GetIssue(), comment) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1525 | cl.SetIssue(0) |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 1526 | |
| 1527 | if retcode == 0: |
| 1528 | hook = POSTUPSTREAM_HOOK_PATTERN % cmd |
| 1529 | if os.path.isfile(hook): |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1530 | RunCommand([hook, base_branch], error_ok=True) |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 1531 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1532 | return 0 |
| 1533 | |
| 1534 | |
| 1535 | @usage('[upstream branch to apply against]') |
| 1536 | def CMDdcommit(parser, args): |
| 1537 | """commit the current changelist via git-svn""" |
| 1538 | if not settings.GetIsGitSvn(): |
thakis@chromium.org | cde3bb6 | 2011-01-20 01:16:14 +0000 | [diff] [blame] | 1539 | message = """This doesn't appear to be an SVN repository. |
| 1540 | If your project has a git mirror with an upstream SVN master, you probably need |
| 1541 | to run 'git svn init', see your project's git mirror documentation. |
| 1542 | If your project has a true writeable upstream repository, you probably want |
| 1543 | to run 'git cl push' instead. |
| 1544 | Choose wisely, if you get this wrong, your commit might appear to succeed but |
| 1545 | will instead be silently ignored.""" |
| 1546 | print(message) |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 1547 | ask_for_data('[Press enter to dcommit or ctrl-C to quit]') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1548 | return SendUpstream(parser, args, 'dcommit') |
| 1549 | |
| 1550 | |
| 1551 | @usage('[upstream branch to apply against]') |
| 1552 | def CMDpush(parser, args): |
| 1553 | """commit the current changelist via git""" |
| 1554 | if settings.GetIsGitSvn(): |
| 1555 | print('This appears to be an SVN repository.') |
| 1556 | print('Are you sure you didn\'t mean \'git cl dcommit\'?') |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 1557 | ask_for_data('[Press enter to push or ctrl-C to quit]') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1558 | return SendUpstream(parser, args, 'push') |
| 1559 | |
| 1560 | |
| 1561 | @usage('<patch url or issue id>') |
| 1562 | def CMDpatch(parser, args): |
| 1563 | """patch in a code review""" |
| 1564 | parser.add_option('-b', dest='newbranch', |
| 1565 | help='create a new branch off trunk for the patch') |
| 1566 | parser.add_option('-f', action='store_true', dest='force', |
| 1567 | help='with -b, clobber any existing branch') |
| 1568 | parser.add_option('--reject', action='store_true', dest='reject', |
| 1569 | help='allow failed patches and spew .rej files') |
| 1570 | parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit', |
| 1571 | help="don't commit after patch applies") |
| 1572 | (options, args) = parser.parse_args(args) |
| 1573 | if len(args) != 1: |
| 1574 | parser.print_help() |
| 1575 | return 1 |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 1576 | issue_arg = args[0] |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1577 | |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1578 | # TODO(maruel): Use apply_issue.py |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 1579 | # TODO(ukai): use gerrit-cherry-pick for gerrit repository? |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1580 | |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1581 | if issue_arg.isdigit(): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1582 | # Input is an issue id. Figure out the URL. |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 1583 | cl = Changelist() |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1584 | issue = int(issue_arg) |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 1585 | patchset = cl.GetMostRecentPatchset(issue) |
| 1586 | patch_data = cl.GetPatchSetDiff(issue, patchset) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1587 | else: |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 1588 | # Assume it's a URL to the patch. Default to https. |
| 1589 | issue_url = gclient_utils.UpgradeToHttps(issue_arg) |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 1590 | match = re.match(r'.*?/issue(\d+)_(\d+).diff', issue_url) |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 1591 | if not match: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1592 | DieWithError('Must pass an issue ID or full URL for ' |
| 1593 | '\'Download raw patch set\'') |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1594 | issue = int(match.group(1)) |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 1595 | patchset = int(match.group(2)) |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 1596 | patch_data = urllib2.urlopen(issue_arg).read() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1597 | |
| 1598 | if options.newbranch: |
| 1599 | if options.force: |
| 1600 | RunGit(['branch', '-D', options.newbranch], |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 1601 | stderr=subprocess2.PIPE, error_ok=True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1602 | RunGit(['checkout', '-b', options.newbranch, |
| 1603 | Changelist().GetUpstreamBranch()]) |
| 1604 | |
| 1605 | # Switch up to the top-level directory, if necessary, in preparation for |
| 1606 | # applying the patch. |
| 1607 | top = RunGit(['rev-parse', '--show-cdup']).strip() |
| 1608 | if top: |
| 1609 | os.chdir(top) |
| 1610 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1611 | # Git patches have a/ at the beginning of source paths. We strip that out |
| 1612 | # with a sed script rather than the -p flag to patch so we can feed either |
| 1613 | # Git or svn-style patches into the same apply command. |
| 1614 | # re.sub() should be used but flags=re.MULTILINE is only in python 2.7. |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 1615 | try: |
| 1616 | patch_data = subprocess2.check_output( |
| 1617 | ['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'], stdin=patch_data) |
| 1618 | except subprocess2.CalledProcessError: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1619 | DieWithError('Git patch mungling failed.') |
| 1620 | logging.info(patch_data) |
| 1621 | # We use "git apply" to apply the patch instead of "patch" so that we can |
| 1622 | # pick up file adds. |
| 1623 | # The --index flag means: also insert into the index (so we catch adds). |
| 1624 | cmd = ['git', 'apply', '--index', '-p0'] |
| 1625 | if options.reject: |
| 1626 | cmd.append('--reject') |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 1627 | try: |
| 1628 | subprocess2.check_call(cmd, stdin=patch_data, stdout=subprocess2.VOID) |
| 1629 | except subprocess2.CalledProcessError: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1630 | DieWithError('Failed to apply the patch') |
| 1631 | |
| 1632 | # If we had an issue, commit the current state and register the issue. |
| 1633 | if not options.nocommit: |
| 1634 | RunGit(['commit', '-m', 'patch from issue %s' % issue]) |
| 1635 | cl = Changelist() |
| 1636 | cl.SetIssue(issue) |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 1637 | cl.SetPatchset(patchset) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1638 | print "Committed patch." |
| 1639 | else: |
| 1640 | print "Patch applied to index." |
| 1641 | return 0 |
| 1642 | |
| 1643 | |
| 1644 | def CMDrebase(parser, args): |
| 1645 | """rebase current branch on top of svn repo""" |
| 1646 | # Provide a wrapper for git svn rebase to help avoid accidental |
| 1647 | # git svn dcommit. |
| 1648 | # It's the only command that doesn't use parser at all since we just defer |
| 1649 | # execution to git-svn. |
maruel@chromium.org | 7507557 | 2011-10-10 19:55:28 +0000 | [diff] [blame] | 1650 | return subprocess2.call(['git', 'svn', 'rebase'] + args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1651 | |
| 1652 | |
| 1653 | def GetTreeStatus(): |
| 1654 | """Fetches the tree status and returns either 'open', 'closed', |
| 1655 | 'unknown' or 'unset'.""" |
| 1656 | url = settings.GetTreeStatusUrl(error_ok=True) |
| 1657 | if url: |
| 1658 | status = urllib2.urlopen(url).read().lower() |
| 1659 | if status.find('closed') != -1 or status == '0': |
| 1660 | return 'closed' |
| 1661 | elif status.find('open') != -1 or status == '1': |
| 1662 | return 'open' |
| 1663 | return 'unknown' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1664 | return 'unset' |
| 1665 | |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1666 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1667 | def GetTreeStatusReason(): |
| 1668 | """Fetches the tree status from a json url and returns the message |
| 1669 | with the reason for the tree to be opened or closed.""" |
msb@chromium.org | bf1a7ba | 2011-02-01 16:21:46 +0000 | [diff] [blame] | 1670 | url = settings.GetTreeStatusUrl() |
| 1671 | json_url = urlparse.urljoin(url, '/current?format=json') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1672 | connection = urllib2.urlopen(json_url) |
| 1673 | status = json.loads(connection.read()) |
| 1674 | connection.close() |
| 1675 | return status['message'] |
| 1676 | |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 1677 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1678 | def CMDtree(parser, args): |
| 1679 | """show the status of the tree""" |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 1680 | _, args = parser.parse_args(args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1681 | status = GetTreeStatus() |
| 1682 | if 'unset' == status: |
| 1683 | print 'You must configure your tree status URL by running "git cl config".' |
| 1684 | return 2 |
| 1685 | |
| 1686 | print "The tree is %s" % status |
| 1687 | print |
| 1688 | print GetTreeStatusReason() |
| 1689 | if status != 'open': |
| 1690 | return 1 |
| 1691 | return 0 |
| 1692 | |
| 1693 | |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 1694 | def CMDtry(parser, args): |
| 1695 | """Triggers a try job through Rietveld.""" |
| 1696 | group = optparse.OptionGroup(parser, "Try job options") |
| 1697 | group.add_option( |
| 1698 | "-b", "--bot", action="append", |
| 1699 | help=("IMPORTANT: specify ONE builder per --bot flag. Use it multiple " |
| 1700 | "times to specify multiple builders. ex: " |
| 1701 | "'-bwin_rel:ui_tests,webkit_unit_tests -bwin_layout'. See " |
| 1702 | "the try server waterfall for the builders name and the tests " |
| 1703 | "available. Can also be used to specify gtest_filter, e.g. " |
| 1704 | "-bwin_rel:base_unittests:ValuesTest.*Value")) |
| 1705 | group.add_option( |
| 1706 | "-r", "--revision", |
| 1707 | help="Revision to use for the try job; default: the " |
| 1708 | "revision will be determined by the try server; see " |
| 1709 | "its waterfall for more info") |
| 1710 | group.add_option( |
| 1711 | "-c", "--clobber", action="store_true", default=False, |
| 1712 | help="Force a clobber before building; e.g. don't do an " |
| 1713 | "incremental build") |
| 1714 | group.add_option( |
| 1715 | "--project", |
| 1716 | help="Override which project to use. Projects are defined " |
| 1717 | "server-side to define what default bot set to use") |
| 1718 | group.add_option( |
| 1719 | "-t", "--testfilter", action="append", default=[], |
| 1720 | help=("Apply a testfilter to all the selected builders. Unless the " |
| 1721 | "builders configurations are similar, use multiple " |
| 1722 | "--bot <builder>:<test> arguments.")) |
| 1723 | group.add_option( |
| 1724 | "-n", "--name", help="Try job name; default to current branch name") |
| 1725 | parser.add_option_group(group) |
| 1726 | options, args = parser.parse_args(args) |
| 1727 | |
| 1728 | if args: |
| 1729 | parser.error('Unknown arguments: %s' % args) |
| 1730 | |
| 1731 | cl = Changelist() |
| 1732 | if not cl.GetIssue(): |
| 1733 | parser.error('Need to upload first') |
| 1734 | |
| 1735 | if not options.name: |
| 1736 | options.name = cl.GetBranch() |
| 1737 | |
| 1738 | # Process --bot and --testfilter. |
| 1739 | if not options.bot: |
| 1740 | # Get try slaves from PRESUBMIT.py files if not specified. |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1741 | change = cl.GetChange( |
| 1742 | RunGit(['merge-base', cl.GetUpstreamBranch(), 'HEAD']).strip(), |
| 1743 | None) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 1744 | options.bot = presubmit_support.DoGetTrySlaves( |
| 1745 | change, |
| 1746 | change.LocalPaths(), |
| 1747 | settings.GetRoot(), |
| 1748 | None, |
| 1749 | None, |
| 1750 | options.verbose, |
| 1751 | sys.stdout) |
| 1752 | if not options.bot: |
| 1753 | parser.error('No default try builder to try, use --bot') |
| 1754 | |
| 1755 | builders_and_tests = {} |
| 1756 | for bot in options.bot: |
| 1757 | if ':' in bot: |
| 1758 | builder, tests = bot.split(':', 1) |
| 1759 | builders_and_tests.setdefault(builder, []).extend(tests.split(',')) |
| 1760 | elif ',' in bot: |
| 1761 | parser.error('Specify one bot per --bot flag') |
| 1762 | else: |
| 1763 | builders_and_tests.setdefault(bot, []).append('defaulttests') |
| 1764 | |
| 1765 | if options.testfilter: |
| 1766 | forced_tests = sum((t.split(',') for t in options.testfilter), []) |
| 1767 | builders_and_tests = dict( |
| 1768 | (b, forced_tests) for b, t in builders_and_tests.iteritems() |
| 1769 | if t != ['compile']) |
| 1770 | |
ilevy@chromium.org | f3b2123 | 2012-09-24 20:48:55 +0000 | [diff] [blame] | 1771 | if any('triggered' in b for b in builders_and_tests): |
| 1772 | print >> sys.stderr, ( |
| 1773 | 'ERROR You are trying to send a job to a triggered bot. This type of' |
| 1774 | ' bot requires an\ninitial job from a parent (usually a builder). ' |
| 1775 | 'Instead send your job to the parent.\n' |
| 1776 | 'Bot list: %s' % builders_and_tests) |
| 1777 | return 1 |
| 1778 | |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 1779 | patchset = cl.GetPatchset() |
| 1780 | if not cl.GetPatchset(): |
binji@chromium.org | 0281f52 | 2012-09-14 13:37:59 +0000 | [diff] [blame] | 1781 | patchset = cl.GetMostRecentPatchset(cl.GetIssue()) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 1782 | |
| 1783 | cl.RpcServer().trigger_try_jobs( |
| 1784 | cl.GetIssue(), patchset, options.name, options.clobber, options.revision, |
| 1785 | builders_and_tests) |
maruel@chromium.org | 072d94b | 2012-09-20 19:20:08 +0000 | [diff] [blame] | 1786 | print('Tried jobs on:') |
| 1787 | length = max(len(builder) for builder in builders_and_tests) |
| 1788 | for builder in sorted(builders_and_tests): |
| 1789 | print ' %*s: %s' % (length, builder, ','.join(builders_and_tests[builder])) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 1790 | return 0 |
| 1791 | |
| 1792 | |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 1793 | @usage('[new upstream branch]') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1794 | def CMDupstream(parser, args): |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 1795 | """prints or sets the name of the upstream branch, if any""" |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 1796 | _, args = parser.parse_args(args) |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 1797 | if len(args) > 1: |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1798 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 1799 | return 0 |
| 1800 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1801 | cl = Changelist() |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 1802 | if args: |
| 1803 | # One arg means set upstream branch. |
| 1804 | RunGit(['branch', '--set-upstream', cl.GetBranch(), args[0]]) |
| 1805 | cl = Changelist() |
| 1806 | print "Upstream branch set to " + cl.GetUpstreamBranch() |
| 1807 | else: |
| 1808 | print cl.GetUpstreamBranch() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1809 | return 0 |
| 1810 | |
| 1811 | |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1812 | def CMDset_commit(parser, args): |
| 1813 | """set the commit bit""" |
| 1814 | _, args = parser.parse_args(args) |
| 1815 | if args: |
| 1816 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
| 1817 | cl = Changelist() |
| 1818 | cl.SetFlag('commit', '1') |
| 1819 | return 0 |
| 1820 | |
| 1821 | |
groby@chromium.org | 411034a | 2013-02-26 15:12:01 +0000 | [diff] [blame] | 1822 | def CMDset_close(parser, args): |
| 1823 | """close the issue""" |
| 1824 | _, args = parser.parse_args(args) |
| 1825 | if args: |
| 1826 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
| 1827 | cl = Changelist() |
| 1828 | # Ensure there actually is an issue to close. |
| 1829 | cl.GetDescription() |
| 1830 | cl.CloseIssue() |
| 1831 | return 0 |
| 1832 | |
| 1833 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1834 | def Command(name): |
| 1835 | return getattr(sys.modules[__name__], 'CMD' + name, None) |
| 1836 | |
| 1837 | |
| 1838 | def CMDhelp(parser, args): |
| 1839 | """print list of commands or help for a specific command""" |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 1840 | _, args = parser.parse_args(args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1841 | if len(args) == 1: |
| 1842 | return main(args + ['--help']) |
| 1843 | parser.print_help() |
| 1844 | return 0 |
| 1845 | |
| 1846 | |
| 1847 | def GenUsage(parser, command): |
| 1848 | """Modify an OptParse object with the function's documentation.""" |
| 1849 | obj = Command(command) |
| 1850 | more = getattr(obj, 'usage_more', '') |
| 1851 | if command == 'help': |
| 1852 | command = '<command>' |
| 1853 | else: |
| 1854 | # OptParser.description prefer nicely non-formatted strings. |
| 1855 | parser.description = re.sub('[\r\n ]{2,}', ' ', obj.__doc__) |
| 1856 | parser.set_usage('usage: %%prog %s [options] %s' % (command, more)) |
| 1857 | |
| 1858 | |
| 1859 | def main(argv): |
| 1860 | """Doesn't parse the arguments here, just find the right subcommand to |
| 1861 | execute.""" |
maruel@chromium.org | 82798cb | 2012-02-23 18:16:12 +0000 | [diff] [blame] | 1862 | if sys.hexversion < 0x02060000: |
| 1863 | print >> sys.stderr, ( |
| 1864 | '\nYour python version %s is unsupported, please upgrade.\n' % |
| 1865 | sys.version.split(' ', 1)[0]) |
| 1866 | return 2 |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 1867 | # Reload settings. |
| 1868 | global settings |
| 1869 | settings = Settings() |
| 1870 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1871 | # Do it late so all commands are listed. |
| 1872 | CMDhelp.usage_more = ('\n\nCommands are:\n' + '\n'.join([ |
| 1873 | ' %-10s %s' % (fn[3:], Command(fn[3:]).__doc__.split('\n')[0].strip()) |
| 1874 | for fn in dir(sys.modules[__name__]) if fn.startswith('CMD')])) |
| 1875 | |
| 1876 | # Create the option parse and add --verbose support. |
| 1877 | parser = optparse.OptionParser() |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1878 | parser.add_option( |
| 1879 | '-v', '--verbose', action='count', default=0, |
| 1880 | help='Use 2 times for more debugging info') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1881 | old_parser_args = parser.parse_args |
| 1882 | def Parse(args): |
| 1883 | options, args = old_parser_args(args) |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1884 | if options.verbose >= 2: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1885 | logging.basicConfig(level=logging.DEBUG) |
maruel@chromium.org | 899e1c1 | 2011-04-07 17:03:18 +0000 | [diff] [blame] | 1886 | elif options.verbose: |
| 1887 | logging.basicConfig(level=logging.INFO) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1888 | else: |
| 1889 | logging.basicConfig(level=logging.WARNING) |
| 1890 | return options, args |
| 1891 | parser.parse_args = Parse |
| 1892 | |
| 1893 | if argv: |
| 1894 | command = Command(argv[0]) |
| 1895 | if command: |
| 1896 | # "fix" the usage and the description now that we know the subcommand. |
| 1897 | GenUsage(parser, argv[0]) |
| 1898 | try: |
| 1899 | return command(parser, argv[1:]) |
| 1900 | except urllib2.HTTPError, e: |
| 1901 | if e.code != 500: |
| 1902 | raise |
| 1903 | DieWithError( |
| 1904 | ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' |
| 1905 | 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) |
| 1906 | |
| 1907 | # Not a known command. Default to help. |
| 1908 | GenUsage(parser, 'help') |
| 1909 | return CMDhelp(parser, argv) |
| 1910 | |
| 1911 | |
| 1912 | if __name__ == '__main__': |
maruel@chromium.org | 6f09cd9 | 2011-04-01 16:38:12 +0000 | [diff] [blame] | 1913 | fix_encoding.fix_encoding() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1914 | sys.exit(main(sys.argv[1:])) |