[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 1 | # coding=utf8 |
[email protected] | 9799a07 | 2012-01-11 00:26:25 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [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 | """Manages a project checkout. |
| 6 | |
agable | c972d18 | 2016-10-24 23:59:13 | [diff] [blame] | 7 | Includes support only for git. |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 8 | """ |
| 9 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 10 | import fnmatch |
| 11 | import logging |
| 12 | import os |
| 13 | import re |
[email protected] | 5e97563 | 2011-09-29 18:07:06 | [diff] [blame] | 14 | import shutil |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 15 | import subprocess |
| 16 | import sys |
| 17 | import tempfile |
| 18 | |
vapier | 9f34371 | 2016-06-22 14:13:20 | [diff] [blame] | 19 | # The configparser module was renamed in Python 3. |
| 20 | try: |
| 21 | import configparser |
| 22 | except ImportError: |
| 23 | import ConfigParser as configparser |
| 24 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 25 | import patch |
| 26 | import scm |
| 27 | import subprocess2 |
| 28 | |
| 29 | |
[email protected] | 9af0a11 | 2013-03-20 20:21:35 | [diff] [blame] | 30 | if sys.platform in ('cygwin', 'win32'): |
| 31 | # Disable timeouts on Windows since we can't have shells with timeouts. |
| 32 | GLOBAL_TIMEOUT = None |
| 33 | FETCH_TIMEOUT = None |
| 34 | else: |
| 35 | # Default timeout of 15 minutes. |
| 36 | GLOBAL_TIMEOUT = 15*60 |
| 37 | # Use a larger timeout for checkout since it can be a genuinely slower |
| 38 | # operation. |
| 39 | FETCH_TIMEOUT = 30*60 |
| 40 | |
| 41 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 42 | def get_code_review_setting(path, key, |
| 43 | codereview_settings_file='codereview.settings'): |
| 44 | """Parses codereview.settings and return the value for the key if present. |
| 45 | |
| 46 | Don't cache the values in case the file is changed.""" |
| 47 | # TODO(maruel): Do not duplicate code. |
| 48 | settings = {} |
| 49 | try: |
| 50 | settings_file = open(os.path.join(path, codereview_settings_file), 'r') |
| 51 | try: |
| 52 | for line in settings_file.readlines(): |
| 53 | if not line or line.startswith('#'): |
| 54 | continue |
| 55 | if not ':' in line: |
| 56 | # Invalid file. |
| 57 | return None |
| 58 | k, v = line.split(':', 1) |
| 59 | settings[k.strip()] = v.strip() |
| 60 | finally: |
| 61 | settings_file.close() |
[email protected] | 004fb71 | 2011-06-21 20:02:16 | [diff] [blame] | 62 | except IOError: |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 63 | return None |
| 64 | return settings.get(key, None) |
| 65 | |
| 66 | |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 67 | def align_stdout(stdout): |
| 68 | """Returns the aligned output of multiple stdouts.""" |
| 69 | output = '' |
| 70 | for item in stdout: |
| 71 | item = item.strip() |
| 72 | if not item: |
| 73 | continue |
| 74 | output += ''.join(' %s\n' % line for line in item.splitlines()) |
| 75 | return output |
| 76 | |
| 77 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 78 | class PatchApplicationFailed(Exception): |
| 79 | """Patch failed to be applied.""" |
skobes | 2f3f137 | 2016-10-25 15:08:27 | [diff] [blame] | 80 | def __init__(self, errors, verbose): |
| 81 | super(PatchApplicationFailed, self).__init__(errors, verbose) |
| 82 | self.errors = errors |
| 83 | self.verbose = verbose |
[email protected] | 34f6855 | 2012-05-09 19:18:36 | [diff] [blame] | 84 | |
| 85 | def __str__(self): |
| 86 | out = [] |
skobes | 2f3f137 | 2016-10-25 15:08:27 | [diff] [blame] | 87 | for e in self.errors: |
| 88 | p, status = e |
| 89 | if p and p.filename: |
| 90 | out.append('Failed to apply patch for %s:' % p.filename) |
| 91 | if status: |
| 92 | out.append(status) |
| 93 | if p and self.verbose: |
| 94 | out.append('Patch: %s' % p.dump()) |
[email protected] | 34f6855 | 2012-05-09 19:18:36 | [diff] [blame] | 95 | return '\n'.join(out) |
| 96 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 97 | |
| 98 | class CheckoutBase(object): |
| 99 | # Set to None to have verbose output. |
| 100 | VOID = subprocess2.VOID |
| 101 | |
[email protected] | 6ed8b50 | 2011-06-12 01:05:35 | [diff] [blame] | 102 | def __init__(self, root_dir, project_name, post_processors): |
| 103 | """ |
| 104 | Args: |
| 105 | post_processor: list of lambda(checkout, patches) to call on each of the |
| 106 | modified files. |
| 107 | """ |
[email protected] | a5129fb | 2011-06-20 18:36:25 | [diff] [blame] | 108 | super(CheckoutBase, self).__init__() |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 109 | self.root_dir = root_dir |
| 110 | self.project_name = project_name |
[email protected] | 3cdb7f3 | 2011-05-05 16:37:24 | [diff] [blame] | 111 | if self.project_name is None: |
| 112 | self.project_path = self.root_dir |
| 113 | else: |
| 114 | self.project_path = os.path.join(self.root_dir, self.project_name) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 115 | # Only used for logging purposes. |
| 116 | self._last_seen_revision = None |
[email protected] | a5129fb | 2011-06-20 18:36:25 | [diff] [blame] | 117 | self.post_processors = post_processors |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 118 | assert self.root_dir |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 119 | assert self.project_path |
[email protected] | 0aca0f9 | 2012-10-01 16:39:45 | [diff] [blame] | 120 | assert os.path.isabs(self.project_path) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 121 | |
| 122 | def get_settings(self, key): |
| 123 | return get_code_review_setting(self.project_path, key) |
| 124 | |
[email protected] | 5191977 | 2011-06-12 01:27:42 | [diff] [blame] | 125 | def prepare(self, revision): |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 126 | """Checks out a clean copy of the tree and removes any local modification. |
| 127 | |
| 128 | This function shouldn't throw unless the remote repository is inaccessible, |
| 129 | there is no free disk space or hard issues like that. |
[email protected] | 5191977 | 2011-06-12 01:27:42 | [diff] [blame] | 130 | |
| 131 | Args: |
| 132 | revision: The revision it should sync to, SCM specific. |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 133 | """ |
| 134 | raise NotImplementedError() |
| 135 | |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 136 | def apply_patch(self, patches, post_processors=None, verbose=False): |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 137 | """Applies a patch and returns the list of modified files. |
| 138 | |
| 139 | This function should throw patch.UnsupportedPatchFormat or |
| 140 | PatchApplicationFailed when relevant. |
[email protected] | 8a1396c | 2011-04-22 00:14:24 | [diff] [blame] | 141 | |
| 142 | Args: |
| 143 | patches: patch.PatchSet object. |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 144 | """ |
| 145 | raise NotImplementedError() |
| 146 | |
| 147 | def commit(self, commit_message, user): |
| 148 | """Commits the patch upstream, while impersonating 'user'.""" |
| 149 | raise NotImplementedError() |
| 150 | |
[email protected] | bc32ad1 | 2012-07-26 13:22:47 | [diff] [blame] | 151 | def revisions(self, rev1, rev2): |
| 152 | """Returns the count of revisions from rev1 to rev2, e.g. len(]rev1, rev2]). |
| 153 | |
| 154 | If rev2 is None, it means 'HEAD'. |
| 155 | |
| 156 | Returns None if there is no link between the two. |
| 157 | """ |
| 158 | raise NotImplementedError() |
| 159 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 160 | |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 161 | class GitCheckout(CheckoutBase): |
| 162 | """Manages a git checkout.""" |
| 163 | def __init__(self, root_dir, project_name, remote_branch, git_url, |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 164 | commit_user, post_processors=None): |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 165 | super(GitCheckout, self).__init__(root_dir, project_name, post_processors) |
| 166 | self.git_url = git_url |
| 167 | self.commit_user = commit_user |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 168 | self.remote_branch = remote_branch |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 169 | # The working branch where patches will be applied. It will track the |
| 170 | # remote branch. |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 171 | self.working_branch = 'working_branch' |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 172 | # There is no reason to not hardcode origin. |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 173 | self.remote = 'origin' |
| 174 | # There is no reason to not hardcode master. |
| 175 | self.master_branch = 'master' |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 176 | |
[email protected] | 5191977 | 2011-06-12 01:27:42 | [diff] [blame] | 177 | def prepare(self, revision): |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 178 | """Resets the git repository in a clean state. |
| 179 | |
| 180 | Checks it out if not present and deletes the working branch. |
| 181 | """ |
[email protected] | 7dc1144 | 2014-03-12 22:37:32 | [diff] [blame] | 182 | assert self.remote_branch |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 183 | assert self.git_url |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 184 | |
| 185 | if not os.path.isdir(self.project_path): |
| 186 | # Clone the repo if the directory is not present. |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 187 | logging.info( |
| 188 | 'Checking out %s in %s', self.project_name, self.project_path) |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 189 | self._check_call_git( |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 190 | ['clone', self.git_url, '-b', self.remote_branch, self.project_path], |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 191 | cwd=None, timeout=FETCH_TIMEOUT) |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 192 | else: |
| 193 | # Throw away all uncommitted changes in the existing checkout. |
| 194 | self._check_call_git(['checkout', self.remote_branch]) |
| 195 | self._check_call_git( |
| 196 | ['reset', '--hard', '--quiet', |
| 197 | '%s/%s' % (self.remote, self.remote_branch)]) |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 198 | |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 199 | if revision: |
| 200 | try: |
| 201 | # Look if the commit hash already exist. If so, we can skip a |
| 202 | # 'git fetch' call. |
[email protected] | 323ec37 | 2014-06-17 01:50:37 | [diff] [blame] | 203 | revision = self._check_output_git(['rev-parse', revision]).rstrip() |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 204 | except subprocess.CalledProcessError: |
| 205 | self._check_call_git( |
| 206 | ['fetch', self.remote, self.remote_branch, '--quiet']) |
[email protected] | 323ec37 | 2014-06-17 01:50:37 | [diff] [blame] | 207 | revision = self._check_output_git(['rev-parse', revision]).rstrip() |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 208 | self._check_call_git(['checkout', '--force', '--quiet', revision]) |
| 209 | else: |
| 210 | branches, active = self._branches() |
| 211 | if active != self.master_branch: |
| 212 | self._check_call_git( |
| 213 | ['checkout', '--force', '--quiet', self.master_branch]) |
| 214 | self._sync_remote_branch() |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 215 | |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 216 | if self.working_branch in branches: |
| 217 | self._call_git(['branch', '-D', self.working_branch]) |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 218 | return self._get_head_commit_hash() |
| 219 | |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 220 | def _sync_remote_branch(self): |
| 221 | """Syncs the remote branch.""" |
| 222 | # We do a 'git pull origin master:refs/remotes/origin/master' instead of |
[email protected] | dabbea2 | 2014-04-21 23:58:11 | [diff] [blame] | 223 | # 'git pull origin master' because from the manpage for git-pull: |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 224 | # A parameter <ref> without a colon is equivalent to <ref>: when |
| 225 | # pulling/fetching, so it merges <ref> into the current branch without |
| 226 | # storing the remote branch anywhere locally. |
| 227 | remote_tracked_path = 'refs/remotes/%s/%s' % ( |
| 228 | self.remote, self.remote_branch) |
| 229 | self._check_call_git( |
| 230 | ['pull', self.remote, |
| 231 | '%s:%s' % (self.remote_branch, remote_tracked_path), |
| 232 | '--quiet']) |
| 233 | |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 234 | def _get_head_commit_hash(self): |
[email protected] | 11145db | 2013-10-03 12:43:40 | [diff] [blame] | 235 | """Gets the current revision (in unicode) from the local branch.""" |
| 236 | return unicode(self._check_output_git(['rev-parse', 'HEAD']).strip()) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 237 | |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 238 | def apply_patch(self, patches, post_processors=None, verbose=False): |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 239 | """Applies a patch on 'working_branch' and switches to it. |
[email protected] | 8a1396c | 2011-04-22 00:14:24 | [diff] [blame] | 240 | |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 241 | The changes remain staged on the current branch. |
[email protected] | 8a1396c | 2011-04-22 00:14:24 | [diff] [blame] | 242 | """ |
[email protected] | b1d1a78 | 2011-09-29 14:13:55 | [diff] [blame] | 243 | post_processors = post_processors or self.post_processors or [] |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 244 | # It this throws, the checkout is corrupted. Maybe worth deleting it and |
| 245 | # trying again? |
[email protected] | 3cdb7f3 | 2011-05-05 16:37:24 | [diff] [blame] | 246 | if self.remote_branch: |
| 247 | self._check_call_git( |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 248 | ['checkout', '-b', self.working_branch, '-t', self.remote_branch, |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 249 | '--quiet']) |
| 250 | |
skobes | 2f3f137 | 2016-10-25 15:08:27 | [diff] [blame] | 251 | errors = [] |
[email protected] | 5e97563 | 2011-09-29 18:07:06 | [diff] [blame] | 252 | for index, p in enumerate(patches): |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 253 | stdout = [] |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 254 | try: |
Edward Lesmes | f879207 | 2017-09-13 08:05:12 | [diff] [blame] | 255 | filepath = os.path.join(self.project_path, p.filename) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 256 | if p.is_delete: |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 257 | if (not os.path.exists(filepath) and |
[email protected] | 5e97563 | 2011-09-29 18:07:06 | [diff] [blame] | 258 | any(p1.source_filename == p.filename for p1 in patches[0:index])): |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 259 | # The file was already deleted if a prior patch with file rename |
| 260 | # was already processed because 'git apply' did it for us. |
[email protected] | 5e97563 | 2011-09-29 18:07:06 | [diff] [blame] | 261 | pass |
| 262 | else: |
Edward Lesmes | f879207 | 2017-09-13 08:05:12 | [diff] [blame] | 263 | stdout.append(self._check_output_git(['rm', p.filename])) |
[email protected] | d9eb69e | 2014-06-05 20:33:37 | [diff] [blame] | 264 | assert(not os.path.exists(filepath)) |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 265 | stdout.append('Deleted.') |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 266 | else: |
Edward Lesmes | f879207 | 2017-09-13 08:05:12 | [diff] [blame] | 267 | dirname = os.path.dirname(p.filename) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 268 | full_dir = os.path.join(self.project_path, dirname) |
| 269 | if dirname and not os.path.isdir(full_dir): |
| 270 | os.makedirs(full_dir) |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 271 | stdout.append('Created missing directory %s.' % dirname) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 272 | if p.is_binary: |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 273 | content = p.get() |
| 274 | with open(filepath, 'wb') as f: |
| 275 | f.write(content) |
| 276 | stdout.append('Added binary file %d bytes' % len(content)) |
Edward Lesmes | f879207 | 2017-09-13 08:05:12 | [diff] [blame] | 277 | cmd = ['add', p.filename] |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 278 | if verbose: |
| 279 | cmd.append('--verbose') |
| 280 | stdout.append(self._check_output_git(cmd)) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 281 | else: |
[email protected] | 58fe662 | 2011-06-03 20:59:27 | [diff] [blame] | 282 | # No need to do anything special with p.is_new or if not |
| 283 | # p.diff_hunks. git apply manages all that already. |
[email protected] | 49dfcde | 2014-09-23 08:14:39 | [diff] [blame] | 284 | cmd = ['apply', '--index', '-3', '-p%s' % p.patchlevel] |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 285 | if verbose: |
| 286 | cmd.append('--verbose') |
| 287 | stdout.append(self._check_output_git(cmd, stdin=p.get(True))) |
[email protected] | b1d1a78 | 2011-09-29 14:13:55 | [diff] [blame] | 288 | for post in post_processors: |
[email protected] | 8a1396c | 2011-04-22 00:14:24 | [diff] [blame] | 289 | post(self, p) |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 290 | if verbose: |
| 291 | print p.filename |
| 292 | print align_stdout(stdout) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 293 | except OSError, e: |
skobes | 2f3f137 | 2016-10-25 15:08:27 | [diff] [blame] | 294 | errors.append((p, '%s%s' % (align_stdout(stdout), e))) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 295 | except subprocess.CalledProcessError, e: |
skobes | 2f3f137 | 2016-10-25 15:08:27 | [diff] [blame] | 296 | errors.append((p, |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 297 | 'While running %s;\n%s%s' % ( |
| 298 | ' '.join(e.cmd), |
| 299 | align_stdout(stdout), |
skobes | 2f3f137 | 2016-10-25 15:08:27 | [diff] [blame] | 300 | align_stdout([getattr(e, 'stdout', '')])))) |
| 301 | if errors: |
| 302 | raise PatchApplicationFailed(errors, verbose) |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 303 | found_files = self._check_output_git( |
Aaron Gable | f4068aa | 2017-12-12 23:14:09 | [diff] [blame] | 304 | ['-c', 'core.quotePath=false', 'diff', '--ignore-submodules', |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 305 | '--name-only', '--staged']).splitlines(False) |
[email protected] | dc6a1d0 | 2014-05-10 04:42:48 | [diff] [blame] | 306 | if sorted(patches.filenames) != sorted(found_files): |
| 307 | extra_files = sorted(set(found_files) - set(patches.filenames)) |
| 308 | unpatched_files = sorted(set(patches.filenames) - set(found_files)) |
| 309 | if extra_files: |
| 310 | print 'Found extra files: %r' % (extra_files,) |
| 311 | if unpatched_files: |
| 312 | print 'Found unpatched files: %r' % (unpatched_files,) |
| 313 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 314 | |
| 315 | def commit(self, commit_message, user): |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 316 | """Commits, updates the commit message and pushes.""" |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 317 | # TODO(hinoka): CQ no longer uses this, I think its deprecated. |
| 318 | # Delete this. |
[email protected] | bb050f6 | 2013-10-03 16:53:54 | [diff] [blame] | 319 | assert self.commit_user |
[email protected] | 1bf5097 | 2011-05-05 19:57:21 | [diff] [blame] | 320 | assert isinstance(commit_message, unicode) |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 321 | current_branch = self._check_output_git( |
| 322 | ['rev-parse', '--abbrev-ref', 'HEAD']).strip() |
| 323 | assert current_branch == self.working_branch |
[email protected] | dabbea2 | 2014-04-21 23:58:11 | [diff] [blame] | 324 | |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 325 | commit_cmd = ['commit', '-m', commit_message] |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 326 | if user and user != self.commit_user: |
| 327 | # We do not have the first or last name of the user, grab the username |
| 328 | # from the email and call it the original author's name. |
| 329 | # TODO(rmistry): Do not need the below if user is already in |
| 330 | # "Name <email>" format. |
| 331 | name = user.split('@')[0] |
| 332 | commit_cmd.extend(['--author', '%s <%s>' % (name, user)]) |
| 333 | self._check_call_git(commit_cmd) |
| 334 | |
| 335 | # Push to the remote repository. |
| 336 | self._check_call_git( |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 337 | ['push', 'origin', '%s:%s' % (self.working_branch, self.remote_branch), |
[email protected] | 3926228 | 2014-03-19 21:07:38 | [diff] [blame] | 338 | '--quiet']) |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 339 | # Get the revision after the push. |
| 340 | revision = self._get_head_commit_hash() |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 341 | # Switch back to the remote_branch and sync it. |
| 342 | self._check_call_git(['checkout', self.remote_branch]) |
| 343 | self._sync_remote_branch() |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 344 | # Delete the working branch since we are done with it. |
| 345 | self._check_call_git(['branch', '-D', self.working_branch]) |
| 346 | |
| 347 | return revision |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 348 | |
| 349 | def _check_call_git(self, args, **kwargs): |
| 350 | kwargs.setdefault('cwd', self.project_path) |
| 351 | kwargs.setdefault('stdout', self.VOID) |
[email protected] | 9af0a11 | 2013-03-20 20:21:35 | [diff] [blame] | 352 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
[email protected] | 44b21b9 | 2012-11-08 19:37:08 | [diff] [blame] | 353 | return subprocess2.check_call_out(['git'] + args, **kwargs) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 354 | |
| 355 | def _call_git(self, args, **kwargs): |
| 356 | """Like check_call but doesn't throw on failure.""" |
| 357 | kwargs.setdefault('cwd', self.project_path) |
| 358 | kwargs.setdefault('stdout', self.VOID) |
[email protected] | 9af0a11 | 2013-03-20 20:21:35 | [diff] [blame] | 359 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
[email protected] | 44b21b9 | 2012-11-08 19:37:08 | [diff] [blame] | 360 | return subprocess2.call(['git'] + args, **kwargs) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 361 | |
| 362 | def _check_output_git(self, args, **kwargs): |
| 363 | kwargs.setdefault('cwd', self.project_path) |
[email protected] | 9af0a11 | 2013-03-20 20:21:35 | [diff] [blame] | 364 | kwargs.setdefault('timeout', GLOBAL_TIMEOUT) |
[email protected] | 87e6d33 | 2011-09-09 19:01:28 | [diff] [blame] | 365 | return subprocess2.check_output( |
[email protected] | 44b21b9 | 2012-11-08 19:37:08 | [diff] [blame] | 366 | ['git'] + args, stderr=subprocess2.STDOUT, **kwargs) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 367 | |
| 368 | def _branches(self): |
| 369 | """Returns the list of branches and the active one.""" |
| 370 | out = self._check_output_git(['branch']).splitlines(False) |
| 371 | branches = [l[2:] for l in out] |
| 372 | active = None |
| 373 | for l in out: |
| 374 | if l.startswith('*'): |
| 375 | active = l[2:] |
| 376 | break |
| 377 | return branches, active |
| 378 | |
[email protected] | bc32ad1 | 2012-07-26 13:22:47 | [diff] [blame] | 379 | def revisions(self, rev1, rev2): |
| 380 | """Returns the number of actual commits between both hash.""" |
| 381 | self._fetch_remote() |
| 382 | |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 383 | rev2 = rev2 or '%s/%s' % (self.remote, self.remote_branch) |
[email protected] | bc32ad1 | 2012-07-26 13:22:47 | [diff] [blame] | 384 | # Revision range is ]rev1, rev2] and ordering matters. |
| 385 | try: |
| 386 | out = self._check_output_git( |
| 387 | ['log', '--format="%H"' , '%s..%s' % (rev1, rev2)]) |
| 388 | except subprocess.CalledProcessError: |
| 389 | return None |
| 390 | return len(out.splitlines()) |
| 391 | |
| 392 | def _fetch_remote(self): |
| 393 | """Fetches the remote without rebasing.""" |
[email protected] | 3b5efdf | 2013-09-05 11:59:40 | [diff] [blame] | 394 | # git fetch is always verbose even with -q, so redirect its output. |
[email protected] | 7e8c19d | 2014-03-19 16:47:37 | [diff] [blame] | 395 | self._check_output_git(['fetch', self.remote, self.remote_branch], |
[email protected] | 9af0a11 | 2013-03-20 20:21:35 | [diff] [blame] | 396 | timeout=FETCH_TIMEOUT) |
[email protected] | bc32ad1 | 2012-07-26 13:22:47 | [diff] [blame] | 397 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 398 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 399 | class ReadOnlyCheckout(object): |
| 400 | """Converts a checkout into a read-only one.""" |
[email protected] | b1d1a78 | 2011-09-29 14:13:55 | [diff] [blame] | 401 | def __init__(self, checkout, post_processors=None): |
[email protected] | a5129fb | 2011-06-20 18:36:25 | [diff] [blame] | 402 | super(ReadOnlyCheckout, self).__init__() |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 403 | self.checkout = checkout |
[email protected] | b1d1a78 | 2011-09-29 14:13:55 | [diff] [blame] | 404 | self.post_processors = (post_processors or []) + ( |
| 405 | self.checkout.post_processors or []) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 406 | |
[email protected] | 5191977 | 2011-06-12 01:27:42 | [diff] [blame] | 407 | def prepare(self, revision): |
| 408 | return self.checkout.prepare(revision) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 409 | |
| 410 | def get_settings(self, key): |
| 411 | return self.checkout.get_settings(key) |
| 412 | |
[email protected] | c4396a1 | 2014-05-10 02:19:27 | [diff] [blame] | 413 | def apply_patch(self, patches, post_processors=None, verbose=False): |
[email protected] | b1d1a78 | 2011-09-29 14:13:55 | [diff] [blame] | 414 | return self.checkout.apply_patch( |
[email protected] | 4dd9f72 | 2012-10-01 16:23:03 | [diff] [blame] | 415 | patches, post_processors or self.post_processors, verbose) |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 416 | |
Quinten Yearsley | b2cc4a9 | 2016-12-15 21:53:26 | [diff] [blame] | 417 | def commit(self, message, user): # pylint: disable=no-self-use |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 418 | logging.info('Would have committed for %s with message: %s' % ( |
| 419 | user, message)) |
| 420 | return 'FAKE' |
| 421 | |
[email protected] | bc32ad1 | 2012-07-26 13:22:47 | [diff] [blame] | 422 | def revisions(self, rev1, rev2): |
| 423 | return self.checkout.revisions(rev1, rev2) |
| 424 | |
[email protected] | dfaecd2 | 2011-04-21 00:33:31 | [diff] [blame] | 425 | @property |
| 426 | def project_name(self): |
| 427 | return self.checkout.project_name |
| 428 | |
| 429 | @property |
| 430 | def project_path(self): |
| 431 | return self.checkout.project_path |