blob: 231b1c438821ddb83134bebe8a6ab3c4988f8119 [file] [log] [blame]
iannucci@chromium.org405b87e2015-11-12 18:08:34 +00001#!/usr/bin/env python
miket@chromium.org183df1a2012-01-04 19:44:55 +00002# Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@chromium.org725f1c32011-04-01 20:24:54 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00006# Copyright (C) 2008 Evan Martin <martine@danga.com>
7
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00008"""A git-command for integrating reviews on Rietveld and Gerrit."""
maruel@chromium.org725f1c32011-04-01 20:24:54 +00009
tapted@chromium.org6a0b07c2013-07-10 01:29:19 +000010from distutils.version import LooseVersion
calamity@chromium.orgffde55c2015-03-12 00:44:17 +000011from multiprocessing.pool import ThreadPool
thakis@chromium.org3421c992014-11-02 02:20:32 +000012import base64
rmistry@google.com2dd99862015-06-22 12:22:18 +000013import collections
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +000014import glob
sheyang@google.com6ebaf782015-05-12 19:17:54 +000015import httplib
maruel@chromium.org4f6852c2012-04-20 20:39:20 +000016import json
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000017import logging
calamity@chromium.orgcf197482016-04-29 20:15:53 +000018import multiprocessing
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000019import optparse
20import os
21import re
ukai@chromium.org78c4b982012-02-14 02:20:26 +000022import stat
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000023import sys
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000024import textwrap
sheyang@google.com6ebaf782015-05-12 19:17:54 +000025import time
26import traceback
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +000027import urllib
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000028import urllib2
maruel@chromium.org967c0a82013-06-17 22:52:24 +000029import urlparse
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +000030import uuid
thestig@chromium.org00858c82013-12-02 23:08:03 +000031import webbrowser
thakis@chromium.org3421c992014-11-02 02:20:32 +000032import zlib
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000033
34try:
maruel@chromium.orgc98c0c52011-04-06 13:39:43 +000035 import readline # pylint: disable=F0401,W0611
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000036except ImportError:
37 pass
38
maruel@chromium.org2e23ce32013-05-07 12:42:28 +000039from third_party import colorama
sheyang@google.com6ebaf782015-05-12 19:17:54 +000040from third_party import httplib2
maruel@chromium.org2a74d372011-03-29 19:05:50 +000041from third_party import upload
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +000042import auth
hinoka@chromium.orgfeb9e2a2015-09-25 19:11:09 +000043from luci_hacks import trigger_luci_job as luci_trigger
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000044import clang_format
tandrii@chromium.org71184c02016-01-13 15:18:44 +000045import commit_queue
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +000046import dart_format
iannucci@chromium.org596cd5c2016-04-04 21:34:39 +000047import setup_color
maruel@chromium.org6f09cd92011-04-01 16:38:12 +000048import fix_encoding
maruel@chromium.org0e0436a2011-10-25 13:32:41 +000049import gclient_utils
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +000050import gerrit_util
szager@chromium.org151ebcf2016-03-09 01:08:25 +000051import git_cache
iannucci@chromium.org9e849272014-04-04 00:31:55 +000052import git_common
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +000053import git_footers
piman@chromium.org336f9122014-09-04 02:16:55 +000054import owners
iannucci@chromium.org9e849272014-04-04 00:31:55 +000055import owners_finder
maruel@chromium.org2a74d372011-03-29 19:05:50 +000056import presubmit_support
maruel@chromium.orgcab38e92011-04-09 00:30:51 +000057import rietveld
maruel@chromium.org2a74d372011-03-29 19:05:50 +000058import scm
maruel@chromium.org0633fb42013-08-16 20:06:14 +000059import subcommand
maruel@chromium.org32f9f5e2011-09-14 13:41:47 +000060import subprocess2
maruel@chromium.org2a74d372011-03-29 19:05:50 +000061import watchlists
62
maruel@chromium.org0633fb42013-08-16 20:06:14 +000063__version__ = '1.0'
maruel@chromium.org2a74d372011-03-29 19:05:50 +000064
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +000065DEFAULT_SERVER = 'https://codereview.appspot.com'
maruel@chromium.org0ba7f962011-01-11 22:13:58 +000066POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000067DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup'
pgervais@chromium.orgd6617f32013-11-19 00:34:54 +000068GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingGit'
rmistry@google.comc68112d2015-03-03 12:48:06 +000069REFS_THAT_ALIAS_TO_OTHER_REFS = {
70 'refs/remotes/origin/lkgr': 'refs/remotes/origin/master',
71 'refs/remotes/origin/lkcr': 'refs/remotes/origin/master',
72}
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000073
thestig@chromium.org44202a22014-03-11 19:22:18 +000074# Valid extensions for files we want to lint.
75DEFAULT_LINT_REGEX = r"(.*\.cpp|.*\.cc|.*\.h)"
76DEFAULT_LINT_IGNORE_REGEX = r"$^"
77
maruel@chromium.org2e23ce32013-05-07 12:42:28 +000078# Shortcut since it quickly becomes redundant.
79Fore = colorama.Fore
maruel@chromium.org90541732011-04-01 17:54:18 +000080
maruel@chromium.orgddd59412011-11-30 14:20:38 +000081# Initialized in main()
82settings = None
83
84
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000085def DieWithError(message):
dpranke@chromium.org970c5222011-03-12 00:32:24 +000086 print >> sys.stderr, message
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000087 sys.exit(1)
88
89
thestig@chromium.org8b0553c2014-02-11 00:33:37 +000090def GetNoGitPagerEnv():
91 env = os.environ.copy()
92 # 'cat' is a magical git string that disables pagers on all platforms.
93 env['GIT_PAGER'] = 'cat'
94 return env
95
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +000096
bsep@chromium.org627d9002016-04-29 00:00:52 +000097def RunCommand(args, error_ok=False, error_message=None, shell=False, **kwargs):
chase@chromium.orgcc51cd02010-12-23 00:48:39 +000098 try:
bsep@chromium.org627d9002016-04-29 00:00:52 +000099 return subprocess2.check_output(args, shell=shell, **kwargs)
maruel@chromium.org78936cb2013-04-11 00:17:52 +0000100 except subprocess2.CalledProcessError as e:
101 logging.debug('Failed running %s', args)
maruel@chromium.org32f9f5e2011-09-14 13:41:47 +0000102 if not error_ok:
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000103 DieWithError(
maruel@chromium.org32f9f5e2011-09-14 13:41:47 +0000104 'Command "%s" failed.\n%s' % (
105 ' '.join(args), error_message or e.stdout or ''))
106 return e.stdout
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000107
108
109def RunGit(args, **kwargs):
maruel@chromium.org32f9f5e2011-09-14 13:41:47 +0000110 """Returns stdout."""
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000111 return RunCommand(['git'] + args, **kwargs)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000112
113
enne@chromium.org3b7e15c2014-01-21 17:44:47 +0000114def RunGitWithCode(args, suppress_stderr=False):
maruel@chromium.org32f9f5e2011-09-14 13:41:47 +0000115 """Returns return code and stdout."""
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000116 try:
enne@chromium.org3b7e15c2014-01-21 17:44:47 +0000117 if suppress_stderr:
118 stderr = subprocess2.VOID
119 else:
120 stderr = sys.stderr
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000121 out, code = subprocess2.communicate(['git'] + args,
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000122 env=GetNoGitPagerEnv(),
enne@chromium.org3b7e15c2014-01-21 17:44:47 +0000123 stdout=subprocess2.PIPE,
124 stderr=stderr)
szager@chromium.org9bb85e22012-06-13 20:28:23 +0000125 return code, out[0]
126 except ValueError:
127 # When the subprocess fails, it returns None. That triggers a ValueError
128 # when trying to unpack the return value into (out, code).
129 return 1, ''
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000130
131
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000132def RunGitSilent(args):
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +0000133 """Returns stdout, suppresses stderr and ignores the return code."""
bauerb@chromium.org27386dd2015-02-16 10:45:39 +0000134 return RunGitWithCode(args, suppress_stderr=True)[1]
135
136
tapted@chromium.org6a0b07c2013-07-10 01:29:19 +0000137def IsGitVersionAtLeast(min_version):
ilevy@chromium.orgcc56ee42013-07-10 22:16:29 +0000138 prefix = 'git version '
tapted@chromium.org6a0b07c2013-07-10 01:29:19 +0000139 version = RunGit(['--version']).strip()
ilevy@chromium.orgcc56ee42013-07-10 22:16:29 +0000140 return (version.startswith(prefix) and
141 LooseVersion(version[len(prefix):]) >= LooseVersion(min_version))
tapted@chromium.org6a0b07c2013-07-10 01:29:19 +0000142
143
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +0000144def BranchExists(branch):
145 """Return True if specified branch exists."""
146 code, _ = RunGitWithCode(['rev-parse', '--verify', branch],
147 suppress_stderr=True)
148 return not code
149
150
maruel@chromium.org90541732011-04-01 17:54:18 +0000151def ask_for_data(prompt):
152 try:
153 return raw_input(prompt)
154 except KeyboardInterrupt:
155 # Hide the exception.
156 sys.exit(1)
157
158
iannucci@chromium.org79540052012-10-19 23:15:26 +0000159def git_set_branch_value(key, value):
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000160 branch = GetCurrentBranch()
rogerta@chromium.orgcaa16552013-03-18 20:45:05 +0000161 if not branch:
162 return
163
164 cmd = ['config']
165 if isinstance(value, int):
166 cmd.append('--int')
167 git_key = 'branch.%s.%s' % (branch, key)
168 RunGit(cmd + [git_key, str(value)])
iannucci@chromium.org79540052012-10-19 23:15:26 +0000169
170
171def git_get_branch_default(key, default):
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000172 branch = GetCurrentBranch()
iannucci@chromium.org79540052012-10-19 23:15:26 +0000173 if branch:
174 git_key = 'branch.%s.%s' % (branch, key)
175 (_, stdout) = RunGitWithCode(['config', '--int', '--get', git_key])
176 try:
177 return int(stdout.strip())
178 except ValueError:
179 pass
180 return default
181
182
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000183def add_git_similarity(parser):
184 parser.add_option(
iannucci@chromium.org79540052012-10-19 23:15:26 +0000185 '--similarity', metavar='SIM', type='int', action='store',
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000186 help='Sets the percentage that a pair of files need to match in order to'
187 ' be considered copies (default 50)')
iannucci@chromium.org79540052012-10-19 23:15:26 +0000188 parser.add_option(
189 '--find-copies', action='store_true',
190 help='Allows git to look for copies.')
191 parser.add_option(
192 '--no-find-copies', action='store_false', dest='find_copies',
193 help='Disallows git from looking for copies.')
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000194
195 old_parser_args = parser.parse_args
196 def Parse(args):
197 options, args = old_parser_args(args)
198
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000199 if options.similarity is None:
iannucci@chromium.org79540052012-10-19 23:15:26 +0000200 options.similarity = git_get_branch_default('git-cl-similarity', 50)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000201 else:
iannucci@chromium.org79540052012-10-19 23:15:26 +0000202 print('Note: Saving similarity of %d%% in git config.'
203 % options.similarity)
204 git_set_branch_value('git-cl-similarity', options.similarity)
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000205
iannucci@chromium.org79540052012-10-19 23:15:26 +0000206 options.similarity = max(0, min(options.similarity, 100))
207
208 if options.find_copies is None:
209 options.find_copies = bool(
210 git_get_branch_default('git-find-copies', True))
211 else:
212 git_set_branch_value('git-find-copies', int(options.find_copies))
iannucci@chromium.org53937ba2012-10-02 18:20:43 +0000213
214 print('Using %d%% similarity for rename/copy detection. '
215 'Override with --similarity.' % options.similarity)
216
217 return options, args
218 parser.parse_args = Parse
219
220
machenbach@chromium.org45453142015-09-15 08:45:22 +0000221def _get_properties_from_options(options):
222 properties = dict(x.split('=', 1) for x in options.properties)
223 for key, val in properties.iteritems():
224 try:
225 properties[key] = json.loads(val)
226 except ValueError:
227 pass # If a value couldn't be evaluated, treat it as a string.
228 return properties
229
230
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000231def _prefix_master(master):
232 """Convert user-specified master name to full master name.
233
234 Buildbucket uses full master name(master.tryserver.chromium.linux) as bucket
235 name, while the developers always use shortened master name
236 (tryserver.chromium.linux) by stripping off the prefix 'master.'. This
237 function does the conversion for buildbucket migration.
238 """
239 prefix = 'master.'
240 if master.startswith(prefix):
241 return master
242 return '%s%s' % (prefix, master)
243
244
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +0000245def _buildbucket_retry(operation_name, http, *args, **kwargs):
246 """Retries requests to buildbucket service and returns parsed json content."""
247 try_count = 0
248 while True:
249 response, content = http.request(*args, **kwargs)
250 try:
251 content_json = json.loads(content)
252 except ValueError:
253 content_json = None
254
255 # Buildbucket could return an error even if status==200.
256 if content_json and content_json.get('error'):
nodir@chromium.orgbaff4e12016-03-08 00:33:57 +0000257 error = content_json.get('error')
258 if error.get('code') == 403:
259 raise BuildbucketResponseException(
260 'Access denied: %s' % error.get('message', ''))
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +0000261 msg = 'Error in response. Reason: %s. Message: %s.' % (
nodir@chromium.orgbaff4e12016-03-08 00:33:57 +0000262 error.get('reason', ''), error.get('message', ''))
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +0000263 raise BuildbucketResponseException(msg)
264
265 if response.status == 200:
266 if not content_json:
267 raise BuildbucketResponseException(
268 'Buildbucket returns invalid json content: %s.\n'
269 'Please file bugs at http://crbug.com, label "Infra-BuildBucket".' %
270 content)
271 return content_json
272 if response.status < 500 or try_count >= 2:
273 raise httplib2.HttpLib2Error(content)
274
275 # status >= 500 means transient failures.
276 logging.debug('Transient errors when %s. Will retry.', operation_name)
277 time.sleep(0.5 + 1.5*try_count)
278 try_count += 1
279 assert False, 'unreachable'
280
281
hinoka@chromium.orgfeb9e2a2015-09-25 19:11:09 +0000282def trigger_luci_job(changelist, masters, options):
283 """Send a job to run on LUCI."""
284 issue_props = changelist.GetIssueProperties()
285 issue = changelist.GetIssue()
286 patchset = changelist.GetMostRecentPatchset()
287 for builders_and_tests in sorted(masters.itervalues()):
tandrii@chromium.org3764fa22015-10-21 16:40:40 +0000288 # TODO(hinoka et al): add support for other properties.
289 # Currently, this completely ignores testfilter and other properties.
290 for builder in sorted(builders_and_tests):
hinoka@chromium.orgfeb9e2a2015-09-25 19:11:09 +0000291 luci_trigger.trigger(
292 builder, 'HEAD', issue, patchset, issue_props['project'])
293
294
machenbach@chromium.org45453142015-09-15 08:45:22 +0000295def trigger_try_jobs(auth_config, changelist, options, masters, category):
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000296 rietveld_url = settings.GetDefaultServerUrl()
297 rietveld_host = urlparse.urlparse(rietveld_url).hostname
298 authenticator = auth.get_authenticator_for_host(rietveld_host, auth_config)
299 http = authenticator.authorize(httplib2.Http())
300 http.force_exception_to_status_code = True
301 issue_props = changelist.GetIssueProperties()
302 issue = changelist.GetIssue()
303 patchset = changelist.GetMostRecentPatchset()
machenbach@chromium.org45453142015-09-15 08:45:22 +0000304 properties = _get_properties_from_options(options)
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000305
306 buildbucket_put_url = (
307 'https://{hostname}/_ah/api/buildbucket/v1/builds/batch'.format(
sheyang@chromium.orgdb375572015-08-17 19:22:23 +0000308 hostname=options.buildbucket_host))
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000309 buildset = 'patch/rietveld/{hostname}/{issue}/{patch}'.format(
310 hostname=rietveld_host,
311 issue=issue,
312 patch=patchset)
313
314 batch_req_body = {'builds': []}
315 print_text = []
316 print_text.append('Tried jobs on:')
317 for master, builders_and_tests in sorted(masters.iteritems()):
318 print_text.append('Master: %s' % master)
319 bucket = _prefix_master(master)
320 for builder, tests in sorted(builders_and_tests.iteritems()):
321 print_text.append(' %s: %s' % (builder, tests))
322 parameters = {
323 'builder_name': builder,
nodir@chromium.orgd2217312015-09-21 15:51:21 +0000324 'changes': [{
325 'author': {'email': issue_props['owner_email']},
326 'revision': options.revision,
327 }],
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000328 'properties': {
329 'category': category,
330 'issue': issue,
331 'master': master,
332 'patch_project': issue_props['project'],
333 'patch_storage': 'rietveld',
334 'patchset': patchset,
335 'reason': options.name,
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000336 'rietveld': rietveld_url,
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000337 },
338 }
machenbach@chromium.org2403e802016-04-29 12:34:42 +0000339 if 'presubmit' in builder.lower():
340 parameters['properties']['dry_run'] = 'true'
tandrii@chromium.org3764fa22015-10-21 16:40:40 +0000341 if tests:
342 parameters['properties']['testfilter'] = tests
machenbach@chromium.org45453142015-09-15 08:45:22 +0000343 if properties:
344 parameters['properties'].update(properties)
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000345 if options.clobber:
346 parameters['properties']['clobber'] = True
347 batch_req_body['builds'].append(
348 {
349 'bucket': bucket,
350 'parameters_json': json.dumps(parameters),
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +0000351 'client_operation_id': str(uuid.uuid4()),
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000352 'tags': ['builder:%s' % builder,
353 'buildset:%s' % buildset,
354 'master:%s' % master,
355 'user_agent:git_cl_try']
356 }
357 )
358
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +0000359 _buildbucket_retry(
360 'triggering tryjobs',
361 http,
362 buildbucket_put_url,
363 'PUT',
364 body=json.dumps(batch_req_body),
365 headers={'Content-Type': 'application/json'}
366 )
tandrii@chromium.org35c61452016-02-26 15:24:57 +0000367 print_text.append('To see results here, run: git cl try-results')
368 print_text.append('To see results in browser, run: git cl web')
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000369 print '\n'.join(print_text)
kjellander@chromium.org44424542015-06-02 18:35:29 +0000370
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000371
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +0000372def fetch_try_jobs(auth_config, changelist, options):
373 """Fetches tryjobs from buildbucket.
374
375 Returns a map from build id to build info as json dictionary.
376 """
377 rietveld_url = settings.GetDefaultServerUrl()
378 rietveld_host = urlparse.urlparse(rietveld_url).hostname
379 authenticator = auth.get_authenticator_for_host(rietveld_host, auth_config)
380 if authenticator.has_cached_credentials():
381 http = authenticator.authorize(httplib2.Http())
382 else:
383 print ('Warning: Some results might be missing because %s' %
384 # Get the message on how to login.
385 auth.LoginRequiredError(rietveld_host).message)
386 http = httplib2.Http()
387
388 http.force_exception_to_status_code = True
389
390 buildset = 'patch/rietveld/{hostname}/{issue}/{patch}'.format(
391 hostname=rietveld_host,
392 issue=changelist.GetIssue(),
393 patch=options.patchset)
394 params = {'tag': 'buildset:%s' % buildset}
395
396 builds = {}
397 while True:
398 url = 'https://{hostname}/_ah/api/buildbucket/v1/search?{params}'.format(
399 hostname=options.buildbucket_host,
400 params=urllib.urlencode(params))
401 content = _buildbucket_retry('fetching tryjobs', http, url, 'GET')
402 for build in content.get('builds', []):
403 builds[build['id']] = build
404 if 'next_cursor' in content:
405 params['start_cursor'] = content['next_cursor']
406 else:
407 break
408 return builds
409
410
411def print_tryjobs(options, builds):
412 """Prints nicely result of fetch_try_jobs."""
413 if not builds:
414 print 'No tryjobs scheduled'
415 return
416
417 # Make a copy, because we'll be modifying builds dictionary.
418 builds = builds.copy()
419 builder_names_cache = {}
420
421 def get_builder(b):
422 try:
423 return builder_names_cache[b['id']]
424 except KeyError:
425 try:
426 parameters = json.loads(b['parameters_json'])
427 name = parameters['builder_name']
428 except (ValueError, KeyError) as error:
429 print 'WARNING: failed to get builder name for build %s: %s' % (
430 b['id'], error)
431 name = None
432 builder_names_cache[b['id']] = name
433 return name
434
435 def get_bucket(b):
436 bucket = b['bucket']
437 if bucket.startswith('master.'):
438 return bucket[len('master.'):]
439 return bucket
440
441 if options.print_master:
442 name_fmt = '%%-%ds %%-%ds' % (
443 max(len(str(get_bucket(b))) for b in builds.itervalues()),
444 max(len(str(get_builder(b))) for b in builds.itervalues()))
445 def get_name(b):
446 return name_fmt % (get_bucket(b), get_builder(b))
447 else:
448 name_fmt = '%%-%ds' % (
449 max(len(str(get_builder(b))) for b in builds.itervalues()))
450 def get_name(b):
451 return name_fmt % get_builder(b)
452
453 def sort_key(b):
454 return b['status'], b.get('result'), get_name(b), b.get('url')
455
456 def pop(title, f, color=None, **kwargs):
457 """Pop matching builds from `builds` dict and print them."""
458
tandrii@chromium.org6cf98c82016-03-15 11:56:00 +0000459 if not options.color or color is None:
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +0000460 colorize = str
461 else:
462 colorize = lambda x: '%s%s%s' % (color, x, Fore.RESET)
463
464 result = []
465 for b in builds.values():
466 if all(b.get(k) == v for k, v in kwargs.iteritems()):
467 builds.pop(b['id'])
468 result.append(b)
469 if result:
470 print colorize(title)
471 for b in sorted(result, key=sort_key):
472 print ' ', colorize('\t'.join(map(str, f(b))))
473
474 total = len(builds)
475 pop(status='COMPLETED', result='SUCCESS',
476 title='Successes:', color=Fore.GREEN,
477 f=lambda b: (get_name(b), b.get('url')))
478 pop(status='COMPLETED', result='FAILURE', failure_reason='INFRA_FAILURE',
479 title='Infra Failures:', color=Fore.MAGENTA,
480 f=lambda b: (get_name(b), b.get('url')))
481 pop(status='COMPLETED', result='FAILURE', failure_reason='BUILD_FAILURE',
482 title='Failures:', color=Fore.RED,
483 f=lambda b: (get_name(b), b.get('url')))
484 pop(status='COMPLETED', result='CANCELED',
485 title='Canceled:', color=Fore.MAGENTA,
486 f=lambda b: (get_name(b),))
487 pop(status='COMPLETED', result='FAILURE',
488 failure_reason='INVALID_BUILD_DEFINITION',
489 title='Wrong master/builder name:', color=Fore.MAGENTA,
490 f=lambda b: (get_name(b),))
491 pop(status='COMPLETED', result='FAILURE',
492 title='Other failures:',
493 f=lambda b: (get_name(b), b.get('failure_reason'), b.get('url')))
494 pop(status='COMPLETED',
495 title='Other finished:',
496 f=lambda b: (get_name(b), b.get('result'), b.get('url')))
497 pop(status='STARTED',
498 title='Started:', color=Fore.YELLOW,
499 f=lambda b: (get_name(b), b.get('url')))
500 pop(status='SCHEDULED',
501 title='Scheduled:',
502 f=lambda b: (get_name(b), 'id=%s' % b['id']))
503 # The last section is just in case buildbucket API changes OR there is a bug.
504 pop(title='Other:',
505 f=lambda b: (get_name(b), 'id=%s' % b['id']))
506 assert len(builds) == 0
507 print 'Total: %d tryjobs' % total
508
509
bauerb@chromium.org866276c2011-03-18 20:09:31 +0000510def MatchSvnGlob(url, base_url, glob_spec, allow_wildcards):
511 """Return the corresponding git ref if |base_url| together with |glob_spec|
512 matches the full |url|.
513
514 If |allow_wildcards| is true, |glob_spec| can contain wildcards (see below).
515 """
516 fetch_suburl, as_ref = glob_spec.split(':')
517 if allow_wildcards:
518 glob_match = re.match('(.+/)?(\*|{[^/]*})(/.+)?', fetch_suburl)
519 if glob_match:
520 # Parse specs like "branches/*/src:refs/remotes/svn/*" or
521 # "branches/{472,597,648}/src:refs/remotes/svn/*".
522 branch_re = re.escape(base_url)
523 if glob_match.group(1):
524 branch_re += '/' + re.escape(glob_match.group(1))
525 wildcard = glob_match.group(2)
526 if wildcard == '*':
527 branch_re += '([^/]*)'
528 else:
529 # Escape and replace surrounding braces with parentheses and commas
530 # with pipe symbols.
531 wildcard = re.escape(wildcard)
532 wildcard = re.sub('^\\\\{', '(', wildcard)
533 wildcard = re.sub('\\\\,', '|', wildcard)
534 wildcard = re.sub('\\\\}$', ')', wildcard)
535 branch_re += wildcard
536 if glob_match.group(3):
537 branch_re += re.escape(glob_match.group(3))
538 match = re.match(branch_re, url)
539 if match:
540 return re.sub('\*$', match.group(1), as_ref)
541
542 # Parse specs like "trunk/src:refs/remotes/origin/trunk".
543 if fetch_suburl:
544 full_url = base_url + '/' + fetch_suburl
545 else:
546 full_url = base_url
547 if full_url == url:
548 return as_ref
549 return None
550
maruel@chromium.org32f9f5e2011-09-14 13:41:47 +0000551
iannucci@chromium.org79540052012-10-19 23:15:26 +0000552def print_stats(similarity, find_copies, args):
maruel@chromium.org49e3d802012-07-18 23:54:45 +0000553 """Prints statistics about the change to the user."""
554 # --no-ext-diff is broken in some versions of Git, so try to work around
555 # this by overriding the environment (but there is still a problem if the
556 # git config key "diff.external" is used).
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000557 env = GetNoGitPagerEnv()
maruel@chromium.org49e3d802012-07-18 23:54:45 +0000558 if 'GIT_EXTERNAL_DIFF' in env:
559 del env['GIT_EXTERNAL_DIFF']
iannucci@chromium.org79540052012-10-19 23:15:26 +0000560
561 if find_copies:
562 similarity_options = ['--find-copies-harder', '-l100000',
563 '-C%s' % similarity]
564 else:
565 similarity_options = ['-M%s' % similarity]
566
szager@chromium.orgd057f9a2014-05-29 21:09:36 +0000567 try:
568 stdout = sys.stdout.fileno()
569 except AttributeError:
570 stdout = None
maruel@chromium.org49e3d802012-07-18 23:54:45 +0000571 return subprocess2.call(
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000572 ['git',
bratell@opera.comf267b0e2013-05-02 09:11:43 +0000573 'diff', '--no-ext-diff', '--stat'] + similarity_options + args,
szager@chromium.orgd057f9a2014-05-29 21:09:36 +0000574 stdout=stdout, env=env)
maruel@chromium.org49e3d802012-07-18 23:54:45 +0000575
576
sheyang@google.com6ebaf782015-05-12 19:17:54 +0000577class BuildbucketResponseException(Exception):
578 pass
579
580
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000581class Settings(object):
582 def __init__(self):
583 self.default_server = None
584 self.cc = None
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000585 self.root = None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000586 self.is_git_svn = None
587 self.svn_branch = None
588 self.tree_status_url = None
589 self.viewvc_url = None
590 self.updated = False
ukai@chromium.orge8077812012-02-03 03:41:46 +0000591 self.is_gerrit = None
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000592 self.squash_gerrit_uploads = None
tandrii@chromium.org28253532016-04-14 13:46:56 +0000593 self.gerrit_skip_ensure_authenticated = None
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000594 self.git_editor = None
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000595 self.project = None
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000596 self.force_https_commit_url = None
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000597 self.pending_ref_prefix = None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000598
599 def LazyUpdateIfNeeded(self):
600 """Updates the settings from a codereview.settings file, if available."""
601 if not self.updated:
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000602 # The only value that actually changes the behavior is
603 # autoupdate = "false". Everything else means "true".
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +0000604 autoupdate = RunGit(['config', 'rietveld.autoupdate'],
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000605 error_ok=True
606 ).strip().lower()
607
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000608 cr_settings_file = FindCodereviewSettingsFile()
pgervais@chromium.org87884cc2014-01-03 22:23:41 +0000609 if autoupdate != 'false' and cr_settings_file:
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000610 LoadCodereviewSettingsFromFile(cr_settings_file)
611 self.updated = True
612
613 def GetDefaultServerUrl(self, error_ok=False):
614 if not self.default_server:
615 self.LazyUpdateIfNeeded()
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000616 self.default_server = gclient_utils.UpgradeToHttps(
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000617 self._GetRietveldConfig('server', error_ok=True))
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000618 if error_ok:
619 return self.default_server
620 if not self.default_server:
621 error_message = ('Could not find settings file. You must configure '
622 'your review setup by running "git cl config".')
maruel@chromium.orgeb5edbc2012-01-16 17:03:28 +0000623 self.default_server = gclient_utils.UpgradeToHttps(
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000624 self._GetRietveldConfig('server', error_message=error_message))
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000625 return self.default_server
626
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000627 @staticmethod
628 def GetRelativeRoot():
629 return RunGit(['rev-parse', '--show-cdup']).strip()
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000630
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000631 def GetRoot(self):
thestig@chromium.org7a54e812014-02-11 19:57:22 +0000632 if self.root is None:
633 self.root = os.path.abspath(self.GetRelativeRoot())
634 return self.root
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000635
szager@chromium.org151ebcf2016-03-09 01:08:25 +0000636 def GetGitMirror(self, remote='origin'):
637 """If this checkout is from a local git mirror, return a Mirror object."""
szager@chromium.org81593742016-03-09 20:27:58 +0000638 local_url = RunGit(['config', '--get', 'remote.%s.url' % remote]).strip()
szager@chromium.org151ebcf2016-03-09 01:08:25 +0000639 if not os.path.isdir(local_url):
640 return None
641 git_cache.Mirror.SetCachePath(os.path.dirname(local_url))
642 remote_url = git_cache.Mirror.CacheDirToUrl(local_url)
643 # Use the /dev/null print_func to avoid terminal spew in WaitForRealCommit.
644 mirror = git_cache.Mirror(remote_url, print_func = lambda *args: None)
645 if mirror.exists():
646 return mirror
647 return None
648
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000649 def GetIsGitSvn(self):
650 """Return true if this repo looks like it's using git-svn."""
651 if self.is_git_svn is None:
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000652 if self.GetPendingRefPrefix():
653 # If PENDING_REF_PREFIX is set then it's a pure git repo no matter what.
654 self.is_git_svn = False
655 else:
656 # If you have any "svn-remote.*" config keys, we think you're using svn.
657 self.is_git_svn = RunGitWithCode(
658 ['config', '--local', '--get-regexp', r'^svn-remote\.'])[0] == 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000659 return self.is_git_svn
660
661 def GetSVNBranch(self):
662 if self.svn_branch is None:
663 if not self.GetIsGitSvn():
664 DieWithError('Repo doesn\'t appear to be a git-svn repo.')
665
666 # Try to figure out which remote branch we're based on.
667 # Strategy:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000668 # 1) iterate through our branch history and find the svn URL.
669 # 2) find the svn-remote that fetches from the URL.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000670
671 # regexp matching the git-svn line that contains the URL.
672 git_svn_re = re.compile(r'^\s*git-svn-id: (\S+)@', re.MULTILINE)
673
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000674 # We don't want to go through all of history, so read a line from the
675 # pipe at a time.
676 # The -100 is an arbitrary limit so we don't search forever.
bratell@opera.com82b91cd2013-07-09 06:33:41 +0000677 cmd = ['git', 'log', '-100', '--pretty=medium']
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000678 proc = subprocess2.Popen(cmd, stdout=subprocess2.PIPE,
679 env=GetNoGitPagerEnv())
maruel@chromium.org740f9d72011-06-10 18:33:10 +0000680 url = None
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000681 for line in proc.stdout:
682 match = git_svn_re.match(line)
683 if match:
684 url = match.group(1)
685 proc.stdout.close() # Cut pipe.
686 break
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000687
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000688 if url:
689 svn_remote_re = re.compile(r'^svn-remote\.([^.]+)\.url (.*)$')
690 remotes = RunGit(['config', '--get-regexp',
691 r'^svn-remote\..*\.url']).splitlines()
692 for remote in remotes:
693 match = svn_remote_re.match(remote)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000694 if match:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000695 remote = match.group(1)
696 base_url = match.group(2)
szager@chromium.org4ac25532013-12-16 22:07:02 +0000697 rewrite_root = RunGit(
698 ['config', 'svn-remote.%s.rewriteRoot' % remote],
699 error_ok=True).strip()
700 if rewrite_root:
701 base_url = rewrite_root
bauerb@chromium.orgade368c2011-03-01 08:57:50 +0000702 fetch_spec = RunGit(
bauerb@chromium.org866276c2011-03-18 20:09:31 +0000703 ['config', 'svn-remote.%s.fetch' % remote],
704 error_ok=True).strip()
705 if fetch_spec:
706 self.svn_branch = MatchSvnGlob(url, base_url, fetch_spec, False)
707 if self.svn_branch:
708 break
709 branch_spec = RunGit(
710 ['config', 'svn-remote.%s.branches' % remote],
711 error_ok=True).strip()
712 if branch_spec:
713 self.svn_branch = MatchSvnGlob(url, base_url, branch_spec, True)
714 if self.svn_branch:
715 break
716 tag_spec = RunGit(
717 ['config', 'svn-remote.%s.tags' % remote],
718 error_ok=True).strip()
719 if tag_spec:
720 self.svn_branch = MatchSvnGlob(url, base_url, tag_spec, True)
721 if self.svn_branch:
722 break
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000723
724 if not self.svn_branch:
725 DieWithError('Can\'t guess svn branch -- try specifying it on the '
726 'command line')
727
728 return self.svn_branch
729
730 def GetTreeStatusUrl(self, error_ok=False):
731 if not self.tree_status_url:
732 error_message = ('You must configure your tree status URL by running '
733 '"git cl config".')
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000734 self.tree_status_url = self._GetRietveldConfig(
735 'tree-status-url', error_ok=error_ok, error_message=error_message)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000736 return self.tree_status_url
737
738 def GetViewVCUrl(self):
739 if not self.viewvc_url:
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000740 self.viewvc_url = self._GetRietveldConfig('viewvc-url', error_ok=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000741 return self.viewvc_url
742
rmistry@google.com90752582014-01-14 21:04:50 +0000743 def GetBugPrefix(self):
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000744 return self._GetRietveldConfig('bug-prefix', error_ok=True)
rmistry@google.com90752582014-01-14 21:04:50 +0000745
rmistry@google.com78948ed2015-07-08 23:09:57 +0000746 def GetIsSkipDependencyUpload(self, branch_name):
747 """Returns true if specified branch should skip dep uploads."""
748 return self._GetBranchConfig(branch_name, 'skip-deps-uploads',
749 error_ok=True)
750
rmistry@google.com5626a922015-02-26 14:03:30 +0000751 def GetRunPostUploadHook(self):
752 run_post_upload_hook = self._GetRietveldConfig(
753 'run-post-upload-hook', error_ok=True)
754 return run_post_upload_hook == "True"
755
bauerb@chromium.orgae6df352011-04-06 17:40:39 +0000756 def GetDefaultCCList(self):
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000757 return self._GetRietveldConfig('cc', error_ok=True)
bauerb@chromium.orgae6df352011-04-06 17:40:39 +0000758
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000759 def GetDefaultPrivateFlag(self):
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000760 return self._GetRietveldConfig('private', error_ok=True)
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +0000761
ukai@chromium.orge8077812012-02-03 03:41:46 +0000762 def GetIsGerrit(self):
763 """Return true if this repo is assosiated with gerrit code review system."""
764 if self.is_gerrit is None:
765 self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True)
766 return self.is_gerrit
767
bauerb@chromium.org54b400c2016-01-14 10:08:25 +0000768 def GetSquashGerritUploads(self):
769 """Return true if uploads to Gerrit should be squashed by default."""
770 if self.squash_gerrit_uploads is None:
771 self.squash_gerrit_uploads = (
772 RunGit(['config', '--bool', 'gerrit.squash-uploads'],
773 error_ok=True).strip() == 'true')
774 return self.squash_gerrit_uploads
775
tandrii@chromium.org28253532016-04-14 13:46:56 +0000776 def GetGerritSkipEnsureAuthenticated(self):
777 """Return True if EnsureAuthenticated should not be done for Gerrit
778 uploads."""
779 if self.gerrit_skip_ensure_authenticated is None:
780 self.gerrit_skip_ensure_authenticated = (
shinyak@chromium.org00dbccd2016-04-15 07:24:43 +0000781 RunGit(['config', '--bool', 'gerrit.skip-ensure-authenticated'],
tandrii@chromium.org28253532016-04-14 13:46:56 +0000782 error_ok=True).strip() == 'true')
783 return self.gerrit_skip_ensure_authenticated
784
jbroman@chromium.org615a2622013-05-03 13:20:14 +0000785 def GetGitEditor(self):
786 """Return the editor specified in the git config, or None if none is."""
787 if self.git_editor is None:
788 self.git_editor = self._GetConfig('core.editor', error_ok=True)
789 return self.git_editor or None
790
thestig@chromium.org44202a22014-03-11 19:22:18 +0000791 def GetLintRegex(self):
792 return (self._GetRietveldConfig('cpplint-regex', error_ok=True) or
793 DEFAULT_LINT_REGEX)
794
795 def GetLintIgnoreRegex(self):
796 return (self._GetRietveldConfig('cpplint-ignore-regex', error_ok=True) or
797 DEFAULT_LINT_IGNORE_REGEX)
798
sheyang@chromium.org152cf832014-06-11 21:37:49 +0000799 def GetProject(self):
800 if not self.project:
801 self.project = self._GetRietveldConfig('project', error_ok=True)
802 return self.project
803
kjellander@chromium.org6abc6522014-12-02 07:34:49 +0000804 def GetForceHttpsCommitUrl(self):
805 if not self.force_https_commit_url:
806 self.force_https_commit_url = self._GetRietveldConfig(
807 'force-https-commit-url', error_ok=True)
808 return self.force_https_commit_url
809
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +0000810 def GetPendingRefPrefix(self):
811 if not self.pending_ref_prefix:
812 self.pending_ref_prefix = self._GetRietveldConfig(
813 'pending-ref-prefix', error_ok=True)
814 return self.pending_ref_prefix
815
thestig@chromium.org8b0553c2014-02-11 00:33:37 +0000816 def _GetRietveldConfig(self, param, **kwargs):
817 return self._GetConfig('rietveld.' + param, **kwargs)
818
rmistry@google.com78948ed2015-07-08 23:09:57 +0000819 def _GetBranchConfig(self, branch_name, param, **kwargs):
820 return self._GetConfig('branch.' + branch_name + '.' + param, **kwargs)
821
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000822 def _GetConfig(self, param, **kwargs):
823 self.LazyUpdateIfNeeded()
824 return RunGit(['config', param], **kwargs).strip()
825
826
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000827def ShortBranchName(branch):
828 """Convert a name like 'refs/heads/foo' to just 'foo'."""
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000829 return branch.replace('refs/heads/', '', 1)
830
831
832def GetCurrentBranchRef():
833 """Returns branch ref (e.g., refs/heads/master) or None."""
834 return RunGit(['symbolic-ref', 'HEAD'],
835 stderr=subprocess2.VOID, error_ok=True).strip() or None
836
837
838def GetCurrentBranch():
839 """Returns current branch or None.
840
841 For refs/heads/* branches, returns just last part. For others, full ref.
842 """
843 branchref = GetCurrentBranchRef()
844 if branchref:
845 return ShortBranchName(branchref)
846 return None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000847
848
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +0000849class _CQState(object):
850 """Enum for states of CL with respect to Commit Queue."""
851 NONE = 'none'
852 DRY_RUN = 'dry_run'
853 COMMIT = 'commit'
854
855 ALL_STATES = [NONE, DRY_RUN, COMMIT]
856
857
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +0000858class _ParsedIssueNumberArgument(object):
859 def __init__(self, issue=None, patchset=None, hostname=None):
860 self.issue = issue
861 self.patchset = patchset
862 self.hostname = hostname
863
864 @property
865 def valid(self):
866 return self.issue is not None
867
868
869class _RietveldParsedIssueNumberArgument(_ParsedIssueNumberArgument):
870 def __init__(self, *args, **kwargs):
871 self.patch_url = kwargs.pop('patch_url', None)
872 super(_RietveldParsedIssueNumberArgument, self).__init__(*args, **kwargs)
873
874
875def ParseIssueNumberArgument(arg):
876 """Parses the issue argument and returns _ParsedIssueNumberArgument."""
877 fail_result = _ParsedIssueNumberArgument()
878
879 if arg.isdigit():
880 return _ParsedIssueNumberArgument(issue=int(arg))
881 if not arg.startswith('http'):
882 return fail_result
883 url = gclient_utils.UpgradeToHttps(arg)
884 try:
885 parsed_url = urlparse.urlparse(url)
886 except ValueError:
887 return fail_result
888 for cls in _CODEREVIEW_IMPLEMENTATIONS.itervalues():
889 tmp = cls.ParseIssueURL(parsed_url)
890 if tmp is not None:
891 return tmp
892 return fail_result
893
894
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000895class Changelist(object):
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000896 """Changelist works with one changelist in local branch.
897
898 Supports two codereview backends: Rietveld or Gerrit, selected at object
899 creation.
900
tandrii@chromium.org8930b3d2016-04-13 14:47:02 +0000901 Notes:
902 * Not safe for concurrent multi-{thread,process} use.
903 * Caches values from current branch. Therefore, re-use after branch change
904 with care.
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000905 """
906
907 def __init__(self, branchref=None, issue=None, codereview=None, **kwargs):
908 """Create a new ChangeList instance.
909
910 If issue is given, the codereview must be given too.
911
912 If `codereview` is given, it must be 'rietveld' or 'gerrit'.
913 Otherwise, it's decided based on current configuration of the local branch,
914 with default being 'rietveld' for backwards compatibility.
915 See _load_codereview_impl for more details.
916
917 **kwargs will be passed directly to codereview implementation.
918 """
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000919 # Poke settings so we get the "configure your server" message if necessary.
maruel@chromium.org379d07a2011-11-30 14:58:10 +0000920 global settings
921 if not settings:
922 # Happens when git_cl.py is used as a utility library.
923 settings = Settings()
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000924
925 if issue:
926 assert codereview, 'codereview must be known, if issue is known'
927
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000928 self.branchref = branchref
929 if self.branchref:
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +0000930 assert branchref.startswith('refs/heads/')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000931 self.branch = ShortBranchName(self.branchref)
932 else:
933 self.branch = None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000934 self.upstream_branch = None
maruel@chromium.org1033efd2013-07-23 23:25:09 +0000935 self.lookedup_issue = False
936 self.issue = issue or None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000937 self.has_description = False
938 self.description = None
maruel@chromium.org1033efd2013-07-23 23:25:09 +0000939 self.lookedup_patchset = False
chase@chromium.orgcc51cd02010-12-23 00:48:39 +0000940 self.patchset = None
bauerb@chromium.orgae6df352011-04-06 17:40:39 +0000941 self.cc = None
942 self.watchers = ()
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +0000943 self._remote = None
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +0000944
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000945 self._codereview_impl = None
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +0000946 self._codereview = None
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000947 self._load_codereview_impl(codereview, **kwargs)
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +0000948 assert self._codereview_impl
949 assert self._codereview in _CODEREVIEW_IMPLEMENTATIONS
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000950
951 def _load_codereview_impl(self, codereview=None, **kwargs):
952 if codereview:
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +0000953 assert codereview in _CODEREVIEW_IMPLEMENTATIONS
954 cls = _CODEREVIEW_IMPLEMENTATIONS[codereview]
955 self._codereview = codereview
956 self._codereview_impl = cls(self, **kwargs)
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000957 return
958
959 # Automatic selection based on issue number set for a current branch.
960 # Rietveld takes precedence over Gerrit.
961 assert not self.issue
962 # Whether we find issue or not, we are doing the lookup.
963 self.lookedup_issue = True
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +0000964 for codereview, cls in _CODEREVIEW_IMPLEMENTATIONS.iteritems():
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000965 setting = cls.IssueSetting(self.GetBranch())
966 issue = RunGit(['config', setting], error_ok=True).strip()
967 if issue:
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +0000968 self._codereview = codereview
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +0000969 self._codereview_impl = cls(self, **kwargs)
970 self.issue = int(issue)
971 return
972
973 # No issue is set for this branch, so decide based on repo-wide settings.
974 return self._load_codereview_impl(
975 codereview='gerrit' if settings.GetIsGerrit() else 'rietveld',
976 **kwargs)
977
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +0000978 def IsGerrit(self):
979 return self._codereview == 'gerrit'
bauerb@chromium.orgae6df352011-04-06 17:40:39 +0000980
981 def GetCCList(self):
982 """Return the users cc'd on this CL.
983
984 Return is a string suitable for passing to gcl with the --cc flag.
985 """
986 if self.cc is None:
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000987 base_cc = settings.GetDefaultCCList()
bauerb@chromium.orgae6df352011-04-06 17:40:39 +0000988 more_cc = ','.join(self.watchers)
989 self.cc = ','.join(filter(None, (base_cc, more_cc))) or ''
990 return self.cc
991
tyoshino@chromium.org99918ab2013-09-30 06:17:28 +0000992 def GetCCListWithoutDefault(self):
993 """Return the users cc'd on this CL excluding default ones."""
994 if self.cc is None:
995 self.cc = ','.join(self.watchers)
996 return self.cc
997
bauerb@chromium.orgae6df352011-04-06 17:40:39 +0000998 def SetWatchers(self, watchers):
999 """Set the list of email addresses that should be cc'd based on the changed
1000 files in this CL.
1001 """
1002 self.watchers = watchers
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001003
1004 def GetBranch(self):
1005 """Returns the short branch name, e.g. 'master'."""
1006 if not self.branch:
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001007 branchref = GetCurrentBranchRef()
szager@chromium.orgd62c61f2014-10-20 22:33:21 +00001008 if not branchref:
1009 return None
1010 self.branchref = branchref
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001011 self.branch = ShortBranchName(self.branchref)
1012 return self.branch
1013
1014 def GetBranchRef(self):
1015 """Returns the full branch name, e.g. 'refs/heads/master'."""
1016 self.GetBranch() # Poke the lazy loader.
1017 return self.branchref
1018
tandrii@chromium.org534f67a2016-04-07 18:47:05 +00001019 def ClearBranch(self):
1020 """Clears cached branch data of this object."""
1021 self.branch = self.branchref = None
1022
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001023 @staticmethod
1024 def FetchUpstreamTuple(branch):
pgervais@chromium.orgd6617f32013-11-19 00:34:54 +00001025 """Returns a tuple containing remote and remote ref,
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001026 e.g. 'origin', 'refs/heads/master'
1027 """
1028 remote = '.'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001029 upstream_branch = RunGit(['config', 'branch.%s.merge' % branch],
1030 error_ok=True).strip()
1031 if upstream_branch:
1032 remote = RunGit(['config', 'branch.%s.remote' % branch]).strip()
1033 else:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +00001034 upstream_branch = RunGit(['config', 'rietveld.upstream-branch'],
1035 error_ok=True).strip()
1036 if upstream_branch:
1037 remote = RunGit(['config', 'rietveld.upstream-remote']).strip()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001038 else:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +00001039 # Fall back on trying a git-svn upstream branch.
1040 if settings.GetIsGitSvn():
1041 upstream_branch = settings.GetSVNBranch()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001042 else:
bauerb@chromium.orgade368c2011-03-01 08:57:50 +00001043 # Else, try to guess the origin remote.
1044 remote_branches = RunGit(['branch', '-r']).split()
1045 if 'origin/master' in remote_branches:
1046 # Fall back on origin/master if it exits.
1047 remote = 'origin'
1048 upstream_branch = 'refs/heads/master'
1049 elif 'origin/trunk' in remote_branches:
1050 # Fall back on origin/trunk if it exists. Generally a shared
1051 # git-svn clone
1052 remote = 'origin'
1053 upstream_branch = 'refs/heads/trunk'
1054 else:
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001055 DieWithError(
1056 'Unable to determine default branch to diff against.\n'
1057 'Either pass complete "git diff"-style arguments, like\n'
1058 ' git cl upload origin/master\n'
1059 'or verify this branch is set up to track another \n'
1060 '(via the --track argument to "git checkout -b ...").')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001061
1062 return remote, upstream_branch
1063
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00001064 def GetCommonAncestorWithUpstream(self):
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +00001065 upstream_branch = self.GetUpstreamBranch()
1066 if not BranchExists(upstream_branch):
1067 DieWithError('The upstream for the current branch (%s) does not exist '
1068 'anymore.\nPlease fix it and try again.' % self.GetBranch())
iannucci@chromium.org9e849272014-04-04 00:31:55 +00001069 return git_common.get_or_create_merge_base(self.GetBranch(),
pgervais@chromium.org8ba38ff2015-06-11 21:41:25 +00001070 upstream_branch)
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00001071
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001072 def GetUpstreamBranch(self):
1073 if self.upstream_branch is None:
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001074 remote, upstream_branch = self.FetchUpstreamTuple(self.GetBranch())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001075 if remote is not '.':
mmoss@chromium.orge7585452014-08-24 01:41:11 +00001076 upstream_branch = upstream_branch.replace('refs/heads/',
1077 'refs/remotes/%s/' % remote)
1078 upstream_branch = upstream_branch.replace('refs/branch-heads/',
1079 'refs/remotes/branch-heads/')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001080 self.upstream_branch = upstream_branch
1081 return self.upstream_branch
1082
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001083 def GetRemoteBranch(self):
jmbaker@chromium.orga2cbbbb2012-03-22 20:40:40 +00001084 if not self._remote:
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001085 remote, branch = None, self.GetBranch()
1086 seen_branches = set()
1087 while branch not in seen_branches:
1088 seen_branches.add(branch)
1089 remote, branch = self.FetchUpstreamTuple(branch)
1090 branch = ShortBranchName(branch)
1091 if remote != '.' or branch.startswith('refs/remotes'):
1092 break
1093 else:
jmbaker@chromium.orga2cbbbb2012-03-22 20:40:40 +00001094 remotes = RunGit(['remote'], error_ok=True).split()
1095 if len(remotes) == 1:
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001096 remote, = remotes
jmbaker@chromium.orga2cbbbb2012-03-22 20:40:40 +00001097 elif 'origin' in remotes:
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001098 remote = 'origin'
jmbaker@chromium.orga2cbbbb2012-03-22 20:40:40 +00001099 logging.warning('Could not determine which remote this change is '
1100 'associated with, so defaulting to "%s". This may '
1101 'not be what you want. You may prevent this message '
1102 'by running "git svn info" as documented here: %s',
1103 self._remote,
1104 GIT_INSTRUCTIONS_URL)
1105 else:
1106 logging.warn('Could not determine which remote this change is '
1107 'associated with. You may prevent this message by '
1108 'running "git svn info" as documented here: %s',
1109 GIT_INSTRUCTIONS_URL)
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001110 branch = 'HEAD'
1111 if branch.startswith('refs/remotes'):
1112 self._remote = (remote, branch)
mmoss@chromium.orge7585452014-08-24 01:41:11 +00001113 elif branch.startswith('refs/branch-heads/'):
1114 self._remote = (remote, branch.replace('refs/', 'refs/remotes/'))
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001115 else:
1116 self._remote = (remote, 'refs/remotes/%s/%s' % (remote, branch))
jmbaker@chromium.orga2cbbbb2012-03-22 20:40:40 +00001117 return self._remote
1118
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001119 def GitSanityChecks(self, upstream_git_obj):
1120 """Checks git repo status and ensures diff is from local commits."""
1121
sbc@chromium.org79706062015-01-14 21:18:12 +00001122 if upstream_git_obj is None:
1123 if self.GetBranch() is None:
1124 print >> sys.stderr, (
dpranke@chromium.orgee87f582015-07-31 18:46:25 +00001125 'ERROR: unable to determine current branch (detached HEAD?)')
sbc@chromium.org79706062015-01-14 21:18:12 +00001126 else:
1127 print >> sys.stderr, (
1128 'ERROR: no upstream branch')
1129 return False
1130
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001131 # Verify the commit we're diffing against is in our current branch.
1132 upstream_sha = RunGit(['rev-parse', '--verify', upstream_git_obj]).strip()
1133 common_ancestor = RunGit(['merge-base', upstream_sha, 'HEAD']).strip()
1134 if upstream_sha != common_ancestor:
1135 print >> sys.stderr, (
1136 'ERROR: %s is not in the current branch. You may need to rebase '
1137 'your tracking branch' % upstream_sha)
1138 return False
1139
1140 # List the commits inside the diff, and verify they are all local.
1141 commits_in_diff = RunGit(
1142 ['rev-list', '^%s' % upstream_sha, 'HEAD']).splitlines()
1143 code, remote_branch = RunGitWithCode(['config', 'gitcl.remotebranch'])
1144 remote_branch = remote_branch.strip()
1145 if code != 0:
1146 _, remote_branch = self.GetRemoteBranch()
1147
1148 commits_in_remote = RunGit(
1149 ['rev-list', '^%s' % upstream_sha, remote_branch]).splitlines()
1150
1151 common_commits = set(commits_in_diff) & set(commits_in_remote)
1152 if common_commits:
1153 print >> sys.stderr, (
1154 'ERROR: Your diff contains %d commits already in %s.\n'
1155 'Run "git log --oneline %s..HEAD" to get a list of commits in '
1156 'the diff. If you are using a custom git flow, you can override'
1157 ' the reference used for this check with "git config '
1158 'gitcl.remotebranch <git-ref>".' % (
1159 len(common_commits), remote_branch, upstream_git_obj))
1160 return False
1161 return True
1162
kalmard@homejinni.com6b0051e2012-04-03 15:45:08 +00001163 def GetGitBaseUrlFromConfig(self):
sheyang@chromium.orga656e702014-05-15 20:43:05 +00001164 """Return the configured base URL from branch.<branchname>.baseurl.
kalmard@homejinni.com6b0051e2012-04-03 15:45:08 +00001165
1166 Returns None if it is not set.
1167 """
sheyang@chromium.orga656e702014-05-15 20:43:05 +00001168 return RunGit(['config', 'branch.%s.base-url' % self.GetBranch()],
1169 error_ok=True).strip()
jmbaker@chromium.orga2cbbbb2012-03-22 20:40:40 +00001170
kjellander@chromium.org6abc6522014-12-02 07:34:49 +00001171 def GetGitSvnRemoteUrl(self):
1172 """Return the configured git-svn remote URL parsed from git svn info.
1173
1174 Returns None if it is not set.
1175 """
1176 # URL is dependent on the current directory.
1177 data = RunGit(['svn', 'info'], cwd=settings.GetRoot())
1178 if data:
1179 keys = dict(line.split(': ', 1) for line in data.splitlines()
1180 if ': ' in line)
1181 return keys.get('URL', None)
1182 return None
1183
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001184 def GetRemoteUrl(self):
1185 """Return the configured remote URL, e.g. 'git://example.org/foo.git/'.
1186
1187 Returns None if there is no remote.
1188 """
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001189 remote, _ = self.GetRemoteBranch()
dyen@chromium.org2a13d4f2014-06-13 00:06:37 +00001190 url = RunGit(['config', 'remote.%s.url' % remote], error_ok=True).strip()
1191
1192 # If URL is pointing to a local directory, it is probably a git cache.
1193 if os.path.isdir(url):
1194 url = RunGit(['config', 'remote.%s.url' % remote],
1195 error_ok=True,
1196 cwd=url).strip()
1197 return url
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001198
tandrii@chromium.org87985d22016-03-24 17:33:33 +00001199 def GetIssue(self):
maruel@chromium.org52424302012-08-29 15:14:30 +00001200 """Returns the issue number as a int or None if not set."""
tandrii@chromium.org87985d22016-03-24 17:33:33 +00001201 if self.issue is None and not self.lookedup_issue:
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001202 issue = RunGit(['config',
1203 self._codereview_impl.IssueSetting(self.GetBranch())],
1204 error_ok=True).strip()
maruel@chromium.org1033efd2013-07-23 23:25:09 +00001205 self.issue = int(issue) or None if issue else None
1206 self.lookedup_issue = True
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001207 return self.issue
1208
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001209 def GetIssueURL(self):
1210 """Get the URL for a particular issue."""
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001211 issue = self.GetIssue()
1212 if not issue:
dbeam@chromium.org015fd3d2013-06-18 19:02:50 +00001213 return None
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001214 return '%s/%s' % (self._codereview_impl.GetCodereviewServer(), issue)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001215
1216 def GetDescription(self, pretty=False):
1217 if not self.has_description:
1218 if self.GetIssue():
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001219 self.description = self._codereview_impl.FetchDescription()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001220 self.has_description = True
1221 if pretty:
1222 wrapper = textwrap.TextWrapper()
1223 wrapper.initial_indent = wrapper.subsequent_indent = ' '
1224 return wrapper.fill(self.description)
1225 return self.description
1226
1227 def GetPatchset(self):
maruel@chromium.org52424302012-08-29 15:14:30 +00001228 """Returns the patchset number as a int or None if not set."""
maruel@chromium.org1033efd2013-07-23 23:25:09 +00001229 if self.patchset is None and not self.lookedup_patchset:
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001230 patchset = RunGit(['config', self._codereview_impl.PatchsetSetting()],
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001231 error_ok=True).strip()
maruel@chromium.org1033efd2013-07-23 23:25:09 +00001232 self.patchset = int(patchset) or None if patchset else None
1233 self.lookedup_patchset = True
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001234 return self.patchset
1235
1236 def SetPatchset(self, patchset):
1237 """Set this branch's patchset. If patchset=0, clears the patchset."""
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001238 patchset_setting = self._codereview_impl.PatchsetSetting()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001239 if patchset:
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001240 RunGit(['config', patchset_setting, str(patchset)])
maruel@chromium.org1033efd2013-07-23 23:25:09 +00001241 self.patchset = patchset
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001242 else:
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001243 RunGit(['config', '--unset', patchset_setting],
maruel@chromium.org32f9f5e2011-09-14 13:41:47 +00001244 stderr=subprocess2.PIPE, error_ok=True)
maruel@chromium.org1033efd2013-07-23 23:25:09 +00001245 self.patchset = None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001246
tandrii@chromium.orga342c922016-03-16 07:08:25 +00001247 def SetIssue(self, issue=None):
1248 """Set this branch's issue. If issue isn't given, clears the issue."""
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001249 issue_setting = self._codereview_impl.IssueSetting(self.GetBranch())
1250 codereview_setting = self._codereview_impl.GetCodereviewServerSetting()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001251 if issue:
maruel@chromium.org1033efd2013-07-23 23:25:09 +00001252 self.issue = issue
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001253 RunGit(['config', issue_setting, str(issue)])
1254 codereview_server = self._codereview_impl.GetCodereviewServer()
1255 if codereview_server:
1256 RunGit(['config', codereview_setting, codereview_server])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001257 else:
tandrii@chromium.org9b7fd712016-06-01 13:45:20 +00001258 # Reset it regardless. It doesn't hurt.
1259 config_settings = [issue_setting, self._codereview_impl.PatchsetSetting()]
1260 for prop in (['last-upload-hash'] +
1261 self._codereview_impl._PostUnsetIssueProperties()):
1262 config_settings.append('branch.%s.%s' % (self.GetBranch(), prop))
1263 for setting in config_settings:
1264 RunGit(['config', '--unset', setting], error_ok=True)
maruel@chromium.org1033efd2013-07-23 23:25:09 +00001265 self.issue = None
tandrii@chromium.org9b7fd712016-06-01 13:45:20 +00001266 self.patchset = None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001267
asvitkine@chromium.org15169952011-09-27 14:30:53 +00001268 def GetChange(self, upstream_branch, author):
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00001269 if not self.GitSanityChecks(upstream_branch):
1270 DieWithError('\nGit sanity check failure')
1271
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00001272 root = settings.GetRelativeRoot()
bratell@opera.comf267b0e2013-05-02 09:11:43 +00001273 if not root:
1274 root = '.'
bauerb@chromium.org512f1ef2011-04-20 15:17:57 +00001275 absroot = os.path.abspath(root)
bauerb@chromium.org6fb99c62011-04-18 15:57:28 +00001276
1277 # We use the sha1 of HEAD as a name of this change.
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00001278 name = RunGitWithCode(['rev-parse', 'HEAD'])[1].strip()
bauerb@chromium.org512f1ef2011-04-20 15:17:57 +00001279 # Need to pass a relative path for msysgit.
maruel@chromium.org2b38e9c2011-10-19 00:04:35 +00001280 try:
maruel@chromium.org80a9ef12011-12-13 20:44:10 +00001281 files = scm.GIT.CaptureStatus([root], '.', upstream_branch)
maruel@chromium.org2b38e9c2011-10-19 00:04:35 +00001282 except subprocess2.CalledProcessError:
1283 DieWithError(
pgervais@chromium.orgd6617f32013-11-19 00:34:54 +00001284 ('\nFailed to diff against upstream branch %s\n\n'
maruel@chromium.org2b38e9c2011-10-19 00:04:35 +00001285 'This branch probably doesn\'t exist anymore. To reset the\n'
1286 'tracking branch, please run\n'
1287 ' git branch --set-upstream %s trunk\n'
1288 'replacing trunk with origin/master or the relevant branch') %
1289 (upstream_branch, self.GetBranch()))
bauerb@chromium.org6fb99c62011-04-18 15:57:28 +00001290
maruel@chromium.org52424302012-08-29 15:14:30 +00001291 issue = self.GetIssue()
1292 patchset = self.GetPatchset()
bauerb@chromium.org6fb99c62011-04-18 15:57:28 +00001293 if issue:
1294 description = self.GetDescription()
1295 else:
1296 # If the change was never uploaded, use the log messages of all commits
1297 # up to the branch point, as git cl upload will prefill the description
1298 # with these log messages.
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00001299 args = ['log', '--pretty=format:%s%n%n%b', '%s...' % (upstream_branch)]
1300 description = RunGitWithCode(args)[1].strip()
maruel@chromium.org03b3bdc2011-06-14 13:04:12 +00001301
1302 if not author:
maruel@chromium.org13f623c2011-07-22 16:02:23 +00001303 author = RunGit(['config', 'user.email']).strip() or None
asvitkine@chromium.org15169952011-09-27 14:30:53 +00001304 return presubmit_support.GitChange(
bauerb@chromium.org6fb99c62011-04-18 15:57:28 +00001305 name,
1306 description,
1307 absroot,
1308 files,
1309 issue,
1310 patchset,
agable@chromium.orgea84ef12014-04-30 19:55:12 +00001311 author,
1312 upstream=upstream_branch)
bauerb@chromium.org6fb99c62011-04-18 15:57:28 +00001313
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001314 def UpdateDescription(self, description):
1315 self.description = description
1316 return self._codereview_impl.UpdateDescriptionRemote(description)
1317
1318 def RunHook(self, committing, may_prompt, verbose, change):
1319 """Calls sys.exit() if the hook fails; returns a HookResults otherwise."""
1320 try:
1321 return presubmit_support.DoPresubmitChecks(change, committing,
1322 verbose=verbose, output_stream=sys.stdout, input_stream=sys.stdin,
1323 default_presubmit=None, may_prompt=may_prompt,
tandrii@chromium.org37b07a72016-04-29 16:42:28 +00001324 rietveld_obj=self._codereview_impl.GetRieveldObjForPresubmit(),
1325 gerrit_obj=self._codereview_impl.GetGerritObjForPresubmit())
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001326 except presubmit_support.PresubmitFailure, e:
1327 DieWithError(
1328 ('%s\nMaybe your depot_tools is out of date?\n'
1329 'If all fails, contact maruel@') % e)
1330
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001331 def CMDPatchIssue(self, issue_arg, reject, nocommit, directory):
1332 """Fetches and applies the issue patch from codereview to local branch."""
tandrii@chromium.orgef7c68c2016-04-07 09:39:39 +00001333 if isinstance(issue_arg, (int, long)) or issue_arg.isdigit():
1334 parsed_issue_arg = _ParsedIssueNumberArgument(int(issue_arg))
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001335 else:
1336 # Assume url.
1337 parsed_issue_arg = self._codereview_impl.ParseIssueURL(
1338 urlparse.urlparse(issue_arg))
1339 if not parsed_issue_arg or not parsed_issue_arg.valid:
1340 DieWithError('Failed to parse issue argument "%s". '
1341 'Must be an issue number or a valid URL.' % issue_arg)
1342 return self._codereview_impl.CMDPatchWithParsedIssue(
1343 parsed_issue_arg, reject, nocommit, directory)
1344
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00001345 def CMDUpload(self, options, git_diff_args, orig_args):
1346 """Uploads a change to codereview."""
1347 if git_diff_args:
1348 # TODO(ukai): is it ok for gerrit case?
1349 base_branch = git_diff_args[0]
1350 else:
1351 if self.GetBranch() is None:
1352 DieWithError('Can\'t upload from detached HEAD state. Get on a branch!')
1353
1354 # Default to diffing against common ancestor of upstream branch
1355 base_branch = self.GetCommonAncestorWithUpstream()
1356 git_diff_args = [base_branch, 'HEAD']
1357
1358 # Make sure authenticated to codereview before running potentially expensive
1359 # hooks. It is a fast, best efforts check. Codereview still can reject the
1360 # authentication during the actual upload.
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001361 self._codereview_impl.EnsureAuthenticated(force=options.force)
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00001362
1363 # Apply watchlists on upload.
1364 change = self.GetChange(base_branch, None)
1365 watchlist = watchlists.Watchlists(change.RepositoryRoot())
1366 files = [f.LocalPath() for f in change.AffectedFiles()]
1367 if not options.bypass_watchlists:
1368 self.SetWatchers(watchlist.GetWatchersForPaths(files))
1369
1370 if not options.bypass_hooks:
1371 if options.reviewers or options.tbr_owners:
1372 # Set the reviewer list now so that presubmit checks can access it.
1373 change_description = ChangeDescription(change.FullDescriptionText())
1374 change_description.update_reviewers(options.reviewers,
1375 options.tbr_owners,
1376 change)
1377 change.SetDescriptionText(change_description.description)
1378 hook_results = self.RunHook(committing=False,
1379 may_prompt=not options.force,
1380 verbose=options.verbose,
1381 change=change)
1382 if not hook_results.should_continue():
1383 return 1
1384 if not options.reviewers and hook_results.reviewers:
1385 options.reviewers = hook_results.reviewers.split(',')
1386
1387 if self.GetIssue():
1388 latest_patchset = self.GetMostRecentPatchset()
1389 local_patchset = self.GetPatchset()
1390 if (latest_patchset and local_patchset and
1391 local_patchset != latest_patchset):
1392 print ('The last upload made from this repository was patchset #%d but '
1393 'the most recent patchset on the server is #%d.'
1394 % (local_patchset, latest_patchset))
1395 print ('Uploading will still work, but if you\'ve uploaded to this '
1396 'issue from another machine or branch the patch you\'re '
1397 'uploading now might not include those changes.')
1398 ask_for_data('About to upload; enter to confirm.')
1399
1400 print_stats(options.similarity, options.find_copies, git_diff_args)
1401 ret = self.CMDUploadChange(options, git_diff_args, change)
1402 if not ret:
1403 git_set_branch_value('last-upload-hash',
1404 RunGit(['rev-parse', 'HEAD']).strip())
1405 # Run post upload hooks, if specified.
1406 if settings.GetRunPostUploadHook():
1407 presubmit_support.DoPostUploadExecuter(
1408 change,
1409 self,
1410 settings.GetRoot(),
1411 options.verbose,
1412 sys.stdout)
1413
1414 # Upload all dependencies if specified.
1415 if options.dependencies:
1416 print
1417 print '--dependencies has been specified.'
1418 print 'All dependent local branches will be re-uploaded.'
1419 print
1420 # Remove the dependencies flag from args so that we do not end up in a
1421 # loop.
1422 orig_args.remove('--dependencies')
1423 ret = upload_branch_deps(self, orig_args)
1424 return ret
1425
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001426 def SetCQState(self, new_state):
1427 """Update the CQ state for latest patchset.
1428
1429 Issue must have been already uploaded and known.
1430 """
1431 assert new_state in _CQState.ALL_STATES
1432 assert self.GetIssue()
1433 return self._codereview_impl.SetCQState(new_state)
1434
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001435 # Forward methods to codereview specific implementation.
1436
1437 def CloseIssue(self):
1438 return self._codereview_impl.CloseIssue()
1439
1440 def GetStatus(self):
1441 return self._codereview_impl.GetStatus()
1442
1443 def GetCodereviewServer(self):
1444 return self._codereview_impl.GetCodereviewServer()
1445
1446 def GetApprovingReviewers(self):
1447 return self._codereview_impl.GetApprovingReviewers()
1448
1449 def GetMostRecentPatchset(self):
1450 return self._codereview_impl.GetMostRecentPatchset()
1451
1452 def __getattr__(self, attr):
1453 # This is because lots of untested code accesses Rietveld-specific stuff
1454 # directly, and it's hard to fix for sure. So, just let it work, and fix
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00001455 # on a case by case basis.
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001456 return getattr(self._codereview_impl, attr)
1457
1458
1459class _ChangelistCodereviewBase(object):
1460 """Abstract base class encapsulating codereview specifics of a changelist."""
1461 def __init__(self, changelist):
1462 self._changelist = changelist # instance of Changelist
1463
1464 def __getattr__(self, attr):
1465 # Forward methods to changelist.
1466 # TODO(tandrii): maybe clean up _GerritChangelistImpl and
1467 # _RietveldChangelistImpl to avoid this hack?
1468 return getattr(self._changelist, attr)
1469
1470 def GetStatus(self):
1471 """Apply a rough heuristic to give a simple summary of an issue's review
1472 or CQ status, assuming adherence to a common workflow.
1473
1474 Returns None if no issue for this branch, or specific string keywords.
1475 """
1476 raise NotImplementedError()
1477
1478 def GetCodereviewServer(self):
1479 """Returns server URL without end slash, like "https://codereview.com"."""
1480 raise NotImplementedError()
1481
1482 def FetchDescription(self):
1483 """Fetches and returns description from the codereview server."""
1484 raise NotImplementedError()
1485
1486 def GetCodereviewServerSetting(self):
1487 """Returns git config setting for the codereview server."""
1488 raise NotImplementedError()
1489
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001490 @classmethod
1491 def IssueSetting(cls, branch):
tandrii@chromium.orgd03bc632016-04-12 14:17:26 +00001492 return 'branch.%s.%s' % (branch, cls.IssueSettingSuffix())
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001493
1494 @classmethod
tandrii@chromium.orgd03bc632016-04-12 14:17:26 +00001495 def IssueSettingSuffix(cls):
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001496 """Returns name of git config setting which stores issue number for a given
1497 branch."""
1498 raise NotImplementedError()
1499
1500 def PatchsetSetting(self):
1501 """Returns name of git config setting which stores issue number."""
1502 raise NotImplementedError()
1503
tandrii@chromium.org9b7fd712016-06-01 13:45:20 +00001504 def _PostUnsetIssueProperties(self):
1505 """Which branch-specific properties to erase when unsettin issue."""
1506 raise NotImplementedError()
1507
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001508 def GetRieveldObjForPresubmit(self):
1509 # This is an unfortunate Rietveld-embeddedness in presubmit.
1510 # For non-Rietveld codereviews, this probably should return a dummy object.
1511 raise NotImplementedError()
1512
tandrii@chromium.org37b07a72016-04-29 16:42:28 +00001513 def GetGerritObjForPresubmit(self):
1514 # None is valid return value, otherwise presubmit_support.GerritAccessor.
1515 return None
1516
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001517 def UpdateDescriptionRemote(self, description):
1518 """Update the description on codereview site."""
1519 raise NotImplementedError()
1520
1521 def CloseIssue(self):
1522 """Closes the issue."""
1523 raise NotImplementedError()
1524
1525 def GetApprovingReviewers(self):
1526 """Returns a list of reviewers approving the change.
1527
1528 Note: not necessarily committers.
1529 """
1530 raise NotImplementedError()
1531
1532 def GetMostRecentPatchset(self):
1533 """Returns the most recent patchset number from the codereview site."""
1534 raise NotImplementedError()
1535
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001536 def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit,
1537 directory):
1538 """Fetches and applies the issue.
1539
1540 Arguments:
1541 parsed_issue_arg: instance of _ParsedIssueNumberArgument.
1542 reject: if True, reject the failed patch instead of switching to 3-way
1543 merge. Rietveld only.
1544 nocommit: do not commit the patch, thus leave the tree dirty. Rietveld
1545 only.
1546 directory: switch to directory before applying the patch. Rietveld only.
1547 """
1548 raise NotImplementedError()
1549
1550 @staticmethod
1551 def ParseIssueURL(parsed_url):
1552 """Parses url and returns instance of _ParsedIssueNumberArgument or None if
1553 failed."""
1554 raise NotImplementedError()
1555
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001556 def EnsureAuthenticated(self, force):
1557 """Best effort check that user is authenticated with codereview server.
1558
1559 Arguments:
1560 force: whether to skip confirmation questions.
1561 """
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00001562 raise NotImplementedError()
1563
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00001564 def CMDUploadChange(self, options, args, change):
1565 """Uploads a change to codereview."""
1566 raise NotImplementedError()
1567
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001568 def SetCQState(self, new_state):
1569 """Update the CQ state for latest patchset.
1570
1571 Issue must have been already uploaded and known.
1572 """
1573 raise NotImplementedError()
1574
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001575
1576class _RietveldChangelistImpl(_ChangelistCodereviewBase):
1577 def __init__(self, changelist, auth_config=None, rietveld_server=None):
1578 super(_RietveldChangelistImpl, self).__init__(changelist)
1579 assert settings, 'must be initialized in _ChangelistCodereviewBase'
1580 settings.GetDefaultServerUrl()
1581
1582 self._rietveld_server = rietveld_server
1583 self._auth_config = auth_config
1584 self._props = None
1585 self._rpc_server = None
1586
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001587 def GetCodereviewServer(self):
1588 if not self._rietveld_server:
1589 # If we're on a branch then get the server potentially associated
1590 # with that branch.
1591 if self.GetIssue():
1592 rietveld_server_setting = self.GetCodereviewServerSetting()
1593 if rietveld_server_setting:
1594 self._rietveld_server = gclient_utils.UpgradeToHttps(RunGit(
1595 ['config', rietveld_server_setting], error_ok=True).strip())
1596 if not self._rietveld_server:
1597 self._rietveld_server = settings.GetDefaultServerUrl()
1598 return self._rietveld_server
1599
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00001600 def EnsureAuthenticated(self, force):
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00001601 """Best effort check that user is authenticated with Rietveld server."""
1602 if self._auth_config.use_oauth2:
1603 authenticator = auth.get_authenticator_for_host(
1604 self.GetCodereviewServer(), self._auth_config)
1605 if not authenticator.has_cached_credentials():
1606 raise auth.LoginRequiredError(self.GetCodereviewServer())
1607
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001608 def FetchDescription(self):
1609 issue = self.GetIssue()
1610 assert issue
1611 try:
1612 return self.RpcServer().get_description(issue).strip()
1613 except urllib2.HTTPError as e:
1614 if e.code == 404:
1615 DieWithError(
1616 ('\nWhile fetching the description for issue %d, received a '
1617 '404 (not found)\n'
1618 'error. It is likely that you deleted this '
1619 'issue on the server. If this is the\n'
1620 'case, please run\n\n'
1621 ' git cl issue 0\n\n'
1622 'to clear the association with the deleted issue. Then run '
1623 'this command again.') % issue)
1624 else:
1625 DieWithError(
1626 '\nFailed to fetch issue description. HTTP error %d' % e.code)
1627 except urllib2.URLError as e:
1628 print >> sys.stderr, (
1629 'Warning: Failed to retrieve CL description due to network '
1630 'failure.')
1631 return ''
1632
1633 def GetMostRecentPatchset(self):
1634 return self.GetIssueProperties()['patchsets'][-1]
1635
1636 def GetPatchSetDiff(self, issue, patchset):
1637 return self.RpcServer().get(
1638 '/download/issue%s_%s.diff' % (issue, patchset))
1639
1640 def GetIssueProperties(self):
1641 if self._props is None:
1642 issue = self.GetIssue()
1643 if not issue:
1644 self._props = {}
1645 else:
1646 self._props = self.RpcServer().get_issue_properties(issue, True)
1647 return self._props
1648
1649 def GetApprovingReviewers(self):
1650 return get_approving_reviewers(self.GetIssueProperties())
1651
1652 def AddComment(self, message):
1653 return self.RpcServer().add_comment(self.GetIssue(), message)
1654
jsbell@chromium.orgb99fbd92014-09-11 17:29:28 +00001655 def GetStatus(self):
1656 """Apply a rough heuristic to give a simple summary of an issue's review
1657 or CQ status, assuming adherence to a common workflow.
1658
1659 Returns None if no issue for this branch, or one of the following keywords:
1660 * 'error' - error from review tool (including deleted issues)
1661 * 'unsent' - not sent for review
1662 * 'waiting' - waiting for review
1663 * 'reply' - waiting for owner to reply to review
1664 * 'lgtm' - LGTM from at least one approved reviewer
1665 * 'commit' - in the commit queue
1666 * 'closed' - closed
1667 """
1668 if not self.GetIssue():
1669 return None
1670
1671 try:
1672 props = self.GetIssueProperties()
1673 except urllib2.HTTPError:
1674 return 'error'
1675
1676 if props.get('closed'):
1677 # Issue is closed.
1678 return 'closed'
tandrii@chromium.orgb4f6a222016-03-03 01:11:04 +00001679 if props.get('commit') and not props.get('cq_dry_run', False):
jsbell@chromium.orgb99fbd92014-09-11 17:29:28 +00001680 # Issue is in the commit queue.
1681 return 'commit'
1682
1683 try:
1684 reviewers = self.GetApprovingReviewers()
1685 except urllib2.HTTPError:
1686 return 'error'
1687
1688 if reviewers:
1689 # Was LGTM'ed.
1690 return 'lgtm'
1691
1692 messages = props.get('messages') or []
1693
1694 if not messages:
1695 # No message was sent.
1696 return 'unsent'
1697 if messages[-1]['sender'] != props.get('owner_email'):
1698 # Non-LGTM reply from non-owner
1699 return 'reply'
1700 return 'waiting'
1701
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001702 def UpdateDescriptionRemote(self, description):
maruel@chromium.orgb021b322013-04-08 17:57:29 +00001703 return self.RpcServer().update_description(
1704 self.GetIssue(), self.description)
1705
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001706 def CloseIssue(self):
maruel@chromium.orgb021b322013-04-08 17:57:29 +00001707 return self.RpcServer().close_issue(self.GetIssue())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001708
maruel@chromium.org27bb3872011-05-30 20:33:19 +00001709 def SetFlag(self, flag, value):
1710 """Patchset must match."""
1711 if not self.GetPatchset():
1712 DieWithError('The patchset needs to match. Send another patchset.')
1713 try:
1714 return self.RpcServer().set_flag(
maruel@chromium.org52424302012-08-29 15:14:30 +00001715 self.GetIssue(), self.GetPatchset(), flag, value)
maruel@chromium.org27bb3872011-05-30 20:33:19 +00001716 except urllib2.HTTPError, e:
1717 if e.code == 404:
1718 DieWithError('The issue %s doesn\'t exist.' % self.GetIssue())
1719 if e.code == 403:
1720 DieWithError(
1721 ('Access denied to issue %s. Maybe the patchset %s doesn\'t '
1722 'match?') % (self.GetIssue(), self.GetPatchset()))
1723 raise
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001724
maruel@chromium.orgcab38e92011-04-09 00:30:51 +00001725 def RpcServer(self):
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001726 """Returns an upload.RpcServer() to access this review's rietveld instance.
1727 """
maruel@chromium.orge77ebbf2011-03-29 20:35:38 +00001728 if not self._rpc_server:
maruel@chromium.org4bac4b52012-11-27 20:33:52 +00001729 self._rpc_server = rietveld.CachingRietveld(
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001730 self.GetCodereviewServer(),
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00001731 self._auth_config or auth.make_auth_config())
maruel@chromium.orge77ebbf2011-03-29 20:35:38 +00001732 return self._rpc_server
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001733
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001734 @classmethod
tandrii@chromium.orgd03bc632016-04-12 14:17:26 +00001735 def IssueSettingSuffix(cls):
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00001736 return 'rietveldissue'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001737
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001738 def PatchsetSetting(self):
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001739 """Return the git setting that stores this change's most recent patchset."""
1740 return 'branch.%s.rietveldpatchset' % self.GetBranch()
1741
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001742 def GetCodereviewServerSetting(self):
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001743 """Returns the git setting that stores this change's rietveld server."""
szager@chromium.orgd62c61f2014-10-20 22:33:21 +00001744 branch = self.GetBranch()
1745 if branch:
1746 return 'branch.%s.rietveldserver' % branch
1747 return None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00001748
tandrii@chromium.org9b7fd712016-06-01 13:45:20 +00001749 def _PostUnsetIssueProperties(self):
1750 """Which branch-specific properties to erase when unsetting issue."""
1751 return ['rietveldserver']
1752
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00001753 def GetRieveldObjForPresubmit(self):
1754 return self.RpcServer()
1755
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00001756 def SetCQState(self, new_state):
1757 props = self.GetIssueProperties()
1758 if props.get('private'):
1759 DieWithError('Cannot set-commit on private issue')
1760
1761 if new_state == _CQState.COMMIT:
1762 self.SetFlag('commit', '1')
1763 elif new_state == _CQState.NONE:
1764 self.SetFlag('commit', '0')
1765 else:
1766 raise NotImplementedError()
1767
1768
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001769 def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit,
1770 directory):
1771 # TODO(maruel): Use apply_issue.py
1772
1773 # PatchIssue should never be called with a dirty tree. It is up to the
1774 # caller to check this, but just in case we assert here since the
1775 # consequences of the caller not checking this could be dire.
1776 assert(not git_common.is_dirty_git_tree('apply'))
1777 assert(parsed_issue_arg.valid)
1778 self._changelist.issue = parsed_issue_arg.issue
1779 if parsed_issue_arg.hostname:
1780 self._rietveld_server = 'https://%s' % parsed_issue_arg.hostname
1781
tandrii@chromium.orgef7c68c2016-04-07 09:39:39 +00001782 if (isinstance(parsed_issue_arg, _RietveldParsedIssueNumberArgument) and
1783 parsed_issue_arg.patch_url):
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00001784 assert parsed_issue_arg.patchset
1785 patchset = parsed_issue_arg.patchset
1786 patch_data = urllib2.urlopen(parsed_issue_arg.patch_url).read()
1787 else:
1788 patchset = parsed_issue_arg.patchset or self.GetMostRecentPatchset()
1789 patch_data = self.GetPatchSetDiff(self.GetIssue(), patchset)
1790
1791 # Switch up to the top-level directory, if necessary, in preparation for
1792 # applying the patch.
1793 top = settings.GetRelativeRoot()
1794 if top:
1795 os.chdir(top)
1796
1797 # Git patches have a/ at the beginning of source paths. We strip that out
1798 # with a sed script rather than the -p flag to patch so we can feed either
1799 # Git or svn-style patches into the same apply command.
1800 # re.sub() should be used but flags=re.MULTILINE is only in python 2.7.
1801 try:
1802 patch_data = subprocess2.check_output(
1803 ['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'], stdin=patch_data)
1804 except subprocess2.CalledProcessError:
1805 DieWithError('Git patch mungling failed.')
1806 logging.info(patch_data)
1807
1808 # We use "git apply" to apply the patch instead of "patch" so that we can
1809 # pick up file adds.
1810 # The --index flag means: also insert into the index (so we catch adds).
1811 cmd = ['git', 'apply', '--index', '-p0']
1812 if directory:
1813 cmd.extend(('--directory', directory))
1814 if reject:
1815 cmd.append('--reject')
1816 elif IsGitVersionAtLeast('1.7.12'):
1817 cmd.append('--3way')
1818 try:
1819 subprocess2.check_call(cmd, env=GetNoGitPagerEnv(),
1820 stdin=patch_data, stdout=subprocess2.VOID)
1821 except subprocess2.CalledProcessError:
1822 print 'Failed to apply the patch'
1823 return 1
1824
1825 # If we had an issue, commit the current state and register the issue.
1826 if not nocommit:
1827 RunGit(['commit', '-m', (self.GetDescription() + '\n\n' +
1828 'patch from issue %(i)s at patchset '
1829 '%(p)s (http://crrev.com/%(i)s#ps%(p)s)'
1830 % {'i': self.GetIssue(), 'p': patchset})])
1831 self.SetIssue(self.GetIssue())
1832 self.SetPatchset(patchset)
1833 print "Committed patch locally."
1834 else:
1835 print "Patch applied to index."
1836 return 0
1837
1838 @staticmethod
1839 def ParseIssueURL(parsed_url):
1840 if not parsed_url.scheme or not parsed_url.scheme.startswith('http'):
1841 return None
1842 # Typical url: https://domain/<issue_number>[/[other]]
1843 match = re.match('/(\d+)(/.*)?$', parsed_url.path)
1844 if match:
1845 return _RietveldParsedIssueNumberArgument(
1846 issue=int(match.group(1)),
1847 hostname=parsed_url.netloc)
1848 # Rietveld patch: https://domain/download/issue<number>_<patchset>.diff
1849 match = re.match(r'/download/issue(\d+)_(\d+).diff$', parsed_url.path)
1850 if match:
1851 return _RietveldParsedIssueNumberArgument(
1852 issue=int(match.group(1)),
1853 patchset=int(match.group(2)),
1854 hostname=parsed_url.netloc,
1855 patch_url=gclient_utils.UpgradeToHttps(parsed_url.geturl()))
1856 return None
1857
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00001858 def CMDUploadChange(self, options, args, change):
1859 """Upload the patch to Rietveld."""
1860 upload_args = ['--assume_yes'] # Don't ask about untracked files.
1861 upload_args.extend(['--server', self.GetCodereviewServer()])
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00001862 upload_args.extend(auth.auth_config_to_command_options(self._auth_config))
1863 if options.emulate_svn_auto_props:
1864 upload_args.append('--emulate_svn_auto_props')
1865
1866 change_desc = None
1867
1868 if options.email is not None:
1869 upload_args.extend(['--email', options.email])
1870
1871 if self.GetIssue():
1872 if options.title:
1873 upload_args.extend(['--title', options.title])
1874 if options.message:
1875 upload_args.extend(['--message', options.message])
1876 upload_args.extend(['--issue', str(self.GetIssue())])
1877 print ('This branch is associated with issue %s. '
1878 'Adding patch to that issue.' % self.GetIssue())
1879 else:
1880 if options.title:
1881 upload_args.extend(['--title', options.title])
1882 message = (options.title or options.message or
1883 CreateDescriptionFromLog(args))
1884 change_desc = ChangeDescription(message)
1885 if options.reviewers or options.tbr_owners:
1886 change_desc.update_reviewers(options.reviewers,
1887 options.tbr_owners,
1888 change)
1889 if not options.force:
1890 change_desc.prompt()
1891
1892 if not change_desc.description:
1893 print "Description is empty; aborting."
1894 return 1
1895
1896 upload_args.extend(['--message', change_desc.description])
1897 if change_desc.get_reviewers():
1898 upload_args.append('--reviewers=%s' % ','.join(
1899 change_desc.get_reviewers()))
1900 if options.send_mail:
1901 if not change_desc.get_reviewers():
1902 DieWithError("Must specify reviewers to send email.")
1903 upload_args.append('--send_mail')
1904
1905 # We check this before applying rietveld.private assuming that in
1906 # rietveld.cc only addresses which we can send private CLs to are listed
1907 # if rietveld.private is set, and so we should ignore rietveld.cc only
1908 # when --private is specified explicitly on the command line.
1909 if options.private:
1910 logging.warn('rietveld.cc is ignored since private flag is specified. '
1911 'You need to review and add them manually if necessary.')
1912 cc = self.GetCCListWithoutDefault()
1913 else:
1914 cc = self.GetCCList()
1915 cc = ','.join(filter(None, (cc, ','.join(options.cc))))
1916 if cc:
1917 upload_args.extend(['--cc', cc])
1918
1919 if options.private or settings.GetDefaultPrivateFlag() == "True":
1920 upload_args.append('--private')
1921
1922 upload_args.extend(['--git_similarity', str(options.similarity)])
1923 if not options.find_copies:
1924 upload_args.extend(['--git_no_find_copies'])
1925
1926 # Include the upstream repo's URL in the change -- this is useful for
1927 # projects that have their source spread across multiple repos.
1928 remote_url = self.GetGitBaseUrlFromConfig()
1929 if not remote_url:
1930 if settings.GetIsGitSvn():
1931 remote_url = self.GetGitSvnRemoteUrl()
1932 else:
1933 if self.GetRemoteUrl() and '/' in self.GetUpstreamBranch():
1934 remote_url = '%s@%s' % (self.GetRemoteUrl(),
1935 self.GetUpstreamBranch().split('/')[-1])
1936 if remote_url:
1937 upload_args.extend(['--base_url', remote_url])
1938 remote, remote_branch = self.GetRemoteBranch()
1939 target_ref = GetTargetRef(remote, remote_branch, options.target_branch,
1940 settings.GetPendingRefPrefix())
1941 if target_ref:
1942 upload_args.extend(['--target_ref', target_ref])
1943
1944 # Look for dependent patchsets. See crbug.com/480453 for more details.
1945 remote, upstream_branch = self.FetchUpstreamTuple(self.GetBranch())
1946 upstream_branch = ShortBranchName(upstream_branch)
1947 if remote is '.':
1948 # A local branch is being tracked.
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00001949 local_branch = upstream_branch
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00001950 if settings.GetIsSkipDependencyUpload(local_branch):
1951 print
1952 print ('Skipping dependency patchset upload because git config '
1953 'branch.%s.skip-deps-uploads is set to True.' % local_branch)
1954 print
1955 else:
1956 auth_config = auth.extract_auth_config_from_options(options)
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00001957 branch_cl = Changelist(branchref='refs/heads/'+local_branch,
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00001958 auth_config=auth_config)
1959 branch_cl_issue_url = branch_cl.GetIssueURL()
1960 branch_cl_issue = branch_cl.GetIssue()
1961 branch_cl_patchset = branch_cl.GetPatchset()
1962 if branch_cl_issue_url and branch_cl_issue and branch_cl_patchset:
1963 upload_args.extend(
1964 ['--depends_on_patchset', '%s:%s' % (
1965 branch_cl_issue, branch_cl_patchset)])
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00001966 print(
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00001967 '\n'
1968 'The current branch (%s) is tracking a local branch (%s) with '
1969 'an associated CL.\n'
1970 'Adding %s/#ps%s as a dependency patchset.\n'
1971 '\n' % (self.GetBranch(), local_branch, branch_cl_issue_url,
1972 branch_cl_patchset))
1973
1974 project = settings.GetProject()
1975 if project:
1976 upload_args.extend(['--project', project])
1977
1978 if options.cq_dry_run:
1979 upload_args.extend(['--cq_dry_run'])
1980
1981 try:
1982 upload_args = ['upload'] + upload_args + args
1983 logging.info('upload.RealMain(%s)', upload_args)
1984 issue, patchset = upload.RealMain(upload_args)
1985 issue = int(issue)
1986 patchset = int(patchset)
1987 except KeyboardInterrupt:
1988 sys.exit(1)
1989 except:
1990 # If we got an exception after the user typed a description for their
1991 # change, back up the description before re-raising.
1992 if change_desc:
1993 backup_path = os.path.expanduser(DESCRIPTION_BACKUP_FILE)
1994 print('\nGot exception while uploading -- saving description to %s\n' %
1995 backup_path)
1996 backup_file = open(backup_path, 'w')
1997 backup_file.write(change_desc.description)
1998 backup_file.close()
1999 raise
2000
2001 if not self.GetIssue():
2002 self.SetIssue(issue)
2003 self.SetPatchset(patchset)
2004
2005 if options.use_commit_queue:
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00002006 self.SetCQState(_CQState.COMMIT)
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002007 return 0
2008
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002009
2010class _GerritChangelistImpl(_ChangelistCodereviewBase):
2011 def __init__(self, changelist, auth_config=None):
2012 # auth_config is Rietveld thing, kept here to preserve interface only.
2013 super(_GerritChangelistImpl, self).__init__(changelist)
2014 self._change_id = None
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00002015 # Lazily cached values.
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002016 self._gerrit_server = None # e.g. https://chromium-review.googlesource.com
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00002017 self._gerrit_host = None # e.g. chromium-review.googlesource.com
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002018
2019 def _GetGerritHost(self):
2020 # Lazy load of configs.
2021 self.GetCodereviewServer()
2022 return self._gerrit_host
2023
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00002024 def _GetGitHost(self):
2025 """Returns git host to be used when uploading change to Gerrit."""
2026 return urlparse.urlparse(self.GetRemoteUrl()).netloc
2027
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002028 def GetCodereviewServer(self):
2029 if not self._gerrit_server:
2030 # If we're on a branch then get the server potentially associated
2031 # with that branch.
2032 if self.GetIssue():
2033 gerrit_server_setting = self.GetCodereviewServerSetting()
2034 if gerrit_server_setting:
2035 self._gerrit_server = RunGit(['config', gerrit_server_setting],
2036 error_ok=True).strip()
2037 if self._gerrit_server:
2038 self._gerrit_host = urlparse.urlparse(self._gerrit_server).netloc
2039 if not self._gerrit_server:
2040 # We assume repo to be hosted on Gerrit, and hence Gerrit server
2041 # has "-review" suffix for lowest level subdomain.
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00002042 parts = self._GetGitHost().split('.')
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002043 parts[0] = parts[0] + '-review'
2044 self._gerrit_host = '.'.join(parts)
2045 self._gerrit_server = 'https://%s' % self._gerrit_host
2046 return self._gerrit_server
2047
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00002048 @classmethod
tandrii@chromium.orgd03bc632016-04-12 14:17:26 +00002049 def IssueSettingSuffix(cls):
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00002050 return 'gerritissue'
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002051
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00002052 def EnsureAuthenticated(self, force):
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00002053 """Best effort check that user is authenticated with Gerrit server."""
tandrii@chromium.org28253532016-04-14 13:46:56 +00002054 if settings.GetGerritSkipEnsureAuthenticated():
2055 # For projects with unusual authentication schemes.
2056 # See http://crbug.com/603378.
2057 return
tandrii@chromium.orgfe30f182016-04-13 12:15:04 +00002058 # Lazy-loader to identify Gerrit and Git hosts.
2059 if gerrit_util.GceAuthenticator.is_gce():
2060 return
2061 self.GetCodereviewServer()
2062 git_host = self._GetGitHost()
2063 assert self._gerrit_server and self._gerrit_host
2064 cookie_auth = gerrit_util.CookiesAuthenticator()
2065
2066 gerrit_auth = cookie_auth.get_auth_header(self._gerrit_host)
2067 git_auth = cookie_auth.get_auth_header(git_host)
2068 if gerrit_auth and git_auth:
2069 if gerrit_auth == git_auth:
2070 return
2071 print((
2072 'WARNING: you have different credentials for Gerrit and git hosts.\n'
2073 ' Check your %s or %s file for credentials of hosts:\n'
2074 ' %s\n'
2075 ' %s\n'
2076 ' %s') %
2077 (cookie_auth.get_gitcookies_path(), cookie_auth.get_netrc_path(),
2078 git_host, self._gerrit_host,
2079 cookie_auth.get_new_password_message(git_host)))
2080 if not force:
2081 ask_for_data('If you know what you are doing, press Enter to continue, '
2082 'Ctrl+C to abort.')
2083 return
2084 else:
2085 missing = (
2086 [] if gerrit_auth else [self._gerrit_host] +
2087 [] if git_auth else [git_host])
2088 DieWithError('Credentials for the following hosts are required:\n'
2089 ' %s\n'
2090 'These are read from %s (or legacy %s)\n'
2091 '%s' % (
2092 '\n '.join(missing),
2093 cookie_auth.get_gitcookies_path(),
2094 cookie_auth.get_netrc_path(),
2095 cookie_auth.get_new_password_message(git_host)))
2096
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00002097
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002098 def PatchsetSetting(self):
2099 """Return the git setting that stores this change's most recent patchset."""
2100 return 'branch.%s.gerritpatchset' % self.GetBranch()
2101
2102 def GetCodereviewServerSetting(self):
2103 """Returns the git setting that stores this change's Gerrit server."""
2104 branch = self.GetBranch()
2105 if branch:
2106 return 'branch.%s.gerritserver' % branch
2107 return None
2108
tandrii@chromium.org9b7fd712016-06-01 13:45:20 +00002109 def _PostUnsetIssueProperties(self):
2110 """Which branch-specific properties to erase when unsetting issue."""
2111 return [
2112 'gerritserver',
2113 'gerritsquashhash',
2114 ]
2115
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002116 def GetRieveldObjForPresubmit(self):
2117 class ThisIsNotRietveldIssue(object):
2118 def __nonzero__(self):
2119 # This is a hack to make presubmit_support think that rietveld is not
2120 # defined, yet still ensure that calls directly result in a decent
2121 # exception message below.
2122 return False
2123
2124 def __getattr__(self, attr):
2125 print(
2126 'You aren\'t using Rietveld at the moment, but Gerrit.\n'
2127 'Using Rietveld in your PRESUBMIT scripts won\'t work.\n'
2128 'Please, either change your PRESUBIT to not use rietveld_obj.%s,\n'
2129 'or use Rietveld for codereview.\n'
2130 'See also http://crbug.com/579160.' % attr)
2131 raise NotImplementedError()
2132 return ThisIsNotRietveldIssue()
2133
tandrii@chromium.org37b07a72016-04-29 16:42:28 +00002134 def GetGerritObjForPresubmit(self):
2135 return presubmit_support.GerritAccessor(self._GetGerritHost())
2136
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002137 def GetStatus(self):
tandrii@chromium.org013a2802016-03-29 09:52:33 +00002138 """Apply a rough heuristic to give a simple summary of an issue's review
2139 or CQ status, assuming adherence to a common workflow.
2140
2141 Returns None if no issue for this branch, or one of the following keywords:
2142 * 'error' - error from review tool (including deleted issues)
2143 * 'unsent' - no reviewers added
2144 * 'waiting' - waiting for review
2145 * 'reply' - waiting for owner to reply to review
2146 * 'not lgtm' - Code-Review -2 from at least one approved reviewer
2147 * 'lgtm' - Code-Review +2 from at least one approved reviewer
2148 * 'commit' - in the commit queue
2149 * 'closed' - abandoned
2150 """
2151 if not self.GetIssue():
2152 return None
2153
2154 try:
2155 data = self._GetChangeDetail(['DETAILED_LABELS', 'CURRENT_REVISION'])
2156 except httplib.HTTPException:
2157 return 'error'
2158
tandrii@chromium.org5e1bf382016-05-17 08:43:24 +00002159 if data['status'] in ('ABANDONED', 'MERGED'):
tandrii@chromium.org013a2802016-03-29 09:52:33 +00002160 return 'closed'
2161
2162 cq_label = data['labels'].get('Commit-Queue', {})
2163 if cq_label:
2164 # Vote value is a stringified integer, which we expect from 0 to 2.
2165 vote_value = cq_label.get('value', '0')
2166 vote_text = cq_label.get('values', {}).get(vote_value, '')
2167 if vote_text.lower() == 'commit':
2168 return 'commit'
2169
2170 lgtm_label = data['labels'].get('Code-Review', {})
2171 if lgtm_label:
2172 if 'rejected' in lgtm_label:
2173 return 'not lgtm'
2174 if 'approved' in lgtm_label:
2175 return 'lgtm'
2176
2177 if not data.get('reviewers', {}).get('REVIEWER', []):
2178 return 'unsent'
2179
2180 messages = data.get('messages', [])
2181 if messages:
2182 owner = data['owner'].get('_account_id')
2183 last_message_author = messages[-1].get('author', {}).get('_account_id')
2184 if owner != last_message_author:
2185 # Some reply from non-owner.
2186 return 'reply'
2187
2188 return 'waiting'
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002189
2190 def GetMostRecentPatchset(self):
tandrii@chromium.org013a2802016-03-29 09:52:33 +00002191 data = self._GetChangeDetail(['CURRENT_REVISION'])
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002192 return data['revisions'][data['current_revision']]['_number']
2193
2194 def FetchDescription(self):
tandrii@chromium.org2d3da632016-04-25 19:23:27 +00002195 data = self._GetChangeDetail(['CURRENT_REVISION'])
2196 current_rev = data['current_revision']
2197 url = data['revisions'][current_rev]['fetch']['http']['url']
2198 return gerrit_util.GetChangeDescriptionFromGitiles(url, current_rev)
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002199
2200 def UpdateDescriptionRemote(self, description):
scottmg@chromium.org6d1266e2016-04-26 11:12:26 +00002201 gerrit_util.SetCommitMessage(self._GetGerritHost(), self.GetIssue(),
2202 description)
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00002203
2204 def CloseIssue(self):
2205 gerrit_util.AbandonChange(self._GetGerritHost(), self.GetIssue(), msg='')
2206
tandrii@chromium.org600b4922016-04-26 10:57:52 +00002207 def GetApprovingReviewers(self):
2208 """Returns a list of reviewers approving the change.
2209
2210 Note: not necessarily committers.
2211 """
2212 raise NotImplementedError()
2213
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +00002214 def SubmitIssue(self, wait_for_merge=True):
2215 gerrit_util.SubmitChange(self._GetGerritHost(), self.GetIssue(),
2216 wait_for_merge=wait_for_merge)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002217
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002218 def _GetChangeDetail(self, options=None, issue=None):
2219 options = options or []
2220 issue = issue or self.GetIssue()
2221 assert issue, 'issue required to query Gerrit'
tandrii@chromium.org11a899e2016-04-13 12:45:44 +00002222 return gerrit_util.GetChangeDetail(self._GetGerritHost(), str(issue),
2223 options)
tandrii@chromium.org013a2802016-03-29 09:52:33 +00002224
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +00002225 def CMDLand(self, force, bypass_hooks, verbose):
2226 if git_common.is_dirty_git_tree('land'):
2227 return 1
2228 differs = True
2229 last_upload = RunGit(['config',
2230 'branch.%s.gerritsquashhash' % self.GetBranch()],
2231 error_ok=True).strip()
2232 # Note: git diff outputs nothing if there is no diff.
2233 if not last_upload or RunGit(['diff', last_upload]).strip():
2234 print('WARNING: some changes from local branch haven\'t been uploaded')
2235 else:
2236 detail = self._GetChangeDetail(['CURRENT_REVISION'])
2237 if detail['current_revision'] == last_upload:
2238 differs = False
2239 else:
2240 print('WARNING: local branch contents differ from latest uploaded '
2241 'patchset')
2242 if differs:
2243 if not force:
2244 ask_for_data(
2245 'Do you want to submit latest Gerrit patchset and bypass hooks?')
2246 print('WARNING: bypassing hooks and submitting latest uploaded patchset')
2247 elif not bypass_hooks:
2248 hook_results = self.RunHook(
2249 committing=True,
2250 may_prompt=not force,
2251 verbose=verbose,
2252 change=self.GetChange(self.GetCommonAncestorWithUpstream(), None))
2253 if not hook_results.should_continue():
2254 return 1
2255
2256 self.SubmitIssue(wait_for_merge=True)
2257 print('Issue %s has been submitted.' % self.GetIssueURL())
2258 return 0
2259
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00002260 def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit,
2261 directory):
2262 assert not reject
2263 assert not nocommit
2264 assert not directory
2265 assert parsed_issue_arg.valid
2266
2267 self._changelist.issue = parsed_issue_arg.issue
2268
2269 if parsed_issue_arg.hostname:
2270 self._gerrit_host = parsed_issue_arg.hostname
2271 self._gerrit_server = 'https://%s' % self._gerrit_host
2272
2273 detail = self._GetChangeDetail(['ALL_REVISIONS'])
2274
2275 if not parsed_issue_arg.patchset:
2276 # Use current revision by default.
2277 revision_info = detail['revisions'][detail['current_revision']]
2278 patchset = int(revision_info['_number'])
2279 else:
2280 patchset = parsed_issue_arg.patchset
2281 for revision_info in detail['revisions'].itervalues():
2282 if int(revision_info['_number']) == parsed_issue_arg.patchset:
2283 break
2284 else:
2285 DieWithError('Couldn\'t find patchset %i in issue %i' %
2286 (parsed_issue_arg.patchset, self.GetIssue()))
2287
2288 fetch_info = revision_info['fetch']['http']
2289 RunGit(['fetch', fetch_info['url'], fetch_info['ref']])
2290 RunGit(['cherry-pick', 'FETCH_HEAD'])
2291 self.SetIssue(self.GetIssue())
2292 self.SetPatchset(patchset)
2293 print('Committed patch for issue %i pathset %i locally' %
2294 (self.GetIssue(), self.GetPatchset()))
2295 return 0
2296
2297 @staticmethod
2298 def ParseIssueURL(parsed_url):
2299 if not parsed_url.scheme or not parsed_url.scheme.startswith('http'):
2300 return None
2301 # Gerrit's new UI is https://domain/c/<issue_number>[/[patchset]]
2302 # But current GWT UI is https://domain/#/c/<issue_number>[/[patchset]]
2303 # Short urls like https://domain/<issue_number> can be used, but don't allow
2304 # specifying the patchset (you'd 404), but we allow that here.
2305 if parsed_url.path == '/':
2306 part = parsed_url.fragment
2307 else:
2308 part = parsed_url.path
2309 match = re.match('(/c)?/(\d+)(/(\d+)?/?)?$', part)
2310 if match:
2311 return _ParsedIssueNumberArgument(
2312 issue=int(match.group(2)),
2313 patchset=int(match.group(4)) if match.group(4) else None,
2314 hostname=parsed_url.netloc)
2315 return None
2316
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002317 def CMDUploadChange(self, options, args, change):
2318 """Upload the current branch to Gerrit."""
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00002319 if options.squash and options.no_squash:
2320 DieWithError('Can only use one of --squash or --no-squash')
2321 options.squash = ((settings.GetSquashGerritUploads() or options.squash) and
2322 not options.no_squash)
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002323 # We assume the remote called "origin" is the one we want.
2324 # It is probably not worthwhile to support different workflows.
2325 gerrit_remote = 'origin'
2326
2327 remote, remote_branch = self.GetRemoteBranch()
2328 branch = GetTargetRef(remote, remote_branch, options.target_branch,
2329 pending_prefix='')
2330
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002331 if options.squash:
2332 if not self.GetIssue():
2333 # TODO(tandrii): deperecate this after 2016Q2. Backwards compatibility
2334 # with shadow branch, which used to contain change-id for a given
2335 # branch, using which we can fetch actual issue number and set it as the
2336 # property of the branch, which is the new way.
2337 message = RunGitSilent([
2338 'show', '--format=%B', '-s',
2339 'refs/heads/git_cl_uploads/%s' % self.GetBranch()])
2340 if message:
2341 change_ids = git_footers.get_footer_change_id(message.strip())
2342 if change_ids and len(change_ids) == 1:
2343 details = self._GetChangeDetail(issue=change_ids[0])
2344 if details:
2345 print('WARNING: found old upload in branch git_cl_uploads/%s '
2346 'corresponding to issue %s' %
2347 (self.GetBranch(), details['_number']))
2348 self.SetIssue(details['_number'])
2349 if not self.GetIssue():
2350 DieWithError(
2351 '\n' # For readability of the blob below.
2352 'Found old upload in branch git_cl_uploads/%s, '
2353 'but failed to find corresponding Gerrit issue.\n'
2354 'If you know the issue number, set it manually first:\n'
2355 ' git cl issue 123456\n'
2356 'If you intended to upload this CL as new issue, '
2357 'just delete or rename the old upload branch:\n'
2358 ' git rename-branch git_cl_uploads/%s old_upload-%s\n'
2359 'After that, please run git cl upload again.' %
2360 tuple([self.GetBranch()] * 3))
2361 # End of backwards compatability.
2362
2363 if self.GetIssue():
2364 # Try to get the message from a previous upload.
2365 message = self.GetDescription()
2366 if not message:
2367 DieWithError(
2368 'failed to fetch description from current Gerrit issue %d\n'
2369 '%s' % (self.GetIssue(), self.GetIssueURL()))
2370 change_id = self._GetChangeDetail()['change_id']
2371 while True:
2372 footer_change_ids = git_footers.get_footer_change_id(message)
2373 if footer_change_ids == [change_id]:
2374 break
2375 if not footer_change_ids:
2376 message = git_footers.add_footer_change_id(message, change_id)
2377 print('WARNING: appended missing Change-Id to issue description')
2378 continue
2379 # There is already a valid footer but with different or several ids.
2380 # Doing this automatically is non-trivial as we don't want to lose
2381 # existing other footers, yet we want to append just 1 desired
2382 # Change-Id. Thus, just create a new footer, but let user verify the
2383 # new description.
2384 message = '%s\n\nChange-Id: %s' % (message, change_id)
2385 print(
2386 'WARNING: issue %s has Change-Id footer(s):\n'
2387 ' %s\n'
2388 'but issue has Change-Id %s, according to Gerrit.\n'
2389 'Please, check the proposed correction to the description, '
2390 'and edit it if necessary but keep the "Change-Id: %s" footer\n'
2391 % (self.GetIssue(), '\n '.join(footer_change_ids), change_id,
2392 change_id))
2393 ask_for_data('Press enter to edit now, Ctrl+C to abort')
2394 if not options.force:
2395 change_desc = ChangeDescription(message)
2396 change_desc.prompt()
2397 message = change_desc.description
2398 if not message:
2399 DieWithError("Description is empty. Aborting...")
2400 # Continue the while loop.
2401 # Sanity check of this code - we should end up with proper message
2402 # footer.
2403 assert [change_id] == git_footers.get_footer_change_id(message)
2404 change_desc = ChangeDescription(message)
2405 else:
2406 change_desc = ChangeDescription(
2407 options.message or CreateDescriptionFromLog(args))
2408 if not options.force:
2409 change_desc.prompt()
2410 if not change_desc.description:
2411 DieWithError("Description is empty. Aborting...")
2412 message = change_desc.description
2413 change_ids = git_footers.get_footer_change_id(message)
2414 if len(change_ids) > 1:
2415 DieWithError('too many Change-Id footers, at most 1 allowed.')
2416 if not change_ids:
2417 # Generate the Change-Id automatically.
2418 message = git_footers.add_footer_change_id(
2419 message, GenerateGerritChangeId(message))
2420 change_desc.set_description(message)
2421 change_ids = git_footers.get_footer_change_id(message)
2422 assert len(change_ids) == 1
2423 change_id = change_ids[0]
2424
2425 remote, upstream_branch = self.FetchUpstreamTuple(self.GetBranch())
2426 if remote is '.':
2427 # If our upstream branch is local, we base our squashed commit on its
2428 # squashed version.
2429 upstream_branch_name = scm.GIT.ShortBranchName(upstream_branch)
2430 # Check the squashed hash of the parent.
2431 parent = RunGit(['config',
2432 'branch.%s.gerritsquashhash' % upstream_branch_name],
2433 error_ok=True).strip()
2434 # Verify that the upstream branch has been uploaded too, otherwise
2435 # Gerrit will create additional CLs when uploading.
2436 if not parent or (RunGitSilent(['rev-parse', upstream_branch + ':']) !=
2437 RunGitSilent(['rev-parse', parent + ':'])):
2438 # TODO(tandrii): remove "old depot_tools" part on April 12, 2016.
2439 DieWithError(
2440 'Upload upstream branch %s first.\n'
2441 'Note: maybe you\'ve uploaded it with --no-squash or with an old '
2442 'version of depot_tools. If so, then re-upload it with:\n'
2443 ' git cl upload --squash\n' % upstream_branch_name)
2444 else:
2445 parent = self.GetCommonAncestorWithUpstream()
2446
2447 tree = RunGit(['rev-parse', 'HEAD:']).strip()
2448 ref_to_push = RunGit(['commit-tree', tree, '-p', parent,
2449 '-m', message]).strip()
2450 else:
2451 change_desc = ChangeDescription(
2452 options.message or CreateDescriptionFromLog(args))
2453 if not change_desc.description:
2454 DieWithError("Description is empty. Aborting...")
2455
2456 if not git_footers.get_footer_change_id(change_desc.description):
2457 DownloadGerritHook(False)
tandrii@chromium.org8930b3d2016-04-13 14:47:02 +00002458 change_desc.set_description(self._AddChangeIdToCommitMessage(options,
2459 args))
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002460 ref_to_push = 'HEAD'
2461 parent = '%s/%s' % (gerrit_remote, branch)
2462 change_id = git_footers.get_footer_change_id(change_desc.description)[0]
2463
2464 assert change_desc
2465 commits = RunGitSilent(['rev-list', '%s..%s' % (parent,
2466 ref_to_push)]).splitlines()
2467 if len(commits) > 1:
2468 print('WARNING: This will upload %d commits. Run the following command '
2469 'to see which commits will be uploaded: ' % len(commits))
2470 print('git log %s..%s' % (parent, ref_to_push))
2471 print('You can also use `git squash-branch` to squash these into a '
2472 'single commit.')
2473 ask_for_data('About to upload; enter to confirm.')
2474
2475 if options.reviewers or options.tbr_owners:
2476 change_desc.update_reviewers(options.reviewers, options.tbr_owners,
2477 change)
2478
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +00002479 # Extra options that can be specified at push time. Doc:
2480 # https://gerrit-review.googlesource.com/Documentation/user-upload.html
2481 refspec_opts = []
2482 if options.title:
2483 # Per doc, spaces must be converted to underscores, and Gerrit will do the
2484 # reverse on its side.
2485 if '_' in options.title:
2486 print('WARNING: underscores in title will be converted to spaces.')
2487 refspec_opts.append('m=' + options.title.replace(' ', '_'))
2488
tandrii@chromium.org8da45402016-05-24 23:11:03 +00002489 if options.send_mail:
2490 if not change_desc.get_reviewers():
2491 DieWithError('Must specify reviewers to send email.')
2492 refspec_opts.append('notify=ALL')
2493 else:
2494 refspec_opts.append('notify=NONE')
2495
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002496 cc = self.GetCCList().split(',')
2497 if options.cc:
2498 cc.extend(options.cc)
2499 cc = filter(None, cc)
2500 if cc:
tandrii@chromium.org074c2af2016-06-03 23:18:40 +00002501 refspec_opts.extend('cc=' + email.strip() for email in cc)
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002502
tandrii@chromium.org8acd8332016-04-13 12:56:03 +00002503 if change_desc.get_reviewers():
2504 refspec_opts.extend('r=' + email.strip()
2505 for email in change_desc.get_reviewers())
2506
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +00002507 refspec_suffix = ''
2508 if refspec_opts:
2509 refspec_suffix = '%' + ','.join(refspec_opts)
2510 assert ' ' not in refspec_suffix, (
2511 'spaces not allowed in refspec: "%s"' % refspec_suffix)
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +00002512 refspec = '%s:refs/for/%s%s' % (ref_to_push, branch, refspec_suffix)
tandrii@chromium.orgbf766ba2016-04-13 12:51:23 +00002513
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002514 push_stdout = gclient_utils.CheckCallAndFilter(
tandrii@chromium.org8acd8332016-04-13 12:56:03 +00002515 ['git', 'push', gerrit_remote, refspec],
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002516 print_stdout=True,
2517 # Flush after every line: useful for seeing progress when running as
2518 # recipe.
2519 filter_fn=lambda _: sys.stdout.flush())
2520
2521 if options.squash:
2522 regex = re.compile(r'remote:\s+https?://[\w\-\.\/]*/(\d+)\s.*')
2523 change_numbers = [m.group(1)
2524 for m in map(regex.match, push_stdout.splitlines())
2525 if m]
2526 if len(change_numbers) != 1:
2527 DieWithError(
2528 ('Created|Updated %d issues on Gerrit, but only 1 expected.\n'
2529 'Change-Id: %s') % (len(change_numbers), change_id))
2530 self.SetIssue(change_numbers[0])
2531 RunGit(['config', 'branch.%s.gerritsquashhash' % self.GetBranch(),
2532 ref_to_push])
2533 return 0
2534
tandrii@chromium.org8930b3d2016-04-13 14:47:02 +00002535 def _AddChangeIdToCommitMessage(self, options, args):
2536 """Re-commits using the current message, assumes the commit hook is in
2537 place.
2538 """
2539 log_desc = options.message or CreateDescriptionFromLog(args)
2540 git_command = ['commit', '--amend', '-m', log_desc]
2541 RunGit(git_command)
2542 new_log_desc = CreateDescriptionFromLog(args)
2543 if git_footers.get_footer_change_id(new_log_desc):
2544 print 'git-cl: Added Change-Id to commit message.'
2545 return new_log_desc
2546 else:
tandrii@chromium.orgb067ec52016-05-31 15:24:44 +00002547 DieWithError('ERROR: Gerrit commit-msg hook not installed.')
tandrii@chromium.orgaa6235b2016-04-11 21:35:29 +00002548
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00002549 def SetCQState(self, new_state):
2550 """Sets the Commit-Queue label assuming canonical CQ config for Gerrit."""
2551 # TODO(tandrii): maybe allow configurability in codereview.settings or by
2552 # self-discovery of label config for this CL using REST API.
2553 vote_map = {
2554 _CQState.NONE: 0,
2555 _CQState.DRY_RUN: 1,
2556 _CQState.COMMIT : 2,
2557 }
2558 gerrit_util.SetReview(self._GetGerritHost(), self.GetIssue(),
2559 labels={'Commit-Queue': vote_map[new_state]})
2560
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +00002561
2562_CODEREVIEW_IMPLEMENTATIONS = {
2563 'rietveld': _RietveldChangelistImpl,
2564 'gerrit': _GerritChangelistImpl,
2565}
2566
tandrii@chromium.org013a2802016-03-29 09:52:33 +00002567
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00002568def _add_codereview_select_options(parser):
2569 """Appends --gerrit and --rietveld options to force specific codereview."""
2570 parser.codereview_group = optparse.OptionGroup(
2571 parser, 'EXPERIMENTAL! Codereview override options')
2572 parser.add_option_group(parser.codereview_group)
2573 parser.codereview_group.add_option(
2574 '--gerrit', action='store_true',
2575 help='Force the use of Gerrit for codereview')
2576 parser.codereview_group.add_option(
2577 '--rietveld', action='store_true',
2578 help='Force the use of Rietveld for codereview')
2579
2580
2581def _process_codereview_select_options(parser, options):
2582 if options.gerrit and options.rietveld:
2583 parser.error('Options --gerrit and --rietveld are mutually exclusive')
2584 options.forced_codereview = None
2585 if options.gerrit:
2586 options.forced_codereview = 'gerrit'
2587 elif options.rietveld:
2588 options.forced_codereview = 'rietveld'
2589
2590
dpranke@chromium.org20254fc2011-03-22 18:28:59 +00002591class ChangeDescription(object):
2592 """Contains a parsed form of the change description."""
maruel@chromium.orgc6f60e82013-04-19 17:01:57 +00002593 R_LINE = r'^[ \t]*(TBR|R)[ \t]*=[ \t]*(.*?)[ \t]*$'
agable@chromium.org42c20792013-09-12 17:34:49 +00002594 BUG_LINE = r'^[ \t]*(BUG)[ \t]*=[ \t]*(.*?)[ \t]*$'
dpranke@chromium.org20254fc2011-03-22 18:28:59 +00002595
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002596 def __init__(self, description):
agable@chromium.org42c20792013-09-12 17:34:49 +00002597 self._description_lines = (description or '').strip().splitlines()
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002598
agable@chromium.org42c20792013-09-12 17:34:49 +00002599 @property # www.logilab.org/ticket/89786
2600 def description(self): # pylint: disable=E0202
2601 return '\n'.join(self._description_lines)
2602
2603 def set_description(self, desc):
2604 if isinstance(desc, basestring):
2605 lines = desc.splitlines()
2606 else:
2607 lines = [line.rstrip() for line in desc]
2608 while lines and not lines[0]:
2609 lines.pop(0)
2610 while lines and not lines[-1]:
2611 lines.pop(-1)
2612 self._description_lines = lines
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002613
piman@chromium.org336f9122014-09-04 02:16:55 +00002614 def update_reviewers(self, reviewers, add_owners_tbr=False, change=None):
agable@chromium.org42c20792013-09-12 17:34:49 +00002615 """Rewrites the R=/TBR= line(s) as a single line each."""
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002616 assert isinstance(reviewers, list), reviewers
piman@chromium.org336f9122014-09-04 02:16:55 +00002617 if not reviewers and not add_owners_tbr:
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002618 return
agable@chromium.org42c20792013-09-12 17:34:49 +00002619 reviewers = reviewers[:]
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002620
agable@chromium.org42c20792013-09-12 17:34:49 +00002621 # Get the set of R= and TBR= lines and remove them from the desciption.
2622 regexp = re.compile(self.R_LINE)
2623 matches = [regexp.match(line) for line in self._description_lines]
2624 new_desc = [l for i, l in enumerate(self._description_lines)
2625 if not matches[i]]
2626 self.set_description(new_desc)
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002627
agable@chromium.org42c20792013-09-12 17:34:49 +00002628 # Construct new unified R= and TBR= lines.
2629 r_names = []
2630 tbr_names = []
2631 for match in matches:
2632 if not match:
2633 continue
2634 people = cleanup_list([match.group(2).strip()])
2635 if match.group(1) == 'TBR':
2636 tbr_names.extend(people)
2637 else:
2638 r_names.extend(people)
2639 for name in r_names:
2640 if name not in reviewers:
2641 reviewers.append(name)
piman@chromium.org336f9122014-09-04 02:16:55 +00002642 if add_owners_tbr:
2643 owners_db = owners.Database(change.RepositoryRoot(),
2644 fopen=file, os_path=os.path, glob=glob.glob)
2645 all_reviewers = set(tbr_names + reviewers)
2646 missing_files = owners_db.files_not_covered_by(change.LocalPaths(),
2647 all_reviewers)
2648 tbr_names.extend(owners_db.reviewers_for(missing_files,
2649 change.author_email))
agable@chromium.org42c20792013-09-12 17:34:49 +00002650 new_r_line = 'R=' + ', '.join(reviewers) if reviewers else None
2651 new_tbr_line = 'TBR=' + ', '.join(tbr_names) if tbr_names else None
2652
2653 # Put the new lines in the description where the old first R= line was.
2654 line_loc = next((i for i, match in enumerate(matches) if match), -1)
2655 if 0 <= line_loc < len(self._description_lines):
2656 if new_tbr_line:
2657 self._description_lines.insert(line_loc, new_tbr_line)
2658 if new_r_line:
2659 self._description_lines.insert(line_loc, new_r_line)
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002660 else:
agable@chromium.org42c20792013-09-12 17:34:49 +00002661 if new_r_line:
2662 self.append_footer(new_r_line)
2663 if new_tbr_line:
2664 self.append_footer(new_tbr_line)
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002665
2666 def prompt(self):
2667 """Asks the user to update the description."""
agable@chromium.org42c20792013-09-12 17:34:49 +00002668 self.set_description([
2669 '# Enter a description of the change.',
2670 '# This will be displayed on the codereview site.',
2671 '# The first line will also be used as the subject of the review.',
alancutter@chromium.orgbd1073e2013-06-01 00:34:38 +00002672 '#--------------------This line is 72 characters long'
agable@chromium.org42c20792013-09-12 17:34:49 +00002673 '--------------------',
2674 ] + self._description_lines)
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002675
agable@chromium.org42c20792013-09-12 17:34:49 +00002676 regexp = re.compile(self.BUG_LINE)
2677 if not any((regexp.match(line) for line in self._description_lines)):
rmistry@google.com90752582014-01-14 21:04:50 +00002678 self.append_footer('BUG=%s' % settings.GetBugPrefix())
agable@chromium.org42c20792013-09-12 17:34:49 +00002679 content = gclient_utils.RunEditor(self.description, True,
jbroman@chromium.org615a2622013-05-03 13:20:14 +00002680 git_editor=settings.GetGitEditor())
maruel@chromium.org0e0436a2011-10-25 13:32:41 +00002681 if not content:
2682 DieWithError('Running editor failed')
agable@chromium.org42c20792013-09-12 17:34:49 +00002683 lines = content.splitlines()
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002684
2685 # Strip off comments.
agable@chromium.org42c20792013-09-12 17:34:49 +00002686 clean_lines = [line.rstrip() for line in lines if not line.startswith('#')]
2687 if not clean_lines:
maruel@chromium.org0e0436a2011-10-25 13:32:41 +00002688 DieWithError('No CL description, aborting')
agable@chromium.org42c20792013-09-12 17:34:49 +00002689 self.set_description(clean_lines)
dpranke@chromium.org20254fc2011-03-22 18:28:59 +00002690
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002691 def append_footer(self, line):
tandrii@chromium.org601e1d12016-06-03 13:03:54 +00002692 """Adds a footer line to the description.
2693
2694 Differentiates legacy "KEY=xxx" footers (used to be called tags) and
2695 Gerrit's footers in the form of "Footer-Key: footer any value" and ensures
2696 that Gerrit footers are always at the end.
2697 """
2698 parsed_footer_line = git_footers.parse_footer(line)
2699 if parsed_footer_line:
2700 # Line is a gerrit footer in the form: Footer-Key: any value.
2701 # Thus, must be appended observing Gerrit footer rules.
2702 self.set_description(
2703 git_footers.add_footer(self.description,
2704 key=parsed_footer_line[0],
2705 value=parsed_footer_line[1]))
2706 return
2707
2708 if not self._description_lines:
2709 self._description_lines.append(line)
2710 return
2711
2712 top_lines, gerrit_footers, _ = git_footers.split_footers(self.description)
2713 if gerrit_footers:
2714 # git_footers.split_footers ensures that there is an empty line before
2715 # actual (gerrit) footers, if any. We have to keep it that way.
2716 assert top_lines and top_lines[-1] == ''
2717 top_lines, separator = top_lines[:-1], top_lines[-1:]
2718 else:
2719 separator = [] # No need for separator if there are no gerrit_footers.
2720
2721 prev_line = top_lines[-1] if top_lines else ''
2722 if (not presubmit_support.Change.TAG_LINE_RE.match(prev_line) or
2723 not presubmit_support.Change.TAG_LINE_RE.match(line)):
2724 top_lines.append('')
2725 top_lines.append(line)
2726 self._description_lines = top_lines + separator + gerrit_footers
dpranke@chromium.org20254fc2011-03-22 18:28:59 +00002727
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002728 def get_reviewers(self):
2729 """Retrieves the list of reviewers."""
agable@chromium.org42c20792013-09-12 17:34:49 +00002730 matches = [re.match(self.R_LINE, line) for line in self._description_lines]
2731 reviewers = [match.group(2).strip() for match in matches if match]
maruel@chromium.org78936cb2013-04-11 00:17:52 +00002732 return cleanup_list(reviewers)
dpranke@chromium.org20254fc2011-03-22 18:28:59 +00002733
2734
maruel@chromium.orge52678e2013-04-26 18:34:44 +00002735def get_approving_reviewers(props):
2736 """Retrieves the reviewers that approved a CL from the issue properties with
2737 messages.
2738
2739 Note that the list may contain reviewers that are not committer, thus are not
2740 considered by the CQ.
2741 """
2742 return sorted(
2743 set(
2744 message['sender']
2745 for message in props['messages']
2746 if message['approval'] and message['sender'] in props['reviewers']
2747 )
2748 )
2749
2750
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002751def FindCodereviewSettingsFile(filename='codereview.settings'):
2752 """Finds the given file starting in the cwd and going up.
2753
2754 Only looks up to the top of the repository unless an
2755 'inherit-review-settings-ok' file exists in the root of the repository.
2756 """
2757 inherit_ok_file = 'inherit-review-settings-ok'
2758 cwd = os.getcwd()
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00002759 root = settings.GetRoot()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002760 if os.path.isfile(os.path.join(root, inherit_ok_file)):
2761 root = '/'
2762 while True:
2763 if filename in os.listdir(cwd):
2764 if os.path.isfile(os.path.join(cwd, filename)):
2765 return open(os.path.join(cwd, filename))
2766 if cwd == root:
2767 break
2768 cwd = os.path.dirname(cwd)
2769
2770
2771def LoadCodereviewSettingsFromFile(fileobj):
2772 """Parse a codereview.settings file and updates hooks."""
maruel@chromium.org99ac1c52012-01-16 14:52:12 +00002773 keyvals = gclient_utils.ParseCodereviewSettingsContent(fileobj.read())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002774
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002775 def SetProperty(name, setting, unset_error_ok=False):
2776 fullname = 'rietveld.' + name
2777 if setting in keyvals:
2778 RunGit(['config', fullname, keyvals[setting]])
2779 else:
2780 RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok)
2781
2782 SetProperty('server', 'CODE_REVIEW_SERVER')
2783 # Only server setting is required. Other settings can be absent.
2784 # In that case, we ignore errors raised during option deletion attempt.
2785 SetProperty('cc', 'CC_LIST', unset_error_ok=True)
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +00002786 SetProperty('private', 'PRIVATE', unset_error_ok=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002787 SetProperty('tree-status-url', 'STATUS', unset_error_ok=True)
2788 SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True)
rmistry@google.com90752582014-01-14 21:04:50 +00002789 SetProperty('bug-prefix', 'BUG_PREFIX', unset_error_ok=True)
thestig@chromium.org44202a22014-03-11 19:22:18 +00002790 SetProperty('cpplint-regex', 'LINT_REGEX', unset_error_ok=True)
kjellander@chromium.org6abc6522014-12-02 07:34:49 +00002791 SetProperty('force-https-commit-url', 'FORCE_HTTPS_COMMIT_URL',
2792 unset_error_ok=True)
thestig@chromium.org44202a22014-03-11 19:22:18 +00002793 SetProperty('cpplint-ignore-regex', 'LINT_IGNORE_REGEX', unset_error_ok=True)
sheyang@chromium.org152cf832014-06-11 21:37:49 +00002794 SetProperty('project', 'PROJECT', unset_error_ok=True)
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00002795 SetProperty('pending-ref-prefix', 'PENDING_REF_PREFIX', unset_error_ok=True)
rmistry@google.com5626a922015-02-26 14:03:30 +00002796 SetProperty('run-post-upload-hook', 'RUN_POST_UPLOAD_HOOK',
2797 unset_error_ok=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002798
ukai@chromium.org7044efc2013-11-28 01:51:21 +00002799 if 'GERRIT_HOST' in keyvals:
ukai@chromium.orge8077812012-02-03 03:41:46 +00002800 RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']])
ukai@chromium.orge8077812012-02-03 03:41:46 +00002801
bauerb@chromium.org54b400c2016-01-14 10:08:25 +00002802 if 'GERRIT_SQUASH_UPLOADS' in keyvals:
2803 RunGit(['config', 'gerrit.squash-uploads',
2804 keyvals['GERRIT_SQUASH_UPLOADS']])
2805
tandrii@chromium.org28253532016-04-14 13:46:56 +00002806 if 'GERRIT_SKIP_ENSURE_AUTHENTICATED' in keyvals:
shinyak@chromium.org00dbccd2016-04-15 07:24:43 +00002807 RunGit(['config', 'gerrit.skip-ensure-authenticated',
tandrii@chromium.org28253532016-04-14 13:46:56 +00002808 keyvals['GERRIT_SKIP_ENSURE_AUTHENTICATED']])
2809
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002810 if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals:
2811 #should be of the form
2812 #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof
2813 #ORIGIN_URL_CONFIG: http://src.chromium.org/git
2814 RunGit(['config', keyvals['PUSH_URL_CONFIG'],
2815 keyvals['ORIGIN_URL_CONFIG']])
2816
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002817
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +00002818def urlretrieve(source, destination):
2819 """urllib is broken for SSL connections via a proxy therefore we
2820 can't use urllib.urlretrieve()."""
2821 with open(destination, 'w') as f:
2822 f.write(urllib2.urlopen(source).read())
2823
2824
ukai@chromium.org712d6102013-11-27 00:52:58 +00002825def hasSheBang(fname):
2826 """Checks fname is a #! script."""
2827 with open(fname) as f:
2828 return f.read(2).startswith('#!')
2829
2830
bpastene@chromium.org917f0ff2016-04-05 00:45:30 +00002831# TODO(bpastene) Remove once a cleaner fix to crbug.com/600473 presents itself.
2832def DownloadHooks(*args, **kwargs):
2833 pass
2834
2835
tandrii@chromium.org18630d62016-03-04 12:06:02 +00002836def DownloadGerritHook(force):
2837 """Download and install Gerrit commit-msg hook.
ukai@chromium.org78c4b982012-02-14 02:20:26 +00002838
2839 Args:
2840 force: True to update hooks. False to install hooks if not present.
2841 """
2842 if not settings.GetIsGerrit():
2843 return
ukai@chromium.org712d6102013-11-27 00:52:58 +00002844 src = 'https://gerrit-review.googlesource.com/tools/hooks/commit-msg'
ukai@chromium.org78c4b982012-02-14 02:20:26 +00002845 dst = os.path.join(settings.GetRoot(), '.git', 'hooks', 'commit-msg')
2846 if not os.access(dst, os.X_OK):
2847 if os.path.exists(dst):
2848 if not force:
2849 return
ukai@chromium.org78c4b982012-02-14 02:20:26 +00002850 try:
tandrii@chromium.org18630d62016-03-04 12:06:02 +00002851 print(
2852 'WARNING: installing Gerrit commit-msg hook.\n'
2853 ' This behavior of git cl will soon be disabled.\n'
2854 ' See bug http://crbug.com/579176.')
joshua.lock@intel.com426f69b2012-08-02 23:41:49 +00002855 urlretrieve(src, dst)
ukai@chromium.org712d6102013-11-27 00:52:58 +00002856 if not hasSheBang(dst):
2857 DieWithError('Not a script: %s\n'
2858 'You need to download from\n%s\n'
2859 'into .git/hooks/commit-msg and '
2860 'chmod +x .git/hooks/commit-msg' % (dst, src))
ukai@chromium.org78c4b982012-02-14 02:20:26 +00002861 os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
2862 except Exception:
2863 if os.path.exists(dst):
2864 os.remove(dst)
ukai@chromium.org712d6102013-11-27 00:52:58 +00002865 DieWithError('\nFailed to download hooks.\n'
2866 'You need to download from\n%s\n'
2867 'into .git/hooks/commit-msg and '
2868 'chmod +x .git/hooks/commit-msg' % src)
ukai@chromium.org78c4b982012-02-14 02:20:26 +00002869
2870
tandrii@chromium.orge7d3d162016-03-15 14:15:57 +00002871
2872def GetRietveldCodereviewSettingsInteractively():
2873 """Prompt the user for settings."""
2874 server = settings.GetDefaultServerUrl(error_ok=True)
2875 prompt = 'Rietveld server (host[:port])'
2876 prompt += ' [%s]' % (server or DEFAULT_SERVER)
2877 newserver = ask_for_data(prompt + ':')
2878 if not server and not newserver:
2879 newserver = DEFAULT_SERVER
2880 if newserver:
2881 newserver = gclient_utils.UpgradeToHttps(newserver)
2882 if newserver != server:
2883 RunGit(['config', 'rietveld.server', newserver])
2884
2885 def SetProperty(initial, caption, name, is_url):
2886 prompt = caption
2887 if initial:
2888 prompt += ' ("x" to clear) [%s]' % initial
2889 new_val = ask_for_data(prompt + ':')
2890 if new_val == 'x':
2891 RunGit(['config', '--unset-all', 'rietveld.' + name], error_ok=True)
2892 elif new_val:
2893 if is_url:
2894 new_val = gclient_utils.UpgradeToHttps(new_val)
2895 if new_val != initial:
2896 RunGit(['config', 'rietveld.' + name, new_val])
2897
2898 SetProperty(settings.GetDefaultCCList(), 'CC list', 'cc', False)
2899 SetProperty(settings.GetDefaultPrivateFlag(),
2900 'Private flag (rietveld only)', 'private', False)
2901 SetProperty(settings.GetTreeStatusUrl(error_ok=True), 'Tree status URL',
2902 'tree-status-url', False)
2903 SetProperty(settings.GetViewVCUrl(), 'ViewVC URL', 'viewvc-url', True)
2904 SetProperty(settings.GetBugPrefix(), 'Bug Prefix', 'bug-prefix', False)
2905 SetProperty(settings.GetRunPostUploadHook(), 'Run Post Upload Hook',
2906 'run-post-upload-hook', False)
2907
maruel@chromium.org0633fb42013-08-16 20:06:14 +00002908@subcommand.usage('[repo root containing codereview.settings]')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002909def CMDconfig(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00002910 """Edits configuration for this tree."""
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002911
tandrii@chromium.orge7d3d162016-03-15 14:15:57 +00002912 print('WARNING: git cl config works for Rietveld only.\n'
tandrii@chromium.org8930b3d2016-04-13 14:47:02 +00002913 'For Gerrit, see http://crbug.com/603116.')
2914 # TODO(tandrii): add Gerrit support as part of http://crbug.com/603116.
pgervais@chromium.org87884cc2014-01-03 22:23:41 +00002915 parser.add_option('--activate-update', action='store_true',
2916 help='activate auto-updating [rietveld] section in '
2917 '.git/config')
2918 parser.add_option('--deactivate-update', action='store_true',
2919 help='deactivate auto-updating [rietveld] section in '
2920 '.git/config')
2921 options, args = parser.parse_args(args)
2922
2923 if options.deactivate_update:
2924 RunGit(['config', 'rietveld.autoupdate', 'false'])
2925 return
2926
2927 if options.activate_update:
2928 RunGit(['config', '--unset', 'rietveld.autoupdate'])
2929 return
2930
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002931 if len(args) == 0:
tandrii@chromium.orge7d3d162016-03-15 14:15:57 +00002932 GetRietveldCodereviewSettingsInteractively()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00002933 return 0
2934
2935 url = args[0]
2936 if not url.endswith('codereview.settings'):
2937 url = os.path.join(url, 'codereview.settings')
2938
2939 # Load code review settings and download hooks (if available).
2940 LoadCodereviewSettingsFromFile(urllib2.urlopen(url))
2941 return 0
2942
2943
kalmard@homejinni.com6b0051e2012-04-03 15:45:08 +00002944def CMDbaseurl(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00002945 """Gets or sets base-url for this branch."""
kalmard@homejinni.com6b0051e2012-04-03 15:45:08 +00002946 branchref = RunGit(['symbolic-ref', 'HEAD']).strip()
2947 branch = ShortBranchName(branchref)
2948 _, args = parser.parse_args(args)
2949 if not args:
2950 print("Current base-url:")
2951 return RunGit(['config', 'branch.%s.base-url' % branch],
2952 error_ok=False).strip()
2953 else:
2954 print("Setting base-url to %s" % args[0])
2955 return RunGit(['config', 'branch.%s.base-url' % branch, args[0]],
2956 error_ok=False).strip()
2957
2958
jsbell@chromium.orgb99fbd92014-09-11 17:29:28 +00002959def color_for_status(status):
2960 """Maps a Changelist status to color, for CMDstatus and other tools."""
2961 return {
2962 'unsent': Fore.RED,
2963 'waiting': Fore.BLUE,
2964 'reply': Fore.YELLOW,
2965 'lgtm': Fore.GREEN,
2966 'commit': Fore.MAGENTA,
2967 'closed': Fore.CYAN,
2968 'error': Fore.WHITE,
2969 }.get(status, Fore.WHITE)
2970
tandrii@chromium.org04ea8462016-04-25 19:51:21 +00002971
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00002972def get_cl_statuses(changes, fine_grained, max_processes=None):
2973 """Returns a blocking iterable of (cl, status) for given branches.
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00002974
2975 If fine_grained is true, this will fetch CL statuses from the server.
2976 Otherwise, simply indicate if there's a matching url for the given branches.
2977
2978 If max_processes is specified, it is used as the maximum number of processes
2979 to spawn to fetch CL status from the server. Otherwise 1 process per branch is
2980 spawned.
calamity@chromium.orgcf197482016-04-29 20:15:53 +00002981
2982 See GetStatus() for a list of possible statuses.
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00002983 """
2984 # Silence upload.py otherwise it becomes unwieldly.
2985 upload.verbosity = 0
2986
2987 if fine_grained:
2988 # Process one branch synchronously to work through authentication, then
2989 # spawn processes to process all the other branches in parallel.
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00002990 if changes:
2991 fetch = lambda cl: (cl, cl.GetStatus())
2992 yield fetch(changes[0])
calamity@chromium.orgcf197482016-04-29 20:15:53 +00002993
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00002994 changes_to_fetch = changes[1:]
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00002995 pool = ThreadPool(
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00002996 min(max_processes, len(changes_to_fetch))
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00002997 if max_processes is not None
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00002998 else len(changes_to_fetch))
calamity@chromium.orgcf197482016-04-29 20:15:53 +00002999
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003000 fetched_cls = set()
3001 it = pool.imap_unordered(fetch, changes_to_fetch).__iter__()
calamity@chromium.orgcf197482016-04-29 20:15:53 +00003002 while True:
3003 try:
3004 row = it.next(timeout=5)
3005 except multiprocessing.TimeoutError:
3006 break
3007
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003008 fetched_cls.add(row[0])
calamity@chromium.orgcf197482016-04-29 20:15:53 +00003009 yield row
3010
3011 # Add any branches that failed to fetch.
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003012 for cl in set(changes_to_fetch) - fetched_cls:
3013 yield (cl, 'error')
calamity@chromium.orgcf197482016-04-29 20:15:53 +00003014
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00003015 else:
3016 # Do not use GetApprovingReviewers(), since it requires an HTTP request.
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003017 for cl in changes:
3018 yield (cl, 'waiting' if cl.GetIssueURL() else 'error')
jsbell@chromium.orgb99fbd92014-09-11 17:29:28 +00003019
rmistry@google.com2dd99862015-06-22 12:22:18 +00003020
3021def upload_branch_deps(cl, args):
3022 """Uploads CLs of local branches that are dependents of the current branch.
3023
3024 If the local branch dependency tree looks like:
3025 test1 -> test2.1 -> test3.1
3026 -> test3.2
3027 -> test2.2 -> test3.3
3028
3029 and you run "git cl upload --dependencies" from test1 then "git cl upload" is
3030 run on the dependent branches in this order:
3031 test2.1, test3.1, test3.2, test2.2, test3.3
3032
3033 Note: This function does not rebase your local dependent branches. Use it when
3034 you make a change to the parent branch that will not conflict with its
3035 dependent branches, and you would like their dependencies updated in
3036 Rietveld.
3037 """
3038 if git_common.is_dirty_git_tree('upload-branch-deps'):
3039 return 1
3040
3041 root_branch = cl.GetBranch()
3042 if root_branch is None:
3043 DieWithError('Can\'t find dependent branches from detached HEAD state. '
3044 'Get on a branch!')
3045 if not cl.GetIssue() or not cl.GetPatchset():
3046 DieWithError('Current branch does not have an uploaded CL. We cannot set '
3047 'patchset dependencies without an uploaded CL.')
3048
3049 branches = RunGit(['for-each-ref',
3050 '--format=%(refname:short) %(upstream:short)',
3051 'refs/heads'])
3052 if not branches:
3053 print('No local branches found.')
3054 return 0
3055
3056 # Create a dictionary of all local branches to the branches that are dependent
3057 # on it.
3058 tracked_to_dependents = collections.defaultdict(list)
3059 for b in branches.splitlines():
3060 tokens = b.split()
3061 if len(tokens) == 2:
3062 branch_name, tracked = tokens
3063 tracked_to_dependents[tracked].append(branch_name)
3064
3065 print
3066 print 'The dependent local branches of %s are:' % root_branch
3067 dependents = []
3068 def traverse_dependents_preorder(branch, padding=''):
3069 dependents_to_process = tracked_to_dependents.get(branch, [])
3070 padding += ' '
3071 for dependent in dependents_to_process:
3072 print '%s%s' % (padding, dependent)
3073 dependents.append(dependent)
3074 traverse_dependents_preorder(dependent, padding)
3075 traverse_dependents_preorder(root_branch)
3076 print
3077
3078 if not dependents:
3079 print 'There are no dependent local branches for %s' % root_branch
3080 return 0
3081
3082 print ('This command will checkout all dependent branches and run '
3083 '"git cl upload".')
3084 ask_for_data('[Press enter to continue or ctrl-C to quit]')
3085
andybons@chromium.org962f9462016-02-03 20:00:42 +00003086 # Add a default patchset title to all upload calls in Rietveld.
tandrii@chromium.org4c72b082016-03-31 22:26:35 +00003087 if not cl.IsGerrit():
andybons@chromium.org962f9462016-02-03 20:00:42 +00003088 args.extend(['-t', 'Updated patchset dependency'])
3089
rmistry@google.com2dd99862015-06-22 12:22:18 +00003090 # Record all dependents that failed to upload.
3091 failures = {}
3092 # Go through all dependents, checkout the branch and upload.
3093 try:
3094 for dependent_branch in dependents:
3095 print
3096 print '--------------------------------------'
3097 print 'Running "git cl upload" from %s:' % dependent_branch
3098 RunGit(['checkout', '-q', dependent_branch])
3099 print
3100 try:
3101 if CMDupload(OptionParser(), args) != 0:
3102 print 'Upload failed for %s!' % dependent_branch
3103 failures[dependent_branch] = 1
3104 except: # pylint: disable=W0702
3105 failures[dependent_branch] = 1
3106 print
3107 finally:
3108 # Swap back to the original root branch.
3109 RunGit(['checkout', '-q', root_branch])
3110
3111 print
3112 print 'Upload complete for dependent branches!'
3113 for dependent_branch in dependents:
3114 upload_status = 'failed' if failures.get(dependent_branch) else 'succeeded'
3115 print ' %s : %s' % (dependent_branch, upload_status)
3116 print
3117
3118 return 0
3119
3120
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003121def CMDstatus(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00003122 """Show status of changelists.
3123
3124 Colors are used to tell the state of the CL unless --fast is used:
jsbell@chromium.orgaeab41a2013-12-10 20:01:22 +00003125 - Red not sent for review or broken
3126 - Blue waiting for review
3127 - Yellow waiting for you to reply to review
3128 - Green LGTM'ed
3129 - Magenta in the commit queue
3130 - Cyan was committed, branch can be deleted
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00003131
3132 Also see 'git cl comments'.
3133 """
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003134 parser.add_option('--field',
3135 help='print only specific field (desc|id|patch|url)')
maruel@chromium.org1033efd2013-07-23 23:25:09 +00003136 parser.add_option('-f', '--fast', action='store_true',
3137 help='Do not retrieve review status')
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00003138 parser.add_option(
3139 '-j', '--maxjobs', action='store', type=int,
3140 help='The maximum number of jobs to use when retrieving review status')
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003141
3142 auth.add_auth_options(parser)
3143 options, args = parser.parse_args(args)
maruel@chromium.org39c0b222013-08-17 16:57:01 +00003144 if args:
3145 parser.error('Unsupported args: %s' % args)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003146 auth_config = auth.extract_auth_config_from_options(options)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003147
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003148 if options.field:
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003149 cl = Changelist(auth_config=auth_config)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003150 if options.field.startswith('desc'):
3151 print cl.GetDescription()
3152 elif options.field == 'id':
3153 issueid = cl.GetIssue()
3154 if issueid:
3155 print issueid
3156 elif options.field == 'patch':
3157 patchset = cl.GetPatchset()
3158 if patchset:
3159 print patchset
3160 elif options.field == 'url':
3161 url = cl.GetIssueURL()
3162 if url:
3163 print url
maruel@chromium.orge25c75b2013-07-23 18:30:56 +00003164 return 0
3165
3166 branches = RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads'])
3167 if not branches:
3168 print('No local branch found.')
3169 return 0
3170
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003171 changes = [
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003172 Changelist(branchref=b, auth_config=auth_config)
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003173 for b in branches.splitlines()]
maruel@chromium.orge25c75b2013-07-23 18:30:56 +00003174 print 'Branches associated with reviews:'
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003175 output = get_cl_statuses(changes,
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00003176 fine_grained=not options.fast,
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003177 max_processes=options.maxjobs)
maruel@chromium.org1033efd2013-07-23 23:25:09 +00003178
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00003179 branch_statuses = {}
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003180 alignment = max(5, max(len(ShortBranchName(c.GetBranch())) for c in changes))
3181 for cl in sorted(changes, key=lambda c: c.GetBranch()):
3182 branch = cl.GetBranch()
calamity@chromium.orgffde55c2015-03-12 00:44:17 +00003183 while branch not in branch_statuses:
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003184 c, status = output.next()
3185 branch_statuses[c.GetBranch()] = status
3186 status = branch_statuses.pop(branch)
3187 url = cl.GetIssueURL()
3188 if url and (not status or status == 'error'):
3189 # The issue probably doesn't exist anymore.
3190 url += ' (broken)'
3191
nodir@chromium.orga6de1f42015-06-10 04:23:17 +00003192 color = color_for_status(status)
maruel@chromium.org885f6512013-07-27 02:17:26 +00003193 reset = Fore.RESET
iannucci@chromium.org596cd5c2016-04-04 21:34:39 +00003194 if not setup_color.IS_TTY:
maruel@chromium.org885f6512013-07-27 02:17:26 +00003195 color = ''
3196 reset = ''
nodir@chromium.orga6de1f42015-06-10 04:23:17 +00003197 status_str = '(%s)' % status if status else ''
3198 print ' %*s : %s%s %s%s' % (
clemensh@chromium.orgcbd7dc32016-05-31 10:33:50 +00003199 alignment, ShortBranchName(branch), color, url,
3200 status_str, reset)
maruel@chromium.org1033efd2013-07-23 23:25:09 +00003201
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003202 cl = Changelist(auth_config=auth_config)
maruel@chromium.orge25c75b2013-07-23 18:30:56 +00003203 print
3204 print 'Current branch:',
maruel@chromium.orge25c75b2013-07-23 18:30:56 +00003205 print cl.GetBranch()
dpranke@chromium.orgee87f582015-07-31 18:46:25 +00003206 if not cl.GetIssue():
3207 print 'No issue assigned.'
3208 return 0
maruel@chromium.orge25c75b2013-07-23 18:30:56 +00003209 print 'Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL())
maruel@chromium.org85616e02014-07-28 15:37:55 +00003210 if not options.fast:
3211 print 'Issue description:'
3212 print cl.GetDescription(pretty=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003213 return 0
3214
3215
maruel@chromium.org39c0b222013-08-17 16:57:01 +00003216def colorize_CMDstatus_doc():
3217 """To be called once in main() to add colors to git cl status help."""
3218 colors = [i for i in dir(Fore) if i[0].isupper()]
3219
3220 def colorize_line(line):
3221 for color in colors:
3222 if color in line.upper():
3223 # Extract whitespaces first and the leading '-'.
3224 indent = len(line) - len(line.lstrip(' ')) + 1
3225 return line[:indent] + getattr(Fore, color) + line[indent:] + Fore.RESET
3226 return line
3227
3228 lines = CMDstatus.__doc__.splitlines()
3229 CMDstatus.__doc__ = '\n'.join(colorize_line(l) for l in lines)
3230
3231
maruel@chromium.org0633fb42013-08-16 20:06:14 +00003232@subcommand.usage('[issue_number]')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003233def CMDissue(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00003234 """Sets or displays the current code review issue number.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003235
3236 Pass issue number 0 to clear the current issue.
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00003237 """
dnj@chromium.org406c4402015-03-03 17:22:28 +00003238 parser.add_option('-r', '--reverse', action='store_true',
3239 help='Lookup the branch(es) for the specified issues. If '
3240 'no issues are specified, all branches with mapped '
3241 'issues will be listed.')
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00003242 _add_codereview_select_options(parser)
dnj@chromium.org406c4402015-03-03 17:22:28 +00003243 options, args = parser.parse_args(args)
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00003244 _process_codereview_select_options(parser, options)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003245
dnj@chromium.org406c4402015-03-03 17:22:28 +00003246 if options.reverse:
3247 branches = RunGit(['for-each-ref', 'refs/heads',
3248 '--format=%(refname:short)']).splitlines()
3249
3250 # Reverse issue lookup.
3251 issue_branch_map = {}
3252 for branch in branches:
3253 cl = Changelist(branchref=branch)
3254 issue_branch_map.setdefault(cl.GetIssue(), []).append(branch)
3255 if not args:
3256 args = sorted(issue_branch_map.iterkeys())
3257 for issue in args:
3258 if not issue:
3259 continue
3260 print 'Branch for issue number %s: %s' % (
3261 issue, ', '.join(issue_branch_map.get(int(issue)) or ('None',)))
3262 else:
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00003263 cl = Changelist(codereview=options.forced_codereview)
dnj@chromium.org406c4402015-03-03 17:22:28 +00003264 if len(args) > 0:
3265 try:
3266 issue = int(args[0])
3267 except ValueError:
3268 DieWithError('Pass a number to set the issue or none to list it.\n'
tandrii@chromium.org8930b3d2016-04-13 14:47:02 +00003269 'Maybe you want to run git cl status?')
dnj@chromium.org406c4402015-03-03 17:22:28 +00003270 cl.SetIssue(issue)
3271 print 'Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003272 return 0
3273
3274
maruel@chromium.org9977a2e2012-06-06 22:30:56 +00003275def CMDcomments(parser, args):
apavlov@chromium.orge4efd512014-11-05 09:05:29 +00003276 """Shows or posts review comments for any changelist."""
3277 parser.add_option('-a', '--add-comment', dest='comment',
3278 help='comment to add to an issue')
3279 parser.add_option('-i', dest='issue',
3280 help="review issue id (defaults to current issue)")
smut@google.comc85ac942015-09-15 16:34:43 +00003281 parser.add_option('-j', '--json-file',
3282 help='File to write JSON summary to')
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003283 auth.add_auth_options(parser)
apavlov@chromium.orge4efd512014-11-05 09:05:29 +00003284 options, args = parser.parse_args(args)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003285 auth_config = auth.extract_auth_config_from_options(options)
maruel@chromium.org9977a2e2012-06-06 22:30:56 +00003286
apavlov@chromium.orge4efd512014-11-05 09:05:29 +00003287 issue = None
3288 if options.issue:
3289 try:
3290 issue = int(options.issue)
3291 except ValueError:
3292 DieWithError('A review issue id is expected to be a number')
3293
tandrii@chromium.orgaa5ced12016-03-29 09:41:14 +00003294 cl = Changelist(issue=issue, codereview='rietveld', auth_config=auth_config)
apavlov@chromium.orge4efd512014-11-05 09:05:29 +00003295
3296 if options.comment:
3297 cl.AddComment(options.comment)
3298 return 0
3299
3300 data = cl.GetIssueProperties()
smut@google.comc85ac942015-09-15 16:34:43 +00003301 summary = []
maruel@chromium.org5cab2d32014-11-11 18:32:41 +00003302 for message in sorted(data.get('messages', []), key=lambda x: x['date']):
smut@google.comc85ac942015-09-15 16:34:43 +00003303 summary.append({
3304 'date': message['date'],
3305 'lgtm': False,
3306 'message': message['text'],
3307 'not_lgtm': False,
3308 'sender': message['sender'],
3309 })
apavlov@chromium.orge4efd512014-11-05 09:05:29 +00003310 if message['disapproval']:
3311 color = Fore.RED
smut@google.comc85ac942015-09-15 16:34:43 +00003312 summary[-1]['not lgtm'] = True
apavlov@chromium.orge4efd512014-11-05 09:05:29 +00003313 elif message['approval']:
3314 color = Fore.GREEN
smut@google.comc85ac942015-09-15 16:34:43 +00003315 summary[-1]['lgtm'] = True
apavlov@chromium.orge4efd512014-11-05 09:05:29 +00003316 elif message['sender'] == data['owner_email']:
3317 color = Fore.MAGENTA
3318 else:
3319 color = Fore.BLUE
3320 print '\n%s%s %s%s' % (
3321 color, message['date'].split('.', 1)[0], message['sender'],
3322 Fore.RESET)
3323 if message['text'].strip():
3324 print '\n'.join(' ' + l for l in message['text'].splitlines())
smut@google.comc85ac942015-09-15 16:34:43 +00003325 if options.json_file:
3326 with open(options.json_file, 'wb') as f:
3327 json.dump(summary, f)
maruel@chromium.org9977a2e2012-06-06 22:30:56 +00003328 return 0
3329
3330
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00003331@subcommand.usage('[codereview url or issue id]')
rsesek@chromium.orgeec76592013-05-20 16:27:57 +00003332def CMDdescription(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00003333 """Brings up the editor for the current CL's description."""
smut@google.com34fb6b12015-07-13 20:03:26 +00003334 parser.add_option('-d', '--display', action='store_true',
3335 help='Display the description instead of opening an editor')
martiniss@chromium.orgd6648e22016-04-29 19:22:16 +00003336 parser.add_option('-n', '--new-description',
3337 help='New description to set for this issue (- for stdin)')
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00003338
3339 _add_codereview_select_options(parser)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003340 auth.add_auth_options(parser)
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00003341 options, args = parser.parse_args(args)
3342 _process_codereview_select_options(parser, options)
3343
3344 target_issue = None
3345 if len(args) > 0:
3346 issue_arg = ParseIssueNumberArgument(args[0])
3347 if not issue_arg.valid:
3348 parser.print_help()
3349 return 1
3350 target_issue = issue_arg.issue
3351
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003352 auth_config = auth.extract_auth_config_from_options(options)
martiniss@chromium.org2b55fe32016-04-26 20:28:54 +00003353
3354 cl = Changelist(
3355 auth_config=auth_config, issue=target_issue,
3356 codereview=options.forced_codereview)
3357
rsesek@chromium.orgeec76592013-05-20 16:27:57 +00003358 if not cl.GetIssue():
3359 DieWithError('This branch has no associated changelist.')
3360 description = ChangeDescription(cl.GetDescription())
martiniss@chromium.orgd6648e22016-04-29 19:22:16 +00003361
smut@google.com34fb6b12015-07-13 20:03:26 +00003362 if options.display:
tandrii@chromium.org8c3b4422016-04-27 13:11:18 +00003363 print description.description
smut@google.com34fb6b12015-07-13 20:03:26 +00003364 return 0
martiniss@chromium.orgd6648e22016-04-29 19:22:16 +00003365
3366 if options.new_description:
3367 text = options.new_description
3368 if text == '-':
3369 text = '\n'.join(l.rstrip() for l in sys.stdin)
3370
3371 description.set_description(text)
3372 else:
3373 description.prompt()
3374
wychen@chromium.org063e4e52015-04-03 06:51:44 +00003375 if cl.GetDescription() != description.description:
3376 cl.UpdateDescription(description.description)
rsesek@chromium.orgeec76592013-05-20 16:27:57 +00003377 return 0
3378
3379
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003380def CreateDescriptionFromLog(args):
3381 """Pulls out the commit log to use as a base for the CL description."""
3382 log_args = []
3383 if len(args) == 1 and not args[0].endswith('.'):
3384 log_args = [args[0] + '..']
3385 elif len(args) == 1 and args[0].endswith('...'):
3386 log_args = [args[0][:-1]]
3387 elif len(args) == 2:
3388 log_args = [args[0] + '..' + args[1]]
3389 else:
3390 log_args = args[:] # Hope for the best!
maruel@chromium.org373af802012-05-25 21:07:33 +00003391 return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003392
3393
thestig@chromium.org44202a22014-03-11 19:22:18 +00003394def CMDlint(parser, args):
3395 """Runs cpplint on the current changelist."""
tzik@chromium.orgf204d4b2014-03-13 07:40:55 +00003396 parser.add_option('--filter', action='append', metavar='-x,+y',
3397 help='Comma-separated list of cpplint\'s category-filters')
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003398 auth.add_auth_options(parser)
3399 options, args = parser.parse_args(args)
3400 auth_config = auth.extract_auth_config_from_options(options)
thestig@chromium.org44202a22014-03-11 19:22:18 +00003401
3402 # Access to a protected member _XX of a client class
3403 # pylint: disable=W0212
3404 try:
3405 import cpplint
3406 import cpplint_chromium
3407 except ImportError:
3408 print "Your depot_tools is missing cpplint.py and/or cpplint_chromium.py."
3409 return 1
3410
3411 # Change the current working directory before calling lint so that it
3412 # shows the correct base.
3413 previous_cwd = os.getcwd()
3414 os.chdir(settings.GetRoot())
3415 try:
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003416 cl = Changelist(auth_config=auth_config)
thestig@chromium.org44202a22014-03-11 19:22:18 +00003417 change = cl.GetChange(cl.GetCommonAncestorWithUpstream(), None)
3418 files = [f.LocalPath() for f in change.AffectedFiles()]
thestig@chromium.org5839eb52014-05-30 16:20:51 +00003419 if not files:
3420 print "Cannot lint an empty CL"
3421 return 1
thestig@chromium.org44202a22014-03-11 19:22:18 +00003422
3423 # Process cpplints arguments if any.
tzik@chromium.orgf204d4b2014-03-13 07:40:55 +00003424 command = args + files
3425 if options.filter:
3426 command = ['--filter=' + ','.join(options.filter)] + command
3427 filenames = cpplint.ParseArguments(command)
thestig@chromium.org44202a22014-03-11 19:22:18 +00003428
3429 white_regex = re.compile(settings.GetLintRegex())
3430 black_regex = re.compile(settings.GetLintIgnoreRegex())
3431 extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace]
3432 for filename in filenames:
3433 if white_regex.match(filename):
3434 if black_regex.match(filename):
3435 print "Ignoring file %s" % filename
3436 else:
3437 cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level,
3438 extra_check_functions)
3439 else:
3440 print "Skipping file %s" % filename
3441 finally:
3442 os.chdir(previous_cwd)
3443 print "Total errors found: %d\n" % cpplint._cpplint_state.error_count
3444 if cpplint._cpplint_state.error_count != 0:
3445 return 1
3446 return 0
3447
3448
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003449def CMDpresubmit(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00003450 """Runs presubmit tests on the current changelist."""
ilevy@chromium.org375a9022013-01-07 01:12:05 +00003451 parser.add_option('-u', '--upload', action='store_true',
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003452 help='Run upload hook instead of the push/dcommit hook')
ilevy@chromium.org375a9022013-01-07 01:12:05 +00003453 parser.add_option('-f', '--force', action='store_true',
sbc@chromium.org495ad152012-09-04 23:07:42 +00003454 help='Run checks even if tree is dirty')
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003455 auth.add_auth_options(parser)
3456 options, args = parser.parse_args(args)
3457 auth_config = auth.extract_auth_config_from_options(options)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003458
sbc@chromium.org71437c02015-04-09 19:29:40 +00003459 if not options.force and git_common.is_dirty_git_tree('presubmit'):
ukai@chromium.org259e4682012-10-25 07:36:33 +00003460 print 'use --force to check even if tree is dirty.'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003461 return 1
3462
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003463 cl = Changelist(auth_config=auth_config)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003464 if args:
3465 base_branch = args[0]
3466 else:
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00003467 # Default to diffing against the common ancestor of the upstream branch.
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00003468 base_branch = cl.GetCommonAncestorWithUpstream()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003469
ilevy@chromium.org051ad0e2013-03-04 21:57:34 +00003470 cl.RunHook(
3471 committing=not options.upload,
3472 may_prompt=False,
3473 verbose=options.verbose,
3474 change=cl.GetChange(base_branch, None))
dpranke@chromium.org0a2bb372011-03-25 01:16:22 +00003475 return 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003476
3477
tandrii@chromium.org65874e12016-03-04 12:03:02 +00003478def GenerateGerritChangeId(message):
3479 """Returns Ixxxxxx...xxx change id.
3480
3481 Works the same way as
3482 https://gerrit-review.googlesource.com/tools/hooks/commit-msg
3483 but can be called on demand on all platforms.
3484
3485 The basic idea is to generate git hash of a state of the tree, original commit
3486 message, author/committer info and timestamps.
3487 """
3488 lines = []
3489 tree_hash = RunGitSilent(['write-tree'])
3490 lines.append('tree %s' % tree_hash.strip())
3491 code, parent = RunGitWithCode(['rev-parse', 'HEAD~0'], suppress_stderr=False)
3492 if code == 0:
3493 lines.append('parent %s' % parent.strip())
3494 author = RunGitSilent(['var', 'GIT_AUTHOR_IDENT'])
3495 lines.append('author %s' % author.strip())
3496 committer = RunGitSilent(['var', 'GIT_COMMITTER_IDENT'])
3497 lines.append('committer %s' % committer.strip())
3498 lines.append('')
3499 # Note: Gerrit's commit-hook actually cleans message of some lines and
3500 # whitespace. This code is not doing this, but it clearly won't decrease
3501 # entropy.
3502 lines.append(message)
3503 change_hash = RunCommand(['git', 'hash-object', '-t', 'commit', '--stdin'],
3504 stdin='\n'.join(lines))
3505 return 'I%s' % change_hash.strip()
3506
3507
wittman@chromium.org455dc922015-01-26 20:15:50 +00003508def GetTargetRef(remote, remote_branch, target_branch, pending_prefix):
3509 """Computes the remote branch ref to use for the CL.
3510
3511 Args:
3512 remote (str): The git remote for the CL.
3513 remote_branch (str): The git remote branch for the CL.
3514 target_branch (str): The target branch specified by the user.
3515 pending_prefix (str): The pending prefix from the settings.
3516 """
3517 if not (remote and remote_branch):
3518 return None
bauerb@chromium.org27386dd2015-02-16 10:45:39 +00003519
wittman@chromium.org455dc922015-01-26 20:15:50 +00003520 if target_branch:
3521 # Cannonicalize branch references to the equivalent local full symbolic
3522 # refs, which are then translated into the remote full symbolic refs
3523 # below.
3524 if '/' not in target_branch:
3525 remote_branch = 'refs/remotes/%s/%s' % (remote, target_branch)
3526 else:
3527 prefix_replacements = (
3528 ('^((refs/)?remotes/)?branch-heads/', 'refs/remotes/branch-heads/'),
3529 ('^((refs/)?remotes/)?%s/' % remote, 'refs/remotes/%s/' % remote),
3530 ('^(refs/)?heads/', 'refs/remotes/%s/' % remote),
3531 )
3532 match = None
3533 for regex, replacement in prefix_replacements:
3534 match = re.search(regex, target_branch)
3535 if match:
3536 remote_branch = target_branch.replace(match.group(0), replacement)
3537 break
3538 if not match:
3539 # This is a branch path but not one we recognize; use as-is.
3540 remote_branch = target_branch
rmistry@google.comc68112d2015-03-03 12:48:06 +00003541 elif remote_branch in REFS_THAT_ALIAS_TO_OTHER_REFS:
3542 # Handle the refs that need to land in different refs.
3543 remote_branch = REFS_THAT_ALIAS_TO_OTHER_REFS[remote_branch]
bauerb@chromium.org27386dd2015-02-16 10:45:39 +00003544
wittman@chromium.org455dc922015-01-26 20:15:50 +00003545 # Create the true path to the remote branch.
3546 # Does the following translation:
3547 # * refs/remotes/origin/refs/diff/test -> refs/diff/test
3548 # * refs/remotes/origin/master -> refs/heads/master
3549 # * refs/remotes/branch-heads/test -> refs/branch-heads/test
3550 if remote_branch.startswith('refs/remotes/%s/refs/' % remote):
3551 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote, '')
3552 elif remote_branch.startswith('refs/remotes/%s/' % remote):
3553 remote_branch = remote_branch.replace('refs/remotes/%s/' % remote,
3554 'refs/heads/')
3555 elif remote_branch.startswith('refs/remotes/branch-heads'):
3556 remote_branch = remote_branch.replace('refs/remotes/', 'refs/')
3557 # If a pending prefix exists then replace refs/ with it.
3558 if pending_prefix:
3559 remote_branch = remote_branch.replace('refs/', pending_prefix)
3560 return remote_branch
3561
3562
maruel@chromium.orgeb52a5c2013-04-10 23:17:09 +00003563def cleanup_list(l):
3564 """Fixes a list so that comma separated items are put as individual items.
3565
3566 So that "--reviewers joe@c,john@c --reviewers joa@c" results in
3567 options.reviewers == sorted(['joe@c', 'john@c', 'joa@c']).
3568 """
3569 items = sum((i.split(',') for i in l), [])
3570 stripped_items = (i.strip() for i in items)
3571 return sorted(filter(None, stripped_items))
3572
3573
maruel@chromium.org0633fb42013-08-16 20:06:14 +00003574@subcommand.usage('[args to "git diff"]')
ukai@chromium.orge8077812012-02-03 03:41:46 +00003575def CMDupload(parser, args):
rmistry@google.com78948ed2015-07-08 23:09:57 +00003576 """Uploads the current changelist to codereview.
3577
3578 Can skip dependency patchset uploads for a branch by running:
3579 git config branch.branch_name.skip-deps-uploads True
3580 To unset run:
3581 git config --unset branch.branch_name.skip-deps-uploads
3582 Can also set the above globally by using the --global flag.
3583 """
ukai@chromium.orge8077812012-02-03 03:41:46 +00003584 parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
3585 help='bypass upload presubmit hook')
brettw@chromium.orgb65c43c2013-06-10 22:04:49 +00003586 parser.add_option('--bypass-watchlists', action='store_true',
3587 dest='bypass_watchlists',
3588 help='bypass watchlists auto CC-ing reviewers')
ukai@chromium.orge8077812012-02-03 03:41:46 +00003589 parser.add_option('-f', action='store_true', dest='force',
3590 help="force yes to questions (don't prompt)")
rogerta@chromium.org420d3b82012-05-14 18:41:38 +00003591 parser.add_option('-m', dest='message', help='message for patchset')
andybons@chromium.org962f9462016-02-03 20:00:42 +00003592 parser.add_option('-t', dest='title',
3593 help='title for patchset (Rietveld only)')
ukai@chromium.orge8077812012-02-03 03:41:46 +00003594 parser.add_option('-r', '--reviewers',
maruel@chromium.orgeb52a5c2013-04-10 23:17:09 +00003595 action='append', default=[],
ukai@chromium.orge8077812012-02-03 03:41:46 +00003596 help='reviewer email addresses')
3597 parser.add_option('--cc',
maruel@chromium.orgeb52a5c2013-04-10 23:17:09 +00003598 action='append', default=[],
ukai@chromium.orge8077812012-02-03 03:41:46 +00003599 help='cc email addresses')
adamk@chromium.org36f47302013-04-05 01:08:31 +00003600 parser.add_option('-s', '--send-mail', action='store_true',
ukai@chromium.orge8077812012-02-03 03:41:46 +00003601 help='send email to reviewer immediately')
pgervais@chromium.orgb9f27512014-08-08 15:52:33 +00003602 parser.add_option('--emulate_svn_auto_props',
3603 '--emulate-svn-auto-props',
3604 action="store_true",
ukai@chromium.orge8077812012-02-03 03:41:46 +00003605 dest="emulate_svn_auto_props",
3606 help="Emulate Subversion's auto properties feature.")
ukai@chromium.orge8077812012-02-03 03:41:46 +00003607 parser.add_option('-c', '--use-commit-queue', action='store_true',
3608 help='tell the commit queue to commit this patchset')
tyoshino@chromium.orgc1737d02013-05-29 14:17:28 +00003609 parser.add_option('--private', action='store_true',
3610 help='set the review private (rietveld only)')
ukai@chromium.org8ef7ab22012-11-28 04:24:52 +00003611 parser.add_option('--target_branch',
pgervais@chromium.orgb9f27512014-08-08 15:52:33 +00003612 '--target-branch',
wittman@chromium.org455dc922015-01-26 20:15:50 +00003613 metavar='TARGET',
3614 help='Apply CL to remote ref TARGET. ' +
3615 'Default: remote branch head, or master')
bauerb@chromium.org27386dd2015-02-16 10:45:39 +00003616 parser.add_option('--squash', action='store_true',
3617 help='Squash multiple commits into one (Gerrit only)')
bauerb@chromium.org54b400c2016-01-14 10:08:25 +00003618 parser.add_option('--no-squash', action='store_true',
3619 help='Don\'t squash multiple commits into one ' +
3620 '(Gerrit only)')
pgervais@chromium.org91141372014-01-09 23:27:20 +00003621 parser.add_option('--email', default=None,
3622 help='email address to use to connect to Rietveld')
piman@chromium.org336f9122014-09-04 02:16:55 +00003623 parser.add_option('--tbr-owners', dest='tbr_owners', action='store_true',
3624 help='add a set of OWNERS to TBR')
tandrii@chromium.orgd50452a2015-11-23 16:38:15 +00003625 parser.add_option('-d', '--cq-dry-run', dest='cq_dry_run',
3626 action='store_true',
rmistry@google.comef966222015-04-07 11:15:01 +00003627 help='Send the patchset to do a CQ dry run right after '
3628 'upload.')
rmistry@google.com2dd99862015-06-22 12:22:18 +00003629 parser.add_option('--dependencies', action='store_true',
3630 help='Uploads CLs of all the local branches that depend on '
3631 'the current branch')
pgervais@chromium.org91141372014-01-09 23:27:20 +00003632
rmistry@google.com2dd99862015-06-22 12:22:18 +00003633 orig_args = args
iannucci@chromium.org53937ba2012-10-02 18:20:43 +00003634 add_git_similarity(parser)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003635 auth.add_auth_options(parser)
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00003636 _add_codereview_select_options(parser)
ukai@chromium.orge8077812012-02-03 03:41:46 +00003637 (options, args) = parser.parse_args(args)
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00003638 _process_codereview_select_options(parser, options)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003639 auth_config = auth.extract_auth_config_from_options(options)
ukai@chromium.orge8077812012-02-03 03:41:46 +00003640
sbc@chromium.org71437c02015-04-09 19:29:40 +00003641 if git_common.is_dirty_git_tree('upload'):
ukai@chromium.orge8077812012-02-03 03:41:46 +00003642 return 1
3643
maruel@chromium.orgeb52a5c2013-04-10 23:17:09 +00003644 options.reviewers = cleanup_list(options.reviewers)
3645 options.cc = cleanup_list(options.cc)
3646
tandrii@chromium.org512d79c2016-03-31 12:55:28 +00003647 # For sanity of test expectations, do this otherwise lazy-loading *now*.
3648 settings.GetIsGerrit()
3649
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00003650 cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview)
tandrii@chromium.org9e6c3a52016-04-12 14:13:08 +00003651 return cl.CMDUpload(options, args, orig_args)
ukai@chromium.orge8077812012-02-03 03:41:46 +00003652
3653
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003654def IsSubmoduleMergeCommit(ref):
3655 # When submodules are added to the repo, we expect there to be a single
3656 # non-git-svn merge commit at remote HEAD with a signature comment.
3657 pattern = '^SVN changes up to revision [0-9]*$'
szager@chromium.orge84b7542012-06-15 21:26:58 +00003658 cmd = ['rev-list', '--merges', '--grep=%s' % pattern, '%s^!' % ref]
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003659 return RunGit(cmd) != ''
3660
3661
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003662def SendUpstream(parser, args, cmd):
pgervais@chromium.orgcee6dc42014-05-07 17:04:03 +00003663 """Common code for CMDland and CmdDCommit
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003664
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +00003665 In case of Gerrit, uses Gerrit REST api to "submit" the issue, which pushes
3666 upstream and closes the issue automatically and atomically.
3667
3668 Otherwise (in case of Rietveld):
3669 Squashes branch into a single commit.
3670 Updates changelog with metadata (e.g. pointer to review).
3671 Pushes/dcommits the code upstream.
3672 Updates review and closes.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003673 """
3674 parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks',
3675 help='bypass upload presubmit hook')
3676 parser.add_option('-m', dest='message',
3677 help="override review description")
3678 parser.add_option('-f', action='store_true', dest='force',
3679 help="force yes to questions (don't prompt)")
3680 parser.add_option('-c', dest='contributor',
3681 help="external contributor for patch (appended to " +
3682 "description and used as author for git). Should be " +
3683 "formatted as 'First Last <email@example.com>'")
iannucci@chromium.org53937ba2012-10-02 18:20:43 +00003684 add_git_similarity(parser)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003685 auth.add_auth_options(parser)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003686 (options, args) = parser.parse_args(args)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00003687 auth_config = auth.extract_auth_config_from_options(options)
3688
3689 cl = Changelist(auth_config=auth_config)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003690
tandrii@chromium.orgd68b62b2016-03-31 16:09:29 +00003691 # TODO(tandrii): refactor this into _RietveldChangelistImpl method.
3692 if cl.IsGerrit():
3693 if options.message:
3694 # This could be implemented, but it requires sending a new patch to
3695 # Gerrit, as Gerrit unlike Rietveld versions messages with patchsets.
3696 # Besides, Gerrit has the ability to change the commit message on submit
3697 # automatically, thus there is no need to support this option (so far?).
3698 parser.error('-m MESSAGE option is not supported for Gerrit.')
3699 if options.contributor:
3700 parser.error(
3701 '-c CONTRIBUTOR option is not supported for Gerrit.\n'
3702 'Before uploading a commit to Gerrit, ensure it\'s author field is '
3703 'the contributor\'s "name <email>". If you can\'t upload such a '
3704 'commit for review, contact your repository admin and request'
3705 '"Forge-Author" permission.')
3706 return cl._codereview_impl.CMDLand(options.force, options.bypass_hooks,
3707 options.verbose)
3708
iannucci@chromium.org5724c962014-04-11 09:32:56 +00003709 current = cl.GetBranch()
3710 remote, upstream_branch = cl.FetchUpstreamTuple(cl.GetBranch())
3711 if not settings.GetIsGitSvn() and remote == '.':
3712 print
3713 print 'Attempting to push branch %r into another local branch!' % current
3714 print
3715 print 'Either reparent this branch on top of origin/master:'
3716 print ' git reparent-branch --root'
3717 print
3718 print 'OR run `git rebase-update` if you think the parent branch is already'
3719 print 'committed.'
3720 print
3721 print ' Current parent: %r' % upstream_branch
3722 return 1
3723
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003724 if not args or cmd == 'land':
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003725 # Default to merging against our best guess of the upstream branch.
3726 args = [cl.GetUpstreamBranch()]
3727
maruel@chromium.org13f623c2011-07-22 16:02:23 +00003728 if options.contributor:
3729 if not re.match('^.*\s<\S+@\S+>$', options.contributor):
3730 print "Please provide contibutor as 'First Last <email@example.com>'"
3731 return 1
3732
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003733 base_branch = args[0]
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003734 base_has_submodules = IsSubmoduleMergeCommit(base_branch)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003735
sbc@chromium.org71437c02015-04-09 19:29:40 +00003736 if git_common.is_dirty_git_tree(cmd):
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003737 return 1
3738
3739 # This rev-list syntax means "show all commits not in my branch that
3740 # are in base_branch".
3741 upstream_commits = RunGit(['rev-list', '^' + cl.GetBranchRef(),
3742 base_branch]).splitlines()
3743 if upstream_commits:
3744 print ('Base branch "%s" has %d commits '
3745 'not in this branch.' % (base_branch, len(upstream_commits)))
3746 print 'Run "git merge %s" before attempting to %s.' % (base_branch, cmd)
3747 return 1
3748
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003749 # This is the revision `svn dcommit` will commit on top of.
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003750 svn_head = None
3751 if cmd == 'dcommit' or base_has_submodules:
3752 svn_head = RunGit(['log', '--grep=^git-svn-id:', '-1',
3753 '--pretty=format:%H'])
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003754
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003755 if cmd == 'dcommit':
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003756 # If the base_head is a submodule merge commit, the first parent of the
3757 # base_head should be a git-svn commit, which is what we're interested in.
3758 base_svn_head = base_branch
3759 if base_has_submodules:
3760 base_svn_head += '^1'
3761
3762 extra_commits = RunGit(['rev-list', '^' + svn_head, base_svn_head])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003763 if extra_commits:
3764 print ('This branch has %d additional commits not upstreamed yet.'
3765 % len(extra_commits.splitlines()))
3766 print ('Upstream "%s" or rebase this branch on top of the upstream trunk '
3767 'before attempting to %s.' % (base_branch, cmd))
3768 return 1
3769
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003770 merge_base = RunGit(['merge-base', base_branch, 'HEAD']).strip()
maruel@chromium.orgb0a63912012-01-17 18:10:16 +00003771 if not options.bypass_hooks:
maruel@chromium.org13f623c2011-07-22 16:02:23 +00003772 author = None
3773 if options.contributor:
3774 author = re.search(r'\<(.*)\>', options.contributor).group(1)
maruel@chromium.orgb0a63912012-01-17 18:10:16 +00003775 hook_results = cl.RunHook(
3776 committing=True,
maruel@chromium.orgb0a63912012-01-17 18:10:16 +00003777 may_prompt=not options.force,
3778 verbose=options.verbose,
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003779 change=cl.GetChange(merge_base, author))
maruel@chromium.orgb0a63912012-01-17 18:10:16 +00003780 if not hook_results.should_continue():
3781 return 1
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003782
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003783 # Check the tree status if the tree status URL is set.
3784 status = GetTreeStatus()
3785 if 'closed' == status:
3786 print('The tree is closed. Please wait for it to reopen. Use '
3787 '"git cl %s --bypass-hooks" to commit on a closed tree.' % cmd)
3788 return 1
3789 elif 'unknown' == status:
3790 print('Unable to determine tree status. Please verify manually and '
3791 'use "git cl %s --bypass-hooks" to commit on a closed tree.' % cmd)
3792 return 1
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003793
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003794 change_desc = ChangeDescription(options.message)
3795 if not change_desc.description and cl.GetIssue():
3796 change_desc = ChangeDescription(cl.GetDescription())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003797
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003798 if not change_desc.description:
erg@chromium.org1a173982012-08-29 20:43:05 +00003799 if not cl.GetIssue() and options.bypass_hooks:
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003800 change_desc = ChangeDescription(CreateDescriptionFromLog([merge_base]))
erg@chromium.org1a173982012-08-29 20:43:05 +00003801 else:
3802 print 'No description set.'
3803 print 'Visit %s/edit to set it.' % (cl.GetIssueURL())
3804 return 1
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003805
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003806 # Keep a separate copy for the commit message, because the commit message
3807 # contains the link to the Rietveld issue, while the Rietveld message contains
3808 # the commit viewvc url.
maruel@chromium.orge52678e2013-04-26 18:34:44 +00003809 # Keep a separate copy for the commit message.
3810 if cl.GetIssue():
maruel@chromium.orgcf087782013-07-23 13:08:48 +00003811 change_desc.update_reviewers(cl.GetApprovingReviewers())
maruel@chromium.orge52678e2013-04-26 18:34:44 +00003812
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003813 commit_desc = ChangeDescription(change_desc.description)
maruel@chromium.orgcc73ad62011-07-06 17:39:26 +00003814 if cl.GetIssue():
smut@google.com4c61dcc2015-06-08 22:31:29 +00003815 # Xcode won't linkify this URL unless there is a non-whitespace character
sergiyb@chromium.org4b39c5f2015-07-07 10:33:12 +00003816 # after it. Add a period on a new line to circumvent this. Also add a space
3817 # before the period to make sure that Gitiles continues to correctly resolve
3818 # the URL.
3819 commit_desc.append_footer('Review URL: %s .' % cl.GetIssueURL())
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003820 if options.contributor:
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003821 commit_desc.append_footer('Patch from %s.' % options.contributor)
3822
agable@chromium.orgeec3ea32013-08-15 20:31:39 +00003823 print('Description:')
3824 print(commit_desc.description)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003825
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003826 branches = [merge_base, cl.GetBranchRef()]
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003827 if not options.force:
iannucci@chromium.org79540052012-10-19 23:15:26 +00003828 print_stats(options.similarity, options.find_copies, branches)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003829
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003830 # We want to squash all this branch's commits into one commit with the proper
3831 # description. We do this by doing a "reset --soft" to the base branch (which
3832 # keeps the working copy the same), then dcommitting that. If origin/master
3833 # has a submodule merge commit, we'll also need to cherry-pick the squashed
3834 # commit onto a branch based on the git-svn head.
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003835 MERGE_BRANCH = 'git-cl-commit'
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003836 CHERRY_PICK_BRANCH = 'git-cl-cherry-pick'
3837 # Delete the branches if they exist.
3838 for branch in [MERGE_BRANCH, CHERRY_PICK_BRANCH]:
3839 showref_cmd = ['show-ref', '--quiet', '--verify', 'refs/heads/%s' % branch]
3840 result = RunGitWithCode(showref_cmd)
3841 if result[0] == 0:
3842 RunGit(['branch', '-D', branch])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003843
3844 # We might be in a directory that's present in this branch but not in the
3845 # trunk. Move up to the top of the tree so that git commands that expect a
3846 # valid CWD won't fail after we check out the merge branch.
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00003847 rel_base_path = settings.GetRelativeRoot()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003848 if rel_base_path:
3849 os.chdir(rel_base_path)
3850
3851 # Stuff our change into the merge branch.
3852 # We wrap in a try...finally block so if anything goes wrong,
3853 # we clean up the branches.
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00003854 retcode = -1
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003855 pushed_to_pending = False
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003856 pending_ref = None
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003857 revision = None
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003858 try:
bauerb@chromium.orgb4a75c42011-03-08 08:35:38 +00003859 RunGit(['checkout', '-q', '-b', MERGE_BRANCH])
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003860 RunGit(['reset', '--soft', merge_base])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003861 if options.contributor:
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003862 RunGit(
3863 [
3864 'commit', '--author', options.contributor,
3865 '-m', commit_desc.description,
3866 ])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003867 else:
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003868 RunGit(['commit', '-m', commit_desc.description])
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003869 if base_has_submodules:
3870 cherry_pick_commit = RunGit(['rev-list', 'HEAD^!']).rstrip()
3871 RunGit(['branch', CHERRY_PICK_BRANCH, svn_head])
3872 RunGit(['checkout', CHERRY_PICK_BRANCH])
3873 RunGit(['cherry-pick', cherry_pick_commit])
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003874 if cmd == 'land':
ilevy@chromium.org0f58fa82012-11-05 01:45:20 +00003875 remote, branch = cl.FetchUpstreamTuple(cl.GetBranch())
szager@chromium.org151ebcf2016-03-09 01:08:25 +00003876 mirror = settings.GetGitMirror(remote)
3877 pushurl = mirror.url if mirror else remote
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003878 pending_prefix = settings.GetPendingRefPrefix()
3879 if not pending_prefix or branch.startswith(pending_prefix):
3880 # If not using refs/pending/heads/* at all, or target ref is already set
3881 # to pending, then push to the target ref directly.
3882 retcode, output = RunGitWithCode(
szager@chromium.org151ebcf2016-03-09 01:08:25 +00003883 ['push', '--porcelain', pushurl, 'HEAD:%s' % branch])
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003884 pushed_to_pending = pending_prefix and branch.startswith(pending_prefix)
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003885 else:
3886 # Cherry-pick the change on top of pending ref and then push it.
3887 assert branch.startswith('refs/'), branch
3888 assert pending_prefix[-1] == '/', pending_prefix
3889 pending_ref = pending_prefix + branch[len('refs/'):]
szager@chromium.org151ebcf2016-03-09 01:08:25 +00003890 retcode, output = PushToGitPending(pushurl, pending_ref, branch)
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003891 pushed_to_pending = (retcode == 0)
iannucci@chromium.org34504a12014-08-29 23:51:37 +00003892 if retcode == 0:
3893 revision = RunGit(['rev-parse', 'HEAD']).strip()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003894 else:
3895 # dcommit the merge branch.
iannucci@chromium.orga1950c42014-12-05 22:15:56 +00003896 cmd_args = [
kjellander@chromium.org6abc6522014-12-02 07:34:49 +00003897 'svn', 'dcommit',
3898 '-C%s' % options.similarity,
3899 '--no-rebase', '--rmdir',
3900 ]
3901 if settings.GetForceHttpsCommitUrl():
3902 # Allow forcing https commit URLs for some projects that don't allow
3903 # committing to http URLs (like Google Code).
3904 remote_url = cl.GetGitSvnRemoteUrl()
3905 if urlparse.urlparse(remote_url).scheme == 'http':
3906 remote_url = remote_url.replace('http://', 'https://')
iannucci@chromium.orga1950c42014-12-05 22:15:56 +00003907 cmd_args.append('--commit-url=%s' % remote_url)
3908 _, output = RunGitWithCode(cmd_args)
iannucci@chromium.org34504a12014-08-29 23:51:37 +00003909 if 'Committed r' in output:
3910 revision = re.match(
3911 '.*?\nCommitted r(\\d+)', output, re.DOTALL).group(1)
3912 logging.debug(output)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003913 finally:
3914 # And then swap back to the original branch and clean up.
3915 RunGit(['checkout', '-q', cl.GetBranch()])
3916 RunGit(['branch', '-D', MERGE_BRANCH])
szager@chromium.org9bb85e22012-06-13 20:28:23 +00003917 if base_has_submodules:
3918 RunGit(['branch', '-D', CHERRY_PICK_BRANCH])
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003919
iannucci@chromium.org34504a12014-08-29 23:51:37 +00003920 if not revision:
iannucci@chromium.org6c217b12014-08-29 22:10:59 +00003921 print 'Failed to push. If this persists, please file a bug.'
iannucci@chromium.org34504a12014-08-29 23:51:37 +00003922 return 1
iannucci@chromium.org6c217b12014-08-29 22:10:59 +00003923
iannucci@chromium.orgbbe9cc52014-09-05 18:25:51 +00003924 killed = False
iannucci@chromium.org6c217b12014-08-29 22:10:59 +00003925 if pushed_to_pending:
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003926 try:
3927 revision = WaitForRealCommit(remote, revision, base_branch, branch)
3928 # We set pushed_to_pending to False, since it made it all the way to the
3929 # real ref.
3930 pushed_to_pending = False
3931 except KeyboardInterrupt:
iannucci@chromium.orgbbe9cc52014-09-05 18:25:51 +00003932 killed = True
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003933
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003934 if cl.GetIssue():
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003935 to_pending = ' to pending queue' if pushed_to_pending else ''
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003936 viewvc_url = settings.GetViewVCUrl()
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003937 if not to_pending:
3938 if viewvc_url and revision:
3939 change_desc.append_footer(
3940 'Committed: %s%s' % (viewvc_url, revision))
3941 elif revision:
3942 change_desc.append_footer('Committed: %s' % (revision,))
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003943 print ('Closing issue '
3944 '(you may be prompted for your codereview password)...')
maruel@chromium.org78936cb2013-04-11 00:17:52 +00003945 cl.UpdateDescription(change_desc.description)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003946 cl.CloseIssue()
maruel@chromium.org1033efd2013-07-23 23:25:09 +00003947 props = cl.GetIssueProperties()
sadrul@chromium.org34b5d822013-02-18 01:39:24 +00003948 patch_num = len(props['patchsets'])
rmistry@google.com52d224a2014-08-27 14:44:41 +00003949 comment = "Committed patchset #%d (id:%d)%s manually as %s" % (
mark@chromium.org782570c2014-09-26 21:48:02 +00003950 patch_num, props['patchsets'][-1], to_pending, revision)
jochen@chromium.org3ec0d542014-01-14 20:00:03 +00003951 if options.bypass_hooks:
3952 comment += ' (tree was closed).' if GetTreeStatus() == 'closed' else '.'
3953 else:
3954 comment += ' (presubmit successful).'
iannucci@chromium.orgb85a3162013-01-26 01:11:13 +00003955 cl.RpcServer().add_comment(cl.GetIssue(), comment)
maruel@chromium.org1033efd2013-07-23 23:25:09 +00003956 cl.SetIssue(None)
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00003957
iannucci@chromium.org6c217b12014-08-29 22:10:59 +00003958 if pushed_to_pending:
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003959 _, branch = cl.FetchUpstreamTuple(cl.GetBranch())
3960 print 'The commit is in the pending queue (%s).' % pending_ref
3961 print (
thakis@chromium.org5f32a962014-09-05 21:33:23 +00003962 'It will show up on %s in ~1 min, once it gets a Cr-Commit-Position '
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003963 'footer.' % branch)
3964
iannucci@chromium.org6c217b12014-08-29 22:10:59 +00003965 hook = POSTUPSTREAM_HOOK_PATTERN % cmd
3966 if os.path.isfile(hook):
3967 RunCommand([hook, merge_base], error_ok=True)
maruel@chromium.org0ba7f962011-01-11 22:13:58 +00003968
iannucci@chromium.orgbbe9cc52014-09-05 18:25:51 +00003969 return 1 if killed else 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00003970
3971
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003972def WaitForRealCommit(remote, pushed_commit, local_base_ref, real_ref):
3973 print
3974 print 'Waiting for commit to be landed on %s...' % real_ref
3975 print '(If you are impatient, you may Ctrl-C once without harm)'
3976 target_tree = RunGit(['rev-parse', '%s:' % pushed_commit]).strip()
3977 current_rev = RunGit(['rev-parse', local_base_ref]).strip()
szager@chromium.org151ebcf2016-03-09 01:08:25 +00003978 mirror = settings.GetGitMirror(remote)
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003979
3980 loop = 0
3981 while True:
3982 sys.stdout.write('fetching (%d)... \r' % loop)
3983 sys.stdout.flush()
3984 loop += 1
3985
szager@chromium.org151ebcf2016-03-09 01:08:25 +00003986 if mirror:
3987 mirror.populate()
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00003988 RunGit(['retry', 'fetch', remote, real_ref], stderr=subprocess2.VOID)
3989 to_rev = RunGit(['rev-parse', 'FETCH_HEAD']).strip()
3990 commits = RunGit(['rev-list', '%s..%s' % (current_rev, to_rev)])
3991 for commit in commits.splitlines():
3992 if RunGit(['rev-parse', '%s:' % commit]).strip() == target_tree:
3993 print 'Found commit on %s' % real_ref
3994 return commit
3995
3996 current_rev = to_rev
3997
3998
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00003999def PushToGitPending(remote, pending_ref, upstream_ref):
4000 """Fetches pending_ref, cherry-picks current HEAD on top of it, pushes.
4001
4002 Returns:
4003 (retcode of last operation, output log of last operation).
4004 """
4005 assert pending_ref.startswith('refs/'), pending_ref
4006 local_pending_ref = 'refs/git-cl/' + pending_ref[len('refs/'):]
4007 cherry = RunGit(['rev-parse', 'HEAD']).strip()
4008 code = 0
4009 out = ''
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004010 max_attempts = 3
4011 attempts_left = max_attempts
4012 while attempts_left:
4013 if attempts_left != max_attempts:
4014 print 'Retrying, %d attempts left...' % (attempts_left - 1,)
4015 attempts_left -= 1
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004016
4017 # Fetch. Retry fetch errors.
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004018 print 'Fetching pending ref %s...' % pending_ref
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004019 code, out = RunGitWithCode(
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004020 ['retry', 'fetch', remote, '+%s:%s' % (pending_ref, local_pending_ref)])
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004021 if code:
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004022 print 'Fetch failed with exit code %d.' % code
4023 if out.strip():
4024 print out.strip()
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004025 continue
4026
4027 # Try to cherry pick. Abort on merge conflicts.
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004028 print 'Cherry-picking commit on top of pending ref...'
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004029 RunGitWithCode(['checkout', local_pending_ref], suppress_stderr=True)
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004030 code, out = RunGitWithCode(['cherry-pick', cherry])
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004031 if code:
4032 print (
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004033 'Your patch doesn\'t apply cleanly to ref \'%s\', '
4034 'the following files have merge conflicts:' % pending_ref)
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004035 print RunGit(['diff', '--name-status', '--diff-filter=U']).strip()
4036 print 'Please rebase your patch and try again.'
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004037 RunGitWithCode(['cherry-pick', '--abort'])
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004038 return code, out
4039
4040 # Applied cleanly, try to push now. Retry on error (flake or non-ff push).
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004041 print 'Pushing commit to %s... It can take a while.' % pending_ref
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004042 code, out = RunGitWithCode(
4043 ['retry', 'push', '--porcelain', remote, 'HEAD:%s' % pending_ref])
4044 if code == 0:
4045 # Success.
iannucci@chromium.orge6896b52014-08-29 01:38:03 +00004046 print 'Commit pushed to pending ref successfully!'
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004047 return code, out
4048
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004049 print 'Push failed with exit code %d.' % code
4050 if out.strip():
4051 print out.strip()
4052 if IsFatalPushFailure(out):
4053 print (
4054 'Fatal push error. Make sure your .netrc credentials and git '
4055 'user.email are correct and you have push access to the repo.')
4056 return code, out
4057
4058 print 'All attempts to push to pending ref failed.'
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004059 return code, out
4060
4061
vadimsh@chromium.org749fbd92014-08-26 21:57:53 +00004062def IsFatalPushFailure(push_stdout):
4063 """True if retrying push won't help."""
4064 return '(prohibited by Gerrit)' in push_stdout
4065
4066
maruel@chromium.org0633fb42013-08-16 20:06:14 +00004067@subcommand.usage('[upstream branch to apply against]')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004068def CMDdcommit(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004069 """Commits the current changelist via git-svn."""
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004070 if not settings.GetIsGitSvn():
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +00004071 if git_footers.get_footer_svn_id():
mmoss@chromium.orgf0e41522015-06-10 19:52:01 +00004072 # If it looks like previous commits were mirrored with git-svn.
4073 message = """This repository appears to be a git-svn mirror, but no
4074upstream SVN master is set. You probably need to run 'git auto-svn' once."""
4075 else:
4076 message = """This doesn't appear to be an SVN repository.
4077If your project has a true, writeable git repository, you probably want to run
4078'git cl land' instead.
4079If your project has a git mirror of an upstream SVN master, you probably need
4080to run 'git svn init'.
4081
4082Using the wrong command might cause your commit to appear to succeed, and the
4083review to be closed, without actually landing upstream. If you choose to
4084proceed, please verify that the commit lands upstream as expected."""
thakis@chromium.orgcde3bb62011-01-20 01:16:14 +00004085 print(message)
maruel@chromium.org90541732011-04-01 17:54:18 +00004086 ask_for_data('[Press enter to dcommit or ctrl-C to quit]')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004087 return SendUpstream(parser, args, 'dcommit')
4088
4089
maruel@chromium.org0633fb42013-08-16 20:06:14 +00004090@subcommand.usage('[upstream branch to apply against]')
pgervais@chromium.orgcee6dc42014-05-07 17:04:03 +00004091def CMDland(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004092 """Commits the current changelist via git."""
tandrii@chromium.org09d7a6a2016-03-04 15:44:48 +00004093 if settings.GetIsGitSvn() or git_footers.get_footer_svn_id():
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004094 print('This appears to be an SVN repository.')
4095 print('Are you sure you didn\'t mean \'git cl dcommit\'?')
mmoss@chromium.orgf0e41522015-06-10 19:52:01 +00004096 print('(Ignore if this is the first commit after migrating from svn->git)')
maruel@chromium.org90541732011-04-01 17:54:18 +00004097 ask_for_data('[Press enter to push or ctrl-C to quit]')
vadimsh@chromium.org566a02a2014-08-22 01:34:13 +00004098 return SendUpstream(parser, args, 'land')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004099
4100
dsinclair@chromium.orgfbed6562015-09-25 21:22:36 +00004101@subcommand.usage('<patch url or issue id or issue url>')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004102def CMDpatch(parser, args):
marq@chromium.orge5e59002013-10-02 23:21:25 +00004103 """Patches in a code review."""
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004104 parser.add_option('-b', dest='newbranch',
4105 help='create a new branch off trunk for the patch')
qsr@chromium.org1ef44af2013-10-16 16:24:32 +00004106 parser.add_option('-f', '--force', action='store_true',
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004107 help='with -b, clobber any existing branch')
qsr@chromium.org1ef44af2013-10-16 16:24:32 +00004108 parser.add_option('-d', '--directory', action='store', metavar='DIR',
4109 help='Change to the directory DIR immediately, '
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004110 'before doing anything else. Rietveld only.')
qsr@chromium.org1ef44af2013-10-16 16:24:32 +00004111 parser.add_option('--reject', action='store_true',
tapted@chromium.org6a0b07c2013-07-10 01:29:19 +00004112 help='failed patches spew .rej files rather than '
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004113 'attempting a 3-way merge. Rietveld only.')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004114 parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit',
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004115 help='don\'t commit after patch applies. Rietveld only.')
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004116
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004117
4118 group = optparse.OptionGroup(
4119 parser,
4120 'Options for continuing work on the current issue uploaded from a '
4121 'different clone (e.g. different machine). Must be used independently '
4122 'from the other options. No issue number should be specified, and the '
4123 'branch must have an issue number associated with it')
4124 group.add_option('--reapply', action='store_true', dest='reapply',
4125 help='Reset the branch and reapply the issue.\n'
4126 'CAUTION: This will undo any local changes in this '
4127 'branch')
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004128
4129 group.add_option('--pull', action='store_true', dest='pull',
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004130 help='Performs a pull before reapplying.')
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004131 parser.add_option_group(group)
4132
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004133 auth.add_auth_options(parser)
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00004134 _add_codereview_select_options(parser)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004135 (options, args) = parser.parse_args(args)
tandrii@chromium.orgdde64622016-04-13 17:11:21 +00004136 _process_codereview_select_options(parser, options)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004137 auth_config = auth.extract_auth_config_from_options(options)
4138
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004139
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004140 if options.reapply :
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00004141 if options.newbranch:
4142 parser.error('--reapply works on the current branch only')
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004143 if len(args) > 0:
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00004144 parser.error('--reapply implies no additional arguments')
dsinclair@chromium.orgfbed6562015-09-25 21:22:36 +00004145
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00004146 cl = Changelist(auth_config=auth_config,
4147 codereview=options.forced_codereview)
4148 if not cl.GetIssue():
4149 parser.error('current branch must have an associated issue')
4150
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004151 upstream = cl.GetUpstreamBranch()
4152 if upstream == None:
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004153 parser.error('No upstream branch specified. Cannot reset branch')
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004154
4155 RunGit(['reset', '--hard', upstream])
4156 if options.pull:
4157 RunGit(['pull'])
mtrofin@chromium.org1d88dd32016-02-04 16:25:12 +00004158
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00004159 return cl.CMDPatchIssue(cl.GetIssue(), options.reject, options.nocommit,
4160 options.directory)
4161
4162 if len(args) != 1 or not args[0]:
4163 parser.error('Must specify issue number or url')
4164
4165 # We don't want uncommitted changes mixed up with the patch.
4166 if git_common.is_dirty_git_tree('patch'):
dsinclair@chromium.orgfbed6562015-09-25 21:22:36 +00004167 return 1
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004168
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00004169 if options.newbranch:
4170 if options.force:
4171 RunGit(['branch', '-D', options.newbranch],
4172 stderr=subprocess2.PIPE, error_ok=True)
4173 RunGit(['new-branch', options.newbranch])
4174
4175 cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview)
4176
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004177 if cl.IsGerrit():
4178 if options.reject:
4179 parser.error('--reject is not supported with Gerrit codereview.')
4180 if options.nocommit:
4181 parser.error('--nocommit is not supported with Gerrit codereview.')
4182 if options.directory:
4183 parser.error('--directory is not supported with Gerrit codereview.')
4184
tandrii@chromium.orgc2786d92016-05-31 19:53:50 +00004185 return cl.CMDPatchIssue(args[0], options.reject, options.nocommit,
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004186 options.directory)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004187
4188
4189def CMDrebase(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004190 """Rebases current branch on top of svn repo."""
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004191 # Provide a wrapper for git svn rebase to help avoid accidental
4192 # git svn dcommit.
4193 # It's the only command that doesn't use parser at all since we just defer
4194 # execution to git-svn.
bratell@opera.com82b91cd2013-07-09 06:33:41 +00004195
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00004196 return RunGitWithCode(['svn', 'rebase'] + args)[1]
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004197
4198
jochen@chromium.org3ec0d542014-01-14 20:00:03 +00004199def GetTreeStatus(url=None):
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004200 """Fetches the tree status and returns either 'open', 'closed',
4201 'unknown' or 'unset'."""
jochen@chromium.org3ec0d542014-01-14 20:00:03 +00004202 url = url or settings.GetTreeStatusUrl(error_ok=True)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004203 if url:
4204 status = urllib2.urlopen(url).read().lower()
4205 if status.find('closed') != -1 or status == '0':
4206 return 'closed'
4207 elif status.find('open') != -1 or status == '1':
4208 return 'open'
4209 return 'unknown'
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004210 return 'unset'
4211
dpranke@chromium.org970c5222011-03-12 00:32:24 +00004212
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004213def GetTreeStatusReason():
4214 """Fetches the tree status from a json url and returns the message
4215 with the reason for the tree to be opened or closed."""
msb@chromium.orgbf1a7ba2011-02-01 16:21:46 +00004216 url = settings.GetTreeStatusUrl()
4217 json_url = urlparse.urljoin(url, '/current?format=json')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004218 connection = urllib2.urlopen(json_url)
4219 status = json.loads(connection.read())
4220 connection.close()
4221 return status['message']
4222
dpranke@chromium.org970c5222011-03-12 00:32:24 +00004223
sheyang@chromium.org2b34d552014-08-14 22:18:42 +00004224def GetBuilderMaster(bot_list):
4225 """For a given builder, fetch the master from AE if available."""
4226 map_url = 'https://builders-map.appspot.com/'
4227 try:
4228 master_map = json.load(urllib2.urlopen(map_url))
4229 except urllib2.URLError as e:
4230 return None, ('Failed to fetch builder-to-master map from %s. Error: %s.' %
4231 (map_url, e))
4232 except ValueError as e:
4233 return None, ('Invalid json string from %s. Error: %s.' % (map_url, e))
4234 if not master_map:
4235 return None, 'Failed to build master map.'
4236
4237 result_master = ''
4238 for bot in bot_list:
4239 builder = bot.split(':', 1)[0]
4240 master_list = master_map.get(builder, [])
4241 if not master_list:
4242 return None, ('No matching master for builder %s.' % builder)
4243 elif len(master_list) > 1:
4244 return None, ('The builder name %s exists in multiple masters %s.' %
4245 (builder, master_list))
4246 else:
4247 cur_master = master_list[0]
4248 if not result_master:
4249 result_master = cur_master
4250 elif result_master != cur_master:
4251 return None, 'The builders do not belong to the same master.'
4252 return result_master, None
4253
4254
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004255def CMDtree(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004256 """Shows the status of the tree."""
dpranke@chromium.org97ae58e2011-03-18 00:29:20 +00004257 _, args = parser.parse_args(args)
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004258 status = GetTreeStatus()
4259 if 'unset' == status:
4260 print 'You must configure your tree status URL by running "git cl config".'
4261 return 2
4262
4263 print "The tree is %s" % status
4264 print
4265 print GetTreeStatusReason()
4266 if status != 'open':
4267 return 1
4268 return 0
4269
4270
maruel@chromium.org15192402012-09-06 12:38:29 +00004271def CMDtry(parser, args):
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00004272 """Triggers try jobs through BuildBucket."""
maruel@chromium.org15192402012-09-06 12:38:29 +00004273 group = optparse.OptionGroup(parser, "Try job options")
4274 group.add_option(
4275 "-b", "--bot", action="append",
4276 help=("IMPORTANT: specify ONE builder per --bot flag. Use it multiple "
4277 "times to specify multiple builders. ex: "
phajdan.jr@chromium.org52914132015-01-22 10:37:09 +00004278 "'-b win_rel -b win_layout'. See "
maruel@chromium.org15192402012-09-06 12:38:29 +00004279 "the try server waterfall for the builders name and the tests "
phajdan.jr@chromium.org52914132015-01-22 10:37:09 +00004280 "available."))
maruel@chromium.org15192402012-09-06 12:38:29 +00004281 group.add_option(
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004282 "-m", "--master", default='',
iannucci@chromium.org9e849272014-04-04 00:31:55 +00004283 help=("Specify a try master where to run the tries."))
hinoka@chromium.orgfeb9e2a2015-09-25 19:11:09 +00004284 group.add_option( "--luci", action='store_true')
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004285 group.add_option(
maruel@chromium.org15192402012-09-06 12:38:29 +00004286 "-r", "--revision",
4287 help="Revision to use for the try job; default: the "
4288 "revision will be determined by the try server; see "
4289 "its waterfall for more info")
4290 group.add_option(
4291 "-c", "--clobber", action="store_true", default=False,
4292 help="Force a clobber before building; e.g. don't do an "
4293 "incremental build")
4294 group.add_option(
4295 "--project",
4296 help="Override which project to use. Projects are defined "
4297 "server-side to define what default bot set to use")
4298 group.add_option(
machenbach@chromium.org45453142015-09-15 08:45:22 +00004299 "-p", "--property", dest="properties", action="append", default=[],
4300 help="Specify generic properties in the form -p key1=value1 -p "
4301 "key2=value2 etc (buildbucket only). The value will be treated as "
4302 "json if decodable, or as string otherwise.")
4303 group.add_option(
maruel@chromium.org15192402012-09-06 12:38:29 +00004304 "-n", "--name", help="Try job name; default to current branch name")
sheyang@google.com6ebaf782015-05-12 19:17:54 +00004305 group.add_option(
sheyang@chromium.orgdb375572015-08-17 19:22:23 +00004306 "--use-rietveld", action="store_true", default=False,
4307 help="Use Rietveld to trigger try jobs.")
4308 group.add_option(
4309 "--buildbucket-host", default='cr-buildbucket.appspot.com',
4310 help="Host of buildbucket. The default host is %default.")
maruel@chromium.org15192402012-09-06 12:38:29 +00004311 parser.add_option_group(group)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004312 auth.add_auth_options(parser)
maruel@chromium.org15192402012-09-06 12:38:29 +00004313 options, args = parser.parse_args(args)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004314 auth_config = auth.extract_auth_config_from_options(options)
maruel@chromium.org15192402012-09-06 12:38:29 +00004315
machenbach@chromium.org45453142015-09-15 08:45:22 +00004316 if options.use_rietveld and options.properties:
4317 parser.error('Properties can only be specified with buildbucket')
4318
4319 # Make sure that all properties are prop=value pairs.
4320 bad_params = [x for x in options.properties if '=' not in x]
4321 if bad_params:
4322 parser.error('Got properties with missing "=": %s' % bad_params)
4323
maruel@chromium.org15192402012-09-06 12:38:29 +00004324 if args:
4325 parser.error('Unknown arguments: %s' % args)
4326
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004327 cl = Changelist(auth_config=auth_config)
maruel@chromium.org15192402012-09-06 12:38:29 +00004328 if not cl.GetIssue():
4329 parser.error('Need to upload first')
4330
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00004331 if cl.IsGerrit():
4332 parser.error(
4333 'Not yet supported for Gerrit (http://crbug.com/599931).\n'
4334 'If your project has Commit Queue, dry run is a workaround:\n'
4335 ' git cl set-commit --dry-run')
4336 # Code below assumes Rietveld issue.
4337 # TODO(tandrii): actually implement for Gerrit http://crbug.com/599931.
4338
jrobbins@chromium.org16f10f72014-06-24 22:14:36 +00004339 props = cl.GetIssueProperties()
agable@chromium.org787e3062014-08-20 16:31:19 +00004340 if props.get('closed'):
4341 parser.error('Cannot send tryjobs for a closed CL')
4342
jrobbins@chromium.org16f10f72014-06-24 22:14:36 +00004343 if props.get('private'):
4344 parser.error('Cannot use trybots with private issue')
4345
maruel@chromium.org15192402012-09-06 12:38:29 +00004346 if not options.name:
4347 options.name = cl.GetBranch()
4348
phajdan.jr@chromium.org8da7f272014-03-14 01:28:39 +00004349 if options.bot and not options.master:
sheyang@chromium.org2b34d552014-08-14 22:18:42 +00004350 options.master, err_msg = GetBuilderMaster(options.bot)
4351 if err_msg:
4352 parser.error('Tryserver master cannot be found because: %s\n'
4353 'Please manually specify the tryserver master'
4354 ', e.g. "-m tryserver.chromium.linux".' % err_msg)
phajdan.jr@chromium.org8da7f272014-03-14 01:28:39 +00004355
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004356 def GetMasterMap():
phajdan.jr@chromium.org52914132015-01-22 10:37:09 +00004357 # Process --bot.
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004358 if not options.bot:
4359 change = cl.GetChange(cl.GetCommonAncestorWithUpstream(), None)
maruel@chromium.org15192402012-09-06 12:38:29 +00004360
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004361 # Get try masters from PRESUBMIT.py files.
4362 masters = presubmit_support.DoGetTryMasters(
4363 change,
4364 change.LocalPaths(),
4365 settings.GetRoot(),
4366 None,
4367 None,
4368 options.verbose,
4369 sys.stdout)
4370 if masters:
4371 return masters
stip@chromium.org43064fd2013-12-18 20:07:44 +00004372
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004373 # Fall back to deprecated method: get try slaves from PRESUBMIT.py files.
4374 options.bot = presubmit_support.DoGetTrySlaves(
4375 change,
4376 change.LocalPaths(),
4377 settings.GetRoot(),
4378 None,
4379 None,
4380 options.verbose,
4381 sys.stdout)
tandrii@chromium.org71184c02016-01-13 15:18:44 +00004382
4383 if not options.bot:
4384 # Get try masters from cq.cfg if any.
4385 # TODO(tandrii): some (but very few) projects store cq.cfg in different
4386 # location.
4387 cq_cfg = os.path.join(change.RepositoryRoot(),
4388 'infra', 'config', 'cq.cfg')
4389 if os.path.exists(cq_cfg):
4390 masters = {}
machenbach@chromium.org59994802016-01-14 10:10:33 +00004391 cq_masters = commit_queue.get_master_builder_map(
4392 cq_cfg, include_experimental=False, include_triggered=False)
tandrii@chromium.org71184c02016-01-13 15:18:44 +00004393 for master, builders in cq_masters.iteritems():
4394 for builder in builders:
4395 # Skip presubmit builders, because these will fail without LGTM.
machenbach@chromium.org2403e802016-04-29 12:34:42 +00004396 masters.setdefault(master, {})[builder] = ['defaulttests']
tandrii@chromium.org71184c02016-01-13 15:18:44 +00004397 if masters:
4398 return masters
4399
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004400 if not options.bot:
4401 parser.error('No default try builder to try, use --bot')
maruel@chromium.org15192402012-09-06 12:38:29 +00004402
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004403 builders_and_tests = {}
4404 # TODO(machenbach): The old style command-line options don't support
4405 # multiple try masters yet.
4406 old_style = filter(lambda x: isinstance(x, basestring), options.bot)
4407 new_style = filter(lambda x: isinstance(x, tuple), options.bot)
4408
4409 for bot in old_style:
4410 if ':' in bot:
phajdan.jr@chromium.org52914132015-01-22 10:37:09 +00004411 parser.error('Specifying testfilter is no longer supported')
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004412 elif ',' in bot:
4413 parser.error('Specify one bot per --bot flag')
4414 else:
tandrii@chromium.org3764fa22015-10-21 16:40:40 +00004415 builders_and_tests.setdefault(bot, [])
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004416
4417 for bot, tests in new_style:
4418 builders_and_tests.setdefault(bot, []).extend(tests)
4419
4420 # Return a master map with one master to be backwards compatible. The
4421 # master name defaults to an empty string, which will cause the master
4422 # not to be set on rietveld (deprecated).
4423 return {options.master: builders_and_tests}
4424
4425 masters = GetMasterMap()
stip@chromium.org43064fd2013-12-18 20:07:44 +00004426
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004427 for builders in masters.itervalues():
4428 if any('triggered' in b for b in builders):
4429 print >> sys.stderr, (
4430 'ERROR You are trying to send a job to a triggered bot. This type of'
4431 ' bot requires an\ninitial job from a parent (usually a builder). '
4432 'Instead send your job to the parent.\n'
4433 'Bot list: %s' % builders)
4434 return 1
ilevy@chromium.orgf3b21232012-09-24 20:48:55 +00004435
ilevy@chromium.org36e420b2013-08-06 23:21:12 +00004436 patchset = cl.GetMostRecentPatchset()
4437 if patchset and patchset != cl.GetPatchset():
4438 print(
4439 '\nWARNING Mismatch between local config and server. Did a previous '
4440 'upload fail?\ngit-cl try always uses latest patchset from rietveld. '
4441 'Continuing using\npatchset %s.\n' % patchset)
hinoka@chromium.orgfeb9e2a2015-09-25 19:11:09 +00004442 if options.luci:
4443 trigger_luci_job(cl, masters, options)
4444 elif not options.use_rietveld:
sheyang@google.com6ebaf782015-05-12 19:17:54 +00004445 try:
4446 trigger_try_jobs(auth_config, cl, options, masters, 'git_cl_try')
4447 except BuildbucketResponseException as ex:
4448 print 'ERROR: %s' % ex
fischman@chromium.orgd246c972013-12-21 22:47:38 +00004449 return 1
sheyang@google.com6ebaf782015-05-12 19:17:54 +00004450 except Exception as e:
4451 stacktrace = (''.join(traceback.format_stack()) + traceback.format_exc())
4452 print 'ERROR: Exception when trying to trigger tryjobs: %s\n%s' % (
4453 e, stacktrace)
4454 return 1
4455 else:
4456 try:
4457 cl.RpcServer().trigger_distributed_try_jobs(
4458 cl.GetIssue(), patchset, options.name, options.clobber,
4459 options.revision, masters)
4460 except urllib2.HTTPError as e:
4461 if e.code == 404:
4462 print('404 from rietveld; '
4463 'did you mean to use "git try" instead of "git cl try"?')
4464 return 1
4465 print('Tried jobs on:')
machenbach@chromium.org58a69cb2014-03-01 02:08:29 +00004466
sheyang@google.com6ebaf782015-05-12 19:17:54 +00004467 for (master, builders) in sorted(masters.iteritems()):
4468 if master:
4469 print 'Master: %s' % master
4470 length = max(len(builder) for builder in builders)
4471 for builder in sorted(builders):
4472 print ' %*s: %s' % (length, builder, ','.join(builders[builder]))
maruel@chromium.org15192402012-09-06 12:38:29 +00004473 return 0
4474
4475
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +00004476def CMDtry_results(parser, args):
4477 group = optparse.OptionGroup(parser, "Try job results options")
4478 group.add_option(
4479 "-p", "--patchset", type=int, help="patchset number if not current.")
4480 group.add_option(
tandrii@chromium.org6cf98c82016-03-15 11:56:00 +00004481 "--print-master", action='store_true', help="print master name as well.")
4482 group.add_option(
iannucci@chromium.org596cd5c2016-04-04 21:34:39 +00004483 "--color", action='store_true', default=setup_color.IS_TTY,
tandrii@chromium.org6cf98c82016-03-15 11:56:00 +00004484 help="force color output, useful when piping output.")
tandrii@chromium.orgb015fac2016-02-26 14:52:01 +00004485 group.add_option(
4486 "--buildbucket-host", default='cr-buildbucket.appspot.com',
4487 help="Host of buildbucket. The default host is %default.")
4488 parser.add_option_group(group)
4489 auth.add_auth_options(parser)
4490 options, args = parser.parse_args(args)
4491 if args:
4492 parser.error('Unrecognized args: %s' % ' '.join(args))
4493
4494 auth_config = auth.extract_auth_config_from_options(options)
4495 cl = Changelist(auth_config=auth_config)
4496 if not cl.GetIssue():
4497 parser.error('Need to upload first')
4498
4499 if not options.patchset:
4500 options.patchset = cl.GetMostRecentPatchset()
4501 if options.patchset and options.patchset != cl.GetPatchset():
4502 print(
4503 '\nWARNING Mismatch between local config and server. Did a previous '
4504 'upload fail?\ngit-cl try always uses latest patchset from rietveld. '
4505 'Continuing using\npatchset %s.\n' % options.patchset)
4506 try:
4507 jobs = fetch_try_jobs(auth_config, cl, options)
4508 except BuildbucketResponseException as ex:
4509 print 'Buildbucket error: %s' % ex
4510 return 1
4511 except Exception as e:
4512 stacktrace = (''.join(traceback.format_stack()) + traceback.format_exc())
4513 print 'ERROR: Exception when trying to fetch tryjobs: %s\n%s' % (
4514 e, stacktrace)
4515 return 1
4516 print_tryjobs(options, jobs)
4517 return 0
4518
4519
maruel@chromium.org0633fb42013-08-16 20:06:14 +00004520@subcommand.usage('[new upstream branch]')
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004521def CMDupstream(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004522 """Prints or sets the name of the upstream branch, if any."""
dpranke@chromium.org97ae58e2011-03-18 00:29:20 +00004523 _, args = parser.parse_args(args)
brettw@chromium.orgac0ba332012-08-09 23:42:53 +00004524 if len(args) > 1:
maruel@chromium.org27bb3872011-05-30 20:33:19 +00004525 parser.error('Unrecognized args: %s' % ' '.join(args))
brettw@chromium.orgac0ba332012-08-09 23:42:53 +00004526
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004527 cl = Changelist()
brettw@chromium.orgac0ba332012-08-09 23:42:53 +00004528 if args:
4529 # One arg means set upstream branch.
bauerb@chromium.orgc9cf90a2014-04-28 20:32:31 +00004530 branch = cl.GetBranch()
4531 RunGit(['branch', '--set-upstream', branch, args[0]])
brettw@chromium.orgac0ba332012-08-09 23:42:53 +00004532 cl = Changelist()
4533 print "Upstream branch set to " + cl.GetUpstreamBranch()
bauerb@chromium.orgc9cf90a2014-04-28 20:32:31 +00004534
4535 # Clear configured merge-base, if there is one.
4536 git_common.remove_merge_base(branch)
brettw@chromium.orgac0ba332012-08-09 23:42:53 +00004537 else:
4538 print cl.GetUpstreamBranch()
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004539 return 0
4540
4541
thestig@chromium.org00858c82013-12-02 23:08:03 +00004542def CMDweb(parser, args):
4543 """Opens the current CL in the web browser."""
4544 _, args = parser.parse_args(args)
4545 if args:
4546 parser.error('Unrecognized args: %s' % ' '.join(args))
4547
4548 issue_url = Changelist().GetIssueURL()
4549 if not issue_url:
4550 print >> sys.stderr, 'ERROR No issue to open'
4551 return 1
4552
4553 webbrowser.open(issue_url)
4554 return 0
4555
4556
maruel@chromium.org27bb3872011-05-30 20:33:19 +00004557def CMDset_commit(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004558 """Sets the commit bit to trigger the Commit Queue."""
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00004559 parser.add_option('-d', '--dry-run', action='store_true',
4560 help='trigger in dry run mode')
4561 parser.add_option('-c', '--clear', action='store_true',
4562 help='stop CQ run, if any')
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004563 auth.add_auth_options(parser)
4564 options, args = parser.parse_args(args)
4565 auth_config = auth.extract_auth_config_from_options(options)
maruel@chromium.org27bb3872011-05-30 20:33:19 +00004566 if args:
4567 parser.error('Unrecognized args: %s' % ' '.join(args))
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00004568 if options.dry_run and options.clear:
4569 parser.error('Make up your mind: both --dry-run and --clear not allowed')
4570
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004571 cl = Changelist(auth_config=auth_config)
tandrii@chromium.orgfa330e82016-04-13 17:09:52 +00004572 if options.clear:
4573 state = _CQState.CLEAR
4574 elif options.dry_run:
4575 state = _CQState.DRY_RUN
4576 else:
4577 state = _CQState.COMMIT
4578 if not cl.GetIssue():
4579 parser.error('Must upload the issue first')
4580 cl.SetCQState(state)
maruel@chromium.org27bb3872011-05-30 20:33:19 +00004581 return 0
4582
4583
groby@chromium.org411034a2013-02-26 15:12:01 +00004584def CMDset_close(parser, args):
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004585 """Closes the issue."""
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004586 auth.add_auth_options(parser)
4587 options, args = parser.parse_args(args)
4588 auth_config = auth.extract_auth_config_from_options(options)
groby@chromium.org411034a2013-02-26 15:12:01 +00004589 if args:
4590 parser.error('Unrecognized args: %s' % ' '.join(args))
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004591 cl = Changelist(auth_config=auth_config)
groby@chromium.org411034a2013-02-26 15:12:01 +00004592 # Ensure there actually is an issue to close.
4593 cl.GetDescription()
4594 cl.CloseIssue()
4595 return 0
4596
4597
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004598def CMDdiff(parser, args):
wychen@chromium.org37b2ec02015-04-03 00:49:15 +00004599 """Shows differences between local tree and last upload."""
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004600 auth.add_auth_options(parser)
4601 options, args = parser.parse_args(args)
4602 auth_config = auth.extract_auth_config_from_options(options)
4603 if args:
4604 parser.error('Unrecognized args: %s' % ' '.join(args))
wychen@chromium.org46309bf2015-04-03 21:04:49 +00004605
4606 # Uncommitted (staged and unstaged) changes will be destroyed by
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004607 # "git reset --hard" if there are merging conflicts in CMDPatchIssue().
wychen@chromium.org46309bf2015-04-03 21:04:49 +00004608 # Staged changes would be committed along with the patch from last
4609 # upload, hence counted toward the "last upload" side in the final
4610 # diff output, and this is not what we want.
sbc@chromium.org71437c02015-04-09 19:29:40 +00004611 if git_common.is_dirty_git_tree('diff'):
wychen@chromium.org46309bf2015-04-03 21:04:49 +00004612 return 1
4613
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004614 cl = Changelist(auth_config=auth_config)
sbc@chromium.org78dc9842013-11-25 18:43:44 +00004615 issue = cl.GetIssue()
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004616 branch = cl.GetBranch()
sbc@chromium.org78dc9842013-11-25 18:43:44 +00004617 if not issue:
4618 DieWithError('No issue found for current branch (%s)' % branch)
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004619 TMP_BRANCH = 'git-cl-diff'
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00004620 base_branch = cl.GetCommonAncestorWithUpstream()
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004621
4622 # Create a new branch based on the merge-base
4623 RunGit(['checkout', '-q', '-b', TMP_BRANCH, base_branch])
tandrii@chromium.org534f67a2016-04-07 18:47:05 +00004624 # Clear cached branch in cl object, to avoid overwriting original CL branch
4625 # properties.
4626 cl.ClearBranch()
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004627 try:
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004628 rtn = cl.CMDPatchIssue(issue, reject=False, nocommit=False, directory=None)
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004629 if rtn != 0:
wychen@chromium.orga872e752015-04-28 23:42:18 +00004630 RunGit(['reset', '--hard'])
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004631 return rtn
4632
wychen@chromium.org06928532015-02-03 02:11:29 +00004633 # Switch back to starting branch and diff against the temporary
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004634 # branch containing the latest rietveld patch.
wychen@chromium.org06928532015-02-03 02:11:29 +00004635 subprocess2.check_call(['git', 'diff', TMP_BRANCH, branch, '--'])
sbc@chromium.org87b9bf02013-09-26 20:35:15 +00004636 finally:
4637 RunGit(['checkout', '-q', branch])
4638 RunGit(['branch', '-D', TMP_BRANCH])
4639
4640 return 0
4641
4642
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00004643def CMDowners(parser, args):
wychen@chromium.org37b2ec02015-04-03 00:49:15 +00004644 """Interactively find the owners for reviewing."""
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00004645 parser.add_option(
4646 '--no-color',
4647 action='store_true',
4648 help='Use this option to disable color output')
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004649 auth.add_auth_options(parser)
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00004650 options, args = parser.parse_args(args)
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004651 auth_config = auth.extract_auth_config_from_options(options)
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00004652
4653 author = RunGit(['config', 'user.email']).strip() or None
4654
vadimsh@chromium.orgcf6a5d22015-04-09 22:02:00 +00004655 cl = Changelist(auth_config=auth_config)
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00004656
4657 if args:
4658 if len(args) > 1:
4659 parser.error('Unknown args')
4660 base_branch = args[0]
4661 else:
4662 # Default to diffing against the common ancestor of the upstream branch.
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00004663 base_branch = cl.GetCommonAncestorWithUpstream()
ikarienator@chromium.orgfaf3fdf2013-09-20 02:11:48 +00004664
4665 change = cl.GetChange(base_branch, None)
4666 return owners_finder.OwnersFinder(
4667 [f.LocalPath() for f in
4668 cl.GetChange(base_branch, None).AffectedFiles()],
4669 change.RepositoryRoot(), author,
4670 fopen=file, os_path=os.path, glob=glob.glob,
4671 disable_color=options.no_color).run()
4672
4673
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004674def BuildGitDiffCmd(diff_type, upstream_commit, args):
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004675 """Generates a diff command."""
4676 # Generate diff for the current branch's changes.
4677 diff_cmd = ['diff', '--no-ext-diff', '--no-prefix', diff_type,
4678 upstream_commit, '--' ]
4679
4680 if args:
4681 for arg in args:
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004682 if os.path.isdir(arg) or os.path.isfile(arg):
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004683 diff_cmd.append(arg)
4684 else:
4685 DieWithError('Argument "%s" is not a file or a directory' % arg)
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004686
4687 return diff_cmd
4688
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004689def MatchingFileType(file_name, extensions):
4690 """Returns true if the file name ends with one of the given extensions."""
4691 return bool([ext for ext in extensions if file_name.lower().endswith(ext)])
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004692
enne@chromium.org555cfe42014-01-29 18:21:39 +00004693@subcommand.usage('[files or directories to diff]')
agable@chromium.orgfab8f822013-05-06 17:43:09 +00004694def CMDformat(parser, args):
sbc@chromium.org9d0644d2015-06-05 23:16:54 +00004695 """Runs auto-formatting tools (clang-format etc.) on the diff."""
thakis@chromium.org9819b1b2014-12-09 21:21:53 +00004696 CLANG_EXTS = ['.cc', '.cpp', '.h', '.mm', '.proto', '.java']
kylechar@chromium.org8b61f112016-02-05 13:28:58 +00004697 GN_EXTS = ['.gn', '.gni']
enne@chromium.org3b7e15c2014-01-21 17:44:47 +00004698 parser.add_option('--full', action='store_true',
4699 help='Reformat the full content of all touched files')
4700 parser.add_option('--dry-run', action='store_true',
4701 help='Don\'t modify any file on disk.')
sbc@chromium.org9d0644d2015-06-05 23:16:54 +00004702 parser.add_option('--python', action='store_true',
4703 help='Format python code with yapf (experimental).')
wittman@chromium.org04d5a222014-03-07 18:30:42 +00004704 parser.add_option('--diff', action='store_true',
4705 help='Print diff to stdout rather than modifying files.')
agable@chromium.orgfab8f822013-05-06 17:43:09 +00004706 opts, args = parser.parse_args(args)
agable@chromium.orgfab8f822013-05-06 17:43:09 +00004707
enne@chromium.orgff7a1fb2013-12-10 19:21:41 +00004708 # git diff generates paths against the root of the repository. Change
4709 # to that directory so clang-format can find files even within subdirs.
thestig@chromium.org8b0553c2014-02-11 00:33:37 +00004710 rel_base_path = settings.GetRelativeRoot()
enne@chromium.orgff7a1fb2013-12-10 19:21:41 +00004711 if rel_base_path:
4712 os.chdir(rel_base_path)
4713
digit@chromium.org29e47272013-05-17 17:01:46 +00004714 # Grab the merge-base commit, i.e. the upstream commit of the current
4715 # branch when it was created or the last time it was rebased. This is
4716 # to cover the case where the user may have called "git fetch origin",
4717 # moving the origin branch to a newer commit, but hasn't rebased yet.
4718 upstream_commit = None
4719 cl = Changelist()
4720 upstream_branch = cl.GetUpstreamBranch()
4721 if upstream_branch:
4722 upstream_commit = RunGit(['merge-base', 'HEAD', upstream_branch])
4723 upstream_commit = upstream_commit.strip()
4724
4725 if not upstream_commit:
4726 DieWithError('Could not find base commit for this branch. '
4727 'Are you in detached state?')
4728
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004729 changed_files_cmd = BuildGitDiffCmd('--name-only', upstream_commit, args)
4730 diff_output = RunGit(changed_files_cmd)
4731 diff_files = diff_output.splitlines()
jkarlin@chromium.orgad21b922016-01-28 17:48:42 +00004732 # Filter out files deleted by this CL
4733 diff_files = [x for x in diff_files if os.path.isfile(x)]
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004734
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004735 clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)]
4736 python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])]
4737 dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])]
kylechar@chromium.org8b61f112016-02-05 13:28:58 +00004738 gn_diff_files = [x for x in diff_files if MatchingFileType(x, GN_EXTS)]
digit@chromium.org29e47272013-05-17 17:01:46 +00004739
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +00004740 top_dir = os.path.normpath(
4741 RunGit(["rev-parse", "--show-toplevel"]).rstrip('\n'))
4742
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004743 # Set to 2 to signal to CheckPatchFormatted() that this patch isn't
4744 # formatted. This is used to block during the presubmit.
4745 return_value = 0
4746
sammc@chromium.org0b35f5d2016-02-25 22:39:23 +00004747 if clang_diff_files:
techtonik@gmail.com5573df12016-04-12 18:34:10 +00004748 # Locate the clang-format binary in the checkout
4749 try:
4750 clang_format_tool = clang_format.FindClangFormatToolInChromiumTree()
4751 except clang_format.NotFoundError, e:
4752 DieWithError(e)
4753
sammc@chromium.org0b35f5d2016-02-25 22:39:23 +00004754 if opts.full:
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004755 cmd = [clang_format_tool]
4756 if not opts.dry_run and not opts.diff:
4757 cmd.append('-i')
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004758 stdout = RunCommand(cmd + clang_diff_files, cwd=top_dir)
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004759 if opts.diff:
4760 sys.stdout.write(stdout)
sammc@chromium.org0b35f5d2016-02-25 22:39:23 +00004761 else:
4762 env = os.environ.copy()
4763 env['PATH'] = str(os.path.dirname(clang_format_tool))
4764 try:
4765 script = clang_format.FindClangFormatScriptInChromiumTree(
4766 'clang-format-diff.py')
4767 except clang_format.NotFoundError, e:
4768 DieWithError(e)
digit@chromium.orgd6ddc1c2013-10-25 15:36:32 +00004769
sammc@chromium.org0b35f5d2016-02-25 22:39:23 +00004770 cmd = [sys.executable, script, '-p0']
4771 if not opts.dry_run and not opts.diff:
4772 cmd.append('-i')
digit@chromium.orgd6ddc1c2013-10-25 15:36:32 +00004773
sammc@chromium.org0b35f5d2016-02-25 22:39:23 +00004774 diff_cmd = BuildGitDiffCmd('-U0', upstream_commit, clang_diff_files)
4775 diff_output = RunGit(diff_cmd)
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004776
sammc@chromium.org0b35f5d2016-02-25 22:39:23 +00004777 stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env)
4778 if opts.diff:
4779 sys.stdout.write(stdout)
4780 if opts.dry_run and len(stdout) > 0:
4781 return_value = 2
agable@chromium.orgfab8f822013-05-06 17:43:09 +00004782
sbc@chromium.org9d0644d2015-06-05 23:16:54 +00004783 # Similar code to above, but using yapf on .py files rather than clang-format
4784 # on C/C++ files
4785 if opts.python:
sbc@chromium.org9d0644d2015-06-05 23:16:54 +00004786 yapf_tool = gclient_utils.FindExecutable('yapf')
4787 if yapf_tool is None:
4788 DieWithError('yapf not found in PATH')
4789
4790 if opts.full:
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004791 if python_diff_files:
sbc@chromium.org9d0644d2015-06-05 23:16:54 +00004792 cmd = [yapf_tool]
4793 if not opts.dry_run and not opts.diff:
4794 cmd.append('-i')
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004795 stdout = RunCommand(cmd + python_diff_files, cwd=top_dir)
sbc@chromium.org9d0644d2015-06-05 23:16:54 +00004796 if opts.diff:
4797 sys.stdout.write(stdout)
4798 else:
4799 # TODO(sbc): yapf --lines mode still has some issues.
4800 # https://github.com/google/yapf/issues/154
4801 DieWithError('--python currently only works with --full')
4802
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004803 # Dart's formatter does not have the nice property of only operating on
4804 # modified chunks, so hard code full.
4805 if dart_diff_files:
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004806 try:
4807 command = [dart_format.FindDartFmtToolInChromiumTree()]
4808 if not opts.dry_run and not opts.diff:
4809 command.append('-w')
jkarlin@chromium.org6f7fa5e2016-01-20 19:32:21 +00004810 command.extend(dart_diff_files)
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004811
ppi@chromium.org6593d932016-03-03 15:41:15 +00004812 stdout = RunCommand(command, cwd=top_dir)
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004813 if opts.dry_run and stdout:
4814 return_value = 2
4815 except dart_format.NotFoundError as e:
erikcorry@chromium.org3e445022015-12-17 09:07:26 +00004816 print ('Warning: Unable to check Dart code formatting. Dart SDK not ' +
4817 'found in this checkout. Files in other languages are still ' +
4818 'formatted.')
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004819
kylechar@chromium.org8b61f112016-02-05 13:28:58 +00004820 # Format GN build files. Always run on full build files for canonical form.
4821 if gn_diff_files:
4822 cmd = ['gn', 'format']
4823 if not opts.dry_run and not opts.diff:
4824 cmd.append('--in-place')
4825 for gn_diff_file in gn_diff_files:
bsep@chromium.org627d9002016-04-29 00:00:52 +00004826 stdout = RunCommand(cmd + [gn_diff_file],
4827 shell=sys.platform == 'win32',
4828 cwd=top_dir)
kylechar@chromium.org8b61f112016-02-05 13:28:58 +00004829 if opts.diff:
4830 sys.stdout.write(stdout)
4831
erg@chromium.orge0a7c5d2015-02-23 20:30:08 +00004832 return return_value
agable@chromium.orgfab8f822013-05-06 17:43:09 +00004833
4834
scottmg@chromium.org84a80c42015-09-22 20:40:37 +00004835@subcommand.usage('<codereview url or issue id>')
4836def CMDcheckout(parser, args):
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00004837 """Checks out a branch associated with a given Rietveld or Gerrit issue."""
scottmg@chromium.org84a80c42015-09-22 20:40:37 +00004838 _, args = parser.parse_args(args)
4839
4840 if len(args) != 1:
4841 parser.print_help()
4842 return 1
4843
tandrii@chromium.orgf86c7d32016-04-01 19:27:30 +00004844 issue_arg = ParseIssueNumberArgument(args[0])
tandrii@chromium.orgde6c9a12016-04-11 15:33:53 +00004845 if not issue_arg.valid:
scottmg@chromium.org84a80c42015-09-22 20:40:37 +00004846 parser.print_help()
4847 return 1
tandrii@chromium.orgabd27e52016-04-11 15:43:32 +00004848 target_issue = str(issue_arg.issue)
scottmg@chromium.org84a80c42015-09-22 20:40:37 +00004849
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00004850 def find_issues(issueprefix):
tandrii@chromium.org26c8fd22016-04-11 21:33:21 +00004851 output = RunGit(['config', '--local', '--get-regexp',
4852 r'branch\..*\.%s' % issueprefix],
4853 error_ok=True)
4854 for key, issue in [x.split() for x in output.splitlines()]:
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00004855 if issue == target_issue:
4856 yield re.sub(r'branch\.(.*)\.%s' % issueprefix, r'\1', key)
scottmg@chromium.org84a80c42015-09-22 20:40:37 +00004857
tandrii@chromium.org5df290f2016-04-11 16:12:29 +00004858 branches = []
4859 for cls in _CODEREVIEW_IMPLEMENTATIONS.values():
tandrii@chromium.orgd03bc632016-04-12 14:17:26 +00004860 branches.extend(find_issues(cls.IssueSettingSuffix()))
scottmg@chromium.org84a80c42015-09-22 20:40:37 +00004861 if len(branches) == 0:
4862 print 'No branch found for issue %s.' % target_issue
4863 return 1
4864 if len(branches) == 1:
4865 RunGit(['checkout', branches[0]])
4866 else:
4867 print 'Multiple branches match issue %s:' % target_issue
4868 for i in range(len(branches)):
4869 print '%d: %s' % (i, branches[i])
4870 which = raw_input('Choose by index: ')
4871 try:
4872 RunGit(['checkout', branches[int(which)]])
4873 except (IndexError, ValueError):
4874 print 'Invalid selection, not checking out any branch.'
4875 return 1
4876
4877 return 0
4878
4879
maruel@chromium.org29404b52014-09-08 22:58:00 +00004880def CMDlol(parser, args):
4881 # This command is intentionally undocumented.
thakis@chromium.org3421c992014-11-02 02:20:32 +00004882 print zlib.decompress(base64.b64decode(
4883 'eNptkLEOwyAMRHe+wupCIqW57v0Vq84WqWtXyrcXnCBsmgMJ+/SSAxMZgRB6NzE'
4884 'E2ObgCKJooYdu4uAQVffUEoE1sRQLxAcqzd7uK2gmStrll1ucV3uZyaY5sXyDd9'
4885 'JAnN+lAXsOMJ90GANAi43mq5/VeeacylKVgi8o6F1SC63FxnagHfJUTfUYdCR/W'
4886 'Ofe+0dHL7PicpytKP750Fh1q2qnLVof4w8OZWNY'))
maruel@chromium.org29404b52014-09-08 22:58:00 +00004887 return 0
4888
4889
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004890class OptionParser(optparse.OptionParser):
4891 """Creates the option parse and add --verbose support."""
4892 def __init__(self, *args, **kwargs):
maruel@chromium.org0633fb42013-08-16 20:06:14 +00004893 optparse.OptionParser.__init__(
4894 self, *args, prog='git cl', version=__version__, **kwargs)
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004895 self.add_option(
4896 '-v', '--verbose', action='count', default=0,
4897 help='Use 2 times for more debugging info')
4898
4899 def parse_args(self, args=None, values=None):
4900 options, args = optparse.OptionParser.parse_args(self, args, values)
4901 levels = [logging.WARNING, logging.INFO, logging.DEBUG]
4902 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
4903 return options, args
4904
iannucci@chromium.orgd9c1b202013-07-24 23:52:11 +00004905
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004906def main(argv):
maruel@chromium.org82798cb2012-02-23 18:16:12 +00004907 if sys.hexversion < 0x02060000:
4908 print >> sys.stderr, (
4909 '\nYour python version %s is unsupported, please upgrade.\n' %
4910 sys.version.split(' ', 1)[0])
4911 return 2
maruel@chromium.org2e23ce32013-05-07 12:42:28 +00004912
maruel@chromium.orgddd59412011-11-30 14:20:38 +00004913 # Reload settings.
4914 global settings
4915 settings = Settings()
4916
maruel@chromium.org39c0b222013-08-17 16:57:01 +00004917 colorize_CMDstatus_doc()
maruel@chromium.org0633fb42013-08-16 20:06:14 +00004918 dispatcher = subcommand.CommandDispatcher(__name__)
4919 try:
4920 return dispatcher.execute(OptionParser(), argv)
vadimsh@chromium.orgeed4df32015-04-10 21:30:20 +00004921 except auth.AuthenticationError as e:
4922 DieWithError(str(e))
maruel@chromium.org0633fb42013-08-16 20:06:14 +00004923 except urllib2.HTTPError, e:
4924 if e.code != 500:
4925 raise
4926 DieWithError(
4927 ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith '
4928 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)))
sbc@chromium.org013731e2015-02-26 18:28:43 +00004929 return 0
chase@chromium.orgcc51cd02010-12-23 00:48:39 +00004930
4931
4932if __name__ == '__main__':
maruel@chromium.org2e23ce32013-05-07 12:42:28 +00004933 # These affect sys.stdout so do it outside of main() to simplify mocks in
4934 # unit testing.
maruel@chromium.org6f09cd92011-04-01 16:38:12 +00004935 fix_encoding.fix_encoding()
iannucci@chromium.org596cd5c2016-04-04 21:34:39 +00004936 setup_color.init()
sbc@chromium.org013731e2015-02-26 18:28:43 +00004937 try:
4938 sys.exit(main(sys.argv[1:]))
4939 except KeyboardInterrupt:
4940 sys.stderr.write('interrupted\n')
4941 sys.exit(1)