iannucci@chromium.org | 405b87e | 2015-11-12 18:08:34 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
miket@chromium.org | 183df1a | 2012-01-04 19:44:55 +0000 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
maruel@chromium.org | 725f1c3 | 2011-04-01 20:24:54 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 6 | # Copyright (C) 2008 Evan Martin <martine@danga.com> |
| 7 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 8 | """A git-command for integrating reviews on Rietveld and Gerrit.""" |
maruel@chromium.org | 725f1c3 | 2011-04-01 20:24:54 +0000 | [diff] [blame] | 9 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 10 | from __future__ import print_function |
| 11 | |
tapted@chromium.org | 6a0b07c | 2013-07-10 01:29:19 +0000 | [diff] [blame] | 12 | from distutils.version import LooseVersion |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 13 | from multiprocessing.pool import ThreadPool |
thakis@chromium.org | 3421c99 | 2014-11-02 02:20:32 +0000 | [diff] [blame] | 14 | import base64 |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 15 | import collections |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 16 | import httplib |
maruel@chromium.org | 4f6852c | 2012-04-20 20:39:20 +0000 | [diff] [blame] | 17 | import json |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 18 | import logging |
calamity@chromium.org | cf19748 | 2016-04-29 20:15:53 +0000 | [diff] [blame] | 19 | import multiprocessing |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 20 | import optparse |
| 21 | import os |
| 22 | import re |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 23 | import stat |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 24 | import sys |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 25 | import textwrap |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 26 | import time |
| 27 | import traceback |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 28 | import urllib |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 29 | import urllib2 |
maruel@chromium.org | 967c0a8 | 2013-06-17 22:52:24 +0000 | [diff] [blame] | 30 | import urlparse |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 31 | import uuid |
thestig@chromium.org | 00858c8 | 2013-12-02 23:08:03 +0000 | [diff] [blame] | 32 | import webbrowser |
thakis@chromium.org | 3421c99 | 2014-11-02 02:20:32 +0000 | [diff] [blame] | 33 | import zlib |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 34 | |
| 35 | try: |
maruel@chromium.org | c98c0c5 | 2011-04-06 13:39:43 +0000 | [diff] [blame] | 36 | import readline # pylint: disable=F0401,W0611 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 37 | except ImportError: |
| 38 | pass |
| 39 | |
maruel@chromium.org | 2e23ce3 | 2013-05-07 12:42:28 +0000 | [diff] [blame] | 40 | from third_party import colorama |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 41 | from third_party import httplib2 |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 42 | from third_party import upload |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 43 | import auth |
hinoka@chromium.org | feb9e2a | 2015-09-25 19:11:09 +0000 | [diff] [blame] | 44 | from luci_hacks import trigger_luci_job as luci_trigger |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 45 | import clang_format |
tandrii@chromium.org | 71184c0 | 2016-01-13 15:18:44 +0000 | [diff] [blame] | 46 | import commit_queue |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 47 | import dart_format |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 48 | import setup_color |
maruel@chromium.org | 6f09cd9 | 2011-04-01 16:38:12 +0000 | [diff] [blame] | 49 | import fix_encoding |
maruel@chromium.org | 0e0436a | 2011-10-25 13:32:41 +0000 | [diff] [blame] | 50 | import gclient_utils |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 51 | import gerrit_util |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 52 | import git_cache |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 53 | import git_common |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 54 | import git_footers |
piman@chromium.org | 336f912 | 2014-09-04 02:16:55 +0000 | [diff] [blame] | 55 | import owners |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 56 | import owners_finder |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 57 | import presubmit_support |
maruel@chromium.org | cab38e9 | 2011-04-09 00:30:51 +0000 | [diff] [blame] | 58 | import rietveld |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 59 | import scm |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 60 | import subcommand |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 61 | import subprocess2 |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 62 | import watchlists |
| 63 | |
tandrii | 7400cf0 | 2016-06-21 08:48:07 -0700 | [diff] [blame] | 64 | __version__ = '2.0' |
maruel@chromium.org | 2a74d37 | 2011-03-29 19:05:50 +0000 | [diff] [blame] | 65 | |
tandrii | 9d2c7a3 | 2016-06-22 03:42:45 -0700 | [diff] [blame] | 66 | COMMIT_BOT_EMAIL = 'commit-bot@chromium.org' |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 67 | DEFAULT_SERVER = 'https://codereview.appspot.com' |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 68 | POSTUPSTREAM_HOOK_PATTERN = '.git/hooks/post-cl-%s' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 69 | DESCRIPTION_BACKUP_FILE = '~/.git_cl_description_backup' |
pgervais@chromium.org | d6617f3 | 2013-11-19 00:34:54 +0000 | [diff] [blame] | 70 | GIT_INSTRUCTIONS_URL = 'http://code.google.com/p/chromium/wiki/UsingGit' |
rmistry@google.com | c68112d | 2015-03-03 12:48:06 +0000 | [diff] [blame] | 71 | REFS_THAT_ALIAS_TO_OTHER_REFS = { |
| 72 | 'refs/remotes/origin/lkgr': 'refs/remotes/origin/master', |
| 73 | 'refs/remotes/origin/lkcr': 'refs/remotes/origin/master', |
| 74 | } |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 75 | |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 76 | # Valid extensions for files we want to lint. |
| 77 | DEFAULT_LINT_REGEX = r"(.*\.cpp|.*\.cc|.*\.h)" |
| 78 | DEFAULT_LINT_IGNORE_REGEX = r"$^" |
| 79 | |
maruel@chromium.org | 2e23ce3 | 2013-05-07 12:42:28 +0000 | [diff] [blame] | 80 | # Shortcut since it quickly becomes redundant. |
| 81 | Fore = colorama.Fore |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 82 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 83 | # Initialized in main() |
| 84 | settings = None |
| 85 | |
| 86 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 87 | def DieWithError(message): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 88 | print(message, file=sys.stderr) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 89 | sys.exit(1) |
| 90 | |
| 91 | |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 92 | def GetNoGitPagerEnv(): |
| 93 | env = os.environ.copy() |
| 94 | # 'cat' is a magical git string that disables pagers on all platforms. |
| 95 | env['GIT_PAGER'] = 'cat' |
| 96 | return env |
| 97 | |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 98 | |
bsep@chromium.org | 627d900 | 2016-04-29 00:00:52 +0000 | [diff] [blame] | 99 | def RunCommand(args, error_ok=False, error_message=None, shell=False, **kwargs): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 100 | try: |
bsep@chromium.org | 627d900 | 2016-04-29 00:00:52 +0000 | [diff] [blame] | 101 | return subprocess2.check_output(args, shell=shell, **kwargs) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 102 | except subprocess2.CalledProcessError as e: |
| 103 | logging.debug('Failed running %s', args) |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 104 | if not error_ok: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 105 | DieWithError( |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 106 | 'Command "%s" failed.\n%s' % ( |
| 107 | ' '.join(args), error_message or e.stdout or '')) |
| 108 | return e.stdout |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def RunGit(args, **kwargs): |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 112 | """Returns stdout.""" |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 113 | return RunCommand(['git'] + args, **kwargs) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 114 | |
| 115 | |
enne@chromium.org | 3b7e15c | 2014-01-21 17:44:47 +0000 | [diff] [blame] | 116 | def RunGitWithCode(args, suppress_stderr=False): |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 117 | """Returns return code and stdout.""" |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 118 | try: |
enne@chromium.org | 3b7e15c | 2014-01-21 17:44:47 +0000 | [diff] [blame] | 119 | if suppress_stderr: |
| 120 | stderr = subprocess2.VOID |
| 121 | else: |
| 122 | stderr = sys.stderr |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 123 | out, code = subprocess2.communicate(['git'] + args, |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 124 | env=GetNoGitPagerEnv(), |
enne@chromium.org | 3b7e15c | 2014-01-21 17:44:47 +0000 | [diff] [blame] | 125 | stdout=subprocess2.PIPE, |
| 126 | stderr=stderr) |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 127 | return code, out[0] |
| 128 | except ValueError: |
| 129 | # When the subprocess fails, it returns None. That triggers a ValueError |
| 130 | # when trying to unpack the return value into (out, code). |
| 131 | return 1, '' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 132 | |
| 133 | |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 134 | def RunGitSilent(args): |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 135 | """Returns stdout, suppresses stderr and ignores the return code.""" |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 136 | return RunGitWithCode(args, suppress_stderr=True)[1] |
| 137 | |
| 138 | |
tapted@chromium.org | 6a0b07c | 2013-07-10 01:29:19 +0000 | [diff] [blame] | 139 | def IsGitVersionAtLeast(min_version): |
ilevy@chromium.org | cc56ee4 | 2013-07-10 22:16:29 +0000 | [diff] [blame] | 140 | prefix = 'git version ' |
tapted@chromium.org | 6a0b07c | 2013-07-10 01:29:19 +0000 | [diff] [blame] | 141 | version = RunGit(['--version']).strip() |
ilevy@chromium.org | cc56ee4 | 2013-07-10 22:16:29 +0000 | [diff] [blame] | 142 | return (version.startswith(prefix) and |
| 143 | LooseVersion(version[len(prefix):]) >= LooseVersion(min_version)) |
tapted@chromium.org | 6a0b07c | 2013-07-10 01:29:19 +0000 | [diff] [blame] | 144 | |
| 145 | |
pgervais@chromium.org | 8ba38ff | 2015-06-11 21:41:25 +0000 | [diff] [blame] | 146 | def BranchExists(branch): |
| 147 | """Return True if specified branch exists.""" |
| 148 | code, _ = RunGitWithCode(['rev-parse', '--verify', branch], |
| 149 | suppress_stderr=True) |
| 150 | return not code |
| 151 | |
| 152 | |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 153 | def ask_for_data(prompt): |
| 154 | try: |
| 155 | return raw_input(prompt) |
| 156 | except KeyboardInterrupt: |
| 157 | # Hide the exception. |
| 158 | sys.exit(1) |
| 159 | |
| 160 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 161 | def git_set_branch_value(key, value): |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 162 | branch = GetCurrentBranch() |
rogerta@chromium.org | caa1655 | 2013-03-18 20:45:05 +0000 | [diff] [blame] | 163 | if not branch: |
| 164 | return |
| 165 | |
| 166 | cmd = ['config'] |
| 167 | if isinstance(value, int): |
| 168 | cmd.append('--int') |
| 169 | git_key = 'branch.%s.%s' % (branch, key) |
| 170 | RunGit(cmd + [git_key, str(value)]) |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 171 | |
| 172 | |
| 173 | def git_get_branch_default(key, default): |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 174 | branch = GetCurrentBranch() |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 175 | if branch: |
| 176 | git_key = 'branch.%s.%s' % (branch, key) |
| 177 | (_, stdout) = RunGitWithCode(['config', '--int', '--get', git_key]) |
| 178 | try: |
| 179 | return int(stdout.strip()) |
| 180 | except ValueError: |
| 181 | pass |
| 182 | return default |
| 183 | |
| 184 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 185 | def add_git_similarity(parser): |
| 186 | parser.add_option( |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 187 | '--similarity', metavar='SIM', type='int', action='store', |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 188 | help='Sets the percentage that a pair of files need to match in order to' |
| 189 | ' be considered copies (default 50)') |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 190 | parser.add_option( |
| 191 | '--find-copies', action='store_true', |
| 192 | help='Allows git to look for copies.') |
| 193 | parser.add_option( |
| 194 | '--no-find-copies', action='store_false', dest='find_copies', |
| 195 | help='Disallows git from looking for copies.') |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 196 | |
| 197 | old_parser_args = parser.parse_args |
| 198 | def Parse(args): |
| 199 | options, args = old_parser_args(args) |
| 200 | |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 201 | if options.similarity is None: |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 202 | options.similarity = git_get_branch_default('git-cl-similarity', 50) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 203 | else: |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 204 | print('Note: Saving similarity of %d%% in git config.' |
| 205 | % options.similarity) |
| 206 | git_set_branch_value('git-cl-similarity', options.similarity) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 207 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 208 | options.similarity = max(0, min(options.similarity, 100)) |
| 209 | |
| 210 | if options.find_copies is None: |
| 211 | options.find_copies = bool( |
| 212 | git_get_branch_default('git-find-copies', True)) |
| 213 | else: |
| 214 | git_set_branch_value('git-find-copies', int(options.find_copies)) |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 215 | |
| 216 | print('Using %d%% similarity for rename/copy detection. ' |
| 217 | 'Override with --similarity.' % options.similarity) |
| 218 | |
| 219 | return options, args |
| 220 | parser.parse_args = Parse |
| 221 | |
| 222 | |
machenbach@chromium.org | 4545314 | 2015-09-15 08:45:22 +0000 | [diff] [blame] | 223 | def _get_properties_from_options(options): |
| 224 | properties = dict(x.split('=', 1) for x in options.properties) |
| 225 | for key, val in properties.iteritems(): |
| 226 | try: |
| 227 | properties[key] = json.loads(val) |
| 228 | except ValueError: |
| 229 | pass # If a value couldn't be evaluated, treat it as a string. |
| 230 | return properties |
| 231 | |
| 232 | |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 233 | def _prefix_master(master): |
| 234 | """Convert user-specified master name to full master name. |
| 235 | |
| 236 | Buildbucket uses full master name(master.tryserver.chromium.linux) as bucket |
| 237 | name, while the developers always use shortened master name |
| 238 | (tryserver.chromium.linux) by stripping off the prefix 'master.'. This |
| 239 | function does the conversion for buildbucket migration. |
| 240 | """ |
| 241 | prefix = 'master.' |
| 242 | if master.startswith(prefix): |
| 243 | return master |
| 244 | return '%s%s' % (prefix, master) |
| 245 | |
| 246 | |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 247 | def _buildbucket_retry(operation_name, http, *args, **kwargs): |
| 248 | """Retries requests to buildbucket service and returns parsed json content.""" |
| 249 | try_count = 0 |
| 250 | while True: |
| 251 | response, content = http.request(*args, **kwargs) |
| 252 | try: |
| 253 | content_json = json.loads(content) |
| 254 | except ValueError: |
| 255 | content_json = None |
| 256 | |
| 257 | # Buildbucket could return an error even if status==200. |
| 258 | if content_json and content_json.get('error'): |
nodir@chromium.org | baff4e1 | 2016-03-08 00:33:57 +0000 | [diff] [blame] | 259 | error = content_json.get('error') |
| 260 | if error.get('code') == 403: |
| 261 | raise BuildbucketResponseException( |
| 262 | 'Access denied: %s' % error.get('message', '')) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 263 | msg = 'Error in response. Reason: %s. Message: %s.' % ( |
nodir@chromium.org | baff4e1 | 2016-03-08 00:33:57 +0000 | [diff] [blame] | 264 | error.get('reason', ''), error.get('message', '')) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 265 | raise BuildbucketResponseException(msg) |
| 266 | |
| 267 | if response.status == 200: |
| 268 | if not content_json: |
| 269 | raise BuildbucketResponseException( |
| 270 | 'Buildbucket returns invalid json content: %s.\n' |
| 271 | 'Please file bugs at http://crbug.com, label "Infra-BuildBucket".' % |
| 272 | content) |
| 273 | return content_json |
| 274 | if response.status < 500 or try_count >= 2: |
| 275 | raise httplib2.HttpLib2Error(content) |
| 276 | |
| 277 | # status >= 500 means transient failures. |
| 278 | logging.debug('Transient errors when %s. Will retry.', operation_name) |
| 279 | time.sleep(0.5 + 1.5*try_count) |
| 280 | try_count += 1 |
| 281 | assert False, 'unreachable' |
| 282 | |
| 283 | |
hinoka@chromium.org | feb9e2a | 2015-09-25 19:11:09 +0000 | [diff] [blame] | 284 | def trigger_luci_job(changelist, masters, options): |
| 285 | """Send a job to run on LUCI.""" |
| 286 | issue_props = changelist.GetIssueProperties() |
| 287 | issue = changelist.GetIssue() |
| 288 | patchset = changelist.GetMostRecentPatchset() |
| 289 | for builders_and_tests in sorted(masters.itervalues()): |
tandrii@chromium.org | 3764fa2 | 2015-10-21 16:40:40 +0000 | [diff] [blame] | 290 | # TODO(hinoka et al): add support for other properties. |
| 291 | # Currently, this completely ignores testfilter and other properties. |
| 292 | for builder in sorted(builders_and_tests): |
hinoka@chromium.org | feb9e2a | 2015-09-25 19:11:09 +0000 | [diff] [blame] | 293 | luci_trigger.trigger( |
| 294 | builder, 'HEAD', issue, patchset, issue_props['project']) |
| 295 | |
| 296 | |
machenbach@chromium.org | 4545314 | 2015-09-15 08:45:22 +0000 | [diff] [blame] | 297 | def trigger_try_jobs(auth_config, changelist, options, masters, category): |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 298 | rietveld_url = settings.GetDefaultServerUrl() |
| 299 | rietveld_host = urlparse.urlparse(rietveld_url).hostname |
| 300 | authenticator = auth.get_authenticator_for_host(rietveld_host, auth_config) |
| 301 | http = authenticator.authorize(httplib2.Http()) |
| 302 | http.force_exception_to_status_code = True |
| 303 | issue_props = changelist.GetIssueProperties() |
| 304 | issue = changelist.GetIssue() |
| 305 | patchset = changelist.GetMostRecentPatchset() |
machenbach@chromium.org | 4545314 | 2015-09-15 08:45:22 +0000 | [diff] [blame] | 306 | properties = _get_properties_from_options(options) |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 307 | |
| 308 | buildbucket_put_url = ( |
| 309 | 'https://{hostname}/_ah/api/buildbucket/v1/builds/batch'.format( |
sheyang@chromium.org | db37557 | 2015-08-17 19:22:23 +0000 | [diff] [blame] | 310 | hostname=options.buildbucket_host)) |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 311 | buildset = 'patch/rietveld/{hostname}/{issue}/{patch}'.format( |
| 312 | hostname=rietveld_host, |
| 313 | issue=issue, |
| 314 | patch=patchset) |
| 315 | |
| 316 | batch_req_body = {'builds': []} |
| 317 | print_text = [] |
| 318 | print_text.append('Tried jobs on:') |
| 319 | for master, builders_and_tests in sorted(masters.iteritems()): |
| 320 | print_text.append('Master: %s' % master) |
| 321 | bucket = _prefix_master(master) |
| 322 | for builder, tests in sorted(builders_and_tests.iteritems()): |
| 323 | print_text.append(' %s: %s' % (builder, tests)) |
| 324 | parameters = { |
| 325 | 'builder_name': builder, |
nodir@chromium.org | d221731 | 2015-09-21 15:51:21 +0000 | [diff] [blame] | 326 | 'changes': [{ |
| 327 | 'author': {'email': issue_props['owner_email']}, |
| 328 | 'revision': options.revision, |
| 329 | }], |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 330 | 'properties': { |
| 331 | 'category': category, |
| 332 | 'issue': issue, |
| 333 | 'master': master, |
| 334 | 'patch_project': issue_props['project'], |
| 335 | 'patch_storage': 'rietveld', |
| 336 | 'patchset': patchset, |
| 337 | 'reason': options.name, |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 338 | 'rietveld': rietveld_url, |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 339 | }, |
| 340 | } |
machenbach@chromium.org | 2403e80 | 2016-04-29 12:34:42 +0000 | [diff] [blame] | 341 | if 'presubmit' in builder.lower(): |
| 342 | parameters['properties']['dry_run'] = 'true' |
tandrii@chromium.org | 3764fa2 | 2015-10-21 16:40:40 +0000 | [diff] [blame] | 343 | if tests: |
| 344 | parameters['properties']['testfilter'] = tests |
machenbach@chromium.org | 4545314 | 2015-09-15 08:45:22 +0000 | [diff] [blame] | 345 | if properties: |
| 346 | parameters['properties'].update(properties) |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 347 | if options.clobber: |
| 348 | parameters['properties']['clobber'] = True |
| 349 | batch_req_body['builds'].append( |
| 350 | { |
| 351 | 'bucket': bucket, |
| 352 | 'parameters_json': json.dumps(parameters), |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 353 | 'client_operation_id': str(uuid.uuid4()), |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 354 | 'tags': ['builder:%s' % builder, |
| 355 | 'buildset:%s' % buildset, |
| 356 | 'master:%s' % master, |
| 357 | 'user_agent:git_cl_try'] |
| 358 | } |
| 359 | ) |
| 360 | |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 361 | _buildbucket_retry( |
| 362 | 'triggering tryjobs', |
| 363 | http, |
| 364 | buildbucket_put_url, |
| 365 | 'PUT', |
| 366 | body=json.dumps(batch_req_body), |
| 367 | headers={'Content-Type': 'application/json'} |
| 368 | ) |
tandrii@chromium.org | 35c6145 | 2016-02-26 15:24:57 +0000 | [diff] [blame] | 369 | print_text.append('To see results here, run: git cl try-results') |
| 370 | print_text.append('To see results in browser, run: git cl web') |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 371 | print('\n'.join(print_text)) |
kjellander@chromium.org | 4442454 | 2015-06-02 18:35:29 +0000 | [diff] [blame] | 372 | |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 373 | |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 374 | def fetch_try_jobs(auth_config, changelist, options): |
| 375 | """Fetches tryjobs from buildbucket. |
| 376 | |
| 377 | Returns a map from build id to build info as json dictionary. |
| 378 | """ |
| 379 | rietveld_url = settings.GetDefaultServerUrl() |
| 380 | rietveld_host = urlparse.urlparse(rietveld_url).hostname |
| 381 | authenticator = auth.get_authenticator_for_host(rietveld_host, auth_config) |
| 382 | if authenticator.has_cached_credentials(): |
| 383 | http = authenticator.authorize(httplib2.Http()) |
| 384 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 385 | print('Warning: Some results might be missing because %s' % |
| 386 | # Get the message on how to login. |
| 387 | (auth.LoginRequiredError(rietveld_host).message,)) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 388 | http = httplib2.Http() |
| 389 | |
| 390 | http.force_exception_to_status_code = True |
| 391 | |
| 392 | buildset = 'patch/rietveld/{hostname}/{issue}/{patch}'.format( |
| 393 | hostname=rietveld_host, |
| 394 | issue=changelist.GetIssue(), |
| 395 | patch=options.patchset) |
| 396 | params = {'tag': 'buildset:%s' % buildset} |
| 397 | |
| 398 | builds = {} |
| 399 | while True: |
| 400 | url = 'https://{hostname}/_ah/api/buildbucket/v1/search?{params}'.format( |
| 401 | hostname=options.buildbucket_host, |
| 402 | params=urllib.urlencode(params)) |
| 403 | content = _buildbucket_retry('fetching tryjobs', http, url, 'GET') |
| 404 | for build in content.get('builds', []): |
| 405 | builds[build['id']] = build |
| 406 | if 'next_cursor' in content: |
| 407 | params['start_cursor'] = content['next_cursor'] |
| 408 | else: |
| 409 | break |
| 410 | return builds |
| 411 | |
| 412 | |
| 413 | def print_tryjobs(options, builds): |
| 414 | """Prints nicely result of fetch_try_jobs.""" |
| 415 | if not builds: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 416 | print('No tryjobs scheduled') |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 417 | return |
| 418 | |
| 419 | # Make a copy, because we'll be modifying builds dictionary. |
| 420 | builds = builds.copy() |
| 421 | builder_names_cache = {} |
| 422 | |
| 423 | def get_builder(b): |
| 424 | try: |
| 425 | return builder_names_cache[b['id']] |
| 426 | except KeyError: |
| 427 | try: |
| 428 | parameters = json.loads(b['parameters_json']) |
| 429 | name = parameters['builder_name'] |
| 430 | except (ValueError, KeyError) as error: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 431 | print('WARNING: failed to get builder name for build %s: %s' % ( |
| 432 | b['id'], error)) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 433 | name = None |
| 434 | builder_names_cache[b['id']] = name |
| 435 | return name |
| 436 | |
| 437 | def get_bucket(b): |
| 438 | bucket = b['bucket'] |
| 439 | if bucket.startswith('master.'): |
| 440 | return bucket[len('master.'):] |
| 441 | return bucket |
| 442 | |
| 443 | if options.print_master: |
| 444 | name_fmt = '%%-%ds %%-%ds' % ( |
| 445 | max(len(str(get_bucket(b))) for b in builds.itervalues()), |
| 446 | max(len(str(get_builder(b))) for b in builds.itervalues())) |
| 447 | def get_name(b): |
| 448 | return name_fmt % (get_bucket(b), get_builder(b)) |
| 449 | else: |
| 450 | name_fmt = '%%-%ds' % ( |
| 451 | max(len(str(get_builder(b))) for b in builds.itervalues())) |
| 452 | def get_name(b): |
| 453 | return name_fmt % get_builder(b) |
| 454 | |
| 455 | def sort_key(b): |
| 456 | return b['status'], b.get('result'), get_name(b), b.get('url') |
| 457 | |
| 458 | def pop(title, f, color=None, **kwargs): |
| 459 | """Pop matching builds from `builds` dict and print them.""" |
| 460 | |
tandrii@chromium.org | 6cf98c8 | 2016-03-15 11:56:00 +0000 | [diff] [blame] | 461 | if not options.color or color is None: |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 462 | colorize = str |
| 463 | else: |
| 464 | colorize = lambda x: '%s%s%s' % (color, x, Fore.RESET) |
| 465 | |
| 466 | result = [] |
| 467 | for b in builds.values(): |
| 468 | if all(b.get(k) == v for k, v in kwargs.iteritems()): |
| 469 | builds.pop(b['id']) |
| 470 | result.append(b) |
| 471 | if result: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 472 | print(colorize(title)) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 473 | for b in sorted(result, key=sort_key): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 474 | print(' ', colorize('\t'.join(map(str, f(b))))) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 475 | |
| 476 | total = len(builds) |
| 477 | pop(status='COMPLETED', result='SUCCESS', |
| 478 | title='Successes:', color=Fore.GREEN, |
| 479 | f=lambda b: (get_name(b), b.get('url'))) |
| 480 | pop(status='COMPLETED', result='FAILURE', failure_reason='INFRA_FAILURE', |
| 481 | title='Infra Failures:', color=Fore.MAGENTA, |
| 482 | f=lambda b: (get_name(b), b.get('url'))) |
| 483 | pop(status='COMPLETED', result='FAILURE', failure_reason='BUILD_FAILURE', |
| 484 | title='Failures:', color=Fore.RED, |
| 485 | f=lambda b: (get_name(b), b.get('url'))) |
| 486 | pop(status='COMPLETED', result='CANCELED', |
| 487 | title='Canceled:', color=Fore.MAGENTA, |
| 488 | f=lambda b: (get_name(b),)) |
| 489 | pop(status='COMPLETED', result='FAILURE', |
| 490 | failure_reason='INVALID_BUILD_DEFINITION', |
| 491 | title='Wrong master/builder name:', color=Fore.MAGENTA, |
| 492 | f=lambda b: (get_name(b),)) |
| 493 | pop(status='COMPLETED', result='FAILURE', |
| 494 | title='Other failures:', |
| 495 | f=lambda b: (get_name(b), b.get('failure_reason'), b.get('url'))) |
| 496 | pop(status='COMPLETED', |
| 497 | title='Other finished:', |
| 498 | f=lambda b: (get_name(b), b.get('result'), b.get('url'))) |
| 499 | pop(status='STARTED', |
| 500 | title='Started:', color=Fore.YELLOW, |
| 501 | f=lambda b: (get_name(b), b.get('url'))) |
| 502 | pop(status='SCHEDULED', |
| 503 | title='Scheduled:', |
| 504 | f=lambda b: (get_name(b), 'id=%s' % b['id'])) |
| 505 | # The last section is just in case buildbucket API changes OR there is a bug. |
| 506 | pop(title='Other:', |
| 507 | f=lambda b: (get_name(b), 'id=%s' % b['id'])) |
| 508 | assert len(builds) == 0 |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 509 | print('Total: %d tryjobs' % total) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 510 | |
| 511 | |
bauerb@chromium.org | 866276c | 2011-03-18 20:09:31 +0000 | [diff] [blame] | 512 | def MatchSvnGlob(url, base_url, glob_spec, allow_wildcards): |
| 513 | """Return the corresponding git ref if |base_url| together with |glob_spec| |
| 514 | matches the full |url|. |
| 515 | |
| 516 | If |allow_wildcards| is true, |glob_spec| can contain wildcards (see below). |
| 517 | """ |
| 518 | fetch_suburl, as_ref = glob_spec.split(':') |
| 519 | if allow_wildcards: |
| 520 | glob_match = re.match('(.+/)?(\*|{[^/]*})(/.+)?', fetch_suburl) |
| 521 | if glob_match: |
| 522 | # Parse specs like "branches/*/src:refs/remotes/svn/*" or |
| 523 | # "branches/{472,597,648}/src:refs/remotes/svn/*". |
| 524 | branch_re = re.escape(base_url) |
| 525 | if glob_match.group(1): |
| 526 | branch_re += '/' + re.escape(glob_match.group(1)) |
| 527 | wildcard = glob_match.group(2) |
| 528 | if wildcard == '*': |
| 529 | branch_re += '([^/]*)' |
| 530 | else: |
| 531 | # Escape and replace surrounding braces with parentheses and commas |
| 532 | # with pipe symbols. |
| 533 | wildcard = re.escape(wildcard) |
| 534 | wildcard = re.sub('^\\\\{', '(', wildcard) |
| 535 | wildcard = re.sub('\\\\,', '|', wildcard) |
| 536 | wildcard = re.sub('\\\\}$', ')', wildcard) |
| 537 | branch_re += wildcard |
| 538 | if glob_match.group(3): |
| 539 | branch_re += re.escape(glob_match.group(3)) |
| 540 | match = re.match(branch_re, url) |
| 541 | if match: |
| 542 | return re.sub('\*$', match.group(1), as_ref) |
| 543 | |
| 544 | # Parse specs like "trunk/src:refs/remotes/origin/trunk". |
| 545 | if fetch_suburl: |
| 546 | full_url = base_url + '/' + fetch_suburl |
| 547 | else: |
| 548 | full_url = base_url |
| 549 | if full_url == url: |
| 550 | return as_ref |
| 551 | return None |
| 552 | |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 553 | |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 554 | def print_stats(similarity, find_copies, args): |
maruel@chromium.org | 49e3d80 | 2012-07-18 23:54:45 +0000 | [diff] [blame] | 555 | """Prints statistics about the change to the user.""" |
| 556 | # --no-ext-diff is broken in some versions of Git, so try to work around |
| 557 | # this by overriding the environment (but there is still a problem if the |
| 558 | # git config key "diff.external" is used). |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 559 | env = GetNoGitPagerEnv() |
maruel@chromium.org | 49e3d80 | 2012-07-18 23:54:45 +0000 | [diff] [blame] | 560 | if 'GIT_EXTERNAL_DIFF' in env: |
| 561 | del env['GIT_EXTERNAL_DIFF'] |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 562 | |
| 563 | if find_copies: |
| 564 | similarity_options = ['--find-copies-harder', '-l100000', |
| 565 | '-C%s' % similarity] |
| 566 | else: |
| 567 | similarity_options = ['-M%s' % similarity] |
| 568 | |
szager@chromium.org | d057f9a | 2014-05-29 21:09:36 +0000 | [diff] [blame] | 569 | try: |
| 570 | stdout = sys.stdout.fileno() |
| 571 | except AttributeError: |
| 572 | stdout = None |
maruel@chromium.org | 49e3d80 | 2012-07-18 23:54:45 +0000 | [diff] [blame] | 573 | return subprocess2.call( |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 574 | ['git', |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 575 | 'diff', '--no-ext-diff', '--stat'] + similarity_options + args, |
szager@chromium.org | d057f9a | 2014-05-29 21:09:36 +0000 | [diff] [blame] | 576 | stdout=stdout, env=env) |
maruel@chromium.org | 49e3d80 | 2012-07-18 23:54:45 +0000 | [diff] [blame] | 577 | |
| 578 | |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 579 | class BuildbucketResponseException(Exception): |
| 580 | pass |
| 581 | |
| 582 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 583 | class Settings(object): |
| 584 | def __init__(self): |
| 585 | self.default_server = None |
| 586 | self.cc = None |
thestig@chromium.org | 7a54e81 | 2014-02-11 19:57:22 +0000 | [diff] [blame] | 587 | self.root = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 588 | self.is_git_svn = None |
| 589 | self.svn_branch = None |
| 590 | self.tree_status_url = None |
| 591 | self.viewvc_url = None |
| 592 | self.updated = False |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 593 | self.is_gerrit = None |
bauerb@chromium.org | 54b400c | 2016-01-14 10:08:25 +0000 | [diff] [blame] | 594 | self.squash_gerrit_uploads = None |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 595 | self.gerrit_skip_ensure_authenticated = None |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 596 | self.git_editor = None |
sheyang@chromium.org | 152cf83 | 2014-06-11 21:37:49 +0000 | [diff] [blame] | 597 | self.project = None |
kjellander@chromium.org | 6abc652 | 2014-12-02 07:34:49 +0000 | [diff] [blame] | 598 | self.force_https_commit_url = None |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 599 | self.pending_ref_prefix = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 600 | |
| 601 | def LazyUpdateIfNeeded(self): |
| 602 | """Updates the settings from a codereview.settings file, if available.""" |
| 603 | if not self.updated: |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 604 | # The only value that actually changes the behavior is |
| 605 | # autoupdate = "false". Everything else means "true". |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 606 | autoupdate = RunGit(['config', 'rietveld.autoupdate'], |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 607 | error_ok=True |
| 608 | ).strip().lower() |
| 609 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 610 | cr_settings_file = FindCodereviewSettingsFile() |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 611 | if autoupdate != 'false' and cr_settings_file: |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 612 | LoadCodereviewSettingsFromFile(cr_settings_file) |
| 613 | self.updated = True |
| 614 | |
| 615 | def GetDefaultServerUrl(self, error_ok=False): |
| 616 | if not self.default_server: |
| 617 | self.LazyUpdateIfNeeded() |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 618 | self.default_server = gclient_utils.UpgradeToHttps( |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 619 | self._GetRietveldConfig('server', error_ok=True)) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 620 | if error_ok: |
| 621 | return self.default_server |
| 622 | if not self.default_server: |
| 623 | error_message = ('Could not find settings file. You must configure ' |
| 624 | 'your review setup by running "git cl config".') |
maruel@chromium.org | eb5edbc | 2012-01-16 17:03:28 +0000 | [diff] [blame] | 625 | self.default_server = gclient_utils.UpgradeToHttps( |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 626 | self._GetRietveldConfig('server', error_message=error_message)) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 627 | return self.default_server |
| 628 | |
thestig@chromium.org | 7a54e81 | 2014-02-11 19:57:22 +0000 | [diff] [blame] | 629 | @staticmethod |
| 630 | def GetRelativeRoot(): |
| 631 | return RunGit(['rev-parse', '--show-cdup']).strip() |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 632 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 633 | def GetRoot(self): |
thestig@chromium.org | 7a54e81 | 2014-02-11 19:57:22 +0000 | [diff] [blame] | 634 | if self.root is None: |
| 635 | self.root = os.path.abspath(self.GetRelativeRoot()) |
| 636 | return self.root |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 637 | |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 638 | def GetGitMirror(self, remote='origin'): |
| 639 | """If this checkout is from a local git mirror, return a Mirror object.""" |
szager@chromium.org | 8159374 | 2016-03-09 20:27:58 +0000 | [diff] [blame] | 640 | local_url = RunGit(['config', '--get', 'remote.%s.url' % remote]).strip() |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 641 | if not os.path.isdir(local_url): |
| 642 | return None |
| 643 | git_cache.Mirror.SetCachePath(os.path.dirname(local_url)) |
| 644 | remote_url = git_cache.Mirror.CacheDirToUrl(local_url) |
| 645 | # Use the /dev/null print_func to avoid terminal spew in WaitForRealCommit. |
| 646 | mirror = git_cache.Mirror(remote_url, print_func = lambda *args: None) |
| 647 | if mirror.exists(): |
| 648 | return mirror |
| 649 | return None |
| 650 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 651 | def GetIsGitSvn(self): |
| 652 | """Return true if this repo looks like it's using git-svn.""" |
| 653 | if self.is_git_svn is None: |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 654 | if self.GetPendingRefPrefix(): |
| 655 | # If PENDING_REF_PREFIX is set then it's a pure git repo no matter what. |
| 656 | self.is_git_svn = False |
| 657 | else: |
| 658 | # If you have any "svn-remote.*" config keys, we think you're using svn. |
| 659 | self.is_git_svn = RunGitWithCode( |
| 660 | ['config', '--local', '--get-regexp', r'^svn-remote\.'])[0] == 0 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 661 | return self.is_git_svn |
| 662 | |
| 663 | def GetSVNBranch(self): |
| 664 | if self.svn_branch is None: |
| 665 | if not self.GetIsGitSvn(): |
| 666 | DieWithError('Repo doesn\'t appear to be a git-svn repo.') |
| 667 | |
| 668 | # Try to figure out which remote branch we're based on. |
| 669 | # Strategy: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 670 | # 1) iterate through our branch history and find the svn URL. |
| 671 | # 2) find the svn-remote that fetches from the URL. |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 672 | |
| 673 | # regexp matching the git-svn line that contains the URL. |
| 674 | git_svn_re = re.compile(r'^\s*git-svn-id: (\S+)@', re.MULTILINE) |
| 675 | |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 676 | # We don't want to go through all of history, so read a line from the |
| 677 | # pipe at a time. |
| 678 | # The -100 is an arbitrary limit so we don't search forever. |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 679 | cmd = ['git', 'log', '-100', '--pretty=medium'] |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 680 | proc = subprocess2.Popen(cmd, stdout=subprocess2.PIPE, |
| 681 | env=GetNoGitPagerEnv()) |
maruel@chromium.org | 740f9d7 | 2011-06-10 18:33:10 +0000 | [diff] [blame] | 682 | url = None |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 683 | for line in proc.stdout: |
| 684 | match = git_svn_re.match(line) |
| 685 | if match: |
| 686 | url = match.group(1) |
| 687 | proc.stdout.close() # Cut pipe. |
| 688 | break |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 689 | |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 690 | if url: |
| 691 | svn_remote_re = re.compile(r'^svn-remote\.([^.]+)\.url (.*)$') |
| 692 | remotes = RunGit(['config', '--get-regexp', |
| 693 | r'^svn-remote\..*\.url']).splitlines() |
| 694 | for remote in remotes: |
| 695 | match = svn_remote_re.match(remote) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 696 | if match: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 697 | remote = match.group(1) |
| 698 | base_url = match.group(2) |
szager@chromium.org | 4ac2553 | 2013-12-16 22:07:02 +0000 | [diff] [blame] | 699 | rewrite_root = RunGit( |
| 700 | ['config', 'svn-remote.%s.rewriteRoot' % remote], |
| 701 | error_ok=True).strip() |
| 702 | if rewrite_root: |
| 703 | base_url = rewrite_root |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 704 | fetch_spec = RunGit( |
bauerb@chromium.org | 866276c | 2011-03-18 20:09:31 +0000 | [diff] [blame] | 705 | ['config', 'svn-remote.%s.fetch' % remote], |
| 706 | error_ok=True).strip() |
| 707 | if fetch_spec: |
| 708 | self.svn_branch = MatchSvnGlob(url, base_url, fetch_spec, False) |
| 709 | if self.svn_branch: |
| 710 | break |
| 711 | branch_spec = RunGit( |
| 712 | ['config', 'svn-remote.%s.branches' % remote], |
| 713 | error_ok=True).strip() |
| 714 | if branch_spec: |
| 715 | self.svn_branch = MatchSvnGlob(url, base_url, branch_spec, True) |
| 716 | if self.svn_branch: |
| 717 | break |
| 718 | tag_spec = RunGit( |
| 719 | ['config', 'svn-remote.%s.tags' % remote], |
| 720 | error_ok=True).strip() |
| 721 | if tag_spec: |
| 722 | self.svn_branch = MatchSvnGlob(url, base_url, tag_spec, True) |
| 723 | if self.svn_branch: |
| 724 | break |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 725 | |
| 726 | if not self.svn_branch: |
| 727 | DieWithError('Can\'t guess svn branch -- try specifying it on the ' |
| 728 | 'command line') |
| 729 | |
| 730 | return self.svn_branch |
| 731 | |
| 732 | def GetTreeStatusUrl(self, error_ok=False): |
| 733 | if not self.tree_status_url: |
| 734 | error_message = ('You must configure your tree status URL by running ' |
| 735 | '"git cl config".') |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 736 | self.tree_status_url = self._GetRietveldConfig( |
| 737 | 'tree-status-url', error_ok=error_ok, error_message=error_message) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 738 | return self.tree_status_url |
| 739 | |
| 740 | def GetViewVCUrl(self): |
| 741 | if not self.viewvc_url: |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 742 | self.viewvc_url = self._GetRietveldConfig('viewvc-url', error_ok=True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 743 | return self.viewvc_url |
| 744 | |
rmistry@google.com | 9075258 | 2014-01-14 21:04:50 +0000 | [diff] [blame] | 745 | def GetBugPrefix(self): |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 746 | return self._GetRietveldConfig('bug-prefix', error_ok=True) |
rmistry@google.com | 9075258 | 2014-01-14 21:04:50 +0000 | [diff] [blame] | 747 | |
rmistry@google.com | 78948ed | 2015-07-08 23:09:57 +0000 | [diff] [blame] | 748 | def GetIsSkipDependencyUpload(self, branch_name): |
| 749 | """Returns true if specified branch should skip dep uploads.""" |
| 750 | return self._GetBranchConfig(branch_name, 'skip-deps-uploads', |
| 751 | error_ok=True) |
| 752 | |
rmistry@google.com | 5626a92 | 2015-02-26 14:03:30 +0000 | [diff] [blame] | 753 | def GetRunPostUploadHook(self): |
| 754 | run_post_upload_hook = self._GetRietveldConfig( |
| 755 | 'run-post-upload-hook', error_ok=True) |
| 756 | return run_post_upload_hook == "True" |
| 757 | |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 758 | def GetDefaultCCList(self): |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 759 | return self._GetRietveldConfig('cc', error_ok=True) |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 760 | |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 761 | def GetDefaultPrivateFlag(self): |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 762 | return self._GetRietveldConfig('private', error_ok=True) |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 763 | |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 764 | def GetIsGerrit(self): |
| 765 | """Return true if this repo is assosiated with gerrit code review system.""" |
| 766 | if self.is_gerrit is None: |
| 767 | self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True) |
| 768 | return self.is_gerrit |
| 769 | |
bauerb@chromium.org | 54b400c | 2016-01-14 10:08:25 +0000 | [diff] [blame] | 770 | def GetSquashGerritUploads(self): |
| 771 | """Return true if uploads to Gerrit should be squashed by default.""" |
| 772 | if self.squash_gerrit_uploads is None: |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 773 | self.squash_gerrit_uploads = self.GetSquashGerritUploadsOverride() |
| 774 | if self.squash_gerrit_uploads is None: |
| 775 | # Default is squash now (http://crbug.com/611892#c23). |
| 776 | self.squash_gerrit_uploads = not ( |
| 777 | RunGit(['config', '--bool', 'gerrit.squash-uploads'], |
| 778 | error_ok=True).strip() == 'false') |
bauerb@chromium.org | 54b400c | 2016-01-14 10:08:25 +0000 | [diff] [blame] | 779 | return self.squash_gerrit_uploads |
| 780 | |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 781 | def GetSquashGerritUploadsOverride(self): |
| 782 | """Return True or False if codereview.settings should be overridden. |
| 783 | |
| 784 | Returns None if no override has been defined. |
| 785 | """ |
| 786 | # See also http://crbug.com/611892#c23 |
| 787 | result = RunGit(['config', '--bool', 'gerrit.override-squash-uploads'], |
| 788 | error_ok=True).strip() |
| 789 | if result == 'true': |
| 790 | return True |
| 791 | if result == 'false': |
| 792 | return False |
| 793 | return None |
| 794 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 795 | def GetGerritSkipEnsureAuthenticated(self): |
| 796 | """Return True if EnsureAuthenticated should not be done for Gerrit |
| 797 | uploads.""" |
| 798 | if self.gerrit_skip_ensure_authenticated is None: |
| 799 | self.gerrit_skip_ensure_authenticated = ( |
shinyak@chromium.org | 00dbccd | 2016-04-15 07:24:43 +0000 | [diff] [blame] | 800 | RunGit(['config', '--bool', 'gerrit.skip-ensure-authenticated'], |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 801 | error_ok=True).strip() == 'true') |
| 802 | return self.gerrit_skip_ensure_authenticated |
| 803 | |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 804 | def GetGitEditor(self): |
| 805 | """Return the editor specified in the git config, or None if none is.""" |
| 806 | if self.git_editor is None: |
| 807 | self.git_editor = self._GetConfig('core.editor', error_ok=True) |
| 808 | return self.git_editor or None |
| 809 | |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 810 | def GetLintRegex(self): |
| 811 | return (self._GetRietveldConfig('cpplint-regex', error_ok=True) or |
| 812 | DEFAULT_LINT_REGEX) |
| 813 | |
| 814 | def GetLintIgnoreRegex(self): |
| 815 | return (self._GetRietveldConfig('cpplint-ignore-regex', error_ok=True) or |
| 816 | DEFAULT_LINT_IGNORE_REGEX) |
| 817 | |
sheyang@chromium.org | 152cf83 | 2014-06-11 21:37:49 +0000 | [diff] [blame] | 818 | def GetProject(self): |
| 819 | if not self.project: |
| 820 | self.project = self._GetRietveldConfig('project', error_ok=True) |
| 821 | return self.project |
| 822 | |
kjellander@chromium.org | 6abc652 | 2014-12-02 07:34:49 +0000 | [diff] [blame] | 823 | def GetForceHttpsCommitUrl(self): |
| 824 | if not self.force_https_commit_url: |
| 825 | self.force_https_commit_url = self._GetRietveldConfig( |
| 826 | 'force-https-commit-url', error_ok=True) |
| 827 | return self.force_https_commit_url |
| 828 | |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 829 | def GetPendingRefPrefix(self): |
| 830 | if not self.pending_ref_prefix: |
| 831 | self.pending_ref_prefix = self._GetRietveldConfig( |
| 832 | 'pending-ref-prefix', error_ok=True) |
| 833 | return self.pending_ref_prefix |
| 834 | |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 835 | def _GetRietveldConfig(self, param, **kwargs): |
| 836 | return self._GetConfig('rietveld.' + param, **kwargs) |
| 837 | |
rmistry@google.com | 78948ed | 2015-07-08 23:09:57 +0000 | [diff] [blame] | 838 | def _GetBranchConfig(self, branch_name, param, **kwargs): |
| 839 | return self._GetConfig('branch.' + branch_name + '.' + param, **kwargs) |
| 840 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 841 | def _GetConfig(self, param, **kwargs): |
| 842 | self.LazyUpdateIfNeeded() |
| 843 | return RunGit(['config', param], **kwargs).strip() |
| 844 | |
| 845 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 846 | def ShortBranchName(branch): |
| 847 | """Convert a name like 'refs/heads/foo' to just 'foo'.""" |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 848 | return branch.replace('refs/heads/', '', 1) |
| 849 | |
| 850 | |
| 851 | def GetCurrentBranchRef(): |
| 852 | """Returns branch ref (e.g., refs/heads/master) or None.""" |
| 853 | return RunGit(['symbolic-ref', 'HEAD'], |
| 854 | stderr=subprocess2.VOID, error_ok=True).strip() or None |
| 855 | |
| 856 | |
| 857 | def GetCurrentBranch(): |
| 858 | """Returns current branch or None. |
| 859 | |
| 860 | For refs/heads/* branches, returns just last part. For others, full ref. |
| 861 | """ |
| 862 | branchref = GetCurrentBranchRef() |
| 863 | if branchref: |
| 864 | return ShortBranchName(branchref) |
| 865 | return None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 866 | |
| 867 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 868 | class _CQState(object): |
| 869 | """Enum for states of CL with respect to Commit Queue.""" |
| 870 | NONE = 'none' |
| 871 | DRY_RUN = 'dry_run' |
| 872 | COMMIT = 'commit' |
| 873 | |
| 874 | ALL_STATES = [NONE, DRY_RUN, COMMIT] |
| 875 | |
| 876 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 877 | class _ParsedIssueNumberArgument(object): |
| 878 | def __init__(self, issue=None, patchset=None, hostname=None): |
| 879 | self.issue = issue |
| 880 | self.patchset = patchset |
| 881 | self.hostname = hostname |
| 882 | |
| 883 | @property |
| 884 | def valid(self): |
| 885 | return self.issue is not None |
| 886 | |
| 887 | |
| 888 | class _RietveldParsedIssueNumberArgument(_ParsedIssueNumberArgument): |
| 889 | def __init__(self, *args, **kwargs): |
| 890 | self.patch_url = kwargs.pop('patch_url', None) |
| 891 | super(_RietveldParsedIssueNumberArgument, self).__init__(*args, **kwargs) |
| 892 | |
| 893 | |
| 894 | def ParseIssueNumberArgument(arg): |
| 895 | """Parses the issue argument and returns _ParsedIssueNumberArgument.""" |
| 896 | fail_result = _ParsedIssueNumberArgument() |
| 897 | |
| 898 | if arg.isdigit(): |
| 899 | return _ParsedIssueNumberArgument(issue=int(arg)) |
| 900 | if not arg.startswith('http'): |
| 901 | return fail_result |
| 902 | url = gclient_utils.UpgradeToHttps(arg) |
| 903 | try: |
| 904 | parsed_url = urlparse.urlparse(url) |
| 905 | except ValueError: |
| 906 | return fail_result |
| 907 | for cls in _CODEREVIEW_IMPLEMENTATIONS.itervalues(): |
| 908 | tmp = cls.ParseIssueURL(parsed_url) |
| 909 | if tmp is not None: |
| 910 | return tmp |
| 911 | return fail_result |
| 912 | |
| 913 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 914 | class Changelist(object): |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 915 | """Changelist works with one changelist in local branch. |
| 916 | |
| 917 | Supports two codereview backends: Rietveld or Gerrit, selected at object |
| 918 | creation. |
| 919 | |
tandrii@chromium.org | 8930b3d | 2016-04-13 14:47:02 +0000 | [diff] [blame] | 920 | Notes: |
| 921 | * Not safe for concurrent multi-{thread,process} use. |
| 922 | * Caches values from current branch. Therefore, re-use after branch change |
| 923 | with care. |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 924 | """ |
| 925 | |
| 926 | def __init__(self, branchref=None, issue=None, codereview=None, **kwargs): |
| 927 | """Create a new ChangeList instance. |
| 928 | |
| 929 | If issue is given, the codereview must be given too. |
| 930 | |
| 931 | If `codereview` is given, it must be 'rietveld' or 'gerrit'. |
| 932 | Otherwise, it's decided based on current configuration of the local branch, |
| 933 | with default being 'rietveld' for backwards compatibility. |
| 934 | See _load_codereview_impl for more details. |
| 935 | |
| 936 | **kwargs will be passed directly to codereview implementation. |
| 937 | """ |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 938 | # Poke settings so we get the "configure your server" message if necessary. |
maruel@chromium.org | 379d07a | 2011-11-30 14:58:10 +0000 | [diff] [blame] | 939 | global settings |
| 940 | if not settings: |
| 941 | # Happens when git_cl.py is used as a utility library. |
| 942 | settings = Settings() |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 943 | |
| 944 | if issue: |
| 945 | assert codereview, 'codereview must be known, if issue is known' |
| 946 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 947 | self.branchref = branchref |
| 948 | if self.branchref: |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 949 | assert branchref.startswith('refs/heads/') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 950 | self.branch = ShortBranchName(self.branchref) |
| 951 | else: |
| 952 | self.branch = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 953 | self.upstream_branch = None |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 954 | self.lookedup_issue = False |
| 955 | self.issue = issue or None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 956 | self.has_description = False |
| 957 | self.description = None |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 958 | self.lookedup_patchset = False |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 959 | self.patchset = None |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 960 | self.cc = None |
| 961 | self.watchers = () |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 962 | self._remote = None |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 963 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 964 | self._codereview_impl = None |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 965 | self._codereview = None |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 966 | self._load_codereview_impl(codereview, **kwargs) |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 967 | assert self._codereview_impl |
| 968 | assert self._codereview in _CODEREVIEW_IMPLEMENTATIONS |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 969 | |
| 970 | def _load_codereview_impl(self, codereview=None, **kwargs): |
| 971 | if codereview: |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 972 | assert codereview in _CODEREVIEW_IMPLEMENTATIONS |
| 973 | cls = _CODEREVIEW_IMPLEMENTATIONS[codereview] |
| 974 | self._codereview = codereview |
| 975 | self._codereview_impl = cls(self, **kwargs) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 976 | return |
| 977 | |
| 978 | # Automatic selection based on issue number set for a current branch. |
| 979 | # Rietveld takes precedence over Gerrit. |
| 980 | assert not self.issue |
| 981 | # Whether we find issue or not, we are doing the lookup. |
| 982 | self.lookedup_issue = True |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 983 | for codereview, cls in _CODEREVIEW_IMPLEMENTATIONS.iteritems(): |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 984 | setting = cls.IssueSetting(self.GetBranch()) |
| 985 | issue = RunGit(['config', setting], error_ok=True).strip() |
| 986 | if issue: |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 987 | self._codereview = codereview |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 988 | self._codereview_impl = cls(self, **kwargs) |
| 989 | self.issue = int(issue) |
| 990 | return |
| 991 | |
| 992 | # No issue is set for this branch, so decide based on repo-wide settings. |
| 993 | return self._load_codereview_impl( |
| 994 | codereview='gerrit' if settings.GetIsGerrit() else 'rietveld', |
| 995 | **kwargs) |
| 996 | |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 997 | def IsGerrit(self): |
| 998 | return self._codereview == 'gerrit' |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 999 | |
| 1000 | def GetCCList(self): |
| 1001 | """Return the users cc'd on this CL. |
| 1002 | |
| 1003 | Return is a string suitable for passing to gcl with the --cc flag. |
| 1004 | """ |
| 1005 | if self.cc is None: |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 1006 | base_cc = settings.GetDefaultCCList() |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 1007 | more_cc = ','.join(self.watchers) |
| 1008 | self.cc = ','.join(filter(None, (base_cc, more_cc))) or '' |
| 1009 | return self.cc |
| 1010 | |
tyoshino@chromium.org | 99918ab | 2013-09-30 06:17:28 +0000 | [diff] [blame] | 1011 | def GetCCListWithoutDefault(self): |
| 1012 | """Return the users cc'd on this CL excluding default ones.""" |
| 1013 | if self.cc is None: |
| 1014 | self.cc = ','.join(self.watchers) |
| 1015 | return self.cc |
| 1016 | |
bauerb@chromium.org | ae6df35 | 2011-04-06 17:40:39 +0000 | [diff] [blame] | 1017 | def SetWatchers(self, watchers): |
| 1018 | """Set the list of email addresses that should be cc'd based on the changed |
| 1019 | files in this CL. |
| 1020 | """ |
| 1021 | self.watchers = watchers |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1022 | |
| 1023 | def GetBranch(self): |
| 1024 | """Returns the short branch name, e.g. 'master'.""" |
| 1025 | if not self.branch: |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1026 | branchref = GetCurrentBranchRef() |
szager@chromium.org | d62c61f | 2014-10-20 22:33:21 +0000 | [diff] [blame] | 1027 | if not branchref: |
| 1028 | return None |
| 1029 | self.branchref = branchref |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1030 | self.branch = ShortBranchName(self.branchref) |
| 1031 | return self.branch |
| 1032 | |
| 1033 | def GetBranchRef(self): |
| 1034 | """Returns the full branch name, e.g. 'refs/heads/master'.""" |
| 1035 | self.GetBranch() # Poke the lazy loader. |
| 1036 | return self.branchref |
| 1037 | |
tandrii@chromium.org | 534f67a | 2016-04-07 18:47:05 +0000 | [diff] [blame] | 1038 | def ClearBranch(self): |
| 1039 | """Clears cached branch data of this object.""" |
| 1040 | self.branch = self.branchref = None |
| 1041 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1042 | @staticmethod |
| 1043 | def FetchUpstreamTuple(branch): |
pgervais@chromium.org | d6617f3 | 2013-11-19 00:34:54 +0000 | [diff] [blame] | 1044 | """Returns a tuple containing remote and remote ref, |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1045 | e.g. 'origin', 'refs/heads/master' |
| 1046 | """ |
| 1047 | remote = '.' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1048 | upstream_branch = RunGit(['config', 'branch.%s.merge' % branch], |
| 1049 | error_ok=True).strip() |
| 1050 | if upstream_branch: |
| 1051 | remote = RunGit(['config', 'branch.%s.remote' % branch]).strip() |
| 1052 | else: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 1053 | upstream_branch = RunGit(['config', 'rietveld.upstream-branch'], |
| 1054 | error_ok=True).strip() |
| 1055 | if upstream_branch: |
| 1056 | remote = RunGit(['config', 'rietveld.upstream-remote']).strip() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1057 | else: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 1058 | # Fall back on trying a git-svn upstream branch. |
| 1059 | if settings.GetIsGitSvn(): |
| 1060 | upstream_branch = settings.GetSVNBranch() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1061 | else: |
bauerb@chromium.org | ade368c | 2011-03-01 08:57:50 +0000 | [diff] [blame] | 1062 | # Else, try to guess the origin remote. |
| 1063 | remote_branches = RunGit(['branch', '-r']).split() |
| 1064 | if 'origin/master' in remote_branches: |
| 1065 | # Fall back on origin/master if it exits. |
| 1066 | remote = 'origin' |
| 1067 | upstream_branch = 'refs/heads/master' |
| 1068 | elif 'origin/trunk' in remote_branches: |
| 1069 | # Fall back on origin/trunk if it exists. Generally a shared |
| 1070 | # git-svn clone |
| 1071 | remote = 'origin' |
| 1072 | upstream_branch = 'refs/heads/trunk' |
| 1073 | else: |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1074 | DieWithError( |
| 1075 | 'Unable to determine default branch to diff against.\n' |
| 1076 | 'Either pass complete "git diff"-style arguments, like\n' |
| 1077 | ' git cl upload origin/master\n' |
| 1078 | 'or verify this branch is set up to track another \n' |
| 1079 | '(via the --track argument to "git checkout -b ...").') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1080 | |
| 1081 | return remote, upstream_branch |
| 1082 | |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 1083 | def GetCommonAncestorWithUpstream(self): |
pgervais@chromium.org | 8ba38ff | 2015-06-11 21:41:25 +0000 | [diff] [blame] | 1084 | upstream_branch = self.GetUpstreamBranch() |
| 1085 | if not BranchExists(upstream_branch): |
| 1086 | DieWithError('The upstream for the current branch (%s) does not exist ' |
| 1087 | 'anymore.\nPlease fix it and try again.' % self.GetBranch()) |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 1088 | return git_common.get_or_create_merge_base(self.GetBranch(), |
pgervais@chromium.org | 8ba38ff | 2015-06-11 21:41:25 +0000 | [diff] [blame] | 1089 | upstream_branch) |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 1090 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1091 | def GetUpstreamBranch(self): |
| 1092 | if self.upstream_branch is None: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1093 | remote, upstream_branch = self.FetchUpstreamTuple(self.GetBranch()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1094 | if remote is not '.': |
mmoss@chromium.org | e758545 | 2014-08-24 01:41:11 +0000 | [diff] [blame] | 1095 | upstream_branch = upstream_branch.replace('refs/heads/', |
| 1096 | 'refs/remotes/%s/' % remote) |
| 1097 | upstream_branch = upstream_branch.replace('refs/branch-heads/', |
| 1098 | 'refs/remotes/branch-heads/') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1099 | self.upstream_branch = upstream_branch |
| 1100 | return self.upstream_branch |
| 1101 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1102 | def GetRemoteBranch(self): |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 1103 | if not self._remote: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1104 | remote, branch = None, self.GetBranch() |
| 1105 | seen_branches = set() |
| 1106 | while branch not in seen_branches: |
| 1107 | seen_branches.add(branch) |
| 1108 | remote, branch = self.FetchUpstreamTuple(branch) |
| 1109 | branch = ShortBranchName(branch) |
| 1110 | if remote != '.' or branch.startswith('refs/remotes'): |
| 1111 | break |
| 1112 | else: |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 1113 | remotes = RunGit(['remote'], error_ok=True).split() |
| 1114 | if len(remotes) == 1: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1115 | remote, = remotes |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 1116 | elif 'origin' in remotes: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1117 | remote = 'origin' |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 1118 | logging.warning('Could not determine which remote this change is ' |
| 1119 | 'associated with, so defaulting to "%s". This may ' |
| 1120 | 'not be what you want. You may prevent this message ' |
| 1121 | 'by running "git svn info" as documented here: %s', |
| 1122 | self._remote, |
| 1123 | GIT_INSTRUCTIONS_URL) |
| 1124 | else: |
| 1125 | logging.warn('Could not determine which remote this change is ' |
| 1126 | 'associated with. You may prevent this message by ' |
| 1127 | 'running "git svn info" as documented here: %s', |
| 1128 | GIT_INSTRUCTIONS_URL) |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1129 | branch = 'HEAD' |
| 1130 | if branch.startswith('refs/remotes'): |
| 1131 | self._remote = (remote, branch) |
mmoss@chromium.org | e758545 | 2014-08-24 01:41:11 +0000 | [diff] [blame] | 1132 | elif branch.startswith('refs/branch-heads/'): |
| 1133 | self._remote = (remote, branch.replace('refs/', 'refs/remotes/')) |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1134 | else: |
| 1135 | self._remote = (remote, 'refs/remotes/%s/%s' % (remote, branch)) |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 1136 | return self._remote |
| 1137 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1138 | def GitSanityChecks(self, upstream_git_obj): |
| 1139 | """Checks git repo status and ensures diff is from local commits.""" |
| 1140 | |
sbc@chromium.org | 7970606 | 2015-01-14 21:18:12 +0000 | [diff] [blame] | 1141 | if upstream_git_obj is None: |
| 1142 | if self.GetBranch() is None: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1143 | print('ERROR: unable to determine current branch (detached HEAD?)', |
| 1144 | file=sys.stderr) |
sbc@chromium.org | 7970606 | 2015-01-14 21:18:12 +0000 | [diff] [blame] | 1145 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1146 | print('ERROR: no upstream branch', file=sys.stderr) |
sbc@chromium.org | 7970606 | 2015-01-14 21:18:12 +0000 | [diff] [blame] | 1147 | return False |
| 1148 | |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1149 | # Verify the commit we're diffing against is in our current branch. |
| 1150 | upstream_sha = RunGit(['rev-parse', '--verify', upstream_git_obj]).strip() |
| 1151 | common_ancestor = RunGit(['merge-base', upstream_sha, 'HEAD']).strip() |
| 1152 | if upstream_sha != common_ancestor: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1153 | print('ERROR: %s is not in the current branch. You may need to rebase ' |
| 1154 | 'your tracking branch' % upstream_sha, file=sys.stderr) |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1155 | return False |
| 1156 | |
| 1157 | # List the commits inside the diff, and verify they are all local. |
| 1158 | commits_in_diff = RunGit( |
| 1159 | ['rev-list', '^%s' % upstream_sha, 'HEAD']).splitlines() |
| 1160 | code, remote_branch = RunGitWithCode(['config', 'gitcl.remotebranch']) |
| 1161 | remote_branch = remote_branch.strip() |
| 1162 | if code != 0: |
| 1163 | _, remote_branch = self.GetRemoteBranch() |
| 1164 | |
| 1165 | commits_in_remote = RunGit( |
| 1166 | ['rev-list', '^%s' % upstream_sha, remote_branch]).splitlines() |
| 1167 | |
| 1168 | common_commits = set(commits_in_diff) & set(commits_in_remote) |
| 1169 | if common_commits: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1170 | print('ERROR: Your diff contains %d commits already in %s.\n' |
| 1171 | 'Run "git log --oneline %s..HEAD" to get a list of commits in ' |
| 1172 | 'the diff. If you are using a custom git flow, you can override' |
| 1173 | ' the reference used for this check with "git config ' |
| 1174 | 'gitcl.remotebranch <git-ref>".' % ( |
| 1175 | len(common_commits), remote_branch, upstream_git_obj), |
| 1176 | file=sys.stderr) |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1177 | return False |
| 1178 | return True |
| 1179 | |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 1180 | def GetGitBaseUrlFromConfig(self): |
sheyang@chromium.org | a656e70 | 2014-05-15 20:43:05 +0000 | [diff] [blame] | 1181 | """Return the configured base URL from branch.<branchname>.baseurl. |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 1182 | |
| 1183 | Returns None if it is not set. |
| 1184 | """ |
sheyang@chromium.org | a656e70 | 2014-05-15 20:43:05 +0000 | [diff] [blame] | 1185 | return RunGit(['config', 'branch.%s.base-url' % self.GetBranch()], |
| 1186 | error_ok=True).strip() |
jmbaker@chromium.org | a2cbbbb | 2012-03-22 20:40:40 +0000 | [diff] [blame] | 1187 | |
kjellander@chromium.org | 6abc652 | 2014-12-02 07:34:49 +0000 | [diff] [blame] | 1188 | def GetGitSvnRemoteUrl(self): |
| 1189 | """Return the configured git-svn remote URL parsed from git svn info. |
| 1190 | |
| 1191 | Returns None if it is not set. |
| 1192 | """ |
| 1193 | # URL is dependent on the current directory. |
| 1194 | data = RunGit(['svn', 'info'], cwd=settings.GetRoot()) |
| 1195 | if data: |
| 1196 | keys = dict(line.split(': ', 1) for line in data.splitlines() |
| 1197 | if ': ' in line) |
| 1198 | return keys.get('URL', None) |
| 1199 | return None |
| 1200 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1201 | def GetRemoteUrl(self): |
| 1202 | """Return the configured remote URL, e.g. 'git://example.org/foo.git/'. |
| 1203 | |
| 1204 | Returns None if there is no remote. |
| 1205 | """ |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1206 | remote, _ = self.GetRemoteBranch() |
dyen@chromium.org | 2a13d4f | 2014-06-13 00:06:37 +0000 | [diff] [blame] | 1207 | url = RunGit(['config', 'remote.%s.url' % remote], error_ok=True).strip() |
| 1208 | |
| 1209 | # If URL is pointing to a local directory, it is probably a git cache. |
| 1210 | if os.path.isdir(url): |
| 1211 | url = RunGit(['config', 'remote.%s.url' % remote], |
| 1212 | error_ok=True, |
| 1213 | cwd=url).strip() |
| 1214 | return url |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1215 | |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 1216 | def GetIssue(self): |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1217 | """Returns the issue number as a int or None if not set.""" |
tandrii@chromium.org | 87985d2 | 2016-03-24 17:33:33 +0000 | [diff] [blame] | 1218 | if self.issue is None and not self.lookedup_issue: |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1219 | issue = RunGit(['config', |
| 1220 | self._codereview_impl.IssueSetting(self.GetBranch())], |
| 1221 | error_ok=True).strip() |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 1222 | self.issue = int(issue) or None if issue else None |
| 1223 | self.lookedup_issue = True |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1224 | return self.issue |
| 1225 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1226 | def GetIssueURL(self): |
| 1227 | """Get the URL for a particular issue.""" |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1228 | issue = self.GetIssue() |
| 1229 | if not issue: |
dbeam@chromium.org | 015fd3d | 2013-06-18 19:02:50 +0000 | [diff] [blame] | 1230 | return None |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1231 | return '%s/%s' % (self._codereview_impl.GetCodereviewServer(), issue) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1232 | |
| 1233 | def GetDescription(self, pretty=False): |
| 1234 | if not self.has_description: |
| 1235 | if self.GetIssue(): |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1236 | self.description = self._codereview_impl.FetchDescription() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1237 | self.has_description = True |
| 1238 | if pretty: |
| 1239 | wrapper = textwrap.TextWrapper() |
| 1240 | wrapper.initial_indent = wrapper.subsequent_indent = ' ' |
| 1241 | return wrapper.fill(self.description) |
| 1242 | return self.description |
| 1243 | |
| 1244 | def GetPatchset(self): |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1245 | """Returns the patchset number as a int or None if not set.""" |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 1246 | if self.patchset is None and not self.lookedup_patchset: |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1247 | patchset = RunGit(['config', self._codereview_impl.PatchsetSetting()], |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1248 | error_ok=True).strip() |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 1249 | self.patchset = int(patchset) or None if patchset else None |
| 1250 | self.lookedup_patchset = True |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1251 | return self.patchset |
| 1252 | |
| 1253 | def SetPatchset(self, patchset): |
| 1254 | """Set this branch's patchset. If patchset=0, clears the patchset.""" |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1255 | patchset_setting = self._codereview_impl.PatchsetSetting() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1256 | if patchset: |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1257 | RunGit(['config', patchset_setting, str(patchset)]) |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 1258 | self.patchset = patchset |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1259 | else: |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1260 | RunGit(['config', '--unset', patchset_setting], |
maruel@chromium.org | 32f9f5e | 2011-09-14 13:41:47 +0000 | [diff] [blame] | 1261 | stderr=subprocess2.PIPE, error_ok=True) |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 1262 | self.patchset = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1263 | |
tandrii@chromium.org | a342c92 | 2016-03-16 07:08:25 +0000 | [diff] [blame] | 1264 | def SetIssue(self, issue=None): |
| 1265 | """Set this branch's issue. If issue isn't given, clears the issue.""" |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1266 | issue_setting = self._codereview_impl.IssueSetting(self.GetBranch()) |
| 1267 | codereview_setting = self._codereview_impl.GetCodereviewServerSetting() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1268 | if issue: |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 1269 | self.issue = issue |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1270 | RunGit(['config', issue_setting, str(issue)]) |
| 1271 | codereview_server = self._codereview_impl.GetCodereviewServer() |
| 1272 | if codereview_server: |
| 1273 | RunGit(['config', codereview_setting, codereview_server]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1274 | else: |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 1275 | # Reset it regardless. It doesn't hurt. |
| 1276 | config_settings = [issue_setting, self._codereview_impl.PatchsetSetting()] |
| 1277 | for prop in (['last-upload-hash'] + |
| 1278 | self._codereview_impl._PostUnsetIssueProperties()): |
| 1279 | config_settings.append('branch.%s.%s' % (self.GetBranch(), prop)) |
| 1280 | for setting in config_settings: |
| 1281 | RunGit(['config', '--unset', setting], error_ok=True) |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 1282 | self.issue = None |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 1283 | self.patchset = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1284 | |
asvitkine@chromium.org | 1516995 | 2011-09-27 14:30:53 +0000 | [diff] [blame] | 1285 | def GetChange(self, upstream_branch, author): |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 1286 | if not self.GitSanityChecks(upstream_branch): |
| 1287 | DieWithError('\nGit sanity check failure') |
| 1288 | |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 1289 | root = settings.GetRelativeRoot() |
bratell@opera.com | f267b0e | 2013-05-02 09:11:43 +0000 | [diff] [blame] | 1290 | if not root: |
| 1291 | root = '.' |
bauerb@chromium.org | 512f1ef | 2011-04-20 15:17:57 +0000 | [diff] [blame] | 1292 | absroot = os.path.abspath(root) |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 1293 | |
| 1294 | # We use the sha1 of HEAD as a name of this change. |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 1295 | name = RunGitWithCode(['rev-parse', 'HEAD'])[1].strip() |
bauerb@chromium.org | 512f1ef | 2011-04-20 15:17:57 +0000 | [diff] [blame] | 1296 | # Need to pass a relative path for msysgit. |
maruel@chromium.org | 2b38e9c | 2011-10-19 00:04:35 +0000 | [diff] [blame] | 1297 | try: |
maruel@chromium.org | 80a9ef1 | 2011-12-13 20:44:10 +0000 | [diff] [blame] | 1298 | files = scm.GIT.CaptureStatus([root], '.', upstream_branch) |
maruel@chromium.org | 2b38e9c | 2011-10-19 00:04:35 +0000 | [diff] [blame] | 1299 | except subprocess2.CalledProcessError: |
| 1300 | DieWithError( |
pgervais@chromium.org | d6617f3 | 2013-11-19 00:34:54 +0000 | [diff] [blame] | 1301 | ('\nFailed to diff against upstream branch %s\n\n' |
maruel@chromium.org | 2b38e9c | 2011-10-19 00:04:35 +0000 | [diff] [blame] | 1302 | 'This branch probably doesn\'t exist anymore. To reset the\n' |
| 1303 | 'tracking branch, please run\n' |
| 1304 | ' git branch --set-upstream %s trunk\n' |
| 1305 | 'replacing trunk with origin/master or the relevant branch') % |
| 1306 | (upstream_branch, self.GetBranch())) |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 1307 | |
maruel@chromium.org | 5242430 | 2012-08-29 15:14:30 +0000 | [diff] [blame] | 1308 | issue = self.GetIssue() |
| 1309 | patchset = self.GetPatchset() |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 1310 | if issue: |
| 1311 | description = self.GetDescription() |
| 1312 | else: |
| 1313 | # If the change was never uploaded, use the log messages of all commits |
| 1314 | # up to the branch point, as git cl upload will prefill the description |
| 1315 | # with these log messages. |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 1316 | args = ['log', '--pretty=format:%s%n%n%b', '%s...' % (upstream_branch)] |
| 1317 | description = RunGitWithCode(args)[1].strip() |
maruel@chromium.org | 03b3bdc | 2011-06-14 13:04:12 +0000 | [diff] [blame] | 1318 | |
| 1319 | if not author: |
maruel@chromium.org | 13f623c | 2011-07-22 16:02:23 +0000 | [diff] [blame] | 1320 | author = RunGit(['config', 'user.email']).strip() or None |
asvitkine@chromium.org | 1516995 | 2011-09-27 14:30:53 +0000 | [diff] [blame] | 1321 | return presubmit_support.GitChange( |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 1322 | name, |
| 1323 | description, |
| 1324 | absroot, |
| 1325 | files, |
| 1326 | issue, |
| 1327 | patchset, |
agable@chromium.org | ea84ef1 | 2014-04-30 19:55:12 +0000 | [diff] [blame] | 1328 | author, |
| 1329 | upstream=upstream_branch) |
bauerb@chromium.org | 6fb99c6 | 2011-04-18 15:57:28 +0000 | [diff] [blame] | 1330 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1331 | def UpdateDescription(self, description): |
| 1332 | self.description = description |
| 1333 | return self._codereview_impl.UpdateDescriptionRemote(description) |
| 1334 | |
| 1335 | def RunHook(self, committing, may_prompt, verbose, change): |
| 1336 | """Calls sys.exit() if the hook fails; returns a HookResults otherwise.""" |
| 1337 | try: |
| 1338 | return presubmit_support.DoPresubmitChecks(change, committing, |
| 1339 | verbose=verbose, output_stream=sys.stdout, input_stream=sys.stdin, |
| 1340 | default_presubmit=None, may_prompt=may_prompt, |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1341 | rietveld_obj=self._codereview_impl.GetRieveldObjForPresubmit(), |
| 1342 | gerrit_obj=self._codereview_impl.GetGerritObjForPresubmit()) |
vapier | fd77ac7 | 2016-06-16 08:33:57 -0700 | [diff] [blame] | 1343 | except presubmit_support.PresubmitFailure as e: |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1344 | DieWithError( |
| 1345 | ('%s\nMaybe your depot_tools is out of date?\n' |
| 1346 | 'If all fails, contact maruel@') % e) |
| 1347 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1348 | def CMDPatchIssue(self, issue_arg, reject, nocommit, directory): |
| 1349 | """Fetches and applies the issue patch from codereview to local branch.""" |
tandrii@chromium.org | ef7c68c | 2016-04-07 09:39:39 +0000 | [diff] [blame] | 1350 | if isinstance(issue_arg, (int, long)) or issue_arg.isdigit(): |
| 1351 | parsed_issue_arg = _ParsedIssueNumberArgument(int(issue_arg)) |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1352 | else: |
| 1353 | # Assume url. |
| 1354 | parsed_issue_arg = self._codereview_impl.ParseIssueURL( |
| 1355 | urlparse.urlparse(issue_arg)) |
| 1356 | if not parsed_issue_arg or not parsed_issue_arg.valid: |
| 1357 | DieWithError('Failed to parse issue argument "%s". ' |
| 1358 | 'Must be an issue number or a valid URL.' % issue_arg) |
| 1359 | return self._codereview_impl.CMDPatchWithParsedIssue( |
| 1360 | parsed_issue_arg, reject, nocommit, directory) |
| 1361 | |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 1362 | def CMDUpload(self, options, git_diff_args, orig_args): |
| 1363 | """Uploads a change to codereview.""" |
| 1364 | if git_diff_args: |
| 1365 | # TODO(ukai): is it ok for gerrit case? |
| 1366 | base_branch = git_diff_args[0] |
| 1367 | else: |
| 1368 | if self.GetBranch() is None: |
| 1369 | DieWithError('Can\'t upload from detached HEAD state. Get on a branch!') |
| 1370 | |
| 1371 | # Default to diffing against common ancestor of upstream branch |
| 1372 | base_branch = self.GetCommonAncestorWithUpstream() |
| 1373 | git_diff_args = [base_branch, 'HEAD'] |
| 1374 | |
| 1375 | # Make sure authenticated to codereview before running potentially expensive |
| 1376 | # hooks. It is a fast, best efforts check. Codereview still can reject the |
| 1377 | # authentication during the actual upload. |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1378 | self._codereview_impl.EnsureAuthenticated(force=options.force) |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 1379 | |
| 1380 | # Apply watchlists on upload. |
| 1381 | change = self.GetChange(base_branch, None) |
| 1382 | watchlist = watchlists.Watchlists(change.RepositoryRoot()) |
| 1383 | files = [f.LocalPath() for f in change.AffectedFiles()] |
| 1384 | if not options.bypass_watchlists: |
| 1385 | self.SetWatchers(watchlist.GetWatchersForPaths(files)) |
| 1386 | |
| 1387 | if not options.bypass_hooks: |
| 1388 | if options.reviewers or options.tbr_owners: |
| 1389 | # Set the reviewer list now so that presubmit checks can access it. |
| 1390 | change_description = ChangeDescription(change.FullDescriptionText()) |
| 1391 | change_description.update_reviewers(options.reviewers, |
| 1392 | options.tbr_owners, |
| 1393 | change) |
| 1394 | change.SetDescriptionText(change_description.description) |
| 1395 | hook_results = self.RunHook(committing=False, |
| 1396 | may_prompt=not options.force, |
| 1397 | verbose=options.verbose, |
| 1398 | change=change) |
| 1399 | if not hook_results.should_continue(): |
| 1400 | return 1 |
| 1401 | if not options.reviewers and hook_results.reviewers: |
| 1402 | options.reviewers = hook_results.reviewers.split(',') |
| 1403 | |
| 1404 | if self.GetIssue(): |
| 1405 | latest_patchset = self.GetMostRecentPatchset() |
| 1406 | local_patchset = self.GetPatchset() |
| 1407 | if (latest_patchset and local_patchset and |
| 1408 | local_patchset != latest_patchset): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1409 | print('The last upload made from this repository was patchset #%d but ' |
| 1410 | 'the most recent patchset on the server is #%d.' |
| 1411 | % (local_patchset, latest_patchset)) |
| 1412 | print('Uploading will still work, but if you\'ve uploaded to this ' |
| 1413 | 'issue from another machine or branch the patch you\'re ' |
| 1414 | 'uploading now might not include those changes.') |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 1415 | ask_for_data('About to upload; enter to confirm.') |
| 1416 | |
| 1417 | print_stats(options.similarity, options.find_copies, git_diff_args) |
| 1418 | ret = self.CMDUploadChange(options, git_diff_args, change) |
| 1419 | if not ret: |
tandrii | 4d0545a | 2016-07-06 03:56:49 -0700 | [diff] [blame] | 1420 | if options.use_commit_queue: |
| 1421 | self.SetCQState(_CQState.COMMIT) |
| 1422 | elif options.cq_dry_run: |
| 1423 | self.SetCQState(_CQState.DRY_RUN) |
| 1424 | |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 1425 | git_set_branch_value('last-upload-hash', |
| 1426 | RunGit(['rev-parse', 'HEAD']).strip()) |
| 1427 | # Run post upload hooks, if specified. |
| 1428 | if settings.GetRunPostUploadHook(): |
| 1429 | presubmit_support.DoPostUploadExecuter( |
| 1430 | change, |
| 1431 | self, |
| 1432 | settings.GetRoot(), |
| 1433 | options.verbose, |
| 1434 | sys.stdout) |
| 1435 | |
| 1436 | # Upload all dependencies if specified. |
| 1437 | if options.dependencies: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1438 | print() |
| 1439 | print('--dependencies has been specified.') |
| 1440 | print('All dependent local branches will be re-uploaded.') |
| 1441 | print() |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 1442 | # Remove the dependencies flag from args so that we do not end up in a |
| 1443 | # loop. |
| 1444 | orig_args.remove('--dependencies') |
| 1445 | ret = upload_branch_deps(self, orig_args) |
| 1446 | return ret |
| 1447 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1448 | def SetCQState(self, new_state): |
| 1449 | """Update the CQ state for latest patchset. |
| 1450 | |
| 1451 | Issue must have been already uploaded and known. |
| 1452 | """ |
| 1453 | assert new_state in _CQState.ALL_STATES |
| 1454 | assert self.GetIssue() |
| 1455 | return self._codereview_impl.SetCQState(new_state) |
| 1456 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1457 | # Forward methods to codereview specific implementation. |
| 1458 | |
| 1459 | def CloseIssue(self): |
| 1460 | return self._codereview_impl.CloseIssue() |
| 1461 | |
| 1462 | def GetStatus(self): |
| 1463 | return self._codereview_impl.GetStatus() |
| 1464 | |
| 1465 | def GetCodereviewServer(self): |
| 1466 | return self._codereview_impl.GetCodereviewServer() |
| 1467 | |
| 1468 | def GetApprovingReviewers(self): |
| 1469 | return self._codereview_impl.GetApprovingReviewers() |
| 1470 | |
| 1471 | def GetMostRecentPatchset(self): |
| 1472 | return self._codereview_impl.GetMostRecentPatchset() |
| 1473 | |
| 1474 | def __getattr__(self, attr): |
| 1475 | # This is because lots of untested code accesses Rietveld-specific stuff |
| 1476 | # directly, and it's hard to fix for sure. So, just let it work, and fix |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 1477 | # on a case by case basis. |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1478 | return getattr(self._codereview_impl, attr) |
| 1479 | |
| 1480 | |
| 1481 | class _ChangelistCodereviewBase(object): |
| 1482 | """Abstract base class encapsulating codereview specifics of a changelist.""" |
| 1483 | def __init__(self, changelist): |
| 1484 | self._changelist = changelist # instance of Changelist |
| 1485 | |
| 1486 | def __getattr__(self, attr): |
| 1487 | # Forward methods to changelist. |
| 1488 | # TODO(tandrii): maybe clean up _GerritChangelistImpl and |
| 1489 | # _RietveldChangelistImpl to avoid this hack? |
| 1490 | return getattr(self._changelist, attr) |
| 1491 | |
| 1492 | def GetStatus(self): |
| 1493 | """Apply a rough heuristic to give a simple summary of an issue's review |
| 1494 | or CQ status, assuming adherence to a common workflow. |
| 1495 | |
| 1496 | Returns None if no issue for this branch, or specific string keywords. |
| 1497 | """ |
| 1498 | raise NotImplementedError() |
| 1499 | |
| 1500 | def GetCodereviewServer(self): |
| 1501 | """Returns server URL without end slash, like "https://codereview.com".""" |
| 1502 | raise NotImplementedError() |
| 1503 | |
| 1504 | def FetchDescription(self): |
| 1505 | """Fetches and returns description from the codereview server.""" |
| 1506 | raise NotImplementedError() |
| 1507 | |
| 1508 | def GetCodereviewServerSetting(self): |
| 1509 | """Returns git config setting for the codereview server.""" |
| 1510 | raise NotImplementedError() |
| 1511 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1512 | @classmethod |
| 1513 | def IssueSetting(cls, branch): |
tandrii@chromium.org | d03bc63 | 2016-04-12 14:17:26 +0000 | [diff] [blame] | 1514 | return 'branch.%s.%s' % (branch, cls.IssueSettingSuffix()) |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1515 | |
| 1516 | @classmethod |
tandrii@chromium.org | d03bc63 | 2016-04-12 14:17:26 +0000 | [diff] [blame] | 1517 | def IssueSettingSuffix(cls): |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1518 | """Returns name of git config setting which stores issue number for a given |
| 1519 | branch.""" |
| 1520 | raise NotImplementedError() |
| 1521 | |
| 1522 | def PatchsetSetting(self): |
| 1523 | """Returns name of git config setting which stores issue number.""" |
| 1524 | raise NotImplementedError() |
| 1525 | |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 1526 | def _PostUnsetIssueProperties(self): |
| 1527 | """Which branch-specific properties to erase when unsettin issue.""" |
| 1528 | raise NotImplementedError() |
| 1529 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1530 | def GetRieveldObjForPresubmit(self): |
| 1531 | # This is an unfortunate Rietveld-embeddedness in presubmit. |
| 1532 | # For non-Rietveld codereviews, this probably should return a dummy object. |
| 1533 | raise NotImplementedError() |
| 1534 | |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 1535 | def GetGerritObjForPresubmit(self): |
| 1536 | # None is valid return value, otherwise presubmit_support.GerritAccessor. |
| 1537 | return None |
| 1538 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1539 | def UpdateDescriptionRemote(self, description): |
| 1540 | """Update the description on codereview site.""" |
| 1541 | raise NotImplementedError() |
| 1542 | |
| 1543 | def CloseIssue(self): |
| 1544 | """Closes the issue.""" |
| 1545 | raise NotImplementedError() |
| 1546 | |
| 1547 | def GetApprovingReviewers(self): |
| 1548 | """Returns a list of reviewers approving the change. |
| 1549 | |
| 1550 | Note: not necessarily committers. |
| 1551 | """ |
| 1552 | raise NotImplementedError() |
| 1553 | |
| 1554 | def GetMostRecentPatchset(self): |
| 1555 | """Returns the most recent patchset number from the codereview site.""" |
| 1556 | raise NotImplementedError() |
| 1557 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1558 | def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit, |
| 1559 | directory): |
| 1560 | """Fetches and applies the issue. |
| 1561 | |
| 1562 | Arguments: |
| 1563 | parsed_issue_arg: instance of _ParsedIssueNumberArgument. |
| 1564 | reject: if True, reject the failed patch instead of switching to 3-way |
| 1565 | merge. Rietveld only. |
| 1566 | nocommit: do not commit the patch, thus leave the tree dirty. Rietveld |
| 1567 | only. |
| 1568 | directory: switch to directory before applying the patch. Rietveld only. |
| 1569 | """ |
| 1570 | raise NotImplementedError() |
| 1571 | |
| 1572 | @staticmethod |
| 1573 | def ParseIssueURL(parsed_url): |
| 1574 | """Parses url and returns instance of _ParsedIssueNumberArgument or None if |
| 1575 | failed.""" |
| 1576 | raise NotImplementedError() |
| 1577 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1578 | def EnsureAuthenticated(self, force): |
| 1579 | """Best effort check that user is authenticated with codereview server. |
| 1580 | |
| 1581 | Arguments: |
| 1582 | force: whether to skip confirmation questions. |
| 1583 | """ |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 1584 | raise NotImplementedError() |
| 1585 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1586 | def CMDUploadChange(self, options, args, change): |
| 1587 | """Uploads a change to codereview.""" |
| 1588 | raise NotImplementedError() |
| 1589 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1590 | def SetCQState(self, new_state): |
| 1591 | """Update the CQ state for latest patchset. |
| 1592 | |
| 1593 | Issue must have been already uploaded and known. |
| 1594 | """ |
| 1595 | raise NotImplementedError() |
| 1596 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1597 | |
| 1598 | class _RietveldChangelistImpl(_ChangelistCodereviewBase): |
| 1599 | def __init__(self, changelist, auth_config=None, rietveld_server=None): |
| 1600 | super(_RietveldChangelistImpl, self).__init__(changelist) |
| 1601 | assert settings, 'must be initialized in _ChangelistCodereviewBase' |
martiniss | 6eda05f | 2016-06-30 10:18:35 -0700 | [diff] [blame] | 1602 | if not rietveld_server: |
| 1603 | settings.GetDefaultServerUrl() |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1604 | |
| 1605 | self._rietveld_server = rietveld_server |
| 1606 | self._auth_config = auth_config |
| 1607 | self._props = None |
| 1608 | self._rpc_server = None |
| 1609 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1610 | def GetCodereviewServer(self): |
| 1611 | if not self._rietveld_server: |
| 1612 | # If we're on a branch then get the server potentially associated |
| 1613 | # with that branch. |
| 1614 | if self.GetIssue(): |
| 1615 | rietveld_server_setting = self.GetCodereviewServerSetting() |
| 1616 | if rietveld_server_setting: |
| 1617 | self._rietveld_server = gclient_utils.UpgradeToHttps(RunGit( |
| 1618 | ['config', rietveld_server_setting], error_ok=True).strip()) |
| 1619 | if not self._rietveld_server: |
| 1620 | self._rietveld_server = settings.GetDefaultServerUrl() |
| 1621 | return self._rietveld_server |
| 1622 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 1623 | def EnsureAuthenticated(self, force): |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 1624 | """Best effort check that user is authenticated with Rietveld server.""" |
| 1625 | if self._auth_config.use_oauth2: |
| 1626 | authenticator = auth.get_authenticator_for_host( |
| 1627 | self.GetCodereviewServer(), self._auth_config) |
| 1628 | if not authenticator.has_cached_credentials(): |
| 1629 | raise auth.LoginRequiredError(self.GetCodereviewServer()) |
| 1630 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1631 | def FetchDescription(self): |
| 1632 | issue = self.GetIssue() |
| 1633 | assert issue |
| 1634 | try: |
| 1635 | return self.RpcServer().get_description(issue).strip() |
| 1636 | except urllib2.HTTPError as e: |
| 1637 | if e.code == 404: |
| 1638 | DieWithError( |
| 1639 | ('\nWhile fetching the description for issue %d, received a ' |
| 1640 | '404 (not found)\n' |
| 1641 | 'error. It is likely that you deleted this ' |
| 1642 | 'issue on the server. If this is the\n' |
| 1643 | 'case, please run\n\n' |
| 1644 | ' git cl issue 0\n\n' |
| 1645 | 'to clear the association with the deleted issue. Then run ' |
| 1646 | 'this command again.') % issue) |
| 1647 | else: |
| 1648 | DieWithError( |
| 1649 | '\nFailed to fetch issue description. HTTP error %d' % e.code) |
| 1650 | except urllib2.URLError as e: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1651 | print('Warning: Failed to retrieve CL description due to network ' |
| 1652 | 'failure.', file=sys.stderr) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1653 | return '' |
| 1654 | |
| 1655 | def GetMostRecentPatchset(self): |
| 1656 | return self.GetIssueProperties()['patchsets'][-1] |
| 1657 | |
| 1658 | def GetPatchSetDiff(self, issue, patchset): |
| 1659 | return self.RpcServer().get( |
| 1660 | '/download/issue%s_%s.diff' % (issue, patchset)) |
| 1661 | |
| 1662 | def GetIssueProperties(self): |
| 1663 | if self._props is None: |
| 1664 | issue = self.GetIssue() |
| 1665 | if not issue: |
| 1666 | self._props = {} |
| 1667 | else: |
| 1668 | self._props = self.RpcServer().get_issue_properties(issue, True) |
| 1669 | return self._props |
| 1670 | |
| 1671 | def GetApprovingReviewers(self): |
| 1672 | return get_approving_reviewers(self.GetIssueProperties()) |
| 1673 | |
| 1674 | def AddComment(self, message): |
| 1675 | return self.RpcServer().add_comment(self.GetIssue(), message) |
| 1676 | |
jsbell@chromium.org | b99fbd9 | 2014-09-11 17:29:28 +0000 | [diff] [blame] | 1677 | def GetStatus(self): |
| 1678 | """Apply a rough heuristic to give a simple summary of an issue's review |
| 1679 | or CQ status, assuming adherence to a common workflow. |
| 1680 | |
| 1681 | Returns None if no issue for this branch, or one of the following keywords: |
| 1682 | * 'error' - error from review tool (including deleted issues) |
| 1683 | * 'unsent' - not sent for review |
| 1684 | * 'waiting' - waiting for review |
| 1685 | * 'reply' - waiting for owner to reply to review |
| 1686 | * 'lgtm' - LGTM from at least one approved reviewer |
| 1687 | * 'commit' - in the commit queue |
| 1688 | * 'closed' - closed |
| 1689 | """ |
| 1690 | if not self.GetIssue(): |
| 1691 | return None |
| 1692 | |
| 1693 | try: |
| 1694 | props = self.GetIssueProperties() |
| 1695 | except urllib2.HTTPError: |
| 1696 | return 'error' |
| 1697 | |
| 1698 | if props.get('closed'): |
| 1699 | # Issue is closed. |
| 1700 | return 'closed' |
tandrii@chromium.org | b4f6a22 | 2016-03-03 01:11:04 +0000 | [diff] [blame] | 1701 | if props.get('commit') and not props.get('cq_dry_run', False): |
jsbell@chromium.org | b99fbd9 | 2014-09-11 17:29:28 +0000 | [diff] [blame] | 1702 | # Issue is in the commit queue. |
| 1703 | return 'commit' |
| 1704 | |
| 1705 | try: |
| 1706 | reviewers = self.GetApprovingReviewers() |
| 1707 | except urllib2.HTTPError: |
| 1708 | return 'error' |
| 1709 | |
| 1710 | if reviewers: |
| 1711 | # Was LGTM'ed. |
| 1712 | return 'lgtm' |
| 1713 | |
| 1714 | messages = props.get('messages') or [] |
| 1715 | |
tandrii | 9d2c7a3 | 2016-06-22 03:42:45 -0700 | [diff] [blame] | 1716 | # Skip CQ messages that don't require owner's action. |
| 1717 | while messages and messages[-1]['sender'] == COMMIT_BOT_EMAIL: |
| 1718 | if 'Dry run:' in messages[-1]['text']: |
| 1719 | messages.pop() |
| 1720 | elif 'The CQ bit was unchecked' in messages[-1]['text']: |
| 1721 | # This message always follows prior messages from CQ, |
| 1722 | # so skip this too. |
| 1723 | messages.pop() |
| 1724 | else: |
| 1725 | # This is probably a CQ messages warranting user attention. |
| 1726 | break |
| 1727 | |
jsbell@chromium.org | b99fbd9 | 2014-09-11 17:29:28 +0000 | [diff] [blame] | 1728 | if not messages: |
| 1729 | # No message was sent. |
| 1730 | return 'unsent' |
| 1731 | if messages[-1]['sender'] != props.get('owner_email'): |
tandrii | 9d2c7a3 | 2016-06-22 03:42:45 -0700 | [diff] [blame] | 1732 | # Non-LGTM reply from non-owner and not CQ bot. |
jsbell@chromium.org | b99fbd9 | 2014-09-11 17:29:28 +0000 | [diff] [blame] | 1733 | return 'reply' |
| 1734 | return 'waiting' |
| 1735 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1736 | def UpdateDescriptionRemote(self, description): |
maruel@chromium.org | b021b32 | 2013-04-08 17:57:29 +0000 | [diff] [blame] | 1737 | return self.RpcServer().update_description( |
| 1738 | self.GetIssue(), self.description) |
| 1739 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1740 | def CloseIssue(self): |
maruel@chromium.org | b021b32 | 2013-04-08 17:57:29 +0000 | [diff] [blame] | 1741 | return self.RpcServer().close_issue(self.GetIssue()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1742 | |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1743 | def SetFlag(self, flag, value): |
tandrii | 4b233bd | 2016-07-06 03:50:29 -0700 | [diff] [blame] | 1744 | return self.SetFlags({flag: value}) |
| 1745 | |
| 1746 | def SetFlags(self, flags): |
| 1747 | """Sets flags on this CL/patchset in Rietveld. |
| 1748 | |
| 1749 | The latest patchset in Rietveld must be the same as latest known locally. |
| 1750 | """ |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1751 | if not self.GetPatchset(): |
| 1752 | DieWithError('The patchset needs to match. Send another patchset.') |
| 1753 | try: |
tandrii | 4b233bd | 2016-07-06 03:50:29 -0700 | [diff] [blame] | 1754 | return self.RpcServer().set_flags( |
| 1755 | self.GetIssue(), self.GetPatchset(), flags) |
vapier | fd77ac7 | 2016-06-16 08:33:57 -0700 | [diff] [blame] | 1756 | except urllib2.HTTPError as e: |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 1757 | if e.code == 404: |
| 1758 | DieWithError('The issue %s doesn\'t exist.' % self.GetIssue()) |
| 1759 | if e.code == 403: |
| 1760 | DieWithError( |
| 1761 | ('Access denied to issue %s. Maybe the patchset %s doesn\'t ' |
| 1762 | 'match?') % (self.GetIssue(), self.GetPatchset())) |
| 1763 | raise |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1764 | |
maruel@chromium.org | cab38e9 | 2011-04-09 00:30:51 +0000 | [diff] [blame] | 1765 | def RpcServer(self): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1766 | """Returns an upload.RpcServer() to access this review's rietveld instance. |
| 1767 | """ |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 1768 | if not self._rpc_server: |
maruel@chromium.org | 4bac4b5 | 2012-11-27 20:33:52 +0000 | [diff] [blame] | 1769 | self._rpc_server = rietveld.CachingRietveld( |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1770 | self.GetCodereviewServer(), |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 1771 | self._auth_config or auth.make_auth_config()) |
maruel@chromium.org | e77ebbf | 2011-03-29 20:35:38 +0000 | [diff] [blame] | 1772 | return self._rpc_server |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1773 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1774 | @classmethod |
tandrii@chromium.org | d03bc63 | 2016-04-12 14:17:26 +0000 | [diff] [blame] | 1775 | def IssueSettingSuffix(cls): |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 1776 | return 'rietveldissue' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1777 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1778 | def PatchsetSetting(self): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1779 | """Return the git setting that stores this change's most recent patchset.""" |
| 1780 | return 'branch.%s.rietveldpatchset' % self.GetBranch() |
| 1781 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1782 | def GetCodereviewServerSetting(self): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1783 | """Returns the git setting that stores this change's rietveld server.""" |
szager@chromium.org | d62c61f | 2014-10-20 22:33:21 +0000 | [diff] [blame] | 1784 | branch = self.GetBranch() |
| 1785 | if branch: |
| 1786 | return 'branch.%s.rietveldserver' % branch |
| 1787 | return None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 1788 | |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 1789 | def _PostUnsetIssueProperties(self): |
| 1790 | """Which branch-specific properties to erase when unsetting issue.""" |
| 1791 | return ['rietveldserver'] |
| 1792 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 1793 | def GetRieveldObjForPresubmit(self): |
| 1794 | return self.RpcServer() |
| 1795 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1796 | def SetCQState(self, new_state): |
| 1797 | props = self.GetIssueProperties() |
| 1798 | if props.get('private'): |
| 1799 | DieWithError('Cannot set-commit on private issue') |
| 1800 | |
| 1801 | if new_state == _CQState.COMMIT: |
| 1802 | self.SetFlag('commit', '1') |
| 1803 | elif new_state == _CQState.NONE: |
tandrii | 4b233bd | 2016-07-06 03:50:29 -0700 | [diff] [blame] | 1804 | self.SetFlags({'commit': '0', 'cq_dry_run': '0'}) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1805 | else: |
tandrii | 4b233bd | 2016-07-06 03:50:29 -0700 | [diff] [blame] | 1806 | assert new_state == _CQState.DRY_RUN |
| 1807 | self.SetFlags({'commit': '1', 'cq_dry_run': '1'}) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 1808 | |
| 1809 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1810 | def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit, |
| 1811 | directory): |
| 1812 | # TODO(maruel): Use apply_issue.py |
| 1813 | |
| 1814 | # PatchIssue should never be called with a dirty tree. It is up to the |
| 1815 | # caller to check this, but just in case we assert here since the |
| 1816 | # consequences of the caller not checking this could be dire. |
| 1817 | assert(not git_common.is_dirty_git_tree('apply')) |
| 1818 | assert(parsed_issue_arg.valid) |
| 1819 | self._changelist.issue = parsed_issue_arg.issue |
| 1820 | if parsed_issue_arg.hostname: |
| 1821 | self._rietveld_server = 'https://%s' % parsed_issue_arg.hostname |
| 1822 | |
tandrii@chromium.org | ef7c68c | 2016-04-07 09:39:39 +0000 | [diff] [blame] | 1823 | if (isinstance(parsed_issue_arg, _RietveldParsedIssueNumberArgument) and |
| 1824 | parsed_issue_arg.patch_url): |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1825 | assert parsed_issue_arg.patchset |
| 1826 | patchset = parsed_issue_arg.patchset |
| 1827 | patch_data = urllib2.urlopen(parsed_issue_arg.patch_url).read() |
| 1828 | else: |
| 1829 | patchset = parsed_issue_arg.patchset or self.GetMostRecentPatchset() |
| 1830 | patch_data = self.GetPatchSetDiff(self.GetIssue(), patchset) |
| 1831 | |
| 1832 | # Switch up to the top-level directory, if necessary, in preparation for |
| 1833 | # applying the patch. |
| 1834 | top = settings.GetRelativeRoot() |
| 1835 | if top: |
| 1836 | os.chdir(top) |
| 1837 | |
| 1838 | # Git patches have a/ at the beginning of source paths. We strip that out |
| 1839 | # with a sed script rather than the -p flag to patch so we can feed either |
| 1840 | # Git or svn-style patches into the same apply command. |
| 1841 | # re.sub() should be used but flags=re.MULTILINE is only in python 2.7. |
| 1842 | try: |
| 1843 | patch_data = subprocess2.check_output( |
| 1844 | ['sed', '-e', 's|^--- a/|--- |; s|^+++ b/|+++ |'], stdin=patch_data) |
| 1845 | except subprocess2.CalledProcessError: |
| 1846 | DieWithError('Git patch mungling failed.') |
| 1847 | logging.info(patch_data) |
| 1848 | |
| 1849 | # We use "git apply" to apply the patch instead of "patch" so that we can |
| 1850 | # pick up file adds. |
| 1851 | # The --index flag means: also insert into the index (so we catch adds). |
| 1852 | cmd = ['git', 'apply', '--index', '-p0'] |
| 1853 | if directory: |
| 1854 | cmd.extend(('--directory', directory)) |
| 1855 | if reject: |
| 1856 | cmd.append('--reject') |
| 1857 | elif IsGitVersionAtLeast('1.7.12'): |
| 1858 | cmd.append('--3way') |
| 1859 | try: |
| 1860 | subprocess2.check_call(cmd, env=GetNoGitPagerEnv(), |
| 1861 | stdin=patch_data, stdout=subprocess2.VOID) |
| 1862 | except subprocess2.CalledProcessError: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1863 | print('Failed to apply the patch') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1864 | return 1 |
| 1865 | |
| 1866 | # If we had an issue, commit the current state and register the issue. |
| 1867 | if not nocommit: |
| 1868 | RunGit(['commit', '-m', (self.GetDescription() + '\n\n' + |
| 1869 | 'patch from issue %(i)s at patchset ' |
| 1870 | '%(p)s (http://crrev.com/%(i)s#ps%(p)s)' |
| 1871 | % {'i': self.GetIssue(), 'p': patchset})]) |
| 1872 | self.SetIssue(self.GetIssue()) |
| 1873 | self.SetPatchset(patchset) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1874 | print('Committed patch locally.') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1875 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1876 | print('Patch applied to index.') |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 1877 | return 0 |
| 1878 | |
| 1879 | @staticmethod |
| 1880 | def ParseIssueURL(parsed_url): |
| 1881 | if not parsed_url.scheme or not parsed_url.scheme.startswith('http'): |
| 1882 | return None |
| 1883 | # Typical url: https://domain/<issue_number>[/[other]] |
| 1884 | match = re.match('/(\d+)(/.*)?$', parsed_url.path) |
| 1885 | if match: |
| 1886 | return _RietveldParsedIssueNumberArgument( |
| 1887 | issue=int(match.group(1)), |
| 1888 | hostname=parsed_url.netloc) |
| 1889 | # Rietveld patch: https://domain/download/issue<number>_<patchset>.diff |
| 1890 | match = re.match(r'/download/issue(\d+)_(\d+).diff$', parsed_url.path) |
| 1891 | if match: |
| 1892 | return _RietveldParsedIssueNumberArgument( |
| 1893 | issue=int(match.group(1)), |
| 1894 | patchset=int(match.group(2)), |
| 1895 | hostname=parsed_url.netloc, |
| 1896 | patch_url=gclient_utils.UpgradeToHttps(parsed_url.geturl())) |
| 1897 | return None |
| 1898 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1899 | def CMDUploadChange(self, options, args, change): |
| 1900 | """Upload the patch to Rietveld.""" |
| 1901 | upload_args = ['--assume_yes'] # Don't ask about untracked files. |
| 1902 | upload_args.extend(['--server', self.GetCodereviewServer()]) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1903 | upload_args.extend(auth.auth_config_to_command_options(self._auth_config)) |
| 1904 | if options.emulate_svn_auto_props: |
| 1905 | upload_args.append('--emulate_svn_auto_props') |
| 1906 | |
| 1907 | change_desc = None |
| 1908 | |
| 1909 | if options.email is not None: |
| 1910 | upload_args.extend(['--email', options.email]) |
| 1911 | |
| 1912 | if self.GetIssue(): |
nodir | ca16600 | 2016-06-27 10:59:51 -0700 | [diff] [blame] | 1913 | if options.title is not None: |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1914 | upload_args.extend(['--title', options.title]) |
| 1915 | if options.message: |
| 1916 | upload_args.extend(['--message', options.message]) |
| 1917 | upload_args.extend(['--issue', str(self.GetIssue())]) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1918 | print('This branch is associated with issue %s. ' |
| 1919 | 'Adding patch to that issue.' % self.GetIssue()) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1920 | else: |
nodir | ca16600 | 2016-06-27 10:59:51 -0700 | [diff] [blame] | 1921 | if options.title is not None: |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1922 | upload_args.extend(['--title', options.title]) |
| 1923 | message = (options.title or options.message or |
| 1924 | CreateDescriptionFromLog(args)) |
| 1925 | change_desc = ChangeDescription(message) |
| 1926 | if options.reviewers or options.tbr_owners: |
| 1927 | change_desc.update_reviewers(options.reviewers, |
| 1928 | options.tbr_owners, |
| 1929 | change) |
| 1930 | if not options.force: |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 1931 | change_desc.prompt(bug=options.bug) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1932 | |
| 1933 | if not change_desc.description: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1934 | print('Description is empty; aborting.') |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1935 | return 1 |
| 1936 | |
| 1937 | upload_args.extend(['--message', change_desc.description]) |
| 1938 | if change_desc.get_reviewers(): |
| 1939 | upload_args.append('--reviewers=%s' % ','.join( |
| 1940 | change_desc.get_reviewers())) |
| 1941 | if options.send_mail: |
| 1942 | if not change_desc.get_reviewers(): |
| 1943 | DieWithError("Must specify reviewers to send email.") |
| 1944 | upload_args.append('--send_mail') |
| 1945 | |
| 1946 | # We check this before applying rietveld.private assuming that in |
| 1947 | # rietveld.cc only addresses which we can send private CLs to are listed |
| 1948 | # if rietveld.private is set, and so we should ignore rietveld.cc only |
| 1949 | # when --private is specified explicitly on the command line. |
| 1950 | if options.private: |
| 1951 | logging.warn('rietveld.cc is ignored since private flag is specified. ' |
| 1952 | 'You need to review and add them manually if necessary.') |
| 1953 | cc = self.GetCCListWithoutDefault() |
| 1954 | else: |
| 1955 | cc = self.GetCCList() |
| 1956 | cc = ','.join(filter(None, (cc, ','.join(options.cc)))) |
| 1957 | if cc: |
| 1958 | upload_args.extend(['--cc', cc]) |
| 1959 | |
| 1960 | if options.private or settings.GetDefaultPrivateFlag() == "True": |
| 1961 | upload_args.append('--private') |
| 1962 | |
| 1963 | upload_args.extend(['--git_similarity', str(options.similarity)]) |
| 1964 | if not options.find_copies: |
| 1965 | upload_args.extend(['--git_no_find_copies']) |
| 1966 | |
| 1967 | # Include the upstream repo's URL in the change -- this is useful for |
| 1968 | # projects that have their source spread across multiple repos. |
| 1969 | remote_url = self.GetGitBaseUrlFromConfig() |
| 1970 | if not remote_url: |
| 1971 | if settings.GetIsGitSvn(): |
| 1972 | remote_url = self.GetGitSvnRemoteUrl() |
| 1973 | else: |
| 1974 | if self.GetRemoteUrl() and '/' in self.GetUpstreamBranch(): |
| 1975 | remote_url = '%s@%s' % (self.GetRemoteUrl(), |
| 1976 | self.GetUpstreamBranch().split('/')[-1]) |
| 1977 | if remote_url: |
| 1978 | upload_args.extend(['--base_url', remote_url]) |
| 1979 | remote, remote_branch = self.GetRemoteBranch() |
| 1980 | target_ref = GetTargetRef(remote, remote_branch, options.target_branch, |
| 1981 | settings.GetPendingRefPrefix()) |
| 1982 | if target_ref: |
| 1983 | upload_args.extend(['--target_ref', target_ref]) |
| 1984 | |
| 1985 | # Look for dependent patchsets. See crbug.com/480453 for more details. |
| 1986 | remote, upstream_branch = self.FetchUpstreamTuple(self.GetBranch()) |
| 1987 | upstream_branch = ShortBranchName(upstream_branch) |
| 1988 | if remote is '.': |
| 1989 | # A local branch is being tracked. |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 1990 | local_branch = upstream_branch |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1991 | if settings.GetIsSkipDependencyUpload(local_branch): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 1992 | print() |
| 1993 | print('Skipping dependency patchset upload because git config ' |
| 1994 | 'branch.%s.skip-deps-uploads is set to True.' % local_branch) |
| 1995 | print() |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1996 | else: |
| 1997 | auth_config = auth.extract_auth_config_from_options(options) |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 1998 | branch_cl = Changelist(branchref='refs/heads/'+local_branch, |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 1999 | auth_config=auth_config) |
| 2000 | branch_cl_issue_url = branch_cl.GetIssueURL() |
| 2001 | branch_cl_issue = branch_cl.GetIssue() |
| 2002 | branch_cl_patchset = branch_cl.GetPatchset() |
| 2003 | if branch_cl_issue_url and branch_cl_issue and branch_cl_patchset: |
| 2004 | upload_args.extend( |
| 2005 | ['--depends_on_patchset', '%s:%s' % ( |
| 2006 | branch_cl_issue, branch_cl_patchset)]) |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 2007 | print( |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2008 | '\n' |
| 2009 | 'The current branch (%s) is tracking a local branch (%s) with ' |
| 2010 | 'an associated CL.\n' |
| 2011 | 'Adding %s/#ps%s as a dependency patchset.\n' |
| 2012 | '\n' % (self.GetBranch(), local_branch, branch_cl_issue_url, |
| 2013 | branch_cl_patchset)) |
| 2014 | |
| 2015 | project = settings.GetProject() |
| 2016 | if project: |
| 2017 | upload_args.extend(['--project', project]) |
| 2018 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2019 | try: |
| 2020 | upload_args = ['upload'] + upload_args + args |
| 2021 | logging.info('upload.RealMain(%s)', upload_args) |
| 2022 | issue, patchset = upload.RealMain(upload_args) |
| 2023 | issue = int(issue) |
| 2024 | patchset = int(patchset) |
| 2025 | except KeyboardInterrupt: |
| 2026 | sys.exit(1) |
| 2027 | except: |
| 2028 | # If we got an exception after the user typed a description for their |
| 2029 | # change, back up the description before re-raising. |
| 2030 | if change_desc: |
| 2031 | backup_path = os.path.expanduser(DESCRIPTION_BACKUP_FILE) |
| 2032 | print('\nGot exception while uploading -- saving description to %s\n' % |
| 2033 | backup_path) |
| 2034 | backup_file = open(backup_path, 'w') |
| 2035 | backup_file.write(change_desc.description) |
| 2036 | backup_file.close() |
| 2037 | raise |
| 2038 | |
| 2039 | if not self.GetIssue(): |
| 2040 | self.SetIssue(issue) |
| 2041 | self.SetPatchset(patchset) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2042 | return 0 |
| 2043 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2044 | |
| 2045 | class _GerritChangelistImpl(_ChangelistCodereviewBase): |
| 2046 | def __init__(self, changelist, auth_config=None): |
| 2047 | # auth_config is Rietveld thing, kept here to preserve interface only. |
| 2048 | super(_GerritChangelistImpl, self).__init__(changelist) |
| 2049 | self._change_id = None |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2050 | # Lazily cached values. |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2051 | self._gerrit_server = None # e.g. https://chromium-review.googlesource.com |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2052 | self._gerrit_host = None # e.g. chromium-review.googlesource.com |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2053 | |
| 2054 | def _GetGerritHost(self): |
| 2055 | # Lazy load of configs. |
| 2056 | self.GetCodereviewServer() |
tandrii | e32e3ea | 2016-06-22 02:52:48 -0700 | [diff] [blame] | 2057 | if self._gerrit_host and '.' not in self._gerrit_host: |
| 2058 | # Abbreviated domain like "chromium" instead of chromium.googlesource.com. |
| 2059 | # This happens for internal stuff http://crbug.com/614312. |
| 2060 | parsed = urlparse.urlparse(self.GetRemoteUrl()) |
| 2061 | if parsed.scheme == 'sso': |
| 2062 | print('WARNING: using non https URLs for remote is likely broken\n' |
| 2063 | ' Your current remote is: %s' % self.GetRemoteUrl()) |
| 2064 | self._gerrit_host = '%s.googlesource.com' % self._gerrit_host |
| 2065 | self._gerrit_server = 'https://%s' % self._gerrit_host |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2066 | return self._gerrit_host |
| 2067 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2068 | def _GetGitHost(self): |
| 2069 | """Returns git host to be used when uploading change to Gerrit.""" |
| 2070 | return urlparse.urlparse(self.GetRemoteUrl()).netloc |
| 2071 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2072 | def GetCodereviewServer(self): |
| 2073 | if not self._gerrit_server: |
| 2074 | # If we're on a branch then get the server potentially associated |
| 2075 | # with that branch. |
| 2076 | if self.GetIssue(): |
| 2077 | gerrit_server_setting = self.GetCodereviewServerSetting() |
| 2078 | if gerrit_server_setting: |
| 2079 | self._gerrit_server = RunGit(['config', gerrit_server_setting], |
| 2080 | error_ok=True).strip() |
| 2081 | if self._gerrit_server: |
| 2082 | self._gerrit_host = urlparse.urlparse(self._gerrit_server).netloc |
| 2083 | if not self._gerrit_server: |
| 2084 | # We assume repo to be hosted on Gerrit, and hence Gerrit server |
| 2085 | # has "-review" suffix for lowest level subdomain. |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2086 | parts = self._GetGitHost().split('.') |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2087 | parts[0] = parts[0] + '-review' |
| 2088 | self._gerrit_host = '.'.join(parts) |
| 2089 | self._gerrit_server = 'https://%s' % self._gerrit_host |
| 2090 | return self._gerrit_server |
| 2091 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 2092 | @classmethod |
tandrii@chromium.org | d03bc63 | 2016-04-12 14:17:26 +0000 | [diff] [blame] | 2093 | def IssueSettingSuffix(cls): |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 2094 | return 'gerritissue' |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2095 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2096 | def EnsureAuthenticated(self, force): |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 2097 | """Best effort check that user is authenticated with Gerrit server.""" |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2098 | if settings.GetGerritSkipEnsureAuthenticated(): |
| 2099 | # For projects with unusual authentication schemes. |
| 2100 | # See http://crbug.com/603378. |
| 2101 | return |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 2102 | # Lazy-loader to identify Gerrit and Git hosts. |
| 2103 | if gerrit_util.GceAuthenticator.is_gce(): |
| 2104 | return |
| 2105 | self.GetCodereviewServer() |
| 2106 | git_host = self._GetGitHost() |
| 2107 | assert self._gerrit_server and self._gerrit_host |
| 2108 | cookie_auth = gerrit_util.CookiesAuthenticator() |
| 2109 | |
| 2110 | gerrit_auth = cookie_auth.get_auth_header(self._gerrit_host) |
| 2111 | git_auth = cookie_auth.get_auth_header(git_host) |
| 2112 | if gerrit_auth and git_auth: |
| 2113 | if gerrit_auth == git_auth: |
| 2114 | return |
| 2115 | print(( |
| 2116 | 'WARNING: you have different credentials for Gerrit and git hosts.\n' |
| 2117 | ' Check your %s or %s file for credentials of hosts:\n' |
| 2118 | ' %s\n' |
| 2119 | ' %s\n' |
| 2120 | ' %s') % |
| 2121 | (cookie_auth.get_gitcookies_path(), cookie_auth.get_netrc_path(), |
| 2122 | git_host, self._gerrit_host, |
| 2123 | cookie_auth.get_new_password_message(git_host))) |
| 2124 | if not force: |
| 2125 | ask_for_data('If you know what you are doing, press Enter to continue, ' |
| 2126 | 'Ctrl+C to abort.') |
| 2127 | return |
| 2128 | else: |
| 2129 | missing = ( |
| 2130 | [] if gerrit_auth else [self._gerrit_host] + |
| 2131 | [] if git_auth else [git_host]) |
| 2132 | DieWithError('Credentials for the following hosts are required:\n' |
| 2133 | ' %s\n' |
| 2134 | 'These are read from %s (or legacy %s)\n' |
| 2135 | '%s' % ( |
| 2136 | '\n '.join(missing), |
| 2137 | cookie_auth.get_gitcookies_path(), |
| 2138 | cookie_auth.get_netrc_path(), |
| 2139 | cookie_auth.get_new_password_message(git_host))) |
| 2140 | |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 2141 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2142 | def PatchsetSetting(self): |
| 2143 | """Return the git setting that stores this change's most recent patchset.""" |
| 2144 | return 'branch.%s.gerritpatchset' % self.GetBranch() |
| 2145 | |
| 2146 | def GetCodereviewServerSetting(self): |
| 2147 | """Returns the git setting that stores this change's Gerrit server.""" |
| 2148 | branch = self.GetBranch() |
| 2149 | if branch: |
| 2150 | return 'branch.%s.gerritserver' % branch |
| 2151 | return None |
| 2152 | |
tandrii@chromium.org | 9b7fd71 | 2016-06-01 13:45:20 +0000 | [diff] [blame] | 2153 | def _PostUnsetIssueProperties(self): |
| 2154 | """Which branch-specific properties to erase when unsetting issue.""" |
| 2155 | return [ |
| 2156 | 'gerritserver', |
| 2157 | 'gerritsquashhash', |
| 2158 | ] |
| 2159 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2160 | def GetRieveldObjForPresubmit(self): |
| 2161 | class ThisIsNotRietveldIssue(object): |
| 2162 | def __nonzero__(self): |
| 2163 | # This is a hack to make presubmit_support think that rietveld is not |
| 2164 | # defined, yet still ensure that calls directly result in a decent |
| 2165 | # exception message below. |
| 2166 | return False |
| 2167 | |
| 2168 | def __getattr__(self, attr): |
| 2169 | print( |
| 2170 | 'You aren\'t using Rietveld at the moment, but Gerrit.\n' |
| 2171 | 'Using Rietveld in your PRESUBMIT scripts won\'t work.\n' |
| 2172 | 'Please, either change your PRESUBIT to not use rietveld_obj.%s,\n' |
| 2173 | 'or use Rietveld for codereview.\n' |
| 2174 | 'See also http://crbug.com/579160.' % attr) |
| 2175 | raise NotImplementedError() |
| 2176 | return ThisIsNotRietveldIssue() |
| 2177 | |
tandrii@chromium.org | 37b07a7 | 2016-04-29 16:42:28 +0000 | [diff] [blame] | 2178 | def GetGerritObjForPresubmit(self): |
| 2179 | return presubmit_support.GerritAccessor(self._GetGerritHost()) |
| 2180 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2181 | def GetStatus(self): |
tandrii@chromium.org | 013a280 | 2016-03-29 09:52:33 +0000 | [diff] [blame] | 2182 | """Apply a rough heuristic to give a simple summary of an issue's review |
| 2183 | or CQ status, assuming adherence to a common workflow. |
| 2184 | |
| 2185 | Returns None if no issue for this branch, or one of the following keywords: |
| 2186 | * 'error' - error from review tool (including deleted issues) |
| 2187 | * 'unsent' - no reviewers added |
| 2188 | * 'waiting' - waiting for review |
| 2189 | * 'reply' - waiting for owner to reply to review |
| 2190 | * 'not lgtm' - Code-Review -2 from at least one approved reviewer |
| 2191 | * 'lgtm' - Code-Review +2 from at least one approved reviewer |
| 2192 | * 'commit' - in the commit queue |
| 2193 | * 'closed' - abandoned |
| 2194 | """ |
| 2195 | if not self.GetIssue(): |
| 2196 | return None |
| 2197 | |
| 2198 | try: |
| 2199 | data = self._GetChangeDetail(['DETAILED_LABELS', 'CURRENT_REVISION']) |
| 2200 | except httplib.HTTPException: |
| 2201 | return 'error' |
| 2202 | |
tandrii@chromium.org | 5e1bf38 | 2016-05-17 08:43:24 +0000 | [diff] [blame] | 2203 | if data['status'] in ('ABANDONED', 'MERGED'): |
tandrii@chromium.org | 013a280 | 2016-03-29 09:52:33 +0000 | [diff] [blame] | 2204 | return 'closed' |
| 2205 | |
| 2206 | cq_label = data['labels'].get('Commit-Queue', {}) |
| 2207 | if cq_label: |
| 2208 | # Vote value is a stringified integer, which we expect from 0 to 2. |
| 2209 | vote_value = cq_label.get('value', '0') |
| 2210 | vote_text = cq_label.get('values', {}).get(vote_value, '') |
| 2211 | if vote_text.lower() == 'commit': |
| 2212 | return 'commit' |
| 2213 | |
| 2214 | lgtm_label = data['labels'].get('Code-Review', {}) |
| 2215 | if lgtm_label: |
| 2216 | if 'rejected' in lgtm_label: |
| 2217 | return 'not lgtm' |
| 2218 | if 'approved' in lgtm_label: |
| 2219 | return 'lgtm' |
| 2220 | |
| 2221 | if not data.get('reviewers', {}).get('REVIEWER', []): |
| 2222 | return 'unsent' |
| 2223 | |
| 2224 | messages = data.get('messages', []) |
| 2225 | if messages: |
| 2226 | owner = data['owner'].get('_account_id') |
| 2227 | last_message_author = messages[-1].get('author', {}).get('_account_id') |
| 2228 | if owner != last_message_author: |
| 2229 | # Some reply from non-owner. |
| 2230 | return 'reply' |
| 2231 | |
| 2232 | return 'waiting' |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2233 | |
| 2234 | def GetMostRecentPatchset(self): |
tandrii@chromium.org | 013a280 | 2016-03-29 09:52:33 +0000 | [diff] [blame] | 2235 | data = self._GetChangeDetail(['CURRENT_REVISION']) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2236 | return data['revisions'][data['current_revision']]['_number'] |
| 2237 | |
| 2238 | def FetchDescription(self): |
tandrii@chromium.org | 2d3da63 | 2016-04-25 19:23:27 +0000 | [diff] [blame] | 2239 | data = self._GetChangeDetail(['CURRENT_REVISION']) |
| 2240 | current_rev = data['current_revision'] |
| 2241 | url = data['revisions'][current_rev]['fetch']['http']['url'] |
| 2242 | return gerrit_util.GetChangeDescriptionFromGitiles(url, current_rev) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2243 | |
| 2244 | def UpdateDescriptionRemote(self, description): |
scottmg@chromium.org | 6d1266e | 2016-04-26 11:12:26 +0000 | [diff] [blame] | 2245 | gerrit_util.SetCommitMessage(self._GetGerritHost(), self.GetIssue(), |
| 2246 | description) |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 2247 | |
| 2248 | def CloseIssue(self): |
| 2249 | gerrit_util.AbandonChange(self._GetGerritHost(), self.GetIssue(), msg='') |
| 2250 | |
tandrii@chromium.org | 600b492 | 2016-04-26 10:57:52 +0000 | [diff] [blame] | 2251 | def GetApprovingReviewers(self): |
| 2252 | """Returns a list of reviewers approving the change. |
| 2253 | |
| 2254 | Note: not necessarily committers. |
| 2255 | """ |
| 2256 | raise NotImplementedError() |
| 2257 | |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 2258 | def SubmitIssue(self, wait_for_merge=True): |
| 2259 | gerrit_util.SubmitChange(self._GetGerritHost(), self.GetIssue(), |
| 2260 | wait_for_merge=wait_for_merge) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2261 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2262 | def _GetChangeDetail(self, options=None, issue=None): |
| 2263 | options = options or [] |
| 2264 | issue = issue or self.GetIssue() |
| 2265 | assert issue, 'issue required to query Gerrit' |
tandrii@chromium.org | 11a899e | 2016-04-13 12:45:44 +0000 | [diff] [blame] | 2266 | return gerrit_util.GetChangeDetail(self._GetGerritHost(), str(issue), |
| 2267 | options) |
tandrii@chromium.org | 013a280 | 2016-03-29 09:52:33 +0000 | [diff] [blame] | 2268 | |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 2269 | def CMDLand(self, force, bypass_hooks, verbose): |
| 2270 | if git_common.is_dirty_git_tree('land'): |
| 2271 | return 1 |
tandrii | d60367b | 2016-06-22 05:25:12 -0700 | [diff] [blame] | 2272 | detail = self._GetChangeDetail(['CURRENT_REVISION', 'LABELS']) |
| 2273 | if u'Commit-Queue' in detail.get('labels', {}): |
| 2274 | if not force: |
| 2275 | ask_for_data('\nIt seems this repository has a Commit Queue, ' |
| 2276 | 'which can test and land changes for you. ' |
| 2277 | 'Are you sure you wish to bypass it?\n' |
| 2278 | 'Press Enter to continue, Ctrl+C to abort.') |
| 2279 | |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 2280 | differs = True |
| 2281 | last_upload = RunGit(['config', |
| 2282 | 'branch.%s.gerritsquashhash' % self.GetBranch()], |
| 2283 | error_ok=True).strip() |
| 2284 | # Note: git diff outputs nothing if there is no diff. |
| 2285 | if not last_upload or RunGit(['diff', last_upload]).strip(): |
| 2286 | print('WARNING: some changes from local branch haven\'t been uploaded') |
| 2287 | else: |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 2288 | if detail['current_revision'] == last_upload: |
| 2289 | differs = False |
| 2290 | else: |
| 2291 | print('WARNING: local branch contents differ from latest uploaded ' |
| 2292 | 'patchset') |
| 2293 | if differs: |
| 2294 | if not force: |
| 2295 | ask_for_data( |
| 2296 | 'Do you want to submit latest Gerrit patchset and bypass hooks?') |
| 2297 | print('WARNING: bypassing hooks and submitting latest uploaded patchset') |
| 2298 | elif not bypass_hooks: |
| 2299 | hook_results = self.RunHook( |
| 2300 | committing=True, |
| 2301 | may_prompt=not force, |
| 2302 | verbose=verbose, |
| 2303 | change=self.GetChange(self.GetCommonAncestorWithUpstream(), None)) |
| 2304 | if not hook_results.should_continue(): |
| 2305 | return 1 |
| 2306 | |
| 2307 | self.SubmitIssue(wait_for_merge=True) |
| 2308 | print('Issue %s has been submitted.' % self.GetIssueURL()) |
| 2309 | return 0 |
| 2310 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 2311 | def CMDPatchWithParsedIssue(self, parsed_issue_arg, reject, nocommit, |
| 2312 | directory): |
| 2313 | assert not reject |
| 2314 | assert not nocommit |
| 2315 | assert not directory |
| 2316 | assert parsed_issue_arg.valid |
| 2317 | |
| 2318 | self._changelist.issue = parsed_issue_arg.issue |
| 2319 | |
| 2320 | if parsed_issue_arg.hostname: |
| 2321 | self._gerrit_host = parsed_issue_arg.hostname |
| 2322 | self._gerrit_server = 'https://%s' % self._gerrit_host |
| 2323 | |
| 2324 | detail = self._GetChangeDetail(['ALL_REVISIONS']) |
| 2325 | |
| 2326 | if not parsed_issue_arg.patchset: |
| 2327 | # Use current revision by default. |
| 2328 | revision_info = detail['revisions'][detail['current_revision']] |
| 2329 | patchset = int(revision_info['_number']) |
| 2330 | else: |
| 2331 | patchset = parsed_issue_arg.patchset |
| 2332 | for revision_info in detail['revisions'].itervalues(): |
| 2333 | if int(revision_info['_number']) == parsed_issue_arg.patchset: |
| 2334 | break |
| 2335 | else: |
| 2336 | DieWithError('Couldn\'t find patchset %i in issue %i' % |
| 2337 | (parsed_issue_arg.patchset, self.GetIssue())) |
| 2338 | |
| 2339 | fetch_info = revision_info['fetch']['http'] |
| 2340 | RunGit(['fetch', fetch_info['url'], fetch_info['ref']]) |
| 2341 | RunGit(['cherry-pick', 'FETCH_HEAD']) |
| 2342 | self.SetIssue(self.GetIssue()) |
| 2343 | self.SetPatchset(patchset) |
| 2344 | print('Committed patch for issue %i pathset %i locally' % |
| 2345 | (self.GetIssue(), self.GetPatchset())) |
| 2346 | return 0 |
| 2347 | |
| 2348 | @staticmethod |
| 2349 | def ParseIssueURL(parsed_url): |
| 2350 | if not parsed_url.scheme or not parsed_url.scheme.startswith('http'): |
| 2351 | return None |
| 2352 | # Gerrit's new UI is https://domain/c/<issue_number>[/[patchset]] |
| 2353 | # But current GWT UI is https://domain/#/c/<issue_number>[/[patchset]] |
| 2354 | # Short urls like https://domain/<issue_number> can be used, but don't allow |
| 2355 | # specifying the patchset (you'd 404), but we allow that here. |
| 2356 | if parsed_url.path == '/': |
| 2357 | part = parsed_url.fragment |
| 2358 | else: |
| 2359 | part = parsed_url.path |
| 2360 | match = re.match('(/c)?/(\d+)(/(\d+)?/?)?$', part) |
| 2361 | if match: |
| 2362 | return _ParsedIssueNumberArgument( |
| 2363 | issue=int(match.group(2)), |
| 2364 | patchset=int(match.group(4)) if match.group(4) else None, |
| 2365 | hostname=parsed_url.netloc) |
| 2366 | return None |
| 2367 | |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2368 | def _GerritCommitMsgHookCheck(self, offer_removal): |
| 2369 | hook = os.path.join(settings.GetRoot(), '.git', 'hooks', 'commit-msg') |
| 2370 | if not os.path.exists(hook): |
| 2371 | return |
| 2372 | # Crude attempt to distinguish Gerrit Codereview hook from potentially |
| 2373 | # custom developer made one. |
| 2374 | data = gclient_utils.FileRead(hook) |
| 2375 | if not('From Gerrit Code Review' in data and 'add_ChangeId()' in data): |
| 2376 | return |
| 2377 | print('Warning: you have Gerrit commit-msg hook installed.\n' |
| 2378 | 'It is not neccessary for uploading with git cl in squash mode, ' |
| 2379 | 'and may interfere with it in subtle ways.\n' |
| 2380 | 'We recommend you remove the commit-msg hook.') |
| 2381 | if offer_removal: |
| 2382 | reply = ask_for_data('Do you want to remove it now? [Yes/No]') |
| 2383 | if reply.lower().startswith('y'): |
| 2384 | gclient_utils.rm_file_or_tree(hook) |
| 2385 | print('Gerrit commit-msg hook removed.') |
| 2386 | else: |
| 2387 | print('OK, will keep Gerrit commit-msg hook in place.') |
| 2388 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2389 | def CMDUploadChange(self, options, args, change): |
| 2390 | """Upload the current branch to Gerrit.""" |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 2391 | if options.squash and options.no_squash: |
| 2392 | DieWithError('Can only use one of --squash or --no-squash') |
tandrii | a60502f | 2016-06-20 02:01:53 -0700 | [diff] [blame] | 2393 | |
| 2394 | if not options.squash and not options.no_squash: |
| 2395 | # Load default for user, repo, squash=true, in this order. |
| 2396 | options.squash = settings.GetSquashGerritUploads() |
| 2397 | elif options.no_squash: |
| 2398 | options.squash = False |
tandrii | 26f3e4e | 2016-06-10 08:37:04 -0700 | [diff] [blame] | 2399 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2400 | # We assume the remote called "origin" is the one we want. |
| 2401 | # It is probably not worthwhile to support different workflows. |
| 2402 | gerrit_remote = 'origin' |
| 2403 | |
| 2404 | remote, remote_branch = self.GetRemoteBranch() |
| 2405 | branch = GetTargetRef(remote, remote_branch, options.target_branch, |
| 2406 | pending_prefix='') |
| 2407 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2408 | if options.squash: |
tandrii | 16e0b4e | 2016-06-07 10:34:28 -0700 | [diff] [blame] | 2409 | self._GerritCommitMsgHookCheck(offer_removal=not options.force) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2410 | if self.GetIssue(): |
| 2411 | # Try to get the message from a previous upload. |
| 2412 | message = self.GetDescription() |
| 2413 | if not message: |
| 2414 | DieWithError( |
| 2415 | 'failed to fetch description from current Gerrit issue %d\n' |
| 2416 | '%s' % (self.GetIssue(), self.GetIssueURL())) |
| 2417 | change_id = self._GetChangeDetail()['change_id'] |
| 2418 | while True: |
| 2419 | footer_change_ids = git_footers.get_footer_change_id(message) |
| 2420 | if footer_change_ids == [change_id]: |
| 2421 | break |
| 2422 | if not footer_change_ids: |
| 2423 | message = git_footers.add_footer_change_id(message, change_id) |
| 2424 | print('WARNING: appended missing Change-Id to issue description') |
| 2425 | continue |
| 2426 | # There is already a valid footer but with different or several ids. |
| 2427 | # Doing this automatically is non-trivial as we don't want to lose |
| 2428 | # existing other footers, yet we want to append just 1 desired |
| 2429 | # Change-Id. Thus, just create a new footer, but let user verify the |
| 2430 | # new description. |
| 2431 | message = '%s\n\nChange-Id: %s' % (message, change_id) |
| 2432 | print( |
| 2433 | 'WARNING: issue %s has Change-Id footer(s):\n' |
| 2434 | ' %s\n' |
| 2435 | 'but issue has Change-Id %s, according to Gerrit.\n' |
| 2436 | 'Please, check the proposed correction to the description, ' |
| 2437 | 'and edit it if necessary but keep the "Change-Id: %s" footer\n' |
| 2438 | % (self.GetIssue(), '\n '.join(footer_change_ids), change_id, |
| 2439 | change_id)) |
| 2440 | ask_for_data('Press enter to edit now, Ctrl+C to abort') |
| 2441 | if not options.force: |
| 2442 | change_desc = ChangeDescription(message) |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 2443 | change_desc.prompt(bug=options.bug) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2444 | message = change_desc.description |
| 2445 | if not message: |
| 2446 | DieWithError("Description is empty. Aborting...") |
| 2447 | # Continue the while loop. |
| 2448 | # Sanity check of this code - we should end up with proper message |
| 2449 | # footer. |
| 2450 | assert [change_id] == git_footers.get_footer_change_id(message) |
| 2451 | change_desc = ChangeDescription(message) |
| 2452 | else: |
| 2453 | change_desc = ChangeDescription( |
| 2454 | options.message or CreateDescriptionFromLog(args)) |
| 2455 | if not options.force: |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 2456 | change_desc.prompt(bug=options.bug) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2457 | if not change_desc.description: |
| 2458 | DieWithError("Description is empty. Aborting...") |
| 2459 | message = change_desc.description |
| 2460 | change_ids = git_footers.get_footer_change_id(message) |
| 2461 | if len(change_ids) > 1: |
| 2462 | DieWithError('too many Change-Id footers, at most 1 allowed.') |
| 2463 | if not change_ids: |
| 2464 | # Generate the Change-Id automatically. |
| 2465 | message = git_footers.add_footer_change_id( |
| 2466 | message, GenerateGerritChangeId(message)) |
| 2467 | change_desc.set_description(message) |
| 2468 | change_ids = git_footers.get_footer_change_id(message) |
| 2469 | assert len(change_ids) == 1 |
| 2470 | change_id = change_ids[0] |
| 2471 | |
| 2472 | remote, upstream_branch = self.FetchUpstreamTuple(self.GetBranch()) |
| 2473 | if remote is '.': |
| 2474 | # If our upstream branch is local, we base our squashed commit on its |
| 2475 | # squashed version. |
| 2476 | upstream_branch_name = scm.GIT.ShortBranchName(upstream_branch) |
| 2477 | # Check the squashed hash of the parent. |
| 2478 | parent = RunGit(['config', |
| 2479 | 'branch.%s.gerritsquashhash' % upstream_branch_name], |
| 2480 | error_ok=True).strip() |
| 2481 | # Verify that the upstream branch has been uploaded too, otherwise |
| 2482 | # Gerrit will create additional CLs when uploading. |
| 2483 | if not parent or (RunGitSilent(['rev-parse', upstream_branch + ':']) != |
| 2484 | RunGitSilent(['rev-parse', parent + ':'])): |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2485 | DieWithError( |
| 2486 | 'Upload upstream branch %s first.\n' |
tandrii | 2bdadf1 | 2016-07-12 12:27:54 -0700 | [diff] [blame] | 2487 | 'Note: maybe you\'ve uploaded it with --no-squash. ' |
| 2488 | 'If so, then re-upload it with:\n' |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2489 | ' git cl upload --squash\n' % upstream_branch_name) |
| 2490 | else: |
| 2491 | parent = self.GetCommonAncestorWithUpstream() |
| 2492 | |
| 2493 | tree = RunGit(['rev-parse', 'HEAD:']).strip() |
| 2494 | ref_to_push = RunGit(['commit-tree', tree, '-p', parent, |
| 2495 | '-m', message]).strip() |
| 2496 | else: |
| 2497 | change_desc = ChangeDescription( |
| 2498 | options.message or CreateDescriptionFromLog(args)) |
| 2499 | if not change_desc.description: |
| 2500 | DieWithError("Description is empty. Aborting...") |
| 2501 | |
| 2502 | if not git_footers.get_footer_change_id(change_desc.description): |
| 2503 | DownloadGerritHook(False) |
tandrii@chromium.org | 8930b3d | 2016-04-13 14:47:02 +0000 | [diff] [blame] | 2504 | change_desc.set_description(self._AddChangeIdToCommitMessage(options, |
| 2505 | args)) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2506 | ref_to_push = 'HEAD' |
| 2507 | parent = '%s/%s' % (gerrit_remote, branch) |
| 2508 | change_id = git_footers.get_footer_change_id(change_desc.description)[0] |
| 2509 | |
| 2510 | assert change_desc |
| 2511 | commits = RunGitSilent(['rev-list', '%s..%s' % (parent, |
| 2512 | ref_to_push)]).splitlines() |
| 2513 | if len(commits) > 1: |
| 2514 | print('WARNING: This will upload %d commits. Run the following command ' |
| 2515 | 'to see which commits will be uploaded: ' % len(commits)) |
| 2516 | print('git log %s..%s' % (parent, ref_to_push)) |
| 2517 | print('You can also use `git squash-branch` to squash these into a ' |
| 2518 | 'single commit.') |
| 2519 | ask_for_data('About to upload; enter to confirm.') |
| 2520 | |
| 2521 | if options.reviewers or options.tbr_owners: |
| 2522 | change_desc.update_reviewers(options.reviewers, options.tbr_owners, |
| 2523 | change) |
| 2524 | |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 2525 | # Extra options that can be specified at push time. Doc: |
| 2526 | # https://gerrit-review.googlesource.com/Documentation/user-upload.html |
| 2527 | refspec_opts = [] |
| 2528 | if options.title: |
| 2529 | # Per doc, spaces must be converted to underscores, and Gerrit will do the |
| 2530 | # reverse on its side. |
| 2531 | if '_' in options.title: |
| 2532 | print('WARNING: underscores in title will be converted to spaces.') |
| 2533 | refspec_opts.append('m=' + options.title.replace(' ', '_')) |
| 2534 | |
tandrii@chromium.org | 8da4540 | 2016-05-24 23:11:03 +0000 | [diff] [blame] | 2535 | if options.send_mail: |
| 2536 | if not change_desc.get_reviewers(): |
| 2537 | DieWithError('Must specify reviewers to send email.') |
| 2538 | refspec_opts.append('notify=ALL') |
| 2539 | else: |
| 2540 | refspec_opts.append('notify=NONE') |
| 2541 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2542 | cc = self.GetCCList().split(',') |
| 2543 | if options.cc: |
| 2544 | cc.extend(options.cc) |
| 2545 | cc = filter(None, cc) |
| 2546 | if cc: |
tandrii@chromium.org | 074c2af | 2016-06-03 23:18:40 +0000 | [diff] [blame] | 2547 | refspec_opts.extend('cc=' + email.strip() for email in cc) |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2548 | |
tandrii@chromium.org | 8acd833 | 2016-04-13 12:56:03 +0000 | [diff] [blame] | 2549 | if change_desc.get_reviewers(): |
| 2550 | refspec_opts.extend('r=' + email.strip() |
| 2551 | for email in change_desc.get_reviewers()) |
| 2552 | |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 2553 | refspec_suffix = '' |
| 2554 | if refspec_opts: |
| 2555 | refspec_suffix = '%' + ','.join(refspec_opts) |
| 2556 | assert ' ' not in refspec_suffix, ( |
| 2557 | 'spaces not allowed in refspec: "%s"' % refspec_suffix) |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 2558 | refspec = '%s:refs/for/%s%s' % (ref_to_push, branch, refspec_suffix) |
tandrii@chromium.org | bf766ba | 2016-04-13 12:51:23 +0000 | [diff] [blame] | 2559 | |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2560 | push_stdout = gclient_utils.CheckCallAndFilter( |
tandrii@chromium.org | 8acd833 | 2016-04-13 12:56:03 +0000 | [diff] [blame] | 2561 | ['git', 'push', gerrit_remote, refspec], |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2562 | print_stdout=True, |
| 2563 | # Flush after every line: useful for seeing progress when running as |
| 2564 | # recipe. |
| 2565 | filter_fn=lambda _: sys.stdout.flush()) |
| 2566 | |
| 2567 | if options.squash: |
| 2568 | regex = re.compile(r'remote:\s+https?://[\w\-\.\/]*/(\d+)\s.*') |
| 2569 | change_numbers = [m.group(1) |
| 2570 | for m in map(regex.match, push_stdout.splitlines()) |
| 2571 | if m] |
| 2572 | if len(change_numbers) != 1: |
| 2573 | DieWithError( |
| 2574 | ('Created|Updated %d issues on Gerrit, but only 1 expected.\n' |
| 2575 | 'Change-Id: %s') % (len(change_numbers), change_id)) |
| 2576 | self.SetIssue(change_numbers[0]) |
| 2577 | RunGit(['config', 'branch.%s.gerritsquashhash' % self.GetBranch(), |
| 2578 | ref_to_push]) |
| 2579 | return 0 |
| 2580 | |
tandrii@chromium.org | 8930b3d | 2016-04-13 14:47:02 +0000 | [diff] [blame] | 2581 | def _AddChangeIdToCommitMessage(self, options, args): |
| 2582 | """Re-commits using the current message, assumes the commit hook is in |
| 2583 | place. |
| 2584 | """ |
| 2585 | log_desc = options.message or CreateDescriptionFromLog(args) |
| 2586 | git_command = ['commit', '--amend', '-m', log_desc] |
| 2587 | RunGit(git_command) |
| 2588 | new_log_desc = CreateDescriptionFromLog(args) |
| 2589 | if git_footers.get_footer_change_id(new_log_desc): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 2590 | print('git-cl: Added Change-Id to commit message.') |
tandrii@chromium.org | 8930b3d | 2016-04-13 14:47:02 +0000 | [diff] [blame] | 2591 | return new_log_desc |
| 2592 | else: |
tandrii@chromium.org | b067ec5 | 2016-05-31 15:24:44 +0000 | [diff] [blame] | 2593 | DieWithError('ERROR: Gerrit commit-msg hook not installed.') |
tandrii@chromium.org | aa6235b | 2016-04-11 21:35:29 +0000 | [diff] [blame] | 2594 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2595 | def SetCQState(self, new_state): |
| 2596 | """Sets the Commit-Queue label assuming canonical CQ config for Gerrit.""" |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 2597 | vote_map = { |
| 2598 | _CQState.NONE: 0, |
| 2599 | _CQState.DRY_RUN: 1, |
| 2600 | _CQState.COMMIT : 2, |
| 2601 | } |
| 2602 | gerrit_util.SetReview(self._GetGerritHost(), self.GetIssue(), |
| 2603 | labels={'Commit-Queue': vote_map[new_state]}) |
| 2604 | |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 2605 | |
| 2606 | _CODEREVIEW_IMPLEMENTATIONS = { |
| 2607 | 'rietveld': _RietveldChangelistImpl, |
| 2608 | 'gerrit': _GerritChangelistImpl, |
| 2609 | } |
| 2610 | |
tandrii@chromium.org | 013a280 | 2016-03-29 09:52:33 +0000 | [diff] [blame] | 2611 | |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 2612 | def _add_codereview_select_options(parser): |
| 2613 | """Appends --gerrit and --rietveld options to force specific codereview.""" |
| 2614 | parser.codereview_group = optparse.OptionGroup( |
| 2615 | parser, 'EXPERIMENTAL! Codereview override options') |
| 2616 | parser.add_option_group(parser.codereview_group) |
| 2617 | parser.codereview_group.add_option( |
| 2618 | '--gerrit', action='store_true', |
| 2619 | help='Force the use of Gerrit for codereview') |
| 2620 | parser.codereview_group.add_option( |
| 2621 | '--rietveld', action='store_true', |
| 2622 | help='Force the use of Rietveld for codereview') |
| 2623 | |
| 2624 | |
| 2625 | def _process_codereview_select_options(parser, options): |
| 2626 | if options.gerrit and options.rietveld: |
| 2627 | parser.error('Options --gerrit and --rietveld are mutually exclusive') |
| 2628 | options.forced_codereview = None |
| 2629 | if options.gerrit: |
| 2630 | options.forced_codereview = 'gerrit' |
| 2631 | elif options.rietveld: |
| 2632 | options.forced_codereview = 'rietveld' |
| 2633 | |
| 2634 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 2635 | def _get_bug_line_values(default_project, bugs): |
| 2636 | """Given default_project and comma separated list of bugs, yields bug line |
| 2637 | values. |
| 2638 | |
| 2639 | Each bug can be either: |
| 2640 | * a number, which is combined with default_project |
| 2641 | * string, which is left as is. |
| 2642 | |
| 2643 | This function may produce more than one line, because bugdroid expects one |
| 2644 | project per line. |
| 2645 | |
| 2646 | >>> list(_get_bug_line_values('v8', '123,chromium:789')) |
| 2647 | ['v8:123', 'chromium:789'] |
| 2648 | """ |
| 2649 | default_bugs = [] |
| 2650 | others = [] |
| 2651 | for bug in bugs.split(','): |
| 2652 | bug = bug.strip() |
| 2653 | if bug: |
| 2654 | try: |
| 2655 | default_bugs.append(int(bug)) |
| 2656 | except ValueError: |
| 2657 | others.append(bug) |
| 2658 | |
| 2659 | if default_bugs: |
| 2660 | default_bugs = ','.join(map(str, default_bugs)) |
| 2661 | if default_project: |
| 2662 | yield '%s:%s' % (default_project, default_bugs) |
| 2663 | else: |
| 2664 | yield default_bugs |
| 2665 | for other in sorted(others): |
| 2666 | # Don't bother finding common prefixes, CLs with >2 bugs are very very rare. |
| 2667 | yield other |
| 2668 | |
| 2669 | |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 2670 | class ChangeDescription(object): |
| 2671 | """Contains a parsed form of the change description.""" |
maruel@chromium.org | c6f60e8 | 2013-04-19 17:01:57 +0000 | [diff] [blame] | 2672 | R_LINE = r'^[ \t]*(TBR|R)[ \t]*=[ \t]*(.*?)[ \t]*$' |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2673 | BUG_LINE = r'^[ \t]*(BUG)[ \t]*=[ \t]*(.*?)[ \t]*$' |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 2674 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2675 | def __init__(self, description): |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2676 | self._description_lines = (description or '').strip().splitlines() |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2677 | |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2678 | @property # www.logilab.org/ticket/89786 |
| 2679 | def description(self): # pylint: disable=E0202 |
| 2680 | return '\n'.join(self._description_lines) |
| 2681 | |
| 2682 | def set_description(self, desc): |
| 2683 | if isinstance(desc, basestring): |
| 2684 | lines = desc.splitlines() |
| 2685 | else: |
| 2686 | lines = [line.rstrip() for line in desc] |
| 2687 | while lines and not lines[0]: |
| 2688 | lines.pop(0) |
| 2689 | while lines and not lines[-1]: |
| 2690 | lines.pop(-1) |
| 2691 | self._description_lines = lines |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2692 | |
piman@chromium.org | 336f912 | 2014-09-04 02:16:55 +0000 | [diff] [blame] | 2693 | def update_reviewers(self, reviewers, add_owners_tbr=False, change=None): |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2694 | """Rewrites the R=/TBR= line(s) as a single line each.""" |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2695 | assert isinstance(reviewers, list), reviewers |
piman@chromium.org | 336f912 | 2014-09-04 02:16:55 +0000 | [diff] [blame] | 2696 | if not reviewers and not add_owners_tbr: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2697 | return |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2698 | reviewers = reviewers[:] |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2699 | |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2700 | # Get the set of R= and TBR= lines and remove them from the desciption. |
| 2701 | regexp = re.compile(self.R_LINE) |
| 2702 | matches = [regexp.match(line) for line in self._description_lines] |
| 2703 | new_desc = [l for i, l in enumerate(self._description_lines) |
| 2704 | if not matches[i]] |
| 2705 | self.set_description(new_desc) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2706 | |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2707 | # Construct new unified R= and TBR= lines. |
| 2708 | r_names = [] |
| 2709 | tbr_names = [] |
| 2710 | for match in matches: |
| 2711 | if not match: |
| 2712 | continue |
| 2713 | people = cleanup_list([match.group(2).strip()]) |
| 2714 | if match.group(1) == 'TBR': |
| 2715 | tbr_names.extend(people) |
| 2716 | else: |
| 2717 | r_names.extend(people) |
| 2718 | for name in r_names: |
| 2719 | if name not in reviewers: |
| 2720 | reviewers.append(name) |
piman@chromium.org | 336f912 | 2014-09-04 02:16:55 +0000 | [diff] [blame] | 2721 | if add_owners_tbr: |
| 2722 | owners_db = owners.Database(change.RepositoryRoot(), |
dtu | 944b605 | 2016-07-14 14:48:21 -0700 | [diff] [blame] | 2723 | fopen=file, os_path=os.path) |
piman@chromium.org | 336f912 | 2014-09-04 02:16:55 +0000 | [diff] [blame] | 2724 | all_reviewers = set(tbr_names + reviewers) |
| 2725 | missing_files = owners_db.files_not_covered_by(change.LocalPaths(), |
| 2726 | all_reviewers) |
| 2727 | tbr_names.extend(owners_db.reviewers_for(missing_files, |
| 2728 | change.author_email)) |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2729 | new_r_line = 'R=' + ', '.join(reviewers) if reviewers else None |
| 2730 | new_tbr_line = 'TBR=' + ', '.join(tbr_names) if tbr_names else None |
| 2731 | |
| 2732 | # Put the new lines in the description where the old first R= line was. |
| 2733 | line_loc = next((i for i, match in enumerate(matches) if match), -1) |
| 2734 | if 0 <= line_loc < len(self._description_lines): |
| 2735 | if new_tbr_line: |
| 2736 | self._description_lines.insert(line_loc, new_tbr_line) |
| 2737 | if new_r_line: |
| 2738 | self._description_lines.insert(line_loc, new_r_line) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2739 | else: |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2740 | if new_r_line: |
| 2741 | self.append_footer(new_r_line) |
| 2742 | if new_tbr_line: |
| 2743 | self.append_footer(new_tbr_line) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2744 | |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 2745 | def prompt(self, bug=None): |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2746 | """Asks the user to update the description.""" |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2747 | self.set_description([ |
| 2748 | '# Enter a description of the change.', |
| 2749 | '# This will be displayed on the codereview site.', |
| 2750 | '# The first line will also be used as the subject of the review.', |
alancutter@chromium.org | bd1073e | 2013-06-01 00:34:38 +0000 | [diff] [blame] | 2751 | '#--------------------This line is 72 characters long' |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2752 | '--------------------', |
| 2753 | ] + self._description_lines) |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2754 | |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2755 | regexp = re.compile(self.BUG_LINE) |
| 2756 | if not any((regexp.match(line) for line in self._description_lines)): |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 2757 | prefix = settings.GetBugPrefix() |
| 2758 | values = list(_get_bug_line_values(prefix, bug or '')) or [prefix] |
| 2759 | for value in values: |
| 2760 | # TODO(tandrii): change this to 'Bug: xxx' to be a proper Gerrit footer. |
| 2761 | self.append_footer('BUG=%s' % value) |
| 2762 | |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2763 | content = gclient_utils.RunEditor(self.description, True, |
jbroman@chromium.org | 615a262 | 2013-05-03 13:20:14 +0000 | [diff] [blame] | 2764 | git_editor=settings.GetGitEditor()) |
maruel@chromium.org | 0e0436a | 2011-10-25 13:32:41 +0000 | [diff] [blame] | 2765 | if not content: |
| 2766 | DieWithError('Running editor failed') |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2767 | lines = content.splitlines() |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2768 | |
| 2769 | # Strip off comments. |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2770 | clean_lines = [line.rstrip() for line in lines if not line.startswith('#')] |
| 2771 | if not clean_lines: |
maruel@chromium.org | 0e0436a | 2011-10-25 13:32:41 +0000 | [diff] [blame] | 2772 | DieWithError('No CL description, aborting') |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2773 | self.set_description(clean_lines) |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 2774 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2775 | def append_footer(self, line): |
tandrii@chromium.org | 601e1d1 | 2016-06-03 13:03:54 +0000 | [diff] [blame] | 2776 | """Adds a footer line to the description. |
| 2777 | |
| 2778 | Differentiates legacy "KEY=xxx" footers (used to be called tags) and |
| 2779 | Gerrit's footers in the form of "Footer-Key: footer any value" and ensures |
| 2780 | that Gerrit footers are always at the end. |
| 2781 | """ |
| 2782 | parsed_footer_line = git_footers.parse_footer(line) |
| 2783 | if parsed_footer_line: |
| 2784 | # Line is a gerrit footer in the form: Footer-Key: any value. |
| 2785 | # Thus, must be appended observing Gerrit footer rules. |
| 2786 | self.set_description( |
| 2787 | git_footers.add_footer(self.description, |
| 2788 | key=parsed_footer_line[0], |
| 2789 | value=parsed_footer_line[1])) |
| 2790 | return |
| 2791 | |
| 2792 | if not self._description_lines: |
| 2793 | self._description_lines.append(line) |
| 2794 | return |
| 2795 | |
| 2796 | top_lines, gerrit_footers, _ = git_footers.split_footers(self.description) |
| 2797 | if gerrit_footers: |
| 2798 | # git_footers.split_footers ensures that there is an empty line before |
| 2799 | # actual (gerrit) footers, if any. We have to keep it that way. |
| 2800 | assert top_lines and top_lines[-1] == '' |
| 2801 | top_lines, separator = top_lines[:-1], top_lines[-1:] |
| 2802 | else: |
| 2803 | separator = [] # No need for separator if there are no gerrit_footers. |
| 2804 | |
| 2805 | prev_line = top_lines[-1] if top_lines else '' |
| 2806 | if (not presubmit_support.Change.TAG_LINE_RE.match(prev_line) or |
| 2807 | not presubmit_support.Change.TAG_LINE_RE.match(line)): |
| 2808 | top_lines.append('') |
| 2809 | top_lines.append(line) |
| 2810 | self._description_lines = top_lines + separator + gerrit_footers |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 2811 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2812 | def get_reviewers(self): |
| 2813 | """Retrieves the list of reviewers.""" |
agable@chromium.org | 42c2079 | 2013-09-12 17:34:49 +0000 | [diff] [blame] | 2814 | matches = [re.match(self.R_LINE, line) for line in self._description_lines] |
| 2815 | reviewers = [match.group(2).strip() for match in matches if match] |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 2816 | return cleanup_list(reviewers) |
dpranke@chromium.org | 20254fc | 2011-03-22 18:28:59 +0000 | [diff] [blame] | 2817 | |
| 2818 | |
maruel@chromium.org | e52678e | 2013-04-26 18:34:44 +0000 | [diff] [blame] | 2819 | def get_approving_reviewers(props): |
| 2820 | """Retrieves the reviewers that approved a CL from the issue properties with |
| 2821 | messages. |
| 2822 | |
| 2823 | Note that the list may contain reviewers that are not committer, thus are not |
| 2824 | considered by the CQ. |
| 2825 | """ |
| 2826 | return sorted( |
| 2827 | set( |
| 2828 | message['sender'] |
| 2829 | for message in props['messages'] |
| 2830 | if message['approval'] and message['sender'] in props['reviewers'] |
| 2831 | ) |
| 2832 | ) |
| 2833 | |
| 2834 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2835 | def FindCodereviewSettingsFile(filename='codereview.settings'): |
| 2836 | """Finds the given file starting in the cwd and going up. |
| 2837 | |
| 2838 | Only looks up to the top of the repository unless an |
| 2839 | 'inherit-review-settings-ok' file exists in the root of the repository. |
| 2840 | """ |
| 2841 | inherit_ok_file = 'inherit-review-settings-ok' |
| 2842 | cwd = os.getcwd() |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 2843 | root = settings.GetRoot() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2844 | if os.path.isfile(os.path.join(root, inherit_ok_file)): |
| 2845 | root = '/' |
| 2846 | while True: |
| 2847 | if filename in os.listdir(cwd): |
| 2848 | if os.path.isfile(os.path.join(cwd, filename)): |
| 2849 | return open(os.path.join(cwd, filename)) |
| 2850 | if cwd == root: |
| 2851 | break |
| 2852 | cwd = os.path.dirname(cwd) |
| 2853 | |
| 2854 | |
| 2855 | def LoadCodereviewSettingsFromFile(fileobj): |
| 2856 | """Parse a codereview.settings file and updates hooks.""" |
maruel@chromium.org | 99ac1c5 | 2012-01-16 14:52:12 +0000 | [diff] [blame] | 2857 | keyvals = gclient_utils.ParseCodereviewSettingsContent(fileobj.read()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2858 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2859 | def SetProperty(name, setting, unset_error_ok=False): |
| 2860 | fullname = 'rietveld.' + name |
| 2861 | if setting in keyvals: |
| 2862 | RunGit(['config', fullname, keyvals[setting]]) |
| 2863 | else: |
| 2864 | RunGit(['config', '--unset-all', fullname], error_ok=unset_error_ok) |
| 2865 | |
| 2866 | SetProperty('server', 'CODE_REVIEW_SERVER') |
| 2867 | # Only server setting is required. Other settings can be absent. |
| 2868 | # In that case, we ignore errors raised during option deletion attempt. |
| 2869 | SetProperty('cc', 'CC_LIST', unset_error_ok=True) |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 2870 | SetProperty('private', 'PRIVATE', unset_error_ok=True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2871 | SetProperty('tree-status-url', 'STATUS', unset_error_ok=True) |
| 2872 | SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True) |
rmistry@google.com | 9075258 | 2014-01-14 21:04:50 +0000 | [diff] [blame] | 2873 | SetProperty('bug-prefix', 'BUG_PREFIX', unset_error_ok=True) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 2874 | SetProperty('cpplint-regex', 'LINT_REGEX', unset_error_ok=True) |
kjellander@chromium.org | 6abc652 | 2014-12-02 07:34:49 +0000 | [diff] [blame] | 2875 | SetProperty('force-https-commit-url', 'FORCE_HTTPS_COMMIT_URL', |
| 2876 | unset_error_ok=True) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 2877 | SetProperty('cpplint-ignore-regex', 'LINT_IGNORE_REGEX', unset_error_ok=True) |
sheyang@chromium.org | 152cf83 | 2014-06-11 21:37:49 +0000 | [diff] [blame] | 2878 | SetProperty('project', 'PROJECT', unset_error_ok=True) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 2879 | SetProperty('pending-ref-prefix', 'PENDING_REF_PREFIX', unset_error_ok=True) |
rmistry@google.com | 5626a92 | 2015-02-26 14:03:30 +0000 | [diff] [blame] | 2880 | SetProperty('run-post-upload-hook', 'RUN_POST_UPLOAD_HOOK', |
| 2881 | unset_error_ok=True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2882 | |
ukai@chromium.org | 7044efc | 2013-11-28 01:51:21 +0000 | [diff] [blame] | 2883 | if 'GERRIT_HOST' in keyvals: |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 2884 | RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']]) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 2885 | |
bauerb@chromium.org | 54b400c | 2016-01-14 10:08:25 +0000 | [diff] [blame] | 2886 | if 'GERRIT_SQUASH_UPLOADS' in keyvals: |
tandrii | 8dd81ea | 2016-06-16 13:24:23 -0700 | [diff] [blame] | 2887 | RunGit(['config', 'gerrit.squash-uploads', |
| 2888 | keyvals['GERRIT_SQUASH_UPLOADS']]) |
bauerb@chromium.org | 54b400c | 2016-01-14 10:08:25 +0000 | [diff] [blame] | 2889 | |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2890 | if 'GERRIT_SKIP_ENSURE_AUTHENTICATED' in keyvals: |
shinyak@chromium.org | 00dbccd | 2016-04-15 07:24:43 +0000 | [diff] [blame] | 2891 | RunGit(['config', 'gerrit.skip-ensure-authenticated', |
tandrii@chromium.org | 2825353 | 2016-04-14 13:46:56 +0000 | [diff] [blame] | 2892 | keyvals['GERRIT_SKIP_ENSURE_AUTHENTICATED']]) |
| 2893 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2894 | if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals: |
| 2895 | #should be of the form |
| 2896 | #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof |
| 2897 | #ORIGIN_URL_CONFIG: http://src.chromium.org/git |
| 2898 | RunGit(['config', keyvals['PUSH_URL_CONFIG'], |
| 2899 | keyvals['ORIGIN_URL_CONFIG']]) |
| 2900 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2901 | |
joshua.lock@intel.com | 426f69b | 2012-08-02 23:41:49 +0000 | [diff] [blame] | 2902 | def urlretrieve(source, destination): |
| 2903 | """urllib is broken for SSL connections via a proxy therefore we |
| 2904 | can't use urllib.urlretrieve().""" |
| 2905 | with open(destination, 'w') as f: |
| 2906 | f.write(urllib2.urlopen(source).read()) |
| 2907 | |
| 2908 | |
ukai@chromium.org | 712d610 | 2013-11-27 00:52:58 +0000 | [diff] [blame] | 2909 | def hasSheBang(fname): |
| 2910 | """Checks fname is a #! script.""" |
| 2911 | with open(fname) as f: |
| 2912 | return f.read(2).startswith('#!') |
| 2913 | |
| 2914 | |
bpastene@chromium.org | 917f0ff | 2016-04-05 00:45:30 +0000 | [diff] [blame] | 2915 | # TODO(bpastene) Remove once a cleaner fix to crbug.com/600473 presents itself. |
| 2916 | def DownloadHooks(*args, **kwargs): |
| 2917 | pass |
| 2918 | |
| 2919 | |
tandrii@chromium.org | 18630d6 | 2016-03-04 12:06:02 +0000 | [diff] [blame] | 2920 | def DownloadGerritHook(force): |
| 2921 | """Download and install Gerrit commit-msg hook. |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 2922 | |
| 2923 | Args: |
| 2924 | force: True to update hooks. False to install hooks if not present. |
| 2925 | """ |
| 2926 | if not settings.GetIsGerrit(): |
| 2927 | return |
ukai@chromium.org | 712d610 | 2013-11-27 00:52:58 +0000 | [diff] [blame] | 2928 | src = 'https://gerrit-review.googlesource.com/tools/hooks/commit-msg' |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 2929 | dst = os.path.join(settings.GetRoot(), '.git', 'hooks', 'commit-msg') |
| 2930 | if not os.access(dst, os.X_OK): |
| 2931 | if os.path.exists(dst): |
| 2932 | if not force: |
| 2933 | return |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 2934 | try: |
joshua.lock@intel.com | 426f69b | 2012-08-02 23:41:49 +0000 | [diff] [blame] | 2935 | urlretrieve(src, dst) |
ukai@chromium.org | 712d610 | 2013-11-27 00:52:58 +0000 | [diff] [blame] | 2936 | if not hasSheBang(dst): |
| 2937 | DieWithError('Not a script: %s\n' |
| 2938 | 'You need to download from\n%s\n' |
| 2939 | 'into .git/hooks/commit-msg and ' |
| 2940 | 'chmod +x .git/hooks/commit-msg' % (dst, src)) |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 2941 | os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) |
| 2942 | except Exception: |
| 2943 | if os.path.exists(dst): |
| 2944 | os.remove(dst) |
ukai@chromium.org | 712d610 | 2013-11-27 00:52:58 +0000 | [diff] [blame] | 2945 | DieWithError('\nFailed to download hooks.\n' |
| 2946 | 'You need to download from\n%s\n' |
| 2947 | 'into .git/hooks/commit-msg and ' |
| 2948 | 'chmod +x .git/hooks/commit-msg' % src) |
ukai@chromium.org | 78c4b98 | 2012-02-14 02:20:26 +0000 | [diff] [blame] | 2949 | |
| 2950 | |
tandrii@chromium.org | e7d3d16 | 2016-03-15 14:15:57 +0000 | [diff] [blame] | 2951 | |
| 2952 | def GetRietveldCodereviewSettingsInteractively(): |
| 2953 | """Prompt the user for settings.""" |
| 2954 | server = settings.GetDefaultServerUrl(error_ok=True) |
| 2955 | prompt = 'Rietveld server (host[:port])' |
| 2956 | prompt += ' [%s]' % (server or DEFAULT_SERVER) |
| 2957 | newserver = ask_for_data(prompt + ':') |
| 2958 | if not server and not newserver: |
| 2959 | newserver = DEFAULT_SERVER |
| 2960 | if newserver: |
| 2961 | newserver = gclient_utils.UpgradeToHttps(newserver) |
| 2962 | if newserver != server: |
| 2963 | RunGit(['config', 'rietveld.server', newserver]) |
| 2964 | |
| 2965 | def SetProperty(initial, caption, name, is_url): |
| 2966 | prompt = caption |
| 2967 | if initial: |
| 2968 | prompt += ' ("x" to clear) [%s]' % initial |
| 2969 | new_val = ask_for_data(prompt + ':') |
| 2970 | if new_val == 'x': |
| 2971 | RunGit(['config', '--unset-all', 'rietveld.' + name], error_ok=True) |
| 2972 | elif new_val: |
| 2973 | if is_url: |
| 2974 | new_val = gclient_utils.UpgradeToHttps(new_val) |
| 2975 | if new_val != initial: |
| 2976 | RunGit(['config', 'rietveld.' + name, new_val]) |
| 2977 | |
| 2978 | SetProperty(settings.GetDefaultCCList(), 'CC list', 'cc', False) |
| 2979 | SetProperty(settings.GetDefaultPrivateFlag(), |
| 2980 | 'Private flag (rietveld only)', 'private', False) |
| 2981 | SetProperty(settings.GetTreeStatusUrl(error_ok=True), 'Tree status URL', |
| 2982 | 'tree-status-url', False) |
| 2983 | SetProperty(settings.GetViewVCUrl(), 'ViewVC URL', 'viewvc-url', True) |
| 2984 | SetProperty(settings.GetBugPrefix(), 'Bug Prefix', 'bug-prefix', False) |
| 2985 | SetProperty(settings.GetRunPostUploadHook(), 'Run Post Upload Hook', |
| 2986 | 'run-post-upload-hook', False) |
| 2987 | |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 2988 | @subcommand.usage('[repo root containing codereview.settings]') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2989 | def CMDconfig(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 2990 | """Edits configuration for this tree.""" |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 2991 | |
tandrii@chromium.org | e7d3d16 | 2016-03-15 14:15:57 +0000 | [diff] [blame] | 2992 | print('WARNING: git cl config works for Rietveld only.\n' |
tandrii@chromium.org | 8930b3d | 2016-04-13 14:47:02 +0000 | [diff] [blame] | 2993 | 'For Gerrit, see http://crbug.com/603116.') |
| 2994 | # TODO(tandrii): add Gerrit support as part of http://crbug.com/603116. |
pgervais@chromium.org | 87884cc | 2014-01-03 22:23:41 +0000 | [diff] [blame] | 2995 | parser.add_option('--activate-update', action='store_true', |
| 2996 | help='activate auto-updating [rietveld] section in ' |
| 2997 | '.git/config') |
| 2998 | parser.add_option('--deactivate-update', action='store_true', |
| 2999 | help='deactivate auto-updating [rietveld] section in ' |
| 3000 | '.git/config') |
| 3001 | options, args = parser.parse_args(args) |
| 3002 | |
| 3003 | if options.deactivate_update: |
| 3004 | RunGit(['config', 'rietveld.autoupdate', 'false']) |
| 3005 | return |
| 3006 | |
| 3007 | if options.activate_update: |
| 3008 | RunGit(['config', '--unset', 'rietveld.autoupdate']) |
| 3009 | return |
| 3010 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3011 | if len(args) == 0: |
tandrii@chromium.org | e7d3d16 | 2016-03-15 14:15:57 +0000 | [diff] [blame] | 3012 | GetRietveldCodereviewSettingsInteractively() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3013 | return 0 |
| 3014 | |
| 3015 | url = args[0] |
| 3016 | if not url.endswith('codereview.settings'): |
| 3017 | url = os.path.join(url, 'codereview.settings') |
| 3018 | |
| 3019 | # Load code review settings and download hooks (if available). |
| 3020 | LoadCodereviewSettingsFromFile(urllib2.urlopen(url)) |
| 3021 | return 0 |
| 3022 | |
| 3023 | |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 3024 | def CMDbaseurl(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 3025 | """Gets or sets base-url for this branch.""" |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 3026 | branchref = RunGit(['symbolic-ref', 'HEAD']).strip() |
| 3027 | branch = ShortBranchName(branchref) |
| 3028 | _, args = parser.parse_args(args) |
| 3029 | if not args: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3030 | print('Current base-url:') |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 3031 | return RunGit(['config', 'branch.%s.base-url' % branch], |
| 3032 | error_ok=False).strip() |
| 3033 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3034 | print('Setting base-url to %s' % args[0]) |
kalmard@homejinni.com | 6b0051e | 2012-04-03 15:45:08 +0000 | [diff] [blame] | 3035 | return RunGit(['config', 'branch.%s.base-url' % branch, args[0]], |
| 3036 | error_ok=False).strip() |
| 3037 | |
| 3038 | |
jsbell@chromium.org | b99fbd9 | 2014-09-11 17:29:28 +0000 | [diff] [blame] | 3039 | def color_for_status(status): |
| 3040 | """Maps a Changelist status to color, for CMDstatus and other tools.""" |
| 3041 | return { |
| 3042 | 'unsent': Fore.RED, |
| 3043 | 'waiting': Fore.BLUE, |
| 3044 | 'reply': Fore.YELLOW, |
| 3045 | 'lgtm': Fore.GREEN, |
| 3046 | 'commit': Fore.MAGENTA, |
| 3047 | 'closed': Fore.CYAN, |
| 3048 | 'error': Fore.WHITE, |
| 3049 | }.get(status, Fore.WHITE) |
| 3050 | |
tandrii@chromium.org | 04ea846 | 2016-04-25 19:51:21 +0000 | [diff] [blame] | 3051 | |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3052 | def get_cl_statuses(changes, fine_grained, max_processes=None): |
| 3053 | """Returns a blocking iterable of (cl, status) for given branches. |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3054 | |
| 3055 | If fine_grained is true, this will fetch CL statuses from the server. |
| 3056 | Otherwise, simply indicate if there's a matching url for the given branches. |
| 3057 | |
| 3058 | If max_processes is specified, it is used as the maximum number of processes |
| 3059 | to spawn to fetch CL status from the server. Otherwise 1 process per branch is |
| 3060 | spawned. |
calamity@chromium.org | cf19748 | 2016-04-29 20:15:53 +0000 | [diff] [blame] | 3061 | |
| 3062 | See GetStatus() for a list of possible statuses. |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3063 | """ |
| 3064 | # Silence upload.py otherwise it becomes unwieldly. |
| 3065 | upload.verbosity = 0 |
| 3066 | |
| 3067 | if fine_grained: |
| 3068 | # Process one branch synchronously to work through authentication, then |
| 3069 | # spawn processes to process all the other branches in parallel. |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3070 | if changes: |
| 3071 | fetch = lambda cl: (cl, cl.GetStatus()) |
| 3072 | yield fetch(changes[0]) |
calamity@chromium.org | cf19748 | 2016-04-29 20:15:53 +0000 | [diff] [blame] | 3073 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3074 | if not changes: |
| 3075 | # Exit early if there was only one branch to fetch. |
| 3076 | return |
| 3077 | |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3078 | changes_to_fetch = changes[1:] |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3079 | pool = ThreadPool( |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3080 | min(max_processes, len(changes_to_fetch)) |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3081 | if max_processes is not None |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3082 | else len(changes_to_fetch)) |
calamity@chromium.org | cf19748 | 2016-04-29 20:15:53 +0000 | [diff] [blame] | 3083 | |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3084 | fetched_cls = set() |
| 3085 | it = pool.imap_unordered(fetch, changes_to_fetch).__iter__() |
calamity@chromium.org | cf19748 | 2016-04-29 20:15:53 +0000 | [diff] [blame] | 3086 | while True: |
| 3087 | try: |
| 3088 | row = it.next(timeout=5) |
| 3089 | except multiprocessing.TimeoutError: |
| 3090 | break |
| 3091 | |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3092 | fetched_cls.add(row[0]) |
calamity@chromium.org | cf19748 | 2016-04-29 20:15:53 +0000 | [diff] [blame] | 3093 | yield row |
| 3094 | |
| 3095 | # Add any branches that failed to fetch. |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3096 | for cl in set(changes_to_fetch) - fetched_cls: |
| 3097 | yield (cl, 'error') |
calamity@chromium.org | cf19748 | 2016-04-29 20:15:53 +0000 | [diff] [blame] | 3098 | |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3099 | else: |
| 3100 | # Do not use GetApprovingReviewers(), since it requires an HTTP request. |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3101 | for cl in changes: |
| 3102 | yield (cl, 'waiting' if cl.GetIssueURL() else 'error') |
jsbell@chromium.org | b99fbd9 | 2014-09-11 17:29:28 +0000 | [diff] [blame] | 3103 | |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3104 | |
| 3105 | def upload_branch_deps(cl, args): |
| 3106 | """Uploads CLs of local branches that are dependents of the current branch. |
| 3107 | |
| 3108 | If the local branch dependency tree looks like: |
| 3109 | test1 -> test2.1 -> test3.1 |
| 3110 | -> test3.2 |
| 3111 | -> test2.2 -> test3.3 |
| 3112 | |
| 3113 | and you run "git cl upload --dependencies" from test1 then "git cl upload" is |
| 3114 | run on the dependent branches in this order: |
| 3115 | test2.1, test3.1, test3.2, test2.2, test3.3 |
| 3116 | |
| 3117 | Note: This function does not rebase your local dependent branches. Use it when |
| 3118 | you make a change to the parent branch that will not conflict with its |
| 3119 | dependent branches, and you would like their dependencies updated in |
| 3120 | Rietveld. |
| 3121 | """ |
| 3122 | if git_common.is_dirty_git_tree('upload-branch-deps'): |
| 3123 | return 1 |
| 3124 | |
| 3125 | root_branch = cl.GetBranch() |
| 3126 | if root_branch is None: |
| 3127 | DieWithError('Can\'t find dependent branches from detached HEAD state. ' |
| 3128 | 'Get on a branch!') |
| 3129 | if not cl.GetIssue() or not cl.GetPatchset(): |
| 3130 | DieWithError('Current branch does not have an uploaded CL. We cannot set ' |
| 3131 | 'patchset dependencies without an uploaded CL.') |
| 3132 | |
| 3133 | branches = RunGit(['for-each-ref', |
| 3134 | '--format=%(refname:short) %(upstream:short)', |
| 3135 | 'refs/heads']) |
| 3136 | if not branches: |
| 3137 | print('No local branches found.') |
| 3138 | return 0 |
| 3139 | |
| 3140 | # Create a dictionary of all local branches to the branches that are dependent |
| 3141 | # on it. |
| 3142 | tracked_to_dependents = collections.defaultdict(list) |
| 3143 | for b in branches.splitlines(): |
| 3144 | tokens = b.split() |
| 3145 | if len(tokens) == 2: |
| 3146 | branch_name, tracked = tokens |
| 3147 | tracked_to_dependents[tracked].append(branch_name) |
| 3148 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3149 | print() |
| 3150 | print('The dependent local branches of %s are:' % root_branch) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3151 | dependents = [] |
| 3152 | def traverse_dependents_preorder(branch, padding=''): |
| 3153 | dependents_to_process = tracked_to_dependents.get(branch, []) |
| 3154 | padding += ' ' |
| 3155 | for dependent in dependents_to_process: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3156 | print('%s%s' % (padding, dependent)) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3157 | dependents.append(dependent) |
| 3158 | traverse_dependents_preorder(dependent, padding) |
| 3159 | traverse_dependents_preorder(root_branch) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3160 | print() |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3161 | |
| 3162 | if not dependents: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3163 | print('There are no dependent local branches for %s' % root_branch) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3164 | return 0 |
| 3165 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3166 | print('This command will checkout all dependent branches and run ' |
| 3167 | '"git cl upload".') |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3168 | ask_for_data('[Press enter to continue or ctrl-C to quit]') |
| 3169 | |
andybons@chromium.org | 962f946 | 2016-02-03 20:00:42 +0000 | [diff] [blame] | 3170 | # Add a default patchset title to all upload calls in Rietveld. |
tandrii@chromium.org | 4c72b08 | 2016-03-31 22:26:35 +0000 | [diff] [blame] | 3171 | if not cl.IsGerrit(): |
andybons@chromium.org | 962f946 | 2016-02-03 20:00:42 +0000 | [diff] [blame] | 3172 | args.extend(['-t', 'Updated patchset dependency']) |
| 3173 | |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3174 | # Record all dependents that failed to upload. |
| 3175 | failures = {} |
| 3176 | # Go through all dependents, checkout the branch and upload. |
| 3177 | try: |
| 3178 | for dependent_branch in dependents: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3179 | print() |
| 3180 | print('--------------------------------------') |
| 3181 | print('Running "git cl upload" from %s:' % dependent_branch) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3182 | RunGit(['checkout', '-q', dependent_branch]) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3183 | print() |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3184 | try: |
| 3185 | if CMDupload(OptionParser(), args) != 0: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3186 | print('Upload failed for %s!' % dependent_branch) |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3187 | failures[dependent_branch] = 1 |
| 3188 | except: # pylint: disable=W0702 |
| 3189 | failures[dependent_branch] = 1 |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3190 | print() |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3191 | finally: |
| 3192 | # Swap back to the original root branch. |
| 3193 | RunGit(['checkout', '-q', root_branch]) |
| 3194 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3195 | print() |
| 3196 | print('Upload complete for dependent branches!') |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3197 | for dependent_branch in dependents: |
| 3198 | upload_status = 'failed' if failures.get(dependent_branch) else 'succeeded' |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3199 | print(' %s : %s' % (dependent_branch, upload_status)) |
| 3200 | print() |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3201 | |
| 3202 | return 0 |
| 3203 | |
| 3204 | |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3205 | def CMDarchive(parser, args): |
| 3206 | """Archives and deletes branches associated with closed changelists.""" |
| 3207 | parser.add_option( |
| 3208 | '-j', '--maxjobs', action='store', type=int, |
| 3209 | help='The maximum number of jobs to use when retrieving review status') |
| 3210 | parser.add_option( |
| 3211 | '-f', '--force', action='store_true', |
| 3212 | help='Bypasses the confirmation prompt.') |
| 3213 | |
| 3214 | auth.add_auth_options(parser) |
| 3215 | options, args = parser.parse_args(args) |
| 3216 | if args: |
| 3217 | parser.error('Unsupported args: %s' % ' '.join(args)) |
| 3218 | auth_config = auth.extract_auth_config_from_options(options) |
| 3219 | |
| 3220 | branches = RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads']) |
| 3221 | if not branches: |
| 3222 | return 0 |
| 3223 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3224 | print('Finding all branches associated with closed issues...') |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3225 | changes = [Changelist(branchref=b, auth_config=auth_config) |
| 3226 | for b in branches.splitlines()] |
| 3227 | alignment = max(5, max(len(c.GetBranch()) for c in changes)) |
| 3228 | statuses = get_cl_statuses(changes, |
| 3229 | fine_grained=True, |
| 3230 | max_processes=options.maxjobs) |
| 3231 | proposal = [(cl.GetBranch(), |
| 3232 | 'git-cl-archived-%s-%s' % (cl.GetIssue(), cl.GetBranch())) |
| 3233 | for cl, status in statuses |
| 3234 | if status == 'closed'] |
| 3235 | proposal.sort() |
| 3236 | |
| 3237 | if not proposal: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3238 | print('No branches with closed codereview issues found.') |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3239 | return 0 |
| 3240 | |
| 3241 | current_branch = GetCurrentBranch() |
| 3242 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3243 | print('\nBranches with closed issues that will be archived:\n') |
| 3244 | print('%*s | %s' % (alignment, 'Branch name', 'Archival tag name')) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3245 | for next_item in proposal: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3246 | print('%*s %s' % (alignment, next_item[0], next_item[1])) |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3247 | |
| 3248 | if any(branch == current_branch for branch, _ in proposal): |
| 3249 | print('You are currently on a branch \'%s\' which is associated with a ' |
| 3250 | 'closed codereview issue, so archive cannot proceed. Please ' |
| 3251 | 'checkout another branch and run this command again.' % |
| 3252 | current_branch) |
| 3253 | return 1 |
| 3254 | |
| 3255 | if not options.force: |
sergiyb | 4a5ecbe | 2016-06-20 09:46:00 -0700 | [diff] [blame] | 3256 | answer = ask_for_data('\nProceed with deletion (Y/n)? ').lower() |
| 3257 | if answer not in ('y', ''): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3258 | print('Aborted.') |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3259 | return 1 |
| 3260 | |
| 3261 | for branch, tagname in proposal: |
| 3262 | RunGit(['tag', tagname, branch]) |
| 3263 | RunGit(['branch', '-D', branch]) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3264 | print('\nJob\'s done!') |
kmarshall | 3bff56b | 2016-06-06 18:31:47 -0700 | [diff] [blame] | 3265 | |
| 3266 | return 0 |
| 3267 | |
| 3268 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3269 | def CMDstatus(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 3270 | """Show status of changelists. |
| 3271 | |
| 3272 | Colors are used to tell the state of the CL unless --fast is used: |
jsbell@chromium.org | aeab41a | 2013-12-10 20:01:22 +0000 | [diff] [blame] | 3273 | - Red not sent for review or broken |
| 3274 | - Blue waiting for review |
| 3275 | - Yellow waiting for you to reply to review |
| 3276 | - Green LGTM'ed |
| 3277 | - Magenta in the commit queue |
| 3278 | - Cyan was committed, branch can be deleted |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 3279 | |
| 3280 | Also see 'git cl comments'. |
| 3281 | """ |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3282 | parser.add_option('--field', |
| 3283 | help='print only specific field (desc|id|patch|url)') |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 3284 | parser.add_option('-f', '--fast', action='store_true', |
| 3285 | help='Do not retrieve review status') |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3286 | parser.add_option( |
| 3287 | '-j', '--maxjobs', action='store', type=int, |
| 3288 | help='The maximum number of jobs to use when retrieving review status') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3289 | |
| 3290 | auth.add_auth_options(parser) |
| 3291 | options, args = parser.parse_args(args) |
maruel@chromium.org | 39c0b22 | 2013-08-17 16:57:01 +0000 | [diff] [blame] | 3292 | if args: |
| 3293 | parser.error('Unsupported args: %s' % args) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3294 | auth_config = auth.extract_auth_config_from_options(options) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3295 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3296 | if options.field: |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3297 | cl = Changelist(auth_config=auth_config) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3298 | if options.field.startswith('desc'): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3299 | print(cl.GetDescription()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3300 | elif options.field == 'id': |
| 3301 | issueid = cl.GetIssue() |
| 3302 | if issueid: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3303 | print(issueid) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3304 | elif options.field == 'patch': |
| 3305 | patchset = cl.GetPatchset() |
| 3306 | if patchset: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3307 | print(patchset) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3308 | elif options.field == 'url': |
| 3309 | url = cl.GetIssueURL() |
| 3310 | if url: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3311 | print(url) |
maruel@chromium.org | e25c75b | 2013-07-23 18:30:56 +0000 | [diff] [blame] | 3312 | return 0 |
| 3313 | |
| 3314 | branches = RunGit(['for-each-ref', '--format=%(refname)', 'refs/heads']) |
| 3315 | if not branches: |
| 3316 | print('No local branch found.') |
| 3317 | return 0 |
| 3318 | |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3319 | changes = [ |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3320 | Changelist(branchref=b, auth_config=auth_config) |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3321 | for b in branches.splitlines()] |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3322 | print('Branches associated with reviews:') |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3323 | output = get_cl_statuses(changes, |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3324 | fine_grained=not options.fast, |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3325 | max_processes=options.maxjobs) |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 3326 | |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3327 | branch_statuses = {} |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3328 | alignment = max(5, max(len(ShortBranchName(c.GetBranch())) for c in changes)) |
| 3329 | for cl in sorted(changes, key=lambda c: c.GetBranch()): |
| 3330 | branch = cl.GetBranch() |
calamity@chromium.org | ffde55c | 2015-03-12 00:44:17 +0000 | [diff] [blame] | 3331 | while branch not in branch_statuses: |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3332 | c, status = output.next() |
| 3333 | branch_statuses[c.GetBranch()] = status |
| 3334 | status = branch_statuses.pop(branch) |
| 3335 | url = cl.GetIssueURL() |
| 3336 | if url and (not status or status == 'error'): |
| 3337 | # The issue probably doesn't exist anymore. |
| 3338 | url += ' (broken)' |
| 3339 | |
nodir@chromium.org | a6de1f4 | 2015-06-10 04:23:17 +0000 | [diff] [blame] | 3340 | color = color_for_status(status) |
maruel@chromium.org | 885f651 | 2013-07-27 02:17:26 +0000 | [diff] [blame] | 3341 | reset = Fore.RESET |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 3342 | if not setup_color.IS_TTY: |
maruel@chromium.org | 885f651 | 2013-07-27 02:17:26 +0000 | [diff] [blame] | 3343 | color = '' |
| 3344 | reset = '' |
nodir@chromium.org | a6de1f4 | 2015-06-10 04:23:17 +0000 | [diff] [blame] | 3345 | status_str = '(%s)' % status if status else '' |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3346 | print(' %*s : %s%s %s%s' % ( |
clemensh@chromium.org | cbd7dc3 | 2016-05-31 10:33:50 +0000 | [diff] [blame] | 3347 | alignment, ShortBranchName(branch), color, url, |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3348 | status_str, reset)) |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 3349 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3350 | cl = Changelist(auth_config=auth_config) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3351 | print() |
| 3352 | print('Current branch:',) |
| 3353 | print(cl.GetBranch()) |
dpranke@chromium.org | ee87f58 | 2015-07-31 18:46:25 +0000 | [diff] [blame] | 3354 | if not cl.GetIssue(): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3355 | print('No issue assigned.') |
dpranke@chromium.org | ee87f58 | 2015-07-31 18:46:25 +0000 | [diff] [blame] | 3356 | return 0 |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3357 | print('Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL())) |
maruel@chromium.org | 85616e0 | 2014-07-28 15:37:55 +0000 | [diff] [blame] | 3358 | if not options.fast: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3359 | print('Issue description:') |
| 3360 | print(cl.GetDescription(pretty=True)) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3361 | return 0 |
| 3362 | |
| 3363 | |
maruel@chromium.org | 39c0b22 | 2013-08-17 16:57:01 +0000 | [diff] [blame] | 3364 | def colorize_CMDstatus_doc(): |
| 3365 | """To be called once in main() to add colors to git cl status help.""" |
| 3366 | colors = [i for i in dir(Fore) if i[0].isupper()] |
| 3367 | |
| 3368 | def colorize_line(line): |
| 3369 | for color in colors: |
| 3370 | if color in line.upper(): |
| 3371 | # Extract whitespaces first and the leading '-'. |
| 3372 | indent = len(line) - len(line.lstrip(' ')) + 1 |
| 3373 | return line[:indent] + getattr(Fore, color) + line[indent:] + Fore.RESET |
| 3374 | return line |
| 3375 | |
| 3376 | lines = CMDstatus.__doc__.splitlines() |
| 3377 | CMDstatus.__doc__ = '\n'.join(colorize_line(l) for l in lines) |
| 3378 | |
| 3379 | |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 3380 | @subcommand.usage('[issue_number]') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3381 | def CMDissue(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 3382 | """Sets or displays the current code review issue number. |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3383 | |
| 3384 | Pass issue number 0 to clear the current issue. |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 3385 | """ |
dnj@chromium.org | 406c440 | 2015-03-03 17:22:28 +0000 | [diff] [blame] | 3386 | parser.add_option('-r', '--reverse', action='store_true', |
| 3387 | help='Lookup the branch(es) for the specified issues. If ' |
| 3388 | 'no issues are specified, all branches with mapped ' |
| 3389 | 'issues will be listed.') |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 3390 | _add_codereview_select_options(parser) |
dnj@chromium.org | 406c440 | 2015-03-03 17:22:28 +0000 | [diff] [blame] | 3391 | options, args = parser.parse_args(args) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 3392 | _process_codereview_select_options(parser, options) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3393 | |
dnj@chromium.org | 406c440 | 2015-03-03 17:22:28 +0000 | [diff] [blame] | 3394 | if options.reverse: |
| 3395 | branches = RunGit(['for-each-ref', 'refs/heads', |
| 3396 | '--format=%(refname:short)']).splitlines() |
| 3397 | |
| 3398 | # Reverse issue lookup. |
| 3399 | issue_branch_map = {} |
| 3400 | for branch in branches: |
| 3401 | cl = Changelist(branchref=branch) |
| 3402 | issue_branch_map.setdefault(cl.GetIssue(), []).append(branch) |
| 3403 | if not args: |
| 3404 | args = sorted(issue_branch_map.iterkeys()) |
| 3405 | for issue in args: |
| 3406 | if not issue: |
| 3407 | continue |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3408 | print('Branch for issue number %s: %s' % ( |
| 3409 | issue, ', '.join(issue_branch_map.get(int(issue)) or ('None',)))) |
dnj@chromium.org | 406c440 | 2015-03-03 17:22:28 +0000 | [diff] [blame] | 3410 | else: |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 3411 | cl = Changelist(codereview=options.forced_codereview) |
dnj@chromium.org | 406c440 | 2015-03-03 17:22:28 +0000 | [diff] [blame] | 3412 | if len(args) > 0: |
| 3413 | try: |
| 3414 | issue = int(args[0]) |
| 3415 | except ValueError: |
| 3416 | DieWithError('Pass a number to set the issue or none to list it.\n' |
tandrii@chromium.org | 8930b3d | 2016-04-13 14:47:02 +0000 | [diff] [blame] | 3417 | 'Maybe you want to run git cl status?') |
dnj@chromium.org | 406c440 | 2015-03-03 17:22:28 +0000 | [diff] [blame] | 3418 | cl.SetIssue(issue) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3419 | print('Issue number: %s (%s)' % (cl.GetIssue(), cl.GetIssueURL())) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3420 | return 0 |
| 3421 | |
| 3422 | |
maruel@chromium.org | 9977a2e | 2012-06-06 22:30:56 +0000 | [diff] [blame] | 3423 | def CMDcomments(parser, args): |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3424 | """Shows or posts review comments for any changelist.""" |
| 3425 | parser.add_option('-a', '--add-comment', dest='comment', |
| 3426 | help='comment to add to an issue') |
| 3427 | parser.add_option('-i', dest='issue', |
| 3428 | help="review issue id (defaults to current issue)") |
smut@google.com | c85ac94 | 2015-09-15 16:34:43 +0000 | [diff] [blame] | 3429 | parser.add_option('-j', '--json-file', |
| 3430 | help='File to write JSON summary to') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3431 | auth.add_auth_options(parser) |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3432 | options, args = parser.parse_args(args) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3433 | auth_config = auth.extract_auth_config_from_options(options) |
maruel@chromium.org | 9977a2e | 2012-06-06 22:30:56 +0000 | [diff] [blame] | 3434 | |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3435 | issue = None |
| 3436 | if options.issue: |
| 3437 | try: |
| 3438 | issue = int(options.issue) |
| 3439 | except ValueError: |
| 3440 | DieWithError('A review issue id is expected to be a number') |
| 3441 | |
tandrii@chromium.org | aa5ced1 | 2016-03-29 09:41:14 +0000 | [diff] [blame] | 3442 | cl = Changelist(issue=issue, codereview='rietveld', auth_config=auth_config) |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3443 | |
| 3444 | if options.comment: |
| 3445 | cl.AddComment(options.comment) |
| 3446 | return 0 |
| 3447 | |
| 3448 | data = cl.GetIssueProperties() |
smut@google.com | c85ac94 | 2015-09-15 16:34:43 +0000 | [diff] [blame] | 3449 | summary = [] |
maruel@chromium.org | 5cab2d3 | 2014-11-11 18:32:41 +0000 | [diff] [blame] | 3450 | for message in sorted(data.get('messages', []), key=lambda x: x['date']): |
smut@google.com | c85ac94 | 2015-09-15 16:34:43 +0000 | [diff] [blame] | 3451 | summary.append({ |
| 3452 | 'date': message['date'], |
| 3453 | 'lgtm': False, |
| 3454 | 'message': message['text'], |
| 3455 | 'not_lgtm': False, |
| 3456 | 'sender': message['sender'], |
| 3457 | }) |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3458 | if message['disapproval']: |
| 3459 | color = Fore.RED |
smut@google.com | c85ac94 | 2015-09-15 16:34:43 +0000 | [diff] [blame] | 3460 | summary[-1]['not lgtm'] = True |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3461 | elif message['approval']: |
| 3462 | color = Fore.GREEN |
smut@google.com | c85ac94 | 2015-09-15 16:34:43 +0000 | [diff] [blame] | 3463 | summary[-1]['lgtm'] = True |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3464 | elif message['sender'] == data['owner_email']: |
| 3465 | color = Fore.MAGENTA |
| 3466 | else: |
| 3467 | color = Fore.BLUE |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3468 | print('\n%s%s %s%s' % ( |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3469 | color, message['date'].split('.', 1)[0], message['sender'], |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3470 | Fore.RESET)) |
apavlov@chromium.org | e4efd51 | 2014-11-05 09:05:29 +0000 | [diff] [blame] | 3471 | if message['text'].strip(): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3472 | print('\n'.join(' ' + l for l in message['text'].splitlines())) |
smut@google.com | c85ac94 | 2015-09-15 16:34:43 +0000 | [diff] [blame] | 3473 | if options.json_file: |
| 3474 | with open(options.json_file, 'wb') as f: |
| 3475 | json.dump(summary, f) |
maruel@chromium.org | 9977a2e | 2012-06-06 22:30:56 +0000 | [diff] [blame] | 3476 | return 0 |
| 3477 | |
| 3478 | |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 3479 | @subcommand.usage('[codereview url or issue id]') |
rsesek@chromium.org | eec7659 | 2013-05-20 16:27:57 +0000 | [diff] [blame] | 3480 | def CMDdescription(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 3481 | """Brings up the editor for the current CL's description.""" |
smut@google.com | 34fb6b1 | 2015-07-13 20:03:26 +0000 | [diff] [blame] | 3482 | parser.add_option('-d', '--display', action='store_true', |
| 3483 | help='Display the description instead of opening an editor') |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 3484 | parser.add_option('-n', '--new-description', |
| 3485 | help='New description to set for this issue (- for stdin)') |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 3486 | |
| 3487 | _add_codereview_select_options(parser) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3488 | auth.add_auth_options(parser) |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 3489 | options, args = parser.parse_args(args) |
| 3490 | _process_codereview_select_options(parser, options) |
| 3491 | |
| 3492 | target_issue = None |
| 3493 | if len(args) > 0: |
martiniss | 6eda05f | 2016-06-30 10:18:35 -0700 | [diff] [blame] | 3494 | target_issue = ParseIssueNumberArgument(args[0]) |
| 3495 | if not target_issue.valid: |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 3496 | parser.print_help() |
| 3497 | return 1 |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 3498 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3499 | auth_config = auth.extract_auth_config_from_options(options) |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 3500 | |
martiniss | 6eda05f | 2016-06-30 10:18:35 -0700 | [diff] [blame] | 3501 | kwargs = { |
| 3502 | 'auth_config': auth_config, |
| 3503 | 'codereview': options.forced_codereview, |
| 3504 | } |
| 3505 | if target_issue: |
| 3506 | kwargs['issue'] = target_issue.issue |
| 3507 | if options.forced_codereview == 'rietveld': |
| 3508 | kwargs['rietveld_server'] = target_issue.hostname |
| 3509 | |
| 3510 | cl = Changelist(**kwargs) |
martiniss@chromium.org | 2b55fe3 | 2016-04-26 20:28:54 +0000 | [diff] [blame] | 3511 | |
rsesek@chromium.org | eec7659 | 2013-05-20 16:27:57 +0000 | [diff] [blame] | 3512 | if not cl.GetIssue(): |
| 3513 | DieWithError('This branch has no associated changelist.') |
| 3514 | description = ChangeDescription(cl.GetDescription()) |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 3515 | |
smut@google.com | 34fb6b1 | 2015-07-13 20:03:26 +0000 | [diff] [blame] | 3516 | if options.display: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3517 | print(description.description) |
smut@google.com | 34fb6b1 | 2015-07-13 20:03:26 +0000 | [diff] [blame] | 3518 | return 0 |
martiniss@chromium.org | d6648e2 | 2016-04-29 19:22:16 +0000 | [diff] [blame] | 3519 | |
| 3520 | if options.new_description: |
| 3521 | text = options.new_description |
| 3522 | if text == '-': |
| 3523 | text = '\n'.join(l.rstrip() for l in sys.stdin) |
| 3524 | |
| 3525 | description.set_description(text) |
| 3526 | else: |
| 3527 | description.prompt() |
| 3528 | |
wychen@chromium.org | 063e4e5 | 2015-04-03 06:51:44 +0000 | [diff] [blame] | 3529 | if cl.GetDescription() != description.description: |
| 3530 | cl.UpdateDescription(description.description) |
rsesek@chromium.org | eec7659 | 2013-05-20 16:27:57 +0000 | [diff] [blame] | 3531 | return 0 |
| 3532 | |
| 3533 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3534 | def CreateDescriptionFromLog(args): |
| 3535 | """Pulls out the commit log to use as a base for the CL description.""" |
| 3536 | log_args = [] |
| 3537 | if len(args) == 1 and not args[0].endswith('.'): |
| 3538 | log_args = [args[0] + '..'] |
| 3539 | elif len(args) == 1 and args[0].endswith('...'): |
| 3540 | log_args = [args[0][:-1]] |
| 3541 | elif len(args) == 2: |
| 3542 | log_args = [args[0] + '..' + args[1]] |
| 3543 | else: |
| 3544 | log_args = args[:] # Hope for the best! |
maruel@chromium.org | 373af80 | 2012-05-25 21:07:33 +0000 | [diff] [blame] | 3545 | return RunGit(['log', '--pretty=format:%s\n\n%b'] + log_args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3546 | |
| 3547 | |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3548 | def CMDlint(parser, args): |
| 3549 | """Runs cpplint on the current changelist.""" |
tzik@chromium.org | f204d4b | 2014-03-13 07:40:55 +0000 | [diff] [blame] | 3550 | parser.add_option('--filter', action='append', metavar='-x,+y', |
| 3551 | help='Comma-separated list of cpplint\'s category-filters') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3552 | auth.add_auth_options(parser) |
| 3553 | options, args = parser.parse_args(args) |
| 3554 | auth_config = auth.extract_auth_config_from_options(options) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3555 | |
| 3556 | # Access to a protected member _XX of a client class |
| 3557 | # pylint: disable=W0212 |
| 3558 | try: |
| 3559 | import cpplint |
| 3560 | import cpplint_chromium |
| 3561 | except ImportError: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3562 | print('Your depot_tools is missing cpplint.py and/or cpplint_chromium.py.') |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3563 | return 1 |
| 3564 | |
| 3565 | # Change the current working directory before calling lint so that it |
| 3566 | # shows the correct base. |
| 3567 | previous_cwd = os.getcwd() |
| 3568 | os.chdir(settings.GetRoot()) |
| 3569 | try: |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3570 | cl = Changelist(auth_config=auth_config) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3571 | change = cl.GetChange(cl.GetCommonAncestorWithUpstream(), None) |
| 3572 | files = [f.LocalPath() for f in change.AffectedFiles()] |
thestig@chromium.org | 5839eb5 | 2014-05-30 16:20:51 +0000 | [diff] [blame] | 3573 | if not files: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3574 | print('Cannot lint an empty CL') |
thestig@chromium.org | 5839eb5 | 2014-05-30 16:20:51 +0000 | [diff] [blame] | 3575 | return 1 |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3576 | |
| 3577 | # Process cpplints arguments if any. |
tzik@chromium.org | f204d4b | 2014-03-13 07:40:55 +0000 | [diff] [blame] | 3578 | command = args + files |
| 3579 | if options.filter: |
| 3580 | command = ['--filter=' + ','.join(options.filter)] + command |
| 3581 | filenames = cpplint.ParseArguments(command) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3582 | |
| 3583 | white_regex = re.compile(settings.GetLintRegex()) |
| 3584 | black_regex = re.compile(settings.GetLintIgnoreRegex()) |
| 3585 | extra_check_functions = [cpplint_chromium.CheckPointerDeclarationWhitespace] |
| 3586 | for filename in filenames: |
| 3587 | if white_regex.match(filename): |
| 3588 | if black_regex.match(filename): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3589 | print('Ignoring file %s' % filename) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3590 | else: |
| 3591 | cpplint.ProcessFile(filename, cpplint._cpplint_state.verbose_level, |
| 3592 | extra_check_functions) |
| 3593 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3594 | print('Skipping file %s' % filename) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3595 | finally: |
| 3596 | os.chdir(previous_cwd) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3597 | print('Total errors found: %d\n' % cpplint._cpplint_state.error_count) |
thestig@chromium.org | 44202a2 | 2014-03-11 19:22:18 +0000 | [diff] [blame] | 3598 | if cpplint._cpplint_state.error_count != 0: |
| 3599 | return 1 |
| 3600 | return 0 |
| 3601 | |
| 3602 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3603 | def CMDpresubmit(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 3604 | """Runs presubmit tests on the current changelist.""" |
ilevy@chromium.org | 375a902 | 2013-01-07 01:12:05 +0000 | [diff] [blame] | 3605 | parser.add_option('-u', '--upload', action='store_true', |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3606 | help='Run upload hook instead of the push/dcommit hook') |
ilevy@chromium.org | 375a902 | 2013-01-07 01:12:05 +0000 | [diff] [blame] | 3607 | parser.add_option('-f', '--force', action='store_true', |
sbc@chromium.org | 495ad15 | 2012-09-04 23:07:42 +0000 | [diff] [blame] | 3608 | help='Run checks even if tree is dirty') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3609 | auth.add_auth_options(parser) |
| 3610 | options, args = parser.parse_args(args) |
| 3611 | auth_config = auth.extract_auth_config_from_options(options) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3612 | |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 3613 | if not options.force and git_common.is_dirty_git_tree('presubmit'): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3614 | print('use --force to check even if tree is dirty.') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3615 | return 1 |
| 3616 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3617 | cl = Changelist(auth_config=auth_config) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3618 | if args: |
| 3619 | base_branch = args[0] |
| 3620 | else: |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 3621 | # Default to diffing against the common ancestor of the upstream branch. |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 3622 | base_branch = cl.GetCommonAncestorWithUpstream() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3623 | |
ilevy@chromium.org | 051ad0e | 2013-03-04 21:57:34 +0000 | [diff] [blame] | 3624 | cl.RunHook( |
| 3625 | committing=not options.upload, |
| 3626 | may_prompt=False, |
| 3627 | verbose=options.verbose, |
| 3628 | change=cl.GetChange(base_branch, None)) |
dpranke@chromium.org | 0a2bb37 | 2011-03-25 01:16:22 +0000 | [diff] [blame] | 3629 | return 0 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3630 | |
| 3631 | |
tandrii@chromium.org | 65874e1 | 2016-03-04 12:03:02 +0000 | [diff] [blame] | 3632 | def GenerateGerritChangeId(message): |
| 3633 | """Returns Ixxxxxx...xxx change id. |
| 3634 | |
| 3635 | Works the same way as |
| 3636 | https://gerrit-review.googlesource.com/tools/hooks/commit-msg |
| 3637 | but can be called on demand on all platforms. |
| 3638 | |
| 3639 | The basic idea is to generate git hash of a state of the tree, original commit |
| 3640 | message, author/committer info and timestamps. |
| 3641 | """ |
| 3642 | lines = [] |
| 3643 | tree_hash = RunGitSilent(['write-tree']) |
| 3644 | lines.append('tree %s' % tree_hash.strip()) |
| 3645 | code, parent = RunGitWithCode(['rev-parse', 'HEAD~0'], suppress_stderr=False) |
| 3646 | if code == 0: |
| 3647 | lines.append('parent %s' % parent.strip()) |
| 3648 | author = RunGitSilent(['var', 'GIT_AUTHOR_IDENT']) |
| 3649 | lines.append('author %s' % author.strip()) |
| 3650 | committer = RunGitSilent(['var', 'GIT_COMMITTER_IDENT']) |
| 3651 | lines.append('committer %s' % committer.strip()) |
| 3652 | lines.append('') |
| 3653 | # Note: Gerrit's commit-hook actually cleans message of some lines and |
| 3654 | # whitespace. This code is not doing this, but it clearly won't decrease |
| 3655 | # entropy. |
| 3656 | lines.append(message) |
| 3657 | change_hash = RunCommand(['git', 'hash-object', '-t', 'commit', '--stdin'], |
| 3658 | stdin='\n'.join(lines)) |
| 3659 | return 'I%s' % change_hash.strip() |
| 3660 | |
| 3661 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 3662 | def GetTargetRef(remote, remote_branch, target_branch, pending_prefix): |
| 3663 | """Computes the remote branch ref to use for the CL. |
| 3664 | |
| 3665 | Args: |
| 3666 | remote (str): The git remote for the CL. |
| 3667 | remote_branch (str): The git remote branch for the CL. |
| 3668 | target_branch (str): The target branch specified by the user. |
| 3669 | pending_prefix (str): The pending prefix from the settings. |
| 3670 | """ |
| 3671 | if not (remote and remote_branch): |
| 3672 | return None |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 3673 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 3674 | if target_branch: |
| 3675 | # Cannonicalize branch references to the equivalent local full symbolic |
| 3676 | # refs, which are then translated into the remote full symbolic refs |
| 3677 | # below. |
| 3678 | if '/' not in target_branch: |
| 3679 | remote_branch = 'refs/remotes/%s/%s' % (remote, target_branch) |
| 3680 | else: |
| 3681 | prefix_replacements = ( |
| 3682 | ('^((refs/)?remotes/)?branch-heads/', 'refs/remotes/branch-heads/'), |
| 3683 | ('^((refs/)?remotes/)?%s/' % remote, 'refs/remotes/%s/' % remote), |
| 3684 | ('^(refs/)?heads/', 'refs/remotes/%s/' % remote), |
| 3685 | ) |
| 3686 | match = None |
| 3687 | for regex, replacement in prefix_replacements: |
| 3688 | match = re.search(regex, target_branch) |
| 3689 | if match: |
| 3690 | remote_branch = target_branch.replace(match.group(0), replacement) |
| 3691 | break |
| 3692 | if not match: |
| 3693 | # This is a branch path but not one we recognize; use as-is. |
| 3694 | remote_branch = target_branch |
rmistry@google.com | c68112d | 2015-03-03 12:48:06 +0000 | [diff] [blame] | 3695 | elif remote_branch in REFS_THAT_ALIAS_TO_OTHER_REFS: |
| 3696 | # Handle the refs that need to land in different refs. |
| 3697 | remote_branch = REFS_THAT_ALIAS_TO_OTHER_REFS[remote_branch] |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 3698 | |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 3699 | # Create the true path to the remote branch. |
| 3700 | # Does the following translation: |
| 3701 | # * refs/remotes/origin/refs/diff/test -> refs/diff/test |
| 3702 | # * refs/remotes/origin/master -> refs/heads/master |
| 3703 | # * refs/remotes/branch-heads/test -> refs/branch-heads/test |
| 3704 | if remote_branch.startswith('refs/remotes/%s/refs/' % remote): |
| 3705 | remote_branch = remote_branch.replace('refs/remotes/%s/' % remote, '') |
| 3706 | elif remote_branch.startswith('refs/remotes/%s/' % remote): |
| 3707 | remote_branch = remote_branch.replace('refs/remotes/%s/' % remote, |
| 3708 | 'refs/heads/') |
| 3709 | elif remote_branch.startswith('refs/remotes/branch-heads'): |
| 3710 | remote_branch = remote_branch.replace('refs/remotes/', 'refs/') |
| 3711 | # If a pending prefix exists then replace refs/ with it. |
| 3712 | if pending_prefix: |
| 3713 | remote_branch = remote_branch.replace('refs/', pending_prefix) |
| 3714 | return remote_branch |
| 3715 | |
| 3716 | |
maruel@chromium.org | eb52a5c | 2013-04-10 23:17:09 +0000 | [diff] [blame] | 3717 | def cleanup_list(l): |
| 3718 | """Fixes a list so that comma separated items are put as individual items. |
| 3719 | |
| 3720 | So that "--reviewers joe@c,john@c --reviewers joa@c" results in |
| 3721 | options.reviewers == sorted(['joe@c', 'john@c', 'joa@c']). |
| 3722 | """ |
| 3723 | items = sum((i.split(',') for i in l), []) |
| 3724 | stripped_items = (i.strip() for i in items) |
| 3725 | return sorted(filter(None, stripped_items)) |
| 3726 | |
| 3727 | |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 3728 | @subcommand.usage('[args to "git diff"]') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3729 | def CMDupload(parser, args): |
rmistry@google.com | 78948ed | 2015-07-08 23:09:57 +0000 | [diff] [blame] | 3730 | """Uploads the current changelist to codereview. |
| 3731 | |
| 3732 | Can skip dependency patchset uploads for a branch by running: |
| 3733 | git config branch.branch_name.skip-deps-uploads True |
| 3734 | To unset run: |
| 3735 | git config --unset branch.branch_name.skip-deps-uploads |
| 3736 | Can also set the above globally by using the --global flag. |
| 3737 | """ |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3738 | parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks', |
| 3739 | help='bypass upload presubmit hook') |
brettw@chromium.org | b65c43c | 2013-06-10 22:04:49 +0000 | [diff] [blame] | 3740 | parser.add_option('--bypass-watchlists', action='store_true', |
| 3741 | dest='bypass_watchlists', |
| 3742 | help='bypass watchlists auto CC-ing reviewers') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3743 | parser.add_option('-f', action='store_true', dest='force', |
| 3744 | help="force yes to questions (don't prompt)") |
rogerta@chromium.org | 420d3b8 | 2012-05-14 18:41:38 +0000 | [diff] [blame] | 3745 | parser.add_option('-m', dest='message', help='message for patchset') |
tandrii | f9aefb7 | 2016-07-01 09:06:51 -0700 | [diff] [blame] | 3746 | parser.add_option('-b', '--bug', |
| 3747 | help='pre-populate the bug number(s) for this issue. ' |
| 3748 | 'If several, separate with commas') |
tandrii | b80458a | 2016-06-23 12:20:07 -0700 | [diff] [blame] | 3749 | parser.add_option('--message-file', dest='message_file', |
| 3750 | help='file which contains message for patchset') |
andybons@chromium.org | 962f946 | 2016-02-03 20:00:42 +0000 | [diff] [blame] | 3751 | parser.add_option('-t', dest='title', |
| 3752 | help='title for patchset (Rietveld only)') |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3753 | parser.add_option('-r', '--reviewers', |
maruel@chromium.org | eb52a5c | 2013-04-10 23:17:09 +0000 | [diff] [blame] | 3754 | action='append', default=[], |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3755 | help='reviewer email addresses') |
| 3756 | parser.add_option('--cc', |
maruel@chromium.org | eb52a5c | 2013-04-10 23:17:09 +0000 | [diff] [blame] | 3757 | action='append', default=[], |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3758 | help='cc email addresses') |
adamk@chromium.org | 36f4730 | 2013-04-05 01:08:31 +0000 | [diff] [blame] | 3759 | parser.add_option('-s', '--send-mail', action='store_true', |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3760 | help='send email to reviewer immediately') |
pgervais@chromium.org | b9f2751 | 2014-08-08 15:52:33 +0000 | [diff] [blame] | 3761 | parser.add_option('--emulate_svn_auto_props', |
| 3762 | '--emulate-svn-auto-props', |
| 3763 | action="store_true", |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3764 | dest="emulate_svn_auto_props", |
| 3765 | help="Emulate Subversion's auto properties feature.") |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3766 | parser.add_option('-c', '--use-commit-queue', action='store_true', |
| 3767 | help='tell the commit queue to commit this patchset') |
tyoshino@chromium.org | c1737d0 | 2013-05-29 14:17:28 +0000 | [diff] [blame] | 3768 | parser.add_option('--private', action='store_true', |
| 3769 | help='set the review private (rietveld only)') |
ukai@chromium.org | 8ef7ab2 | 2012-11-28 04:24:52 +0000 | [diff] [blame] | 3770 | parser.add_option('--target_branch', |
pgervais@chromium.org | b9f2751 | 2014-08-08 15:52:33 +0000 | [diff] [blame] | 3771 | '--target-branch', |
wittman@chromium.org | 455dc92 | 2015-01-26 20:15:50 +0000 | [diff] [blame] | 3772 | metavar='TARGET', |
| 3773 | help='Apply CL to remote ref TARGET. ' + |
| 3774 | 'Default: remote branch head, or master') |
bauerb@chromium.org | 27386dd | 2015-02-16 10:45:39 +0000 | [diff] [blame] | 3775 | parser.add_option('--squash', action='store_true', |
| 3776 | help='Squash multiple commits into one (Gerrit only)') |
bauerb@chromium.org | 54b400c | 2016-01-14 10:08:25 +0000 | [diff] [blame] | 3777 | parser.add_option('--no-squash', action='store_true', |
| 3778 | help='Don\'t squash multiple commits into one ' + |
| 3779 | '(Gerrit only)') |
pgervais@chromium.org | 9114137 | 2014-01-09 23:27:20 +0000 | [diff] [blame] | 3780 | parser.add_option('--email', default=None, |
| 3781 | help='email address to use to connect to Rietveld') |
piman@chromium.org | 336f912 | 2014-09-04 02:16:55 +0000 | [diff] [blame] | 3782 | parser.add_option('--tbr-owners', dest='tbr_owners', action='store_true', |
| 3783 | help='add a set of OWNERS to TBR') |
tandrii@chromium.org | d50452a | 2015-11-23 16:38:15 +0000 | [diff] [blame] | 3784 | parser.add_option('-d', '--cq-dry-run', dest='cq_dry_run', |
| 3785 | action='store_true', |
rmistry@google.com | ef96622 | 2015-04-07 11:15:01 +0000 | [diff] [blame] | 3786 | help='Send the patchset to do a CQ dry run right after ' |
| 3787 | 'upload.') |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3788 | parser.add_option('--dependencies', action='store_true', |
| 3789 | help='Uploads CLs of all the local branches that depend on ' |
| 3790 | 'the current branch') |
pgervais@chromium.org | 9114137 | 2014-01-09 23:27:20 +0000 | [diff] [blame] | 3791 | |
rmistry@google.com | 2dd9986 | 2015-06-22 12:22:18 +0000 | [diff] [blame] | 3792 | orig_args = args |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 3793 | add_git_similarity(parser) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3794 | auth.add_auth_options(parser) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 3795 | _add_codereview_select_options(parser) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3796 | (options, args) = parser.parse_args(args) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 3797 | _process_codereview_select_options(parser, options) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3798 | auth_config = auth.extract_auth_config_from_options(options) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3799 | |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 3800 | if git_common.is_dirty_git_tree('upload'): |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3801 | return 1 |
| 3802 | |
maruel@chromium.org | eb52a5c | 2013-04-10 23:17:09 +0000 | [diff] [blame] | 3803 | options.reviewers = cleanup_list(options.reviewers) |
| 3804 | options.cc = cleanup_list(options.cc) |
| 3805 | |
tandrii | b80458a | 2016-06-23 12:20:07 -0700 | [diff] [blame] | 3806 | if options.message_file: |
| 3807 | if options.message: |
| 3808 | parser.error('only one of --message and --message-file allowed.') |
| 3809 | options.message = gclient_utils.FileRead(options.message_file) |
| 3810 | options.message_file = None |
| 3811 | |
tandrii | 4d0545a | 2016-07-06 03:56:49 -0700 | [diff] [blame] | 3812 | if options.cq_dry_run and options.use_commit_queue: |
| 3813 | parser.error('only one of --use-commit-queue and --cq-dry-run allowed.') |
| 3814 | |
tandrii@chromium.org | 512d79c | 2016-03-31 12:55:28 +0000 | [diff] [blame] | 3815 | # For sanity of test expectations, do this otherwise lazy-loading *now*. |
| 3816 | settings.GetIsGerrit() |
| 3817 | |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 3818 | cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview) |
tandrii@chromium.org | 9e6c3a5 | 2016-04-12 14:13:08 +0000 | [diff] [blame] | 3819 | return cl.CMDUpload(options, args, orig_args) |
ukai@chromium.org | e807781 | 2012-02-03 03:41:46 +0000 | [diff] [blame] | 3820 | |
| 3821 | |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 3822 | def IsSubmoduleMergeCommit(ref): |
| 3823 | # When submodules are added to the repo, we expect there to be a single |
| 3824 | # non-git-svn merge commit at remote HEAD with a signature comment. |
| 3825 | pattern = '^SVN changes up to revision [0-9]*$' |
szager@chromium.org | e84b754 | 2012-06-15 21:26:58 +0000 | [diff] [blame] | 3826 | cmd = ['rev-list', '--merges', '--grep=%s' % pattern, '%s^!' % ref] |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 3827 | return RunGit(cmd) != '' |
| 3828 | |
| 3829 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3830 | def SendUpstream(parser, args, cmd): |
pgervais@chromium.org | cee6dc4 | 2014-05-07 17:04:03 +0000 | [diff] [blame] | 3831 | """Common code for CMDland and CmdDCommit |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3832 | |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 3833 | In case of Gerrit, uses Gerrit REST api to "submit" the issue, which pushes |
| 3834 | upstream and closes the issue automatically and atomically. |
| 3835 | |
| 3836 | Otherwise (in case of Rietveld): |
| 3837 | Squashes branch into a single commit. |
| 3838 | Updates changelog with metadata (e.g. pointer to review). |
| 3839 | Pushes/dcommits the code upstream. |
| 3840 | Updates review and closes. |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3841 | """ |
| 3842 | parser.add_option('--bypass-hooks', action='store_true', dest='bypass_hooks', |
| 3843 | help='bypass upload presubmit hook') |
| 3844 | parser.add_option('-m', dest='message', |
| 3845 | help="override review description") |
| 3846 | parser.add_option('-f', action='store_true', dest='force', |
| 3847 | help="force yes to questions (don't prompt)") |
| 3848 | parser.add_option('-c', dest='contributor', |
| 3849 | help="external contributor for patch (appended to " + |
| 3850 | "description and used as author for git). Should be " + |
| 3851 | "formatted as 'First Last <email@example.com>'") |
iannucci@chromium.org | 53937ba | 2012-10-02 18:20:43 +0000 | [diff] [blame] | 3852 | add_git_similarity(parser) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3853 | auth.add_auth_options(parser) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3854 | (options, args) = parser.parse_args(args) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 3855 | auth_config = auth.extract_auth_config_from_options(options) |
| 3856 | |
| 3857 | cl = Changelist(auth_config=auth_config) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3858 | |
tandrii@chromium.org | d68b62b | 2016-03-31 16:09:29 +0000 | [diff] [blame] | 3859 | # TODO(tandrii): refactor this into _RietveldChangelistImpl method. |
| 3860 | if cl.IsGerrit(): |
| 3861 | if options.message: |
| 3862 | # This could be implemented, but it requires sending a new patch to |
| 3863 | # Gerrit, as Gerrit unlike Rietveld versions messages with patchsets. |
| 3864 | # Besides, Gerrit has the ability to change the commit message on submit |
| 3865 | # automatically, thus there is no need to support this option (so far?). |
| 3866 | parser.error('-m MESSAGE option is not supported for Gerrit.') |
| 3867 | if options.contributor: |
| 3868 | parser.error( |
| 3869 | '-c CONTRIBUTOR option is not supported for Gerrit.\n' |
| 3870 | 'Before uploading a commit to Gerrit, ensure it\'s author field is ' |
| 3871 | 'the contributor\'s "name <email>". If you can\'t upload such a ' |
| 3872 | 'commit for review, contact your repository admin and request' |
| 3873 | '"Forge-Author" permission.') |
| 3874 | return cl._codereview_impl.CMDLand(options.force, options.bypass_hooks, |
| 3875 | options.verbose) |
| 3876 | |
iannucci@chromium.org | 5724c96 | 2014-04-11 09:32:56 +0000 | [diff] [blame] | 3877 | current = cl.GetBranch() |
| 3878 | remote, upstream_branch = cl.FetchUpstreamTuple(cl.GetBranch()) |
| 3879 | if not settings.GetIsGitSvn() and remote == '.': |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3880 | print() |
| 3881 | print('Attempting to push branch %r into another local branch!' % current) |
| 3882 | print() |
| 3883 | print('Either reparent this branch on top of origin/master:') |
| 3884 | print(' git reparent-branch --root') |
| 3885 | print() |
| 3886 | print('OR run `git rebase-update` if you think the parent branch is ') |
| 3887 | print('already committed.') |
| 3888 | print() |
| 3889 | print(' Current parent: %r' % upstream_branch) |
iannucci@chromium.org | 5724c96 | 2014-04-11 09:32:56 +0000 | [diff] [blame] | 3890 | return 1 |
| 3891 | |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 3892 | if not args or cmd == 'land': |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3893 | # Default to merging against our best guess of the upstream branch. |
| 3894 | args = [cl.GetUpstreamBranch()] |
| 3895 | |
maruel@chromium.org | 13f623c | 2011-07-22 16:02:23 +0000 | [diff] [blame] | 3896 | if options.contributor: |
| 3897 | if not re.match('^.*\s<\S+@\S+>$', options.contributor): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3898 | print("Please provide contibutor as 'First Last <email@example.com>'") |
maruel@chromium.org | 13f623c | 2011-07-22 16:02:23 +0000 | [diff] [blame] | 3899 | return 1 |
| 3900 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3901 | base_branch = args[0] |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 3902 | base_has_submodules = IsSubmoduleMergeCommit(base_branch) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3903 | |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 3904 | if git_common.is_dirty_git_tree(cmd): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3905 | return 1 |
| 3906 | |
| 3907 | # This rev-list syntax means "show all commits not in my branch that |
| 3908 | # are in base_branch". |
| 3909 | upstream_commits = RunGit(['rev-list', '^' + cl.GetBranchRef(), |
| 3910 | base_branch]).splitlines() |
| 3911 | if upstream_commits: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3912 | print('Base branch "%s" has %d commits ' |
| 3913 | 'not in this branch.' % (base_branch, len(upstream_commits))) |
| 3914 | print('Run "git merge %s" before attempting to %s.' % (base_branch, cmd)) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3915 | return 1 |
| 3916 | |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 3917 | # This is the revision `svn dcommit` will commit on top of. |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 3918 | svn_head = None |
| 3919 | if cmd == 'dcommit' or base_has_submodules: |
| 3920 | svn_head = RunGit(['log', '--grep=^git-svn-id:', '-1', |
| 3921 | '--pretty=format:%H']) |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 3922 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3923 | if cmd == 'dcommit': |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 3924 | # If the base_head is a submodule merge commit, the first parent of the |
| 3925 | # base_head should be a git-svn commit, which is what we're interested in. |
| 3926 | base_svn_head = base_branch |
| 3927 | if base_has_submodules: |
| 3928 | base_svn_head += '^1' |
| 3929 | |
| 3930 | extra_commits = RunGit(['rev-list', '^' + svn_head, base_svn_head]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3931 | if extra_commits: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3932 | print('This branch has %d additional commits not upstreamed yet.' |
| 3933 | % len(extra_commits.splitlines())) |
| 3934 | print('Upstream "%s" or rebase this branch on top of the upstream trunk ' |
| 3935 | 'before attempting to %s.' % (base_branch, cmd)) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3936 | return 1 |
| 3937 | |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 3938 | merge_base = RunGit(['merge-base', base_branch, 'HEAD']).strip() |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 3939 | if not options.bypass_hooks: |
maruel@chromium.org | 13f623c | 2011-07-22 16:02:23 +0000 | [diff] [blame] | 3940 | author = None |
| 3941 | if options.contributor: |
| 3942 | author = re.search(r'\<(.*)\>', options.contributor).group(1) |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 3943 | hook_results = cl.RunHook( |
| 3944 | committing=True, |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 3945 | may_prompt=not options.force, |
| 3946 | verbose=options.verbose, |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 3947 | change=cl.GetChange(merge_base, author)) |
maruel@chromium.org | b0a6391 | 2012-01-17 18:10:16 +0000 | [diff] [blame] | 3948 | if not hook_results.should_continue(): |
| 3949 | return 1 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3950 | |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 3951 | # Check the tree status if the tree status URL is set. |
| 3952 | status = GetTreeStatus() |
| 3953 | if 'closed' == status: |
| 3954 | print('The tree is closed. Please wait for it to reopen. Use ' |
| 3955 | '"git cl %s --bypass-hooks" to commit on a closed tree.' % cmd) |
| 3956 | return 1 |
| 3957 | elif 'unknown' == status: |
| 3958 | print('Unable to determine tree status. Please verify manually and ' |
| 3959 | 'use "git cl %s --bypass-hooks" to commit on a closed tree.' % cmd) |
| 3960 | return 1 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3961 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 3962 | change_desc = ChangeDescription(options.message) |
| 3963 | if not change_desc.description and cl.GetIssue(): |
| 3964 | change_desc = ChangeDescription(cl.GetDescription()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3965 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 3966 | if not change_desc.description: |
erg@chromium.org | 1a17398 | 2012-08-29 20:43:05 +0000 | [diff] [blame] | 3967 | if not cl.GetIssue() and options.bypass_hooks: |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 3968 | change_desc = ChangeDescription(CreateDescriptionFromLog([merge_base])) |
erg@chromium.org | 1a17398 | 2012-08-29 20:43:05 +0000 | [diff] [blame] | 3969 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 3970 | print('No description set.') |
| 3971 | print('Visit %s/edit to set it.' % (cl.GetIssueURL())) |
erg@chromium.org | 1a17398 | 2012-08-29 20:43:05 +0000 | [diff] [blame] | 3972 | return 1 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3973 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 3974 | # Keep a separate copy for the commit message, because the commit message |
| 3975 | # contains the link to the Rietveld issue, while the Rietveld message contains |
| 3976 | # the commit viewvc url. |
maruel@chromium.org | e52678e | 2013-04-26 18:34:44 +0000 | [diff] [blame] | 3977 | # Keep a separate copy for the commit message. |
| 3978 | if cl.GetIssue(): |
maruel@chromium.org | cf08778 | 2013-07-23 13:08:48 +0000 | [diff] [blame] | 3979 | change_desc.update_reviewers(cl.GetApprovingReviewers()) |
maruel@chromium.org | e52678e | 2013-04-26 18:34:44 +0000 | [diff] [blame] | 3980 | |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 3981 | commit_desc = ChangeDescription(change_desc.description) |
maruel@chromium.org | cc73ad6 | 2011-07-06 17:39:26 +0000 | [diff] [blame] | 3982 | if cl.GetIssue(): |
smut@google.com | 4c61dcc | 2015-06-08 22:31:29 +0000 | [diff] [blame] | 3983 | # Xcode won't linkify this URL unless there is a non-whitespace character |
sergiyb@chromium.org | 4b39c5f | 2015-07-07 10:33:12 +0000 | [diff] [blame] | 3984 | # after it. Add a period on a new line to circumvent this. Also add a space |
| 3985 | # before the period to make sure that Gitiles continues to correctly resolve |
| 3986 | # the URL. |
| 3987 | commit_desc.append_footer('Review URL: %s .' % cl.GetIssueURL()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3988 | if options.contributor: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 3989 | commit_desc.append_footer('Patch from %s.' % options.contributor) |
| 3990 | |
agable@chromium.org | eec3ea3 | 2013-08-15 20:31:39 +0000 | [diff] [blame] | 3991 | print('Description:') |
| 3992 | print(commit_desc.description) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3993 | |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 3994 | branches = [merge_base, cl.GetBranchRef()] |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3995 | if not options.force: |
iannucci@chromium.org | 7954005 | 2012-10-19 23:15:26 +0000 | [diff] [blame] | 3996 | print_stats(options.similarity, options.find_copies, branches) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 3997 | |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 3998 | # We want to squash all this branch's commits into one commit with the proper |
| 3999 | # description. We do this by doing a "reset --soft" to the base branch (which |
| 4000 | # keeps the working copy the same), then dcommitting that. If origin/master |
| 4001 | # has a submodule merge commit, we'll also need to cherry-pick the squashed |
| 4002 | # commit onto a branch based on the git-svn head. |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4003 | MERGE_BRANCH = 'git-cl-commit' |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 4004 | CHERRY_PICK_BRANCH = 'git-cl-cherry-pick' |
| 4005 | # Delete the branches if they exist. |
| 4006 | for branch in [MERGE_BRANCH, CHERRY_PICK_BRANCH]: |
| 4007 | showref_cmd = ['show-ref', '--quiet', '--verify', 'refs/heads/%s' % branch] |
| 4008 | result = RunGitWithCode(showref_cmd) |
| 4009 | if result[0] == 0: |
| 4010 | RunGit(['branch', '-D', branch]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4011 | |
| 4012 | # We might be in a directory that's present in this branch but not in the |
| 4013 | # trunk. Move up to the top of the tree so that git commands that expect a |
| 4014 | # valid CWD won't fail after we check out the merge branch. |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 4015 | rel_base_path = settings.GetRelativeRoot() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4016 | if rel_base_path: |
| 4017 | os.chdir(rel_base_path) |
| 4018 | |
| 4019 | # Stuff our change into the merge branch. |
| 4020 | # We wrap in a try...finally block so if anything goes wrong, |
| 4021 | # we clean up the branches. |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 4022 | retcode = -1 |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4023 | pushed_to_pending = False |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4024 | pending_ref = None |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4025 | revision = None |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4026 | try: |
bauerb@chromium.org | b4a75c4 | 2011-03-08 08:35:38 +0000 | [diff] [blame] | 4027 | RunGit(['checkout', '-q', '-b', MERGE_BRANCH]) |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4028 | RunGit(['reset', '--soft', merge_base]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4029 | if options.contributor: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 4030 | RunGit( |
| 4031 | [ |
| 4032 | 'commit', '--author', options.contributor, |
| 4033 | '-m', commit_desc.description, |
| 4034 | ]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4035 | else: |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 4036 | RunGit(['commit', '-m', commit_desc.description]) |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 4037 | if base_has_submodules: |
| 4038 | cherry_pick_commit = RunGit(['rev-list', 'HEAD^!']).rstrip() |
| 4039 | RunGit(['branch', CHERRY_PICK_BRANCH, svn_head]) |
| 4040 | RunGit(['checkout', CHERRY_PICK_BRANCH]) |
| 4041 | RunGit(['cherry-pick', cherry_pick_commit]) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4042 | if cmd == 'land': |
ilevy@chromium.org | 0f58fa8 | 2012-11-05 01:45:20 +0000 | [diff] [blame] | 4043 | remote, branch = cl.FetchUpstreamTuple(cl.GetBranch()) |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 4044 | mirror = settings.GetGitMirror(remote) |
| 4045 | pushurl = mirror.url if mirror else remote |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4046 | pending_prefix = settings.GetPendingRefPrefix() |
| 4047 | if not pending_prefix or branch.startswith(pending_prefix): |
| 4048 | # If not using refs/pending/heads/* at all, or target ref is already set |
| 4049 | # to pending, then push to the target ref directly. |
| 4050 | retcode, output = RunGitWithCode( |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 4051 | ['push', '--porcelain', pushurl, 'HEAD:%s' % branch]) |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4052 | pushed_to_pending = pending_prefix and branch.startswith(pending_prefix) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4053 | else: |
| 4054 | # Cherry-pick the change on top of pending ref and then push it. |
| 4055 | assert branch.startswith('refs/'), branch |
| 4056 | assert pending_prefix[-1] == '/', pending_prefix |
| 4057 | pending_ref = pending_prefix + branch[len('refs/'):] |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 4058 | retcode, output = PushToGitPending(pushurl, pending_ref, branch) |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4059 | pushed_to_pending = (retcode == 0) |
iannucci@chromium.org | 34504a1 | 2014-08-29 23:51:37 +0000 | [diff] [blame] | 4060 | if retcode == 0: |
| 4061 | revision = RunGit(['rev-parse', 'HEAD']).strip() |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4062 | else: |
| 4063 | # dcommit the merge branch. |
iannucci@chromium.org | a1950c4 | 2014-12-05 22:15:56 +0000 | [diff] [blame] | 4064 | cmd_args = [ |
kjellander@chromium.org | 6abc652 | 2014-12-02 07:34:49 +0000 | [diff] [blame] | 4065 | 'svn', 'dcommit', |
| 4066 | '-C%s' % options.similarity, |
| 4067 | '--no-rebase', '--rmdir', |
| 4068 | ] |
| 4069 | if settings.GetForceHttpsCommitUrl(): |
| 4070 | # Allow forcing https commit URLs for some projects that don't allow |
| 4071 | # committing to http URLs (like Google Code). |
| 4072 | remote_url = cl.GetGitSvnRemoteUrl() |
| 4073 | if urlparse.urlparse(remote_url).scheme == 'http': |
| 4074 | remote_url = remote_url.replace('http://', 'https://') |
iannucci@chromium.org | a1950c4 | 2014-12-05 22:15:56 +0000 | [diff] [blame] | 4075 | cmd_args.append('--commit-url=%s' % remote_url) |
| 4076 | _, output = RunGitWithCode(cmd_args) |
iannucci@chromium.org | 34504a1 | 2014-08-29 23:51:37 +0000 | [diff] [blame] | 4077 | if 'Committed r' in output: |
| 4078 | revision = re.match( |
| 4079 | '.*?\nCommitted r(\\d+)', output, re.DOTALL).group(1) |
| 4080 | logging.debug(output) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4081 | finally: |
| 4082 | # And then swap back to the original branch and clean up. |
| 4083 | RunGit(['checkout', '-q', cl.GetBranch()]) |
| 4084 | RunGit(['branch', '-D', MERGE_BRANCH]) |
szager@chromium.org | 9bb85e2 | 2012-06-13 20:28:23 +0000 | [diff] [blame] | 4085 | if base_has_submodules: |
| 4086 | RunGit(['branch', '-D', CHERRY_PICK_BRANCH]) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4087 | |
iannucci@chromium.org | 34504a1 | 2014-08-29 23:51:37 +0000 | [diff] [blame] | 4088 | if not revision: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4089 | print('Failed to push. If this persists, please file a bug.') |
iannucci@chromium.org | 34504a1 | 2014-08-29 23:51:37 +0000 | [diff] [blame] | 4090 | return 1 |
iannucci@chromium.org | 6c217b1 | 2014-08-29 22:10:59 +0000 | [diff] [blame] | 4091 | |
iannucci@chromium.org | bbe9cc5 | 2014-09-05 18:25:51 +0000 | [diff] [blame] | 4092 | killed = False |
iannucci@chromium.org | 6c217b1 | 2014-08-29 22:10:59 +0000 | [diff] [blame] | 4093 | if pushed_to_pending: |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4094 | try: |
| 4095 | revision = WaitForRealCommit(remote, revision, base_branch, branch) |
| 4096 | # We set pushed_to_pending to False, since it made it all the way to the |
| 4097 | # real ref. |
| 4098 | pushed_to_pending = False |
| 4099 | except KeyboardInterrupt: |
iannucci@chromium.org | bbe9cc5 | 2014-09-05 18:25:51 +0000 | [diff] [blame] | 4100 | killed = True |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4101 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4102 | if cl.GetIssue(): |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4103 | to_pending = ' to pending queue' if pushed_to_pending else '' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4104 | viewvc_url = settings.GetViewVCUrl() |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4105 | if not to_pending: |
| 4106 | if viewvc_url and revision: |
| 4107 | change_desc.append_footer( |
| 4108 | 'Committed: %s%s' % (viewvc_url, revision)) |
| 4109 | elif revision: |
| 4110 | change_desc.append_footer('Committed: %s' % (revision,)) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4111 | print('Closing issue ' |
| 4112 | '(you may be prompted for your codereview password)...') |
maruel@chromium.org | 78936cb | 2013-04-11 00:17:52 +0000 | [diff] [blame] | 4113 | cl.UpdateDescription(change_desc.description) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4114 | cl.CloseIssue() |
maruel@chromium.org | 1033efd | 2013-07-23 23:25:09 +0000 | [diff] [blame] | 4115 | props = cl.GetIssueProperties() |
sadrul@chromium.org | 34b5d82 | 2013-02-18 01:39:24 +0000 | [diff] [blame] | 4116 | patch_num = len(props['patchsets']) |
rmistry@google.com | 52d224a | 2014-08-27 14:44:41 +0000 | [diff] [blame] | 4117 | comment = "Committed patchset #%d (id:%d)%s manually as %s" % ( |
mark@chromium.org | 782570c | 2014-09-26 21:48:02 +0000 | [diff] [blame] | 4118 | patch_num, props['patchsets'][-1], to_pending, revision) |
jochen@chromium.org | 3ec0d54 | 2014-01-14 20:00:03 +0000 | [diff] [blame] | 4119 | if options.bypass_hooks: |
| 4120 | comment += ' (tree was closed).' if GetTreeStatus() == 'closed' else '.' |
| 4121 | else: |
| 4122 | comment += ' (presubmit successful).' |
iannucci@chromium.org | b85a316 | 2013-01-26 01:11:13 +0000 | [diff] [blame] | 4123 | cl.RpcServer().add_comment(cl.GetIssue(), comment) |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 4124 | |
iannucci@chromium.org | 6c217b1 | 2014-08-29 22:10:59 +0000 | [diff] [blame] | 4125 | if pushed_to_pending: |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4126 | _, branch = cl.FetchUpstreamTuple(cl.GetBranch()) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4127 | print('The commit is in the pending queue (%s).' % pending_ref) |
| 4128 | print('It will show up on %s in ~1 min, once it gets a Cr-Commit-Position ' |
| 4129 | 'footer.' % branch) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4130 | |
iannucci@chromium.org | 6c217b1 | 2014-08-29 22:10:59 +0000 | [diff] [blame] | 4131 | hook = POSTUPSTREAM_HOOK_PATTERN % cmd |
| 4132 | if os.path.isfile(hook): |
| 4133 | RunCommand([hook, merge_base], error_ok=True) |
maruel@chromium.org | 0ba7f96 | 2011-01-11 22:13:58 +0000 | [diff] [blame] | 4134 | |
iannucci@chromium.org | bbe9cc5 | 2014-09-05 18:25:51 +0000 | [diff] [blame] | 4135 | return 1 if killed else 0 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4136 | |
| 4137 | |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4138 | def WaitForRealCommit(remote, pushed_commit, local_base_ref, real_ref): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4139 | print() |
| 4140 | print('Waiting for commit to be landed on %s...' % real_ref) |
| 4141 | print('(If you are impatient, you may Ctrl-C once without harm)') |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4142 | target_tree = RunGit(['rev-parse', '%s:' % pushed_commit]).strip() |
| 4143 | current_rev = RunGit(['rev-parse', local_base_ref]).strip() |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 4144 | mirror = settings.GetGitMirror(remote) |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4145 | |
| 4146 | loop = 0 |
| 4147 | while True: |
| 4148 | sys.stdout.write('fetching (%d)... \r' % loop) |
| 4149 | sys.stdout.flush() |
| 4150 | loop += 1 |
| 4151 | |
szager@chromium.org | 151ebcf | 2016-03-09 01:08:25 +0000 | [diff] [blame] | 4152 | if mirror: |
| 4153 | mirror.populate() |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4154 | RunGit(['retry', 'fetch', remote, real_ref], stderr=subprocess2.VOID) |
| 4155 | to_rev = RunGit(['rev-parse', 'FETCH_HEAD']).strip() |
| 4156 | commits = RunGit(['rev-list', '%s..%s' % (current_rev, to_rev)]) |
| 4157 | for commit in commits.splitlines(): |
| 4158 | if RunGit(['rev-parse', '%s:' % commit]).strip() == target_tree: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4159 | print('Found commit on %s' % real_ref) |
iannucci@chromium.org | e6896b5 | 2014-08-29 01:38:03 +0000 | [diff] [blame] | 4160 | return commit |
| 4161 | |
| 4162 | current_rev = to_rev |
| 4163 | |
| 4164 | |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4165 | def PushToGitPending(remote, pending_ref, upstream_ref): |
| 4166 | """Fetches pending_ref, cherry-picks current HEAD on top of it, pushes. |
| 4167 | |
| 4168 | Returns: |
| 4169 | (retcode of last operation, output log of last operation). |
| 4170 | """ |
| 4171 | assert pending_ref.startswith('refs/'), pending_ref |
| 4172 | local_pending_ref = 'refs/git-cl/' + pending_ref[len('refs/'):] |
| 4173 | cherry = RunGit(['rev-parse', 'HEAD']).strip() |
| 4174 | code = 0 |
| 4175 | out = '' |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4176 | max_attempts = 3 |
| 4177 | attempts_left = max_attempts |
| 4178 | while attempts_left: |
| 4179 | if attempts_left != max_attempts: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4180 | print('Retrying, %d attempts left...' % (attempts_left - 1,)) |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4181 | attempts_left -= 1 |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4182 | |
| 4183 | # Fetch. Retry fetch errors. |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4184 | print('Fetching pending ref %s...' % pending_ref) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4185 | code, out = RunGitWithCode( |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4186 | ['retry', 'fetch', remote, '+%s:%s' % (pending_ref, local_pending_ref)]) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4187 | if code: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4188 | print('Fetch failed with exit code %d.' % code) |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4189 | if out.strip(): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4190 | print(out.strip()) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4191 | continue |
| 4192 | |
| 4193 | # Try to cherry pick. Abort on merge conflicts. |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4194 | print('Cherry-picking commit on top of pending ref...') |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4195 | RunGitWithCode(['checkout', local_pending_ref], suppress_stderr=True) |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4196 | code, out = RunGitWithCode(['cherry-pick', cherry]) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4197 | if code: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4198 | print('Your patch doesn\'t apply cleanly to ref \'%s\', ' |
| 4199 | 'the following files have merge conflicts:' % pending_ref) |
| 4200 | print(RunGit(['diff', '--name-status', '--diff-filter=U']).strip()) |
| 4201 | print('Please rebase your patch and try again.') |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4202 | RunGitWithCode(['cherry-pick', '--abort']) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4203 | return code, out |
| 4204 | |
| 4205 | # Applied cleanly, try to push now. Retry on error (flake or non-ff push). |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4206 | print('Pushing commit to %s... It can take a while.' % pending_ref) |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4207 | code, out = RunGitWithCode( |
| 4208 | ['retry', 'push', '--porcelain', remote, 'HEAD:%s' % pending_ref]) |
| 4209 | if code == 0: |
| 4210 | # Success. |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4211 | print('Commit pushed to pending ref successfully!') |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4212 | return code, out |
| 4213 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4214 | print('Push failed with exit code %d.' % code) |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4215 | if out.strip(): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4216 | print(out.strip()) |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4217 | if IsFatalPushFailure(out): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4218 | print('Fatal push error. Make sure your .netrc credentials and git ' |
| 4219 | 'user.email are correct and you have push access to the repo.') |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4220 | return code, out |
| 4221 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4222 | print('All attempts to push to pending ref failed.') |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4223 | return code, out |
| 4224 | |
| 4225 | |
vadimsh@chromium.org | 749fbd9 | 2014-08-26 21:57:53 +0000 | [diff] [blame] | 4226 | def IsFatalPushFailure(push_stdout): |
| 4227 | """True if retrying push won't help.""" |
| 4228 | return '(prohibited by Gerrit)' in push_stdout |
| 4229 | |
| 4230 | |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 4231 | @subcommand.usage('[upstream branch to apply against]') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4232 | def CMDdcommit(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 4233 | """Commits the current changelist via git-svn.""" |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4234 | if not settings.GetIsGitSvn(): |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 4235 | if git_footers.get_footer_svn_id(): |
mmoss@chromium.org | f0e4152 | 2015-06-10 19:52:01 +0000 | [diff] [blame] | 4236 | # If it looks like previous commits were mirrored with git-svn. |
| 4237 | message = """This repository appears to be a git-svn mirror, but no |
| 4238 | upstream SVN master is set. You probably need to run 'git auto-svn' once.""" |
| 4239 | else: |
| 4240 | message = """This doesn't appear to be an SVN repository. |
| 4241 | If your project has a true, writeable git repository, you probably want to run |
| 4242 | 'git cl land' instead. |
| 4243 | If your project has a git mirror of an upstream SVN master, you probably need |
| 4244 | to run 'git svn init'. |
| 4245 | |
| 4246 | Using the wrong command might cause your commit to appear to succeed, and the |
| 4247 | review to be closed, without actually landing upstream. If you choose to |
| 4248 | proceed, please verify that the commit lands upstream as expected.""" |
thakis@chromium.org | cde3bb6 | 2011-01-20 01:16:14 +0000 | [diff] [blame] | 4249 | print(message) |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 4250 | ask_for_data('[Press enter to dcommit or ctrl-C to quit]') |
tandrii | 3bb82ff | 2016-06-17 07:36:36 -0700 | [diff] [blame] | 4251 | # TODO(tandrii): kill this post SVN migration with |
| 4252 | # https://codereview.chromium.org/2076683002 |
| 4253 | print('WARNING: chrome infrastructure is migrating SVN repos to Git.\n' |
| 4254 | 'Please let us know of this project you are committing to:' |
| 4255 | ' http://crbug.com/600451') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4256 | return SendUpstream(parser, args, 'dcommit') |
| 4257 | |
| 4258 | |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 4259 | @subcommand.usage('[upstream branch to apply against]') |
pgervais@chromium.org | cee6dc4 | 2014-05-07 17:04:03 +0000 | [diff] [blame] | 4260 | def CMDland(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 4261 | """Commits the current changelist via git.""" |
tandrii@chromium.org | 09d7a6a | 2016-03-04 15:44:48 +0000 | [diff] [blame] | 4262 | if settings.GetIsGitSvn() or git_footers.get_footer_svn_id(): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4263 | print('This appears to be an SVN repository.') |
| 4264 | print('Are you sure you didn\'t mean \'git cl dcommit\'?') |
mmoss@chromium.org | f0e4152 | 2015-06-10 19:52:01 +0000 | [diff] [blame] | 4265 | print('(Ignore if this is the first commit after migrating from svn->git)') |
maruel@chromium.org | 9054173 | 2011-04-01 17:54:18 +0000 | [diff] [blame] | 4266 | ask_for_data('[Press enter to push or ctrl-C to quit]') |
vadimsh@chromium.org | 566a02a | 2014-08-22 01:34:13 +0000 | [diff] [blame] | 4267 | return SendUpstream(parser, args, 'land') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4268 | |
| 4269 | |
dsinclair@chromium.org | fbed656 | 2015-09-25 21:22:36 +0000 | [diff] [blame] | 4270 | @subcommand.usage('<patch url or issue id or issue url>') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4271 | def CMDpatch(parser, args): |
marq@chromium.org | e5e5900 | 2013-10-02 23:21:25 +0000 | [diff] [blame] | 4272 | """Patches in a code review.""" |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4273 | parser.add_option('-b', dest='newbranch', |
| 4274 | help='create a new branch off trunk for the patch') |
qsr@chromium.org | 1ef44af | 2013-10-16 16:24:32 +0000 | [diff] [blame] | 4275 | parser.add_option('-f', '--force', action='store_true', |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4276 | help='with -b, clobber any existing branch') |
qsr@chromium.org | 1ef44af | 2013-10-16 16:24:32 +0000 | [diff] [blame] | 4277 | parser.add_option('-d', '--directory', action='store', metavar='DIR', |
| 4278 | help='Change to the directory DIR immediately, ' |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4279 | 'before doing anything else. Rietveld only.') |
qsr@chromium.org | 1ef44af | 2013-10-16 16:24:32 +0000 | [diff] [blame] | 4280 | parser.add_option('--reject', action='store_true', |
tapted@chromium.org | 6a0b07c | 2013-07-10 01:29:19 +0000 | [diff] [blame] | 4281 | help='failed patches spew .rej files rather than ' |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4282 | 'attempting a 3-way merge. Rietveld only.') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4283 | parser.add_option('-n', '--no-commit', action='store_true', dest='nocommit', |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4284 | help='don\'t commit after patch applies. Rietveld only.') |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4285 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4286 | |
| 4287 | group = optparse.OptionGroup( |
| 4288 | parser, |
| 4289 | 'Options for continuing work on the current issue uploaded from a ' |
| 4290 | 'different clone (e.g. different machine). Must be used independently ' |
| 4291 | 'from the other options. No issue number should be specified, and the ' |
| 4292 | 'branch must have an issue number associated with it') |
| 4293 | group.add_option('--reapply', action='store_true', dest='reapply', |
| 4294 | help='Reset the branch and reapply the issue.\n' |
| 4295 | 'CAUTION: This will undo any local changes in this ' |
| 4296 | 'branch') |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4297 | |
| 4298 | group.add_option('--pull', action='store_true', dest='pull', |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4299 | help='Performs a pull before reapplying.') |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4300 | parser.add_option_group(group) |
| 4301 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4302 | auth.add_auth_options(parser) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 4303 | _add_codereview_select_options(parser) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4304 | (options, args) = parser.parse_args(args) |
tandrii@chromium.org | dde6462 | 2016-04-13 17:11:21 +0000 | [diff] [blame] | 4305 | _process_codereview_select_options(parser, options) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4306 | auth_config = auth.extract_auth_config_from_options(options) |
| 4307 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4308 | |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4309 | if options.reapply : |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 4310 | if options.newbranch: |
| 4311 | parser.error('--reapply works on the current branch only') |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4312 | if len(args) > 0: |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 4313 | parser.error('--reapply implies no additional arguments') |
dsinclair@chromium.org | fbed656 | 2015-09-25 21:22:36 +0000 | [diff] [blame] | 4314 | |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 4315 | cl = Changelist(auth_config=auth_config, |
| 4316 | codereview=options.forced_codereview) |
| 4317 | if not cl.GetIssue(): |
| 4318 | parser.error('current branch must have an associated issue') |
| 4319 | |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4320 | upstream = cl.GetUpstreamBranch() |
| 4321 | if upstream == None: |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4322 | parser.error('No upstream branch specified. Cannot reset branch') |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4323 | |
| 4324 | RunGit(['reset', '--hard', upstream]) |
| 4325 | if options.pull: |
| 4326 | RunGit(['pull']) |
mtrofin@chromium.org | 1d88dd3 | 2016-02-04 16:25:12 +0000 | [diff] [blame] | 4327 | |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 4328 | return cl.CMDPatchIssue(cl.GetIssue(), options.reject, options.nocommit, |
| 4329 | options.directory) |
| 4330 | |
| 4331 | if len(args) != 1 or not args[0]: |
| 4332 | parser.error('Must specify issue number or url') |
| 4333 | |
| 4334 | # We don't want uncommitted changes mixed up with the patch. |
| 4335 | if git_common.is_dirty_git_tree('patch'): |
dsinclair@chromium.org | fbed656 | 2015-09-25 21:22:36 +0000 | [diff] [blame] | 4336 | return 1 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4337 | |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 4338 | if options.newbranch: |
| 4339 | if options.force: |
| 4340 | RunGit(['branch', '-D', options.newbranch], |
| 4341 | stderr=subprocess2.PIPE, error_ok=True) |
| 4342 | RunGit(['new-branch', options.newbranch]) |
| 4343 | |
| 4344 | cl = Changelist(auth_config=auth_config, codereview=options.forced_codereview) |
| 4345 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4346 | if cl.IsGerrit(): |
| 4347 | if options.reject: |
| 4348 | parser.error('--reject is not supported with Gerrit codereview.') |
| 4349 | if options.nocommit: |
| 4350 | parser.error('--nocommit is not supported with Gerrit codereview.') |
| 4351 | if options.directory: |
| 4352 | parser.error('--directory is not supported with Gerrit codereview.') |
| 4353 | |
tandrii@chromium.org | c2786d9 | 2016-05-31 19:53:50 +0000 | [diff] [blame] | 4354 | return cl.CMDPatchIssue(args[0], options.reject, options.nocommit, |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4355 | options.directory) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4356 | |
| 4357 | |
| 4358 | def CMDrebase(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 4359 | """Rebases current branch on top of svn repo.""" |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4360 | # Provide a wrapper for git svn rebase to help avoid accidental |
| 4361 | # git svn dcommit. |
| 4362 | # It's the only command that doesn't use parser at all since we just defer |
| 4363 | # execution to git-svn. |
bratell@opera.com | 82b91cd | 2013-07-09 06:33:41 +0000 | [diff] [blame] | 4364 | |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 4365 | return RunGitWithCode(['svn', 'rebase'] + args)[1] |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4366 | |
| 4367 | |
jochen@chromium.org | 3ec0d54 | 2014-01-14 20:00:03 +0000 | [diff] [blame] | 4368 | def GetTreeStatus(url=None): |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4369 | """Fetches the tree status and returns either 'open', 'closed', |
| 4370 | 'unknown' or 'unset'.""" |
jochen@chromium.org | 3ec0d54 | 2014-01-14 20:00:03 +0000 | [diff] [blame] | 4371 | url = url or settings.GetTreeStatusUrl(error_ok=True) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4372 | if url: |
| 4373 | status = urllib2.urlopen(url).read().lower() |
| 4374 | if status.find('closed') != -1 or status == '0': |
| 4375 | return 'closed' |
| 4376 | elif status.find('open') != -1 or status == '1': |
| 4377 | return 'open' |
| 4378 | return 'unknown' |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4379 | return 'unset' |
| 4380 | |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 4381 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4382 | def GetTreeStatusReason(): |
| 4383 | """Fetches the tree status from a json url and returns the message |
| 4384 | with the reason for the tree to be opened or closed.""" |
msb@chromium.org | bf1a7ba | 2011-02-01 16:21:46 +0000 | [diff] [blame] | 4385 | url = settings.GetTreeStatusUrl() |
| 4386 | json_url = urlparse.urljoin(url, '/current?format=json') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4387 | connection = urllib2.urlopen(json_url) |
| 4388 | status = json.loads(connection.read()) |
| 4389 | connection.close() |
| 4390 | return status['message'] |
| 4391 | |
dpranke@chromium.org | 970c522 | 2011-03-12 00:32:24 +0000 | [diff] [blame] | 4392 | |
sheyang@chromium.org | 2b34d55 | 2014-08-14 22:18:42 +0000 | [diff] [blame] | 4393 | def GetBuilderMaster(bot_list): |
| 4394 | """For a given builder, fetch the master from AE if available.""" |
| 4395 | map_url = 'https://builders-map.appspot.com/' |
| 4396 | try: |
| 4397 | master_map = json.load(urllib2.urlopen(map_url)) |
| 4398 | except urllib2.URLError as e: |
| 4399 | return None, ('Failed to fetch builder-to-master map from %s. Error: %s.' % |
| 4400 | (map_url, e)) |
| 4401 | except ValueError as e: |
| 4402 | return None, ('Invalid json string from %s. Error: %s.' % (map_url, e)) |
| 4403 | if not master_map: |
| 4404 | return None, 'Failed to build master map.' |
| 4405 | |
| 4406 | result_master = '' |
| 4407 | for bot in bot_list: |
| 4408 | builder = bot.split(':', 1)[0] |
| 4409 | master_list = master_map.get(builder, []) |
| 4410 | if not master_list: |
| 4411 | return None, ('No matching master for builder %s.' % builder) |
| 4412 | elif len(master_list) > 1: |
| 4413 | return None, ('The builder name %s exists in multiple masters %s.' % |
| 4414 | (builder, master_list)) |
| 4415 | else: |
| 4416 | cur_master = master_list[0] |
| 4417 | if not result_master: |
| 4418 | result_master = cur_master |
| 4419 | elif result_master != cur_master: |
| 4420 | return None, 'The builders do not belong to the same master.' |
| 4421 | return result_master, None |
| 4422 | |
| 4423 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4424 | def CMDtree(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 4425 | """Shows the status of the tree.""" |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 4426 | _, args = parser.parse_args(args) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4427 | status = GetTreeStatus() |
| 4428 | if 'unset' == status: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4429 | print('You must configure your tree status URL by running "git cl config".') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4430 | return 2 |
| 4431 | |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4432 | print('The tree is %s' % status) |
| 4433 | print() |
| 4434 | print(GetTreeStatusReason()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4435 | if status != 'open': |
| 4436 | return 1 |
| 4437 | return 0 |
| 4438 | |
| 4439 | |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4440 | def CMDtry(parser, args): |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 4441 | """Triggers try jobs through BuildBucket.""" |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4442 | group = optparse.OptionGroup(parser, "Try job options") |
| 4443 | group.add_option( |
| 4444 | "-b", "--bot", action="append", |
| 4445 | help=("IMPORTANT: specify ONE builder per --bot flag. Use it multiple " |
| 4446 | "times to specify multiple builders. ex: " |
phajdan.jr@chromium.org | 5291413 | 2015-01-22 10:37:09 +0000 | [diff] [blame] | 4447 | "'-b win_rel -b win_layout'. See " |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4448 | "the try server waterfall for the builders name and the tests " |
phajdan.jr@chromium.org | 5291413 | 2015-01-22 10:37:09 +0000 | [diff] [blame] | 4449 | "available.")) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4450 | group.add_option( |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4451 | "-m", "--master", default='', |
iannucci@chromium.org | 9e84927 | 2014-04-04 00:31:55 +0000 | [diff] [blame] | 4452 | help=("Specify a try master where to run the tries.")) |
hinoka@chromium.org | feb9e2a | 2015-09-25 19:11:09 +0000 | [diff] [blame] | 4453 | group.add_option( "--luci", action='store_true') |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4454 | group.add_option( |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4455 | "-r", "--revision", |
| 4456 | help="Revision to use for the try job; default: the " |
| 4457 | "revision will be determined by the try server; see " |
| 4458 | "its waterfall for more info") |
| 4459 | group.add_option( |
| 4460 | "-c", "--clobber", action="store_true", default=False, |
| 4461 | help="Force a clobber before building; e.g. don't do an " |
| 4462 | "incremental build") |
| 4463 | group.add_option( |
| 4464 | "--project", |
| 4465 | help="Override which project to use. Projects are defined " |
| 4466 | "server-side to define what default bot set to use") |
| 4467 | group.add_option( |
machenbach@chromium.org | 4545314 | 2015-09-15 08:45:22 +0000 | [diff] [blame] | 4468 | "-p", "--property", dest="properties", action="append", default=[], |
| 4469 | help="Specify generic properties in the form -p key1=value1 -p " |
| 4470 | "key2=value2 etc (buildbucket only). The value will be treated as " |
| 4471 | "json if decodable, or as string otherwise.") |
| 4472 | group.add_option( |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4473 | "-n", "--name", help="Try job name; default to current branch name") |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 4474 | group.add_option( |
sheyang@chromium.org | db37557 | 2015-08-17 19:22:23 +0000 | [diff] [blame] | 4475 | "--use-rietveld", action="store_true", default=False, |
| 4476 | help="Use Rietveld to trigger try jobs.") |
| 4477 | group.add_option( |
| 4478 | "--buildbucket-host", default='cr-buildbucket.appspot.com', |
| 4479 | help="Host of buildbucket. The default host is %default.") |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4480 | parser.add_option_group(group) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4481 | auth.add_auth_options(parser) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4482 | options, args = parser.parse_args(args) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4483 | auth_config = auth.extract_auth_config_from_options(options) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4484 | |
machenbach@chromium.org | 4545314 | 2015-09-15 08:45:22 +0000 | [diff] [blame] | 4485 | if options.use_rietveld and options.properties: |
| 4486 | parser.error('Properties can only be specified with buildbucket') |
| 4487 | |
| 4488 | # Make sure that all properties are prop=value pairs. |
| 4489 | bad_params = [x for x in options.properties if '=' not in x] |
| 4490 | if bad_params: |
| 4491 | parser.error('Got properties with missing "=": %s' % bad_params) |
| 4492 | |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4493 | if args: |
| 4494 | parser.error('Unknown arguments: %s' % args) |
| 4495 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4496 | cl = Changelist(auth_config=auth_config) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4497 | if not cl.GetIssue(): |
| 4498 | parser.error('Need to upload first') |
| 4499 | |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 4500 | if cl.IsGerrit(): |
| 4501 | parser.error( |
| 4502 | 'Not yet supported for Gerrit (http://crbug.com/599931).\n' |
| 4503 | 'If your project has Commit Queue, dry run is a workaround:\n' |
| 4504 | ' git cl set-commit --dry-run') |
| 4505 | # Code below assumes Rietveld issue. |
| 4506 | # TODO(tandrii): actually implement for Gerrit http://crbug.com/599931. |
| 4507 | |
jrobbins@chromium.org | 16f10f7 | 2014-06-24 22:14:36 +0000 | [diff] [blame] | 4508 | props = cl.GetIssueProperties() |
agable@chromium.org | 787e306 | 2014-08-20 16:31:19 +0000 | [diff] [blame] | 4509 | if props.get('closed'): |
| 4510 | parser.error('Cannot send tryjobs for a closed CL') |
| 4511 | |
jrobbins@chromium.org | 16f10f7 | 2014-06-24 22:14:36 +0000 | [diff] [blame] | 4512 | if props.get('private'): |
| 4513 | parser.error('Cannot use trybots with private issue') |
| 4514 | |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4515 | if not options.name: |
| 4516 | options.name = cl.GetBranch() |
| 4517 | |
phajdan.jr@chromium.org | 8da7f27 | 2014-03-14 01:28:39 +0000 | [diff] [blame] | 4518 | if options.bot and not options.master: |
sheyang@chromium.org | 2b34d55 | 2014-08-14 22:18:42 +0000 | [diff] [blame] | 4519 | options.master, err_msg = GetBuilderMaster(options.bot) |
| 4520 | if err_msg: |
| 4521 | parser.error('Tryserver master cannot be found because: %s\n' |
| 4522 | 'Please manually specify the tryserver master' |
| 4523 | ', e.g. "-m tryserver.chromium.linux".' % err_msg) |
phajdan.jr@chromium.org | 8da7f27 | 2014-03-14 01:28:39 +0000 | [diff] [blame] | 4524 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4525 | def GetMasterMap(): |
phajdan.jr@chromium.org | 5291413 | 2015-01-22 10:37:09 +0000 | [diff] [blame] | 4526 | # Process --bot. |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4527 | if not options.bot: |
| 4528 | change = cl.GetChange(cl.GetCommonAncestorWithUpstream(), None) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4529 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4530 | # Get try masters from PRESUBMIT.py files. |
| 4531 | masters = presubmit_support.DoGetTryMasters( |
| 4532 | change, |
| 4533 | change.LocalPaths(), |
| 4534 | settings.GetRoot(), |
| 4535 | None, |
| 4536 | None, |
| 4537 | options.verbose, |
| 4538 | sys.stdout) |
| 4539 | if masters: |
| 4540 | return masters |
stip@chromium.org | 43064fd | 2013-12-18 20:07:44 +0000 | [diff] [blame] | 4541 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4542 | # Fall back to deprecated method: get try slaves from PRESUBMIT.py files. |
| 4543 | options.bot = presubmit_support.DoGetTrySlaves( |
| 4544 | change, |
| 4545 | change.LocalPaths(), |
| 4546 | settings.GetRoot(), |
| 4547 | None, |
| 4548 | None, |
| 4549 | options.verbose, |
| 4550 | sys.stdout) |
tandrii@chromium.org | 71184c0 | 2016-01-13 15:18:44 +0000 | [diff] [blame] | 4551 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4552 | if not options.bot: |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 4553 | return {} |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4554 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4555 | builders_and_tests = {} |
| 4556 | # TODO(machenbach): The old style command-line options don't support |
| 4557 | # multiple try masters yet. |
| 4558 | old_style = filter(lambda x: isinstance(x, basestring), options.bot) |
| 4559 | new_style = filter(lambda x: isinstance(x, tuple), options.bot) |
| 4560 | |
| 4561 | for bot in old_style: |
| 4562 | if ':' in bot: |
phajdan.jr@chromium.org | 5291413 | 2015-01-22 10:37:09 +0000 | [diff] [blame] | 4563 | parser.error('Specifying testfilter is no longer supported') |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4564 | elif ',' in bot: |
| 4565 | parser.error('Specify one bot per --bot flag') |
| 4566 | else: |
tandrii@chromium.org | 3764fa2 | 2015-10-21 16:40:40 +0000 | [diff] [blame] | 4567 | builders_and_tests.setdefault(bot, []) |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4568 | |
| 4569 | for bot, tests in new_style: |
| 4570 | builders_and_tests.setdefault(bot, []).extend(tests) |
| 4571 | |
| 4572 | # Return a master map with one master to be backwards compatible. The |
| 4573 | # master name defaults to an empty string, which will cause the master |
| 4574 | # not to be set on rietveld (deprecated). |
| 4575 | return {options.master: builders_and_tests} |
| 4576 | |
| 4577 | masters = GetMasterMap() |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 4578 | if not masters: |
| 4579 | # Default to triggering Dry Run (see http://crbug.com/625697). |
| 4580 | if options.verbose: |
| 4581 | print('git cl try with no bots now defaults to CQ Dry Run.') |
| 4582 | try: |
| 4583 | cl.SetCQState(_CQState.DRY_RUN) |
| 4584 | print('scheduled CQ Dry Run on %s' % cl.GetIssueURL()) |
| 4585 | return 0 |
| 4586 | except KeyboardInterrupt: |
| 4587 | raise |
| 4588 | except: |
| 4589 | print('WARNING: failed to trigger CQ Dry Run.\n' |
| 4590 | 'Either:\n' |
| 4591 | ' * your project has no CQ\n' |
| 4592 | ' * you don\'t have permission to trigger Dry Run\n' |
| 4593 | ' * bug in this code (see stack trace below).\n' |
| 4594 | 'Consider specifying which bots to trigger manually ' |
| 4595 | 'or asking your project owners for permissions ' |
| 4596 | 'or contacting Chrome Infrastructure team at ' |
| 4597 | 'https://www.chromium.org/infra\n\n') |
| 4598 | # Still raise exception so that stack trace is printed. |
| 4599 | raise |
stip@chromium.org | 43064fd | 2013-12-18 20:07:44 +0000 | [diff] [blame] | 4600 | |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4601 | for builders in masters.itervalues(): |
| 4602 | if any('triggered' in b for b in builders): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4603 | print('ERROR You are trying to send a job to a triggered bot. This type ' |
| 4604 | 'of bot requires an\ninitial job from a parent (usually a builder).' |
| 4605 | ' Instead send your job to the parent.\n' |
| 4606 | 'Bot list: %s' % builders, file=sys.stderr) |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4607 | return 1 |
ilevy@chromium.org | f3b2123 | 2012-09-24 20:48:55 +0000 | [diff] [blame] | 4608 | |
ilevy@chromium.org | 36e420b | 2013-08-06 23:21:12 +0000 | [diff] [blame] | 4609 | patchset = cl.GetMostRecentPatchset() |
| 4610 | if patchset and patchset != cl.GetPatchset(): |
| 4611 | print( |
| 4612 | '\nWARNING Mismatch between local config and server. Did a previous ' |
| 4613 | 'upload fail?\ngit-cl try always uses latest patchset from rietveld. ' |
| 4614 | 'Continuing using\npatchset %s.\n' % patchset) |
hinoka@chromium.org | feb9e2a | 2015-09-25 19:11:09 +0000 | [diff] [blame] | 4615 | if options.luci: |
| 4616 | trigger_luci_job(cl, masters, options) |
| 4617 | elif not options.use_rietveld: |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 4618 | try: |
| 4619 | trigger_try_jobs(auth_config, cl, options, masters, 'git_cl_try') |
| 4620 | except BuildbucketResponseException as ex: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4621 | print('ERROR: %s' % ex) |
fischman@chromium.org | d246c97 | 2013-12-21 22:47:38 +0000 | [diff] [blame] | 4622 | return 1 |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 4623 | except Exception as e: |
| 4624 | stacktrace = (''.join(traceback.format_stack()) + traceback.format_exc()) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4625 | print('ERROR: Exception when trying to trigger tryjobs: %s\n%s' % |
| 4626 | (e, stacktrace)) |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 4627 | return 1 |
| 4628 | else: |
| 4629 | try: |
| 4630 | cl.RpcServer().trigger_distributed_try_jobs( |
| 4631 | cl.GetIssue(), patchset, options.name, options.clobber, |
| 4632 | options.revision, masters) |
| 4633 | except urllib2.HTTPError as e: |
| 4634 | if e.code == 404: |
| 4635 | print('404 from rietveld; ' |
| 4636 | 'did you mean to use "git try" instead of "git cl try"?') |
| 4637 | return 1 |
| 4638 | print('Tried jobs on:') |
machenbach@chromium.org | 58a69cb | 2014-03-01 02:08:29 +0000 | [diff] [blame] | 4639 | |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 4640 | for (master, builders) in sorted(masters.iteritems()): |
| 4641 | if master: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4642 | print('Master: %s' % master) |
sheyang@google.com | 6ebaf78 | 2015-05-12 19:17:54 +0000 | [diff] [blame] | 4643 | length = max(len(builder) for builder in builders) |
| 4644 | for builder in sorted(builders): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4645 | print(' %*s: %s' % (length, builder, ','.join(builders[builder]))) |
maruel@chromium.org | 1519240 | 2012-09-06 12:38:29 +0000 | [diff] [blame] | 4646 | return 0 |
| 4647 | |
| 4648 | |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 4649 | def CMDtry_results(parser, args): |
| 4650 | group = optparse.OptionGroup(parser, "Try job results options") |
| 4651 | group.add_option( |
| 4652 | "-p", "--patchset", type=int, help="patchset number if not current.") |
| 4653 | group.add_option( |
tandrii@chromium.org | 6cf98c8 | 2016-03-15 11:56:00 +0000 | [diff] [blame] | 4654 | "--print-master", action='store_true', help="print master name as well.") |
| 4655 | group.add_option( |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 4656 | "--color", action='store_true', default=setup_color.IS_TTY, |
tandrii@chromium.org | 6cf98c8 | 2016-03-15 11:56:00 +0000 | [diff] [blame] | 4657 | help="force color output, useful when piping output.") |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 4658 | group.add_option( |
| 4659 | "--buildbucket-host", default='cr-buildbucket.appspot.com', |
| 4660 | help="Host of buildbucket. The default host is %default.") |
| 4661 | parser.add_option_group(group) |
| 4662 | auth.add_auth_options(parser) |
| 4663 | options, args = parser.parse_args(args) |
| 4664 | if args: |
| 4665 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
| 4666 | |
| 4667 | auth_config = auth.extract_auth_config_from_options(options) |
| 4668 | cl = Changelist(auth_config=auth_config) |
| 4669 | if not cl.GetIssue(): |
| 4670 | parser.error('Need to upload first') |
| 4671 | |
| 4672 | if not options.patchset: |
| 4673 | options.patchset = cl.GetMostRecentPatchset() |
| 4674 | if options.patchset and options.patchset != cl.GetPatchset(): |
| 4675 | print( |
| 4676 | '\nWARNING Mismatch between local config and server. Did a previous ' |
| 4677 | 'upload fail?\ngit-cl try always uses latest patchset from rietveld. ' |
| 4678 | 'Continuing using\npatchset %s.\n' % options.patchset) |
| 4679 | try: |
| 4680 | jobs = fetch_try_jobs(auth_config, cl, options) |
| 4681 | except BuildbucketResponseException as ex: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4682 | print('Buildbucket error: %s' % ex) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 4683 | return 1 |
| 4684 | except Exception as e: |
| 4685 | stacktrace = (''.join(traceback.format_stack()) + traceback.format_exc()) |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4686 | print('ERROR: Exception when trying to fetch tryjobs: %s\n%s' % |
| 4687 | (e, stacktrace)) |
tandrii@chromium.org | b015fac | 2016-02-26 14:52:01 +0000 | [diff] [blame] | 4688 | return 1 |
| 4689 | print_tryjobs(options, jobs) |
| 4690 | return 0 |
| 4691 | |
| 4692 | |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 4693 | @subcommand.usage('[new upstream branch]') |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4694 | def CMDupstream(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 4695 | """Prints or sets the name of the upstream branch, if any.""" |
dpranke@chromium.org | 97ae58e | 2011-03-18 00:29:20 +0000 | [diff] [blame] | 4696 | _, args = parser.parse_args(args) |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 4697 | if len(args) > 1: |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 4698 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 4699 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4700 | cl = Changelist() |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 4701 | if args: |
| 4702 | # One arg means set upstream branch. |
bauerb@chromium.org | c9cf90a | 2014-04-28 20:32:31 +0000 | [diff] [blame] | 4703 | branch = cl.GetBranch() |
| 4704 | RunGit(['branch', '--set-upstream', branch, args[0]]) |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 4705 | cl = Changelist() |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4706 | print('Upstream branch set to %s' % (cl.GetUpstreamBranch(),)) |
bauerb@chromium.org | c9cf90a | 2014-04-28 20:32:31 +0000 | [diff] [blame] | 4707 | |
| 4708 | # Clear configured merge-base, if there is one. |
| 4709 | git_common.remove_merge_base(branch) |
brettw@chromium.org | ac0ba33 | 2012-08-09 23:42:53 +0000 | [diff] [blame] | 4710 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4711 | print(cl.GetUpstreamBranch()) |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 4712 | return 0 |
| 4713 | |
| 4714 | |
thestig@chromium.org | 00858c8 | 2013-12-02 23:08:03 +0000 | [diff] [blame] | 4715 | def CMDweb(parser, args): |
| 4716 | """Opens the current CL in the web browser.""" |
| 4717 | _, args = parser.parse_args(args) |
| 4718 | if args: |
| 4719 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
| 4720 | |
| 4721 | issue_url = Changelist().GetIssueURL() |
| 4722 | if not issue_url: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4723 | print('ERROR No issue to open', file=sys.stderr) |
thestig@chromium.org | 00858c8 | 2013-12-02 23:08:03 +0000 | [diff] [blame] | 4724 | return 1 |
| 4725 | |
| 4726 | webbrowser.open(issue_url) |
| 4727 | return 0 |
| 4728 | |
| 4729 | |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 4730 | def CMDset_commit(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 4731 | """Sets the commit bit to trigger the Commit Queue.""" |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 4732 | parser.add_option('-d', '--dry-run', action='store_true', |
| 4733 | help='trigger in dry run mode') |
| 4734 | parser.add_option('-c', '--clear', action='store_true', |
| 4735 | help='stop CQ run, if any') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4736 | auth.add_auth_options(parser) |
| 4737 | options, args = parser.parse_args(args) |
| 4738 | auth_config = auth.extract_auth_config_from_options(options) |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 4739 | if args: |
| 4740 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 4741 | if options.dry_run and options.clear: |
| 4742 | parser.error('Make up your mind: both --dry-run and --clear not allowed') |
| 4743 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4744 | cl = Changelist(auth_config=auth_config) |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 4745 | if options.clear: |
tandrii | d9e5ce5 | 2016-07-13 02:32:59 -0700 | [diff] [blame] | 4746 | state = _CQState.NONE |
tandrii@chromium.org | fa330e8 | 2016-04-13 17:09:52 +0000 | [diff] [blame] | 4747 | elif options.dry_run: |
| 4748 | state = _CQState.DRY_RUN |
| 4749 | else: |
| 4750 | state = _CQState.COMMIT |
| 4751 | if not cl.GetIssue(): |
| 4752 | parser.error('Must upload the issue first') |
tandrii | 9de9ec6 | 2016-07-13 03:01:59 -0700 | [diff] [blame] | 4753 | cl.SetCQState(state) |
maruel@chromium.org | 27bb387 | 2011-05-30 20:33:19 +0000 | [diff] [blame] | 4754 | return 0 |
| 4755 | |
| 4756 | |
groby@chromium.org | 411034a | 2013-02-26 15:12:01 +0000 | [diff] [blame] | 4757 | def CMDset_close(parser, args): |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 4758 | """Closes the issue.""" |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4759 | auth.add_auth_options(parser) |
| 4760 | options, args = parser.parse_args(args) |
| 4761 | auth_config = auth.extract_auth_config_from_options(options) |
groby@chromium.org | 411034a | 2013-02-26 15:12:01 +0000 | [diff] [blame] | 4762 | if args: |
| 4763 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4764 | cl = Changelist(auth_config=auth_config) |
groby@chromium.org | 411034a | 2013-02-26 15:12:01 +0000 | [diff] [blame] | 4765 | # Ensure there actually is an issue to close. |
| 4766 | cl.GetDescription() |
| 4767 | cl.CloseIssue() |
| 4768 | return 0 |
| 4769 | |
| 4770 | |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4771 | def CMDdiff(parser, args): |
wychen@chromium.org | 37b2ec0 | 2015-04-03 00:49:15 +0000 | [diff] [blame] | 4772 | """Shows differences between local tree and last upload.""" |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4773 | auth.add_auth_options(parser) |
| 4774 | options, args = parser.parse_args(args) |
| 4775 | auth_config = auth.extract_auth_config_from_options(options) |
| 4776 | if args: |
| 4777 | parser.error('Unrecognized args: %s' % ' '.join(args)) |
wychen@chromium.org | 46309bf | 2015-04-03 21:04:49 +0000 | [diff] [blame] | 4778 | |
| 4779 | # Uncommitted (staged and unstaged) changes will be destroyed by |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4780 | # "git reset --hard" if there are merging conflicts in CMDPatchIssue(). |
wychen@chromium.org | 46309bf | 2015-04-03 21:04:49 +0000 | [diff] [blame] | 4781 | # Staged changes would be committed along with the patch from last |
| 4782 | # upload, hence counted toward the "last upload" side in the final |
| 4783 | # diff output, and this is not what we want. |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 4784 | if git_common.is_dirty_git_tree('diff'): |
wychen@chromium.org | 46309bf | 2015-04-03 21:04:49 +0000 | [diff] [blame] | 4785 | return 1 |
| 4786 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4787 | cl = Changelist(auth_config=auth_config) |
sbc@chromium.org | 78dc984 | 2013-11-25 18:43:44 +0000 | [diff] [blame] | 4788 | issue = cl.GetIssue() |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4789 | branch = cl.GetBranch() |
sbc@chromium.org | 78dc984 | 2013-11-25 18:43:44 +0000 | [diff] [blame] | 4790 | if not issue: |
| 4791 | DieWithError('No issue found for current branch (%s)' % branch) |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4792 | TMP_BRANCH = 'git-cl-diff' |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 4793 | base_branch = cl.GetCommonAncestorWithUpstream() |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4794 | |
| 4795 | # Create a new branch based on the merge-base |
| 4796 | RunGit(['checkout', '-q', '-b', TMP_BRANCH, base_branch]) |
tandrii@chromium.org | 534f67a | 2016-04-07 18:47:05 +0000 | [diff] [blame] | 4797 | # Clear cached branch in cl object, to avoid overwriting original CL branch |
| 4798 | # properties. |
| 4799 | cl.ClearBranch() |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4800 | try: |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 4801 | rtn = cl.CMDPatchIssue(issue, reject=False, nocommit=False, directory=None) |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4802 | if rtn != 0: |
wychen@chromium.org | a872e75 | 2015-04-28 23:42:18 +0000 | [diff] [blame] | 4803 | RunGit(['reset', '--hard']) |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4804 | return rtn |
| 4805 | |
wychen@chromium.org | 0692853 | 2015-02-03 02:11:29 +0000 | [diff] [blame] | 4806 | # Switch back to starting branch and diff against the temporary |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4807 | # branch containing the latest rietveld patch. |
wychen@chromium.org | 0692853 | 2015-02-03 02:11:29 +0000 | [diff] [blame] | 4808 | subprocess2.check_call(['git', 'diff', TMP_BRANCH, branch, '--']) |
sbc@chromium.org | 87b9bf0 | 2013-09-26 20:35:15 +0000 | [diff] [blame] | 4809 | finally: |
| 4810 | RunGit(['checkout', '-q', branch]) |
| 4811 | RunGit(['branch', '-D', TMP_BRANCH]) |
| 4812 | |
| 4813 | return 0 |
| 4814 | |
| 4815 | |
ikarienator@chromium.org | faf3fdf | 2013-09-20 02:11:48 +0000 | [diff] [blame] | 4816 | def CMDowners(parser, args): |
wychen@chromium.org | 37b2ec0 | 2015-04-03 00:49:15 +0000 | [diff] [blame] | 4817 | """Interactively find the owners for reviewing.""" |
ikarienator@chromium.org | faf3fdf | 2013-09-20 02:11:48 +0000 | [diff] [blame] | 4818 | parser.add_option( |
| 4819 | '--no-color', |
| 4820 | action='store_true', |
| 4821 | help='Use this option to disable color output') |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4822 | auth.add_auth_options(parser) |
ikarienator@chromium.org | faf3fdf | 2013-09-20 02:11:48 +0000 | [diff] [blame] | 4823 | options, args = parser.parse_args(args) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4824 | auth_config = auth.extract_auth_config_from_options(options) |
ikarienator@chromium.org | faf3fdf | 2013-09-20 02:11:48 +0000 | [diff] [blame] | 4825 | |
| 4826 | author = RunGit(['config', 'user.email']).strip() or None |
| 4827 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 4828 | cl = Changelist(auth_config=auth_config) |
ikarienator@chromium.org | faf3fdf | 2013-09-20 02:11:48 +0000 | [diff] [blame] | 4829 | |
| 4830 | if args: |
| 4831 | if len(args) > 1: |
| 4832 | parser.error('Unknown args') |
| 4833 | base_branch = args[0] |
| 4834 | else: |
| 4835 | # Default to diffing against the common ancestor of the upstream branch. |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 4836 | base_branch = cl.GetCommonAncestorWithUpstream() |
ikarienator@chromium.org | faf3fdf | 2013-09-20 02:11:48 +0000 | [diff] [blame] | 4837 | |
| 4838 | change = cl.GetChange(base_branch, None) |
| 4839 | return owners_finder.OwnersFinder( |
| 4840 | [f.LocalPath() for f in |
| 4841 | cl.GetChange(base_branch, None).AffectedFiles()], |
| 4842 | change.RepositoryRoot(), author, |
dtu | 944b605 | 2016-07-14 14:48:21 -0700 | [diff] [blame] | 4843 | fopen=file, os_path=os.path, |
ikarienator@chromium.org | faf3fdf | 2013-09-20 02:11:48 +0000 | [diff] [blame] | 4844 | disable_color=options.no_color).run() |
| 4845 | |
| 4846 | |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4847 | def BuildGitDiffCmd(diff_type, upstream_commit, args): |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4848 | """Generates a diff command.""" |
| 4849 | # Generate diff for the current branch's changes. |
| 4850 | diff_cmd = ['diff', '--no-ext-diff', '--no-prefix', diff_type, |
| 4851 | upstream_commit, '--' ] |
| 4852 | |
| 4853 | if args: |
| 4854 | for arg in args: |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4855 | if os.path.isdir(arg) or os.path.isfile(arg): |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4856 | diff_cmd.append(arg) |
| 4857 | else: |
| 4858 | DieWithError('Argument "%s" is not a file or a directory' % arg) |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4859 | |
| 4860 | return diff_cmd |
| 4861 | |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4862 | def MatchingFileType(file_name, extensions): |
| 4863 | """Returns true if the file name ends with one of the given extensions.""" |
| 4864 | return bool([ext for ext in extensions if file_name.lower().endswith(ext)]) |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4865 | |
enne@chromium.org | 555cfe4 | 2014-01-29 18:21:39 +0000 | [diff] [blame] | 4866 | @subcommand.usage('[files or directories to diff]') |
agable@chromium.org | fab8f82 | 2013-05-06 17:43:09 +0000 | [diff] [blame] | 4867 | def CMDformat(parser, args): |
sbc@chromium.org | 9d0644d | 2015-06-05 23:16:54 +0000 | [diff] [blame] | 4868 | """Runs auto-formatting tools (clang-format etc.) on the diff.""" |
zengster | bf47014 | 2016-07-07 16:43:00 -0700 | [diff] [blame] | 4869 | CLANG_EXTS = ['.cc', '.cpp', '.h', '.m', '.mm', '.proto', '.java'] |
kylechar | 58edce2 | 2016-06-17 06:07:51 -0700 | [diff] [blame] | 4870 | GN_EXTS = ['.gn', '.gni', '.typemap'] |
enne@chromium.org | 3b7e15c | 2014-01-21 17:44:47 +0000 | [diff] [blame] | 4871 | parser.add_option('--full', action='store_true', |
| 4872 | help='Reformat the full content of all touched files') |
| 4873 | parser.add_option('--dry-run', action='store_true', |
| 4874 | help='Don\'t modify any file on disk.') |
sbc@chromium.org | 9d0644d | 2015-06-05 23:16:54 +0000 | [diff] [blame] | 4875 | parser.add_option('--python', action='store_true', |
| 4876 | help='Format python code with yapf (experimental).') |
wittman@chromium.org | 04d5a22 | 2014-03-07 18:30:42 +0000 | [diff] [blame] | 4877 | parser.add_option('--diff', action='store_true', |
| 4878 | help='Print diff to stdout rather than modifying files.') |
agable@chromium.org | fab8f82 | 2013-05-06 17:43:09 +0000 | [diff] [blame] | 4879 | opts, args = parser.parse_args(args) |
agable@chromium.org | fab8f82 | 2013-05-06 17:43:09 +0000 | [diff] [blame] | 4880 | |
enne@chromium.org | ff7a1fb | 2013-12-10 19:21:41 +0000 | [diff] [blame] | 4881 | # git diff generates paths against the root of the repository. Change |
| 4882 | # to that directory so clang-format can find files even within subdirs. |
thestig@chromium.org | 8b0553c | 2014-02-11 00:33:37 +0000 | [diff] [blame] | 4883 | rel_base_path = settings.GetRelativeRoot() |
enne@chromium.org | ff7a1fb | 2013-12-10 19:21:41 +0000 | [diff] [blame] | 4884 | if rel_base_path: |
| 4885 | os.chdir(rel_base_path) |
| 4886 | |
digit@chromium.org | 29e4727 | 2013-05-17 17:01:46 +0000 | [diff] [blame] | 4887 | # Grab the merge-base commit, i.e. the upstream commit of the current |
| 4888 | # branch when it was created or the last time it was rebased. This is |
| 4889 | # to cover the case where the user may have called "git fetch origin", |
| 4890 | # moving the origin branch to a newer commit, but hasn't rebased yet. |
| 4891 | upstream_commit = None |
| 4892 | cl = Changelist() |
| 4893 | upstream_branch = cl.GetUpstreamBranch() |
| 4894 | if upstream_branch: |
| 4895 | upstream_commit = RunGit(['merge-base', 'HEAD', upstream_branch]) |
| 4896 | upstream_commit = upstream_commit.strip() |
| 4897 | |
| 4898 | if not upstream_commit: |
| 4899 | DieWithError('Could not find base commit for this branch. ' |
| 4900 | 'Are you in detached state?') |
| 4901 | |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4902 | changed_files_cmd = BuildGitDiffCmd('--name-only', upstream_commit, args) |
| 4903 | diff_output = RunGit(changed_files_cmd) |
| 4904 | diff_files = diff_output.splitlines() |
jkarlin@chromium.org | ad21b92 | 2016-01-28 17:48:42 +0000 | [diff] [blame] | 4905 | # Filter out files deleted by this CL |
| 4906 | diff_files = [x for x in diff_files if os.path.isfile(x)] |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4907 | |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4908 | clang_diff_files = [x for x in diff_files if MatchingFileType(x, CLANG_EXTS)] |
| 4909 | python_diff_files = [x for x in diff_files if MatchingFileType(x, ['.py'])] |
| 4910 | dart_diff_files = [x for x in diff_files if MatchingFileType(x, ['.dart'])] |
kylechar@chromium.org | 8b61f11 | 2016-02-05 13:28:58 +0000 | [diff] [blame] | 4911 | gn_diff_files = [x for x in diff_files if MatchingFileType(x, GN_EXTS)] |
digit@chromium.org | 29e4727 | 2013-05-17 17:01:46 +0000 | [diff] [blame] | 4912 | |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 4913 | top_dir = os.path.normpath( |
| 4914 | RunGit(["rev-parse", "--show-toplevel"]).rstrip('\n')) |
| 4915 | |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4916 | # Set to 2 to signal to CheckPatchFormatted() that this patch isn't |
| 4917 | # formatted. This is used to block during the presubmit. |
| 4918 | return_value = 0 |
| 4919 | |
sammc@chromium.org | 0b35f5d | 2016-02-25 22:39:23 +0000 | [diff] [blame] | 4920 | if clang_diff_files: |
techtonik@gmail.com | 5573df1 | 2016-04-12 18:34:10 +0000 | [diff] [blame] | 4921 | # Locate the clang-format binary in the checkout |
| 4922 | try: |
| 4923 | clang_format_tool = clang_format.FindClangFormatToolInChromiumTree() |
vapier | fd77ac7 | 2016-06-16 08:33:57 -0700 | [diff] [blame] | 4924 | except clang_format.NotFoundError as e: |
techtonik@gmail.com | 5573df1 | 2016-04-12 18:34:10 +0000 | [diff] [blame] | 4925 | DieWithError(e) |
| 4926 | |
sammc@chromium.org | 0b35f5d | 2016-02-25 22:39:23 +0000 | [diff] [blame] | 4927 | if opts.full: |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4928 | cmd = [clang_format_tool] |
| 4929 | if not opts.dry_run and not opts.diff: |
| 4930 | cmd.append('-i') |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4931 | stdout = RunCommand(cmd + clang_diff_files, cwd=top_dir) |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4932 | if opts.diff: |
| 4933 | sys.stdout.write(stdout) |
sammc@chromium.org | 0b35f5d | 2016-02-25 22:39:23 +0000 | [diff] [blame] | 4934 | else: |
| 4935 | env = os.environ.copy() |
| 4936 | env['PATH'] = str(os.path.dirname(clang_format_tool)) |
| 4937 | try: |
| 4938 | script = clang_format.FindClangFormatScriptInChromiumTree( |
| 4939 | 'clang-format-diff.py') |
vapier | fd77ac7 | 2016-06-16 08:33:57 -0700 | [diff] [blame] | 4940 | except clang_format.NotFoundError as e: |
sammc@chromium.org | 0b35f5d | 2016-02-25 22:39:23 +0000 | [diff] [blame] | 4941 | DieWithError(e) |
digit@chromium.org | d6ddc1c | 2013-10-25 15:36:32 +0000 | [diff] [blame] | 4942 | |
sammc@chromium.org | 0b35f5d | 2016-02-25 22:39:23 +0000 | [diff] [blame] | 4943 | cmd = [sys.executable, script, '-p0'] |
| 4944 | if not opts.dry_run and not opts.diff: |
| 4945 | cmd.append('-i') |
digit@chromium.org | d6ddc1c | 2013-10-25 15:36:32 +0000 | [diff] [blame] | 4946 | |
sammc@chromium.org | 0b35f5d | 2016-02-25 22:39:23 +0000 | [diff] [blame] | 4947 | diff_cmd = BuildGitDiffCmd('-U0', upstream_commit, clang_diff_files) |
| 4948 | diff_output = RunGit(diff_cmd) |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4949 | |
sammc@chromium.org | 0b35f5d | 2016-02-25 22:39:23 +0000 | [diff] [blame] | 4950 | stdout = RunCommand(cmd, stdin=diff_output, cwd=top_dir, env=env) |
| 4951 | if opts.diff: |
| 4952 | sys.stdout.write(stdout) |
| 4953 | if opts.dry_run and len(stdout) > 0: |
| 4954 | return_value = 2 |
agable@chromium.org | fab8f82 | 2013-05-06 17:43:09 +0000 | [diff] [blame] | 4955 | |
sbc@chromium.org | 9d0644d | 2015-06-05 23:16:54 +0000 | [diff] [blame] | 4956 | # Similar code to above, but using yapf on .py files rather than clang-format |
| 4957 | # on C/C++ files |
| 4958 | if opts.python: |
sbc@chromium.org | 9d0644d | 2015-06-05 23:16:54 +0000 | [diff] [blame] | 4959 | yapf_tool = gclient_utils.FindExecutable('yapf') |
| 4960 | if yapf_tool is None: |
| 4961 | DieWithError('yapf not found in PATH') |
| 4962 | |
| 4963 | if opts.full: |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4964 | if python_diff_files: |
sbc@chromium.org | 9d0644d | 2015-06-05 23:16:54 +0000 | [diff] [blame] | 4965 | cmd = [yapf_tool] |
| 4966 | if not opts.dry_run and not opts.diff: |
| 4967 | cmd.append('-i') |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4968 | stdout = RunCommand(cmd + python_diff_files, cwd=top_dir) |
sbc@chromium.org | 9d0644d | 2015-06-05 23:16:54 +0000 | [diff] [blame] | 4969 | if opts.diff: |
| 4970 | sys.stdout.write(stdout) |
| 4971 | else: |
| 4972 | # TODO(sbc): yapf --lines mode still has some issues. |
| 4973 | # https://github.com/google/yapf/issues/154 |
| 4974 | DieWithError('--python currently only works with --full') |
| 4975 | |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4976 | # Dart's formatter does not have the nice property of only operating on |
| 4977 | # modified chunks, so hard code full. |
| 4978 | if dart_diff_files: |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4979 | try: |
| 4980 | command = [dart_format.FindDartFmtToolInChromiumTree()] |
| 4981 | if not opts.dry_run and not opts.diff: |
| 4982 | command.append('-w') |
jkarlin@chromium.org | 6f7fa5e | 2016-01-20 19:32:21 +0000 | [diff] [blame] | 4983 | command.extend(dart_diff_files) |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4984 | |
ppi@chromium.org | 6593d93 | 2016-03-03 15:41:15 +0000 | [diff] [blame] | 4985 | stdout = RunCommand(command, cwd=top_dir) |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4986 | if opts.dry_run and stdout: |
| 4987 | return_value = 2 |
| 4988 | except dart_format.NotFoundError as e: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 4989 | print('Warning: Unable to check Dart code formatting. Dart SDK not ' |
| 4990 | 'found in this checkout. Files in other languages are still ' |
| 4991 | 'formatted.') |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 4992 | |
kylechar@chromium.org | 8b61f11 | 2016-02-05 13:28:58 +0000 | [diff] [blame] | 4993 | # Format GN build files. Always run on full build files for canonical form. |
| 4994 | if gn_diff_files: |
| 4995 | cmd = ['gn', 'format'] |
| 4996 | if not opts.dry_run and not opts.diff: |
| 4997 | cmd.append('--in-place') |
| 4998 | for gn_diff_file in gn_diff_files: |
bsep@chromium.org | 627d900 | 2016-04-29 00:00:52 +0000 | [diff] [blame] | 4999 | stdout = RunCommand(cmd + [gn_diff_file], |
| 5000 | shell=sys.platform == 'win32', |
| 5001 | cwd=top_dir) |
kylechar@chromium.org | 8b61f11 | 2016-02-05 13:28:58 +0000 | [diff] [blame] | 5002 | if opts.diff: |
| 5003 | sys.stdout.write(stdout) |
| 5004 | |
erg@chromium.org | e0a7c5d | 2015-02-23 20:30:08 +0000 | [diff] [blame] | 5005 | return return_value |
agable@chromium.org | fab8f82 | 2013-05-06 17:43:09 +0000 | [diff] [blame] | 5006 | |
| 5007 | |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5008 | @subcommand.usage('<codereview url or issue id>') |
| 5009 | def CMDcheckout(parser, args): |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 5010 | """Checks out a branch associated with a given Rietveld or Gerrit issue.""" |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5011 | _, args = parser.parse_args(args) |
| 5012 | |
| 5013 | if len(args) != 1: |
| 5014 | parser.print_help() |
| 5015 | return 1 |
| 5016 | |
tandrii@chromium.org | f86c7d3 | 2016-04-01 19:27:30 +0000 | [diff] [blame] | 5017 | issue_arg = ParseIssueNumberArgument(args[0]) |
tandrii@chromium.org | de6c9a1 | 2016-04-11 15:33:53 +0000 | [diff] [blame] | 5018 | if not issue_arg.valid: |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5019 | parser.print_help() |
| 5020 | return 1 |
tandrii@chromium.org | abd27e5 | 2016-04-11 15:43:32 +0000 | [diff] [blame] | 5021 | target_issue = str(issue_arg.issue) |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5022 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 5023 | def find_issues(issueprefix): |
tandrii@chromium.org | 26c8fd2 | 2016-04-11 21:33:21 +0000 | [diff] [blame] | 5024 | output = RunGit(['config', '--local', '--get-regexp', |
| 5025 | r'branch\..*\.%s' % issueprefix], |
| 5026 | error_ok=True) |
| 5027 | for key, issue in [x.split() for x in output.splitlines()]: |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 5028 | if issue == target_issue: |
| 5029 | yield re.sub(r'branch\.(.*)\.%s' % issueprefix, r'\1', key) |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5030 | |
tandrii@chromium.org | 5df290f | 2016-04-11 16:12:29 +0000 | [diff] [blame] | 5031 | branches = [] |
| 5032 | for cls in _CODEREVIEW_IMPLEMENTATIONS.values(): |
tandrii@chromium.org | d03bc63 | 2016-04-12 14:17:26 +0000 | [diff] [blame] | 5033 | branches.extend(find_issues(cls.IssueSettingSuffix())) |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5034 | if len(branches) == 0: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 5035 | print('No branch found for issue %s.' % target_issue) |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5036 | return 1 |
| 5037 | if len(branches) == 1: |
| 5038 | RunGit(['checkout', branches[0]]) |
| 5039 | else: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 5040 | print('Multiple branches match issue %s:' % target_issue) |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5041 | for i in range(len(branches)): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 5042 | print('%d: %s' % (i, branches[i])) |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5043 | which = raw_input('Choose by index: ') |
| 5044 | try: |
| 5045 | RunGit(['checkout', branches[int(which)]]) |
| 5046 | except (IndexError, ValueError): |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 5047 | print('Invalid selection, not checking out any branch.') |
scottmg@chromium.org | 84a80c4 | 2015-09-22 20:40:37 +0000 | [diff] [blame] | 5048 | return 1 |
| 5049 | |
| 5050 | return 0 |
| 5051 | |
| 5052 | |
maruel@chromium.org | 29404b5 | 2014-09-08 22:58:00 +0000 | [diff] [blame] | 5053 | def CMDlol(parser, args): |
| 5054 | # This command is intentionally undocumented. |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 5055 | print(zlib.decompress(base64.b64decode( |
thakis@chromium.org | 3421c99 | 2014-11-02 02:20:32 +0000 | [diff] [blame] | 5056 | 'eNptkLEOwyAMRHe+wupCIqW57v0Vq84WqWtXyrcXnCBsmgMJ+/SSAxMZgRB6NzE' |
| 5057 | 'E2ObgCKJooYdu4uAQVffUEoE1sRQLxAcqzd7uK2gmStrll1ucV3uZyaY5sXyDd9' |
| 5058 | 'JAnN+lAXsOMJ90GANAi43mq5/VeeacylKVgi8o6F1SC63FxnagHfJUTfUYdCR/W' |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 5059 | 'Ofe+0dHL7PicpytKP750Fh1q2qnLVof4w8OZWNY'))) |
maruel@chromium.org | 29404b5 | 2014-09-08 22:58:00 +0000 | [diff] [blame] | 5060 | return 0 |
| 5061 | |
| 5062 | |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 5063 | class OptionParser(optparse.OptionParser): |
| 5064 | """Creates the option parse and add --verbose support.""" |
| 5065 | def __init__(self, *args, **kwargs): |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 5066 | optparse.OptionParser.__init__( |
| 5067 | self, *args, prog='git cl', version=__version__, **kwargs) |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 5068 | self.add_option( |
| 5069 | '-v', '--verbose', action='count', default=0, |
| 5070 | help='Use 2 times for more debugging info') |
| 5071 | |
| 5072 | def parse_args(self, args=None, values=None): |
| 5073 | options, args = optparse.OptionParser.parse_args(self, args, values) |
| 5074 | levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
| 5075 | logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) |
| 5076 | return options, args |
| 5077 | |
iannucci@chromium.org | d9c1b20 | 2013-07-24 23:52:11 +0000 | [diff] [blame] | 5078 | |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 5079 | def main(argv): |
maruel@chromium.org | 82798cb | 2012-02-23 18:16:12 +0000 | [diff] [blame] | 5080 | if sys.hexversion < 0x02060000: |
vapier | a7fbd5a | 2016-06-16 09:17:49 -0700 | [diff] [blame] | 5081 | print('\nYour python version %s is unsupported, please upgrade.\n' % |
| 5082 | (sys.version.split(' ', 1)[0],), file=sys.stderr) |
maruel@chromium.org | 82798cb | 2012-02-23 18:16:12 +0000 | [diff] [blame] | 5083 | return 2 |
maruel@chromium.org | 2e23ce3 | 2013-05-07 12:42:28 +0000 | [diff] [blame] | 5084 | |
maruel@chromium.org | ddd5941 | 2011-11-30 14:20:38 +0000 | [diff] [blame] | 5085 | # Reload settings. |
| 5086 | global settings |
| 5087 | settings = Settings() |
| 5088 | |
maruel@chromium.org | 39c0b22 | 2013-08-17 16:57:01 +0000 | [diff] [blame] | 5089 | colorize_CMDstatus_doc() |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 5090 | dispatcher = subcommand.CommandDispatcher(__name__) |
| 5091 | try: |
| 5092 | return dispatcher.execute(OptionParser(), argv) |
vadimsh@chromium.org | eed4df3 | 2015-04-10 21:30:20 +0000 | [diff] [blame] | 5093 | except auth.AuthenticationError as e: |
| 5094 | DieWithError(str(e)) |
vapier | fd77ac7 | 2016-06-16 08:33:57 -0700 | [diff] [blame] | 5095 | except urllib2.HTTPError as e: |
maruel@chromium.org | 0633fb4 | 2013-08-16 20:06:14 +0000 | [diff] [blame] | 5096 | if e.code != 500: |
| 5097 | raise |
| 5098 | DieWithError( |
| 5099 | ('AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' |
| 5100 | 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e))) |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 5101 | return 0 |
chase@chromium.org | cc51cd0 | 2010-12-23 00:48:39 +0000 | [diff] [blame] | 5102 | |
| 5103 | |
| 5104 | if __name__ == '__main__': |
maruel@chromium.org | 2e23ce3 | 2013-05-07 12:42:28 +0000 | [diff] [blame] | 5105 | # These affect sys.stdout so do it outside of main() to simplify mocks in |
| 5106 | # unit testing. |
maruel@chromium.org | 6f09cd9 | 2011-04-01 16:38:12 +0000 | [diff] [blame] | 5107 | fix_encoding.fix_encoding() |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 5108 | setup_color.init() |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 5109 | try: |
| 5110 | sys.exit(main(sys.argv[1:])) |
| 5111 | except KeyboardInterrupt: |
| 5112 | sys.stderr.write('interrupted\n') |
| 5113 | sys.exit(1) |