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