iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 1 | # Copyright 2014 The Chromium Authors. All rights reserved. |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # Monkeypatch IMapIterator so that Ctrl-C can kill everything properly. |
| 6 | # Derived from https://gist.github.com/aljungberg/626518 |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 7 | |
| 8 | from __future__ import print_function |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 9 | from __future__ import unicode_literals |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 10 | |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 11 | import multiprocessing.pool |
Josip Sokcevic | de6c456 | 2020-03-26 00:39:42 +0000 | [diff] [blame] | 12 | import sys |
| 13 | import threading |
| 14 | |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 15 | from multiprocessing.pool import IMapIterator |
Josip Sokcevic | de6c456 | 2020-03-26 00:39:42 +0000 | [diff] [blame] | 16 | |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 17 | def wrapper(func): |
| 18 | def wrap(self, timeout=None): |
Josip Sokcevic | de6c456 | 2020-03-26 00:39:42 +0000 | [diff] [blame] | 19 | default_timeout = (1 << 31 if sys.version_info.major == 2 else |
| 20 | threading.TIMEOUT_MAX) |
| 21 | return func(self, timeout=timeout or default_timeout) |
| 22 | |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 23 | return wrap |
| 24 | IMapIterator.next = wrapper(IMapIterator.next) |
| 25 | IMapIterator.__next__ = IMapIterator.next |
| 26 | # TODO(iannucci): Monkeypatch all other 'wait' methods too. |
| 27 | |
| 28 | |
| 29 | import binascii |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 30 | import collections |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 31 | import contextlib |
| 32 | import functools |
| 33 | import logging |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 34 | import os |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 35 | import re |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 36 | import setup_color |
sammc@chromium.org | 900a33f | 2015-09-29 06:57:09 +0000 | [diff] [blame] | 37 | import shutil |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 38 | import signal |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 39 | import tempfile |
iannucci@chromium.org | 3f23cdf | 2014-04-15 20:02:44 +0000 | [diff] [blame] | 40 | import textwrap |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 41 | |
| 42 | import subprocess2 |
| 43 | |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 44 | from io import BytesIO |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 45 | |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 46 | |
Edward Lemur | b800fde | 2020-01-10 23:04:44 +0000 | [diff] [blame] | 47 | if sys.version_info.major == 2: |
| 48 | # On Python 3, BrokenPipeError is raised instead. |
| 49 | BrokenPipeError = IOError |
| 50 | |
| 51 | |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 52 | ROOT = os.path.abspath(os.path.dirname(__file__)) |
iannucci@chromium.org | 0d9e59c | 2016-01-09 08:08:41 +0000 | [diff] [blame] | 53 | IS_WIN = sys.platform == 'win32' |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 54 | TEST_MODE = False |
| 55 | |
Dan Jacques | 209a681 | 2017-07-12 11:40:20 -0700 | [diff] [blame] | 56 | |
| 57 | def win_find_git(): |
| 58 | for elem in os.environ.get('PATH', '').split(os.pathsep): |
| 59 | for candidate in ('git.exe', 'git.bat'): |
| 60 | path = os.path.join(elem, candidate) |
| 61 | if os.path.isfile(path): |
| 62 | return path |
| 63 | raise ValueError('Could not find Git on PATH.') |
| 64 | |
| 65 | |
| 66 | GIT_EXE = 'git' if not IS_WIN else win_find_git() |
| 67 | |
| 68 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 69 | FREEZE = 'FREEZE' |
| 70 | FREEZE_SECTIONS = { |
| 71 | 'indexed': 'soft', |
| 72 | 'unindexed': 'mixed' |
| 73 | } |
| 74 | FREEZE_MATCHER = re.compile(r'%s.(%s)' % (FREEZE, '|'.join(FREEZE_SECTIONS))) |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 75 | |
| 76 | |
Dan Jacques | 2f8b0c1 | 2017-04-05 12:57:21 -0700 | [diff] [blame] | 77 | # NOTE: This list is DEPRECATED in favor of the Infra Git wrapper: |
| 78 | # https://chromium.googlesource.com/infra/infra/+/master/go/src/infra/tools/git |
| 79 | # |
| 80 | # New entries should be added to the Git wrapper, NOT to this list. "git_retry" |
| 81 | # is, similarly, being deprecated in favor of the Git wrapper. |
| 82 | # |
| 83 | # --- |
| 84 | # |
dnj@chromium.org | de219ec | 2014-07-28 17:39:08 +0000 | [diff] [blame] | 85 | # Retry a git operation if git returns a error response with any of these |
| 86 | # messages. It's all observed 'bad' GoB responses so far. |
| 87 | # |
| 88 | # This list is inspired/derived from the one in ChromiumOS's Chromite: |
| 89 | # <CHROMITE>/lib/git.py::GIT_TRANSIENT_ERRORS |
| 90 | # |
| 91 | # It was last imported from '7add3ac29564d98ac35ce426bc295e743e7c0c02'. |
| 92 | GIT_TRANSIENT_ERRORS = ( |
| 93 | # crbug.com/285832 |
iannucci@chromium.org | 6e95d40 | 2014-08-29 22:10:55 +0000 | [diff] [blame] | 94 | r'!.*\[remote rejected\].*\(error in hook\)', |
dnj@chromium.org | de219ec | 2014-07-28 17:39:08 +0000 | [diff] [blame] | 95 | |
| 96 | # crbug.com/289932 |
iannucci@chromium.org | 6e95d40 | 2014-08-29 22:10:55 +0000 | [diff] [blame] | 97 | r'!.*\[remote rejected\].*\(failed to lock\)', |
dnj@chromium.org | de219ec | 2014-07-28 17:39:08 +0000 | [diff] [blame] | 98 | |
| 99 | # crbug.com/307156 |
iannucci@chromium.org | 6e95d40 | 2014-08-29 22:10:55 +0000 | [diff] [blame] | 100 | r'!.*\[remote rejected\].*\(error in Gerrit backend\)', |
dnj@chromium.org | de219ec | 2014-07-28 17:39:08 +0000 | [diff] [blame] | 101 | |
| 102 | # crbug.com/285832 |
| 103 | r'remote error: Internal Server Error', |
| 104 | |
| 105 | # crbug.com/294449 |
| 106 | r'fatal: Couldn\'t find remote ref ', |
| 107 | |
| 108 | # crbug.com/220543 |
| 109 | r'git fetch_pack: expected ACK/NAK, got', |
| 110 | |
| 111 | # crbug.com/189455 |
| 112 | r'protocol error: bad pack header', |
| 113 | |
| 114 | # crbug.com/202807 |
| 115 | r'The remote end hung up unexpectedly', |
| 116 | |
| 117 | # crbug.com/298189 |
| 118 | r'TLS packet with unexpected length was received', |
| 119 | |
| 120 | # crbug.com/187444 |
| 121 | r'RPC failed; result=\d+, HTTP code = \d+', |
| 122 | |
dnj@chromium.org | de219ec | 2014-07-28 17:39:08 +0000 | [diff] [blame] | 123 | # crbug.com/388876 |
| 124 | r'Connection timed out', |
dnj@chromium.org | 45cddd6 | 2014-11-06 19:36:42 +0000 | [diff] [blame] | 125 | |
| 126 | # crbug.com/430343 |
| 127 | # TODO(dnj): Resync with Chromite. |
| 128 | r'The requested URL returned error: 5\d+', |
Arikon | b3a2148 | 2016-07-22 10:12:24 -0700 | [diff] [blame] | 129 | |
| 130 | r'Connection reset by peer', |
| 131 | |
| 132 | r'Unable to look up', |
| 133 | |
| 134 | r'Couldn\'t resolve host', |
dnj@chromium.org | de219ec | 2014-07-28 17:39:08 +0000 | [diff] [blame] | 135 | ) |
| 136 | |
| 137 | GIT_TRANSIENT_ERRORS_RE = re.compile('|'.join(GIT_TRANSIENT_ERRORS), |
| 138 | re.IGNORECASE) |
| 139 | |
raphael.kubo.da.costa@intel.com | 58d05b0 | 2015-06-24 08:54:41 +0000 | [diff] [blame] | 140 | # git's for-each-ref command first supported the upstream:track token in its |
| 141 | # format string in version 1.9.0, but some usages were broken until 2.3.0. |
| 142 | # See git commit b6160d95 for more information. |
| 143 | MIN_UPSTREAM_TRACK_GIT_VERSION = (2, 3) |
dnj@chromium.org | de219ec | 2014-07-28 17:39:08 +0000 | [diff] [blame] | 144 | |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 145 | class BadCommitRefException(Exception): |
| 146 | def __init__(self, refs): |
| 147 | msg = ('one of %s does not seem to be a valid commitref.' % |
| 148 | str(refs)) |
| 149 | super(BadCommitRefException, self).__init__(msg) |
| 150 | |
| 151 | |
| 152 | def memoize_one(**kwargs): |
| 153 | """Memoizes a single-argument pure function. |
| 154 | |
| 155 | Values of None are not cached. |
| 156 | |
| 157 | Kwargs: |
| 158 | threadsafe (bool) - REQUIRED. Specifies whether to use locking around |
| 159 | cache manipulation functions. This is a kwarg so that users of memoize_one |
| 160 | are forced to explicitly and verbosely pick True or False. |
| 161 | |
| 162 | Adds three methods to the decorated function: |
| 163 | * get(key, default=None) - Gets the value for this key from the cache. |
| 164 | * set(key, value) - Sets the value for this key from the cache. |
| 165 | * clear() - Drops the entire contents of the cache. Useful for unittests. |
| 166 | * update(other) - Updates the contents of the cache from another dict. |
| 167 | """ |
| 168 | assert 'threadsafe' in kwargs, 'Must specify threadsafe={True,False}' |
| 169 | threadsafe = kwargs['threadsafe'] |
| 170 | |
| 171 | if threadsafe: |
| 172 | def withlock(lock, f): |
| 173 | def inner(*args, **kwargs): |
| 174 | with lock: |
| 175 | return f(*args, **kwargs) |
| 176 | return inner |
| 177 | else: |
| 178 | def withlock(_lock, f): |
| 179 | return f |
| 180 | |
| 181 | def decorator(f): |
| 182 | # Instantiate the lock in decorator, in case users of memoize_one do: |
| 183 | # |
| 184 | # memoizer = memoize_one(threadsafe=True) |
| 185 | # |
| 186 | # @memoizer |
| 187 | # def fn1(val): ... |
| 188 | # |
| 189 | # @memoizer |
| 190 | # def fn2(val): ... |
| 191 | |
| 192 | lock = threading.Lock() if threadsafe else None |
| 193 | cache = {} |
| 194 | _get = withlock(lock, cache.get) |
| 195 | _set = withlock(lock, cache.__setitem__) |
| 196 | |
| 197 | @functools.wraps(f) |
| 198 | def inner(arg): |
| 199 | ret = _get(arg) |
| 200 | if ret is None: |
| 201 | ret = f(arg) |
| 202 | if ret is not None: |
| 203 | _set(arg, ret) |
| 204 | return ret |
| 205 | inner.get = _get |
| 206 | inner.set = _set |
| 207 | inner.clear = withlock(lock, cache.clear) |
| 208 | inner.update = withlock(lock, cache.update) |
| 209 | return inner |
| 210 | return decorator |
| 211 | |
| 212 | |
| 213 | def _ScopedPool_initer(orig, orig_args): # pragma: no cover |
| 214 | """Initializer method for ScopedPool's subprocesses. |
| 215 | |
| 216 | This helps ScopedPool handle Ctrl-C's correctly. |
| 217 | """ |
| 218 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 219 | if orig: |
| 220 | orig(*orig_args) |
| 221 | |
| 222 | |
| 223 | @contextlib.contextmanager |
| 224 | def ScopedPool(*args, **kwargs): |
| 225 | """Context Manager which returns a multiprocessing.pool instance which |
| 226 | correctly deals with thrown exceptions. |
| 227 | |
| 228 | *args - Arguments to multiprocessing.pool |
| 229 | |
| 230 | Kwargs: |
| 231 | kind ('threads', 'procs') - The type of underlying coprocess to use. |
| 232 | **etc - Arguments to multiprocessing.pool |
| 233 | """ |
| 234 | if kwargs.pop('kind', None) == 'threads': |
| 235 | pool = multiprocessing.pool.ThreadPool(*args, **kwargs) |
| 236 | else: |
| 237 | orig, orig_args = kwargs.get('initializer'), kwargs.get('initargs', ()) |
| 238 | kwargs['initializer'] = _ScopedPool_initer |
| 239 | kwargs['initargs'] = orig, orig_args |
| 240 | pool = multiprocessing.pool.Pool(*args, **kwargs) |
| 241 | |
| 242 | try: |
| 243 | yield pool |
| 244 | pool.close() |
| 245 | except: |
| 246 | pool.terminate() |
| 247 | raise |
| 248 | finally: |
| 249 | pool.join() |
| 250 | |
| 251 | |
| 252 | class ProgressPrinter(object): |
| 253 | """Threaded single-stat status message printer.""" |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 254 | def __init__(self, fmt, enabled=None, fout=sys.stderr, period=0.5): |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 255 | """Create a ProgressPrinter. |
| 256 | |
| 257 | Use it as a context manager which produces a simple 'increment' method: |
| 258 | |
| 259 | with ProgressPrinter('(%%(count)d/%d)' % 1000) as inc: |
| 260 | for i in xrange(1000): |
| 261 | # do stuff |
| 262 | if i % 10 == 0: |
| 263 | inc(10) |
| 264 | |
| 265 | Args: |
| 266 | fmt - String format with a single '%(count)d' where the counter value |
| 267 | should go. |
| 268 | enabled (bool) - If this is None, will default to True if |
| 269 | logging.getLogger() is set to INFO or more verbose. |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 270 | fout (file-like) - The stream to print status messages to. |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 271 | period (float) - The time in seconds for the printer thread to wait |
| 272 | between printing. |
| 273 | """ |
| 274 | self.fmt = fmt |
| 275 | if enabled is None: # pragma: no cover |
| 276 | self.enabled = logging.getLogger().isEnabledFor(logging.INFO) |
| 277 | else: |
| 278 | self.enabled = enabled |
| 279 | |
| 280 | self._count = 0 |
| 281 | self._dead = False |
| 282 | self._dead_cond = threading.Condition() |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 283 | self._stream = fout |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 284 | self._thread = threading.Thread(target=self._run) |
| 285 | self._period = period |
| 286 | |
| 287 | def _emit(self, s): |
| 288 | if self.enabled: |
| 289 | self._stream.write('\r' + s) |
| 290 | self._stream.flush() |
| 291 | |
| 292 | def _run(self): |
| 293 | with self._dead_cond: |
| 294 | while not self._dead: |
| 295 | self._emit(self.fmt % {'count': self._count}) |
| 296 | self._dead_cond.wait(self._period) |
| 297 | self._emit((self.fmt + '\n') % {'count': self._count}) |
| 298 | |
| 299 | def inc(self, amount=1): |
| 300 | self._count += amount |
| 301 | |
| 302 | def __enter__(self): |
| 303 | self._thread.start() |
| 304 | return self.inc |
| 305 | |
| 306 | def __exit__(self, _exc_type, _exc_value, _traceback): |
| 307 | self._dead = True |
| 308 | with self._dead_cond: |
| 309 | self._dead_cond.notifyAll() |
| 310 | self._thread.join() |
| 311 | del self._thread |
| 312 | |
| 313 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 314 | def once(function): |
| 315 | """@Decorates |function| so that it only performs its action once, no matter |
| 316 | how many times the decorated |function| is called.""" |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 317 | has_run = [False] |
| 318 | def _wrapper(*args, **kwargs): |
| 319 | if not has_run[0]: |
| 320 | has_run[0] = True |
| 321 | function(*args, **kwargs) |
| 322 | return _wrapper |
| 323 | |
| 324 | |
| 325 | def unicode_repr(s): |
| 326 | result = repr(s) |
| 327 | return result[1:] if result.startswith('u') else result |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | ## Git functions |
| 331 | |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 332 | def die(message, *args): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 333 | print(textwrap.dedent(message % args), file=sys.stderr) |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 334 | sys.exit(1) |
| 335 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 336 | |
Mark Mentovai | f548d08 | 2017-03-08 13:32:00 -0500 | [diff] [blame] | 337 | def blame(filename, revision=None, porcelain=False, abbrev=None, *_args): |
mgiuca@chromium.org | 8193756 | 2016-02-03 08:00:53 +0000 | [diff] [blame] | 338 | command = ['blame'] |
| 339 | if porcelain: |
| 340 | command.append('-p') |
| 341 | if revision is not None: |
| 342 | command.append(revision) |
Mark Mentovai | f548d08 | 2017-03-08 13:32:00 -0500 | [diff] [blame] | 343 | if abbrev is not None: |
| 344 | command.append('--abbrev=%d' % abbrev) |
mgiuca@chromium.org | 8193756 | 2016-02-03 08:00:53 +0000 | [diff] [blame] | 345 | command.extend(['--', filename]) |
| 346 | return run(*command) |
| 347 | |
| 348 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 349 | def branch_config(branch, option, default=None): |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 350 | return get_config('branch.%s.%s' % (branch, option), default=default) |
iannucci@chromium.org | 0d9e59c | 2016-01-09 08:08:41 +0000 | [diff] [blame] | 351 | |
| 352 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 353 | def branch_config_map(option): |
| 354 | """Return {branch: <|option| value>} for all branches.""" |
| 355 | try: |
| 356 | reg = re.compile(r'^branch\.(.*)\.%s$' % option) |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 357 | lines = get_config_regexp(reg.pattern) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 358 | return {reg.match(k).group(1): v for k, v in (l.split() for l in lines)} |
| 359 | except subprocess2.CalledProcessError: |
| 360 | return {} |
| 361 | |
| 362 | |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 363 | def branches(use_limit=True, *args): |
akuegel@chromium.org | 58888e1 | 2015-06-09 15:26:37 +0000 | [diff] [blame] | 364 | NO_BRANCH = ('* (no branch', '* (detached', '* (HEAD detached') |
iannucci@chromium.org | 3f23cdf | 2014-04-15 20:02:44 +0000 | [diff] [blame] | 365 | |
| 366 | key = 'depot-tools.branch-limit' |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 367 | limit = get_config_int(key, 20) |
iannucci@chromium.org | 3f23cdf | 2014-04-15 20:02:44 +0000 | [diff] [blame] | 368 | |
| 369 | raw_branches = run('branch', *args).splitlines() |
| 370 | |
| 371 | num = len(raw_branches) |
iannucci@chromium.org | 3f23cdf | 2014-04-15 20:02:44 +0000 | [diff] [blame] | 372 | |
Francois Doray | d42c681 | 2017-05-30 15:10:20 -0400 | [diff] [blame] | 373 | if use_limit and num > limit: |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 374 | die("""\ |
| 375 | Your git repo has too many branches (%d/%d) for this tool to work well. |
| 376 | |
| 377 | You may adjust this limit by running: |
iannucci@chromium.org | 3f23cdf | 2014-04-15 20:02:44 +0000 | [diff] [blame] | 378 | git config %s <new_limit> |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 379 | |
| 380 | You may also try cleaning up your old branches by running: |
| 381 | git cl archive |
| 382 | """, num, limit, key) |
iannucci@chromium.org | 3f23cdf | 2014-04-15 20:02:44 +0000 | [diff] [blame] | 383 | |
| 384 | for line in raw_branches: |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 385 | if line.startswith(NO_BRANCH): |
| 386 | continue |
| 387 | yield line.split()[-1] |
| 388 | |
| 389 | |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 390 | def get_config(option, default=None): |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 391 | try: |
| 392 | return run('config', '--get', option) or default |
| 393 | except subprocess2.CalledProcessError: |
| 394 | return default |
| 395 | |
| 396 | |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 397 | def get_config_int(option, default=0): |
| 398 | assert isinstance(default, int) |
| 399 | try: |
| 400 | return int(get_config(option, default)) |
| 401 | except ValueError: |
| 402 | return default |
| 403 | |
| 404 | |
| 405 | def get_config_list(option): |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 406 | try: |
| 407 | return run('config', '--get-all', option).split() |
| 408 | except subprocess2.CalledProcessError: |
| 409 | return [] |
| 410 | |
| 411 | |
agable | 7aa2ddd | 2016-06-21 07:47:00 -0700 | [diff] [blame] | 412 | def get_config_regexp(pattern): |
| 413 | if IS_WIN: # pragma: no cover |
| 414 | # this madness is because we call git.bat which calls git.exe which calls |
| 415 | # bash.exe (or something to that effect). Each layer divides the number of |
| 416 | # ^'s by 2. |
| 417 | pattern = pattern.replace('^', '^' * 8) |
| 418 | return run('config', '--get-regexp', pattern).splitlines() |
| 419 | |
| 420 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 421 | def current_branch(): |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 422 | try: |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 423 | return run('rev-parse', '--abbrev-ref', 'HEAD') |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 424 | except subprocess2.CalledProcessError: |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 425 | return None |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 426 | |
| 427 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 428 | def del_branch_config(branch, option, scope='local'): |
| 429 | del_config('branch.%s.%s' % (branch, option), scope=scope) |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 430 | |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 431 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 432 | def del_config(option, scope='local'): |
| 433 | try: |
| 434 | run('config', '--' + scope, '--unset', option) |
| 435 | except subprocess2.CalledProcessError: |
| 436 | pass |
| 437 | |
| 438 | |
mgiuca@chromium.org | 01d2cde | 2016-02-05 03:25:41 +0000 | [diff] [blame] | 439 | def diff(oldrev, newrev, *args): |
| 440 | return run('diff', oldrev, newrev, *args) |
| 441 | |
| 442 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 443 | def freeze(): |
| 444 | took_action = False |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 445 | key = 'depot-tools.freeze-size-limit' |
| 446 | MB = 2**20 |
| 447 | limit_mb = get_config_int(key, 100) |
| 448 | untracked_bytes = 0 |
| 449 | |
iannucci | eaca033 | 2016-08-03 16:46:50 -0700 | [diff] [blame] | 450 | root_path = repo_root() |
| 451 | |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 452 | for f, s in status(): |
| 453 | if is_unmerged(s): |
| 454 | die("Cannot freeze unmerged changes!") |
| 455 | if limit_mb > 0: |
| 456 | if s.lstat == '?': |
iannucci | eaca033 | 2016-08-03 16:46:50 -0700 | [diff] [blame] | 457 | untracked_bytes += os.stat(os.path.join(root_path, f)).st_size |
Bruce Dawson | 4bff3fd | 2018-01-04 14:44:23 -0800 | [diff] [blame] | 458 | if limit_mb > 0 and untracked_bytes > limit_mb * MB: |
| 459 | die("""\ |
| 460 | You appear to have too much untracked+unignored data in your git |
| 461 | checkout: %.1f / %d MB. |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 462 | |
Bruce Dawson | 4bff3fd | 2018-01-04 14:44:23 -0800 | [diff] [blame] | 463 | Run `git status` to see what it is. |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 464 | |
Bruce Dawson | 4bff3fd | 2018-01-04 14:44:23 -0800 | [diff] [blame] | 465 | In addition to making many git commands slower, this will prevent |
| 466 | depot_tools from freezing your in-progress changes. |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 467 | |
Bruce Dawson | 4bff3fd | 2018-01-04 14:44:23 -0800 | [diff] [blame] | 468 | You should add untracked data that you want to ignore to your repo's |
| 469 | .git/info/exclude |
| 470 | file. See `git help ignore` for the format of this file. |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 471 | |
Quinten Yearsley | 925cedb | 2020-04-13 17:49:39 +0000 | [diff] [blame^] | 472 | If this data is intended as part of your commit, you may adjust the |
Bruce Dawson | 4bff3fd | 2018-01-04 14:44:23 -0800 | [diff] [blame] | 473 | freeze limit by running: |
| 474 | git config %s <new_limit> |
| 475 | Where <new_limit> is an integer threshold in megabytes.""", |
| 476 | untracked_bytes / (MB * 1.0), limit_mb, key) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 477 | |
| 478 | try: |
iannucci@chromium.org | 3b4f228 | 2015-09-17 15:46:00 +0000 | [diff] [blame] | 479 | run('commit', '--no-verify', '-m', FREEZE + '.indexed') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 480 | took_action = True |
| 481 | except subprocess2.CalledProcessError: |
| 482 | pass |
| 483 | |
agable | 96e179b | 2016-06-24 10:32:51 -0700 | [diff] [blame] | 484 | add_errors = False |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 485 | try: |
agable | 96e179b | 2016-06-24 10:32:51 -0700 | [diff] [blame] | 486 | run('add', '-A', '--ignore-errors') |
| 487 | except subprocess2.CalledProcessError: |
| 488 | add_errors = True |
| 489 | |
| 490 | try: |
iannucci@chromium.org | 3b4f228 | 2015-09-17 15:46:00 +0000 | [diff] [blame] | 491 | run('commit', '--no-verify', '-m', FREEZE + '.unindexed') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 492 | took_action = True |
| 493 | except subprocess2.CalledProcessError: |
| 494 | pass |
| 495 | |
agable | 96e179b | 2016-06-24 10:32:51 -0700 | [diff] [blame] | 496 | ret = [] |
| 497 | if add_errors: |
| 498 | ret.append('Failed to index some unindexed files.') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 499 | if not took_action: |
agable | 96e179b | 2016-06-24 10:32:51 -0700 | [diff] [blame] | 500 | ret.append('Nothing to freeze.') |
| 501 | return ' '.join(ret) or None |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 502 | |
| 503 | |
| 504 | def get_branch_tree(): |
| 505 | """Get the dictionary of {branch: parent}, compatible with topo_iter. |
| 506 | |
| 507 | Returns a tuple of (skipped, <branch_tree dict>) where skipped is a set of |
| 508 | branches without upstream branches defined. |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 509 | """ |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 510 | skipped = set() |
| 511 | branch_tree = {} |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 512 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 513 | for branch in branches(): |
| 514 | parent = upstream(branch) |
| 515 | if not parent: |
| 516 | skipped.add(branch) |
| 517 | continue |
| 518 | branch_tree[branch] = parent |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 519 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 520 | return skipped, branch_tree |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 521 | |
| 522 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 523 | def get_or_create_merge_base(branch, parent=None): |
| 524 | """Finds the configured merge base for branch. |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 525 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 526 | If parent is supplied, it's used instead of calling upstream(branch). |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 527 | """ |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 528 | base = branch_config(branch, 'base') |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 529 | base_upstream = branch_config(branch, 'base-upstream') |
iannucci@chromium.org | edeaa81 | 2014-03-26 21:27:47 +0000 | [diff] [blame] | 530 | parent = parent or upstream(branch) |
sbc@chromium.org | 7970606 | 2015-01-14 21:18:12 +0000 | [diff] [blame] | 531 | if parent is None or branch is None: |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 532 | return None |
iannucci@chromium.org | edeaa81 | 2014-03-26 21:27:47 +0000 | [diff] [blame] | 533 | actual_merge_base = run('merge-base', parent, branch) |
| 534 | |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 535 | if base_upstream != parent: |
| 536 | base = None |
| 537 | base_upstream = None |
| 538 | |
iannucci@chromium.org | edeaa81 | 2014-03-26 21:27:47 +0000 | [diff] [blame] | 539 | def is_ancestor(a, b): |
| 540 | return run_with_retcode('merge-base', '--is-ancestor', a, b) == 0 |
| 541 | |
clemensh@chromium.org | c3fe99d | 2016-04-19 08:39:55 +0000 | [diff] [blame] | 542 | if base and base != actual_merge_base: |
iannucci@chromium.org | edeaa81 | 2014-03-26 21:27:47 +0000 | [diff] [blame] | 543 | if not is_ancestor(base, branch): |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 544 | logging.debug('Found WRONG pre-set merge-base for %s: %s', branch, base) |
| 545 | base = None |
iannucci@chromium.org | edeaa81 | 2014-03-26 21:27:47 +0000 | [diff] [blame] | 546 | elif is_ancestor(base, actual_merge_base): |
| 547 | logging.debug('Found OLD pre-set merge-base for %s: %s', branch, base) |
| 548 | base = None |
| 549 | else: |
| 550 | logging.debug('Found pre-set merge-base for %s: %s', branch, base) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 551 | |
| 552 | if not base: |
iannucci@chromium.org | edeaa81 | 2014-03-26 21:27:47 +0000 | [diff] [blame] | 553 | base = actual_merge_base |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 554 | manual_merge_base(branch, base, parent) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 555 | |
| 556 | return base |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 557 | |
| 558 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 559 | def hash_multi(*reflike): |
| 560 | return run('rev-parse', *reflike).splitlines() |
iannucci@chromium.org | 97345eb | 2014-03-13 07:55:15 +0000 | [diff] [blame] | 561 | |
| 562 | |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 563 | def hash_one(reflike, short=False): |
| 564 | args = ['rev-parse', reflike] |
| 565 | if short: |
| 566 | args.insert(1, '--short') |
| 567 | return run(*args) |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 568 | |
| 569 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 570 | def in_rebase(): |
| 571 | git_dir = run('rev-parse', '--git-dir') |
| 572 | return ( |
| 573 | os.path.exists(os.path.join(git_dir, 'rebase-merge')) or |
| 574 | os.path.exists(os.path.join(git_dir, 'rebase-apply'))) |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 575 | |
| 576 | |
| 577 | def intern_f(f, kind='blob'): |
| 578 | """Interns a file object into the git object store. |
| 579 | |
| 580 | Args: |
| 581 | f (file-like object) - The file-like object to intern |
| 582 | kind (git object type) - One of 'blob', 'commit', 'tree', 'tag'. |
| 583 | |
| 584 | Returns the git hash of the interned object (hex encoded). |
| 585 | """ |
| 586 | ret = run('hash-object', '-t', kind, '-w', '--stdin', stdin=f) |
| 587 | f.close() |
| 588 | return ret |
| 589 | |
| 590 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 591 | def is_dormant(branch): |
| 592 | # TODO(iannucci): Do an oldness check? |
| 593 | return branch_config(branch, 'dormant', 'false') != 'false' |
| 594 | |
| 595 | |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 596 | def is_unmerged(stat_value): |
| 597 | return ( |
| 598 | 'U' in (stat_value.lstat, stat_value.rstat) or |
| 599 | ((stat_value.lstat == stat_value.rstat) and stat_value.lstat in 'AD') |
| 600 | ) |
| 601 | |
| 602 | |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 603 | def manual_merge_base(branch, base, parent): |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 604 | set_branch_config(branch, 'base', base) |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 605 | set_branch_config(branch, 'base-upstream', parent) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 606 | |
| 607 | |
| 608 | def mktree(treedict): |
| 609 | """Makes a git tree object and returns its hash. |
| 610 | |
| 611 | See |tree()| for the values of mode, type, and ref. |
| 612 | |
| 613 | Args: |
| 614 | treedict - { name: (mode, type, ref) } |
| 615 | """ |
| 616 | with tempfile.TemporaryFile() as f: |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 617 | for name, (mode, typ, ref) in treedict.items(): |
Edward Lemur | 71681bf | 2019-10-09 23:46:20 +0000 | [diff] [blame] | 618 | f.write(('%s %s %s\t%s\0' % (mode, typ, ref, name)).encode('utf-8')) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 619 | f.seek(0) |
| 620 | return run('mktree', '-z', stdin=f) |
| 621 | |
| 622 | |
| 623 | def parse_commitrefs(*commitrefs): |
| 624 | """Returns binary encoded commit hashes for one or more commitrefs. |
| 625 | |
| 626 | A commitref is anything which can resolve to a commit. Popular examples: |
| 627 | * 'HEAD' |
| 628 | * 'origin/master' |
| 629 | * 'cool_branch~2' |
| 630 | """ |
| 631 | try: |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 632 | return [binascii.unhexlify(h) for h in hash_multi(*commitrefs)] |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 633 | except subprocess2.CalledProcessError: |
| 634 | raise BadCommitRefException(commitrefs) |
| 635 | |
| 636 | |
sbc@chromium.org | 384039b | 2014-10-13 21:01:00 +0000 | [diff] [blame] | 637 | RebaseRet = collections.namedtuple('RebaseRet', 'success stdout stderr') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 638 | |
| 639 | |
| 640 | def rebase(parent, start, branch, abort=False): |
| 641 | """Rebases |start|..|branch| onto the branch |parent|. |
| 642 | |
| 643 | Args: |
| 644 | parent - The new parent ref for the rebased commits. |
| 645 | start - The commit to start from |
| 646 | branch - The branch to rebase |
| 647 | abort - If True, will call git-rebase --abort in the event that the rebase |
| 648 | doesn't complete successfully. |
| 649 | |
| 650 | Returns a namedtuple with fields: |
| 651 | success - a boolean indicating that the rebase command completed |
| 652 | successfully. |
| 653 | message - if the rebase failed, this contains the stdout of the failed |
| 654 | rebase. |
| 655 | """ |
| 656 | try: |
| 657 | args = ['--onto', parent, start, branch] |
| 658 | if TEST_MODE: |
| 659 | args.insert(0, '--committer-date-is-author-date') |
| 660 | run('rebase', *args) |
sbc@chromium.org | 384039b | 2014-10-13 21:01:00 +0000 | [diff] [blame] | 661 | return RebaseRet(True, '', '') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 662 | except subprocess2.CalledProcessError as cpe: |
| 663 | if abort: |
iannucci@chromium.org | dabb78b | 2015-06-11 23:17:28 +0000 | [diff] [blame] | 664 | run_with_retcode('rebase', '--abort') # ignore failure |
sbc@chromium.org | 384039b | 2014-10-13 21:01:00 +0000 | [diff] [blame] | 665 | return RebaseRet(False, cpe.stdout, cpe.stderr) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 666 | |
| 667 | |
| 668 | def remove_merge_base(branch): |
| 669 | del_branch_config(branch, 'base') |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 670 | del_branch_config(branch, 'base-upstream') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 671 | |
| 672 | |
mgiuca@chromium.org | 8193756 | 2016-02-03 08:00:53 +0000 | [diff] [blame] | 673 | def repo_root(): |
| 674 | """Returns the absolute path to the repository root.""" |
| 675 | return run('rev-parse', '--show-toplevel') |
| 676 | |
| 677 | |
Jeffrey Yasskin | 6b52dc2 | 2019-12-06 18:32:21 +0000 | [diff] [blame] | 678 | def upstream_default(): |
| 679 | """Returns the default branch name of the origin repository.""" |
| 680 | try: |
| 681 | return run('rev-parse', '--abbrev-ref', 'origin/HEAD') |
| 682 | except subprocess2.CalledProcessError: |
| 683 | return 'origin/master' |
| 684 | |
| 685 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 686 | def root(): |
Jeffrey Yasskin | 6b52dc2 | 2019-12-06 18:32:21 +0000 | [diff] [blame] | 687 | return get_config('depot-tools.upstream', upstream_default()) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 688 | |
| 689 | |
mgiuca@chromium.org | 8193756 | 2016-02-03 08:00:53 +0000 | [diff] [blame] | 690 | @contextlib.contextmanager |
| 691 | def less(): # pragma: no cover |
| 692 | """Runs 'less' as context manager yielding its stdin as a PIPE. |
| 693 | |
| 694 | Automatically checks if sys.stdout is a non-TTY stream. If so, it avoids |
| 695 | running less and just yields sys.stdout. |
Edward Lemur | 0d462e9 | 2020-01-08 20:11:31 +0000 | [diff] [blame] | 696 | |
| 697 | The returned PIPE is opened on binary mode. |
mgiuca@chromium.org | 8193756 | 2016-02-03 08:00:53 +0000 | [diff] [blame] | 698 | """ |
iannucci@chromium.org | 596cd5c | 2016-04-04 21:34:39 +0000 | [diff] [blame] | 699 | if not setup_color.IS_TTY: |
Edward Lemur | 5e94b80 | 2019-11-26 21:44:08 +0000 | [diff] [blame] | 700 | # On Python 3, sys.stdout doesn't accept bytes, and sys.stdout.buffer must |
| 701 | # be used. |
| 702 | yield getattr(sys.stdout, 'buffer', sys.stdout) |
mgiuca@chromium.org | 8193756 | 2016-02-03 08:00:53 +0000 | [diff] [blame] | 703 | return |
| 704 | |
| 705 | # Run with the same options that git uses (see setup_pager in git repo). |
| 706 | # -F: Automatically quit if the output is less than one screen. |
| 707 | # -R: Don't escape ANSI color codes. |
| 708 | # -X: Don't clear the screen before starting. |
| 709 | cmd = ('less', '-FRX') |
| 710 | try: |
| 711 | proc = subprocess2.Popen(cmd, stdin=subprocess2.PIPE) |
| 712 | yield proc.stdin |
| 713 | finally: |
Edward Lemur | b800fde | 2020-01-10 23:04:44 +0000 | [diff] [blame] | 714 | try: |
| 715 | proc.stdin.close() |
| 716 | except BrokenPipeError: |
| 717 | # BrokenPipeError is raised if proc has already completed, |
| 718 | pass |
mgiuca@chromium.org | 8193756 | 2016-02-03 08:00:53 +0000 | [diff] [blame] | 719 | proc.wait() |
| 720 | |
| 721 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 722 | def run(*cmd, **kwargs): |
| 723 | """The same as run_with_stderr, except it only returns stdout.""" |
| 724 | return run_with_stderr(*cmd, **kwargs)[0] |
| 725 | |
| 726 | |
agable@chromium.org | d629fb4 | 2014-10-01 09:40:10 +0000 | [diff] [blame] | 727 | def run_with_retcode(*cmd, **kwargs): |
| 728 | """Run a command but only return the status code.""" |
| 729 | try: |
| 730 | run(*cmd, **kwargs) |
| 731 | return 0 |
| 732 | except subprocess2.CalledProcessError as cpe: |
| 733 | return cpe.returncode |
| 734 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 735 | def run_stream(*cmd, **kwargs): |
| 736 | """Runs a git command. Returns stdout as a PIPE (file-like object). |
| 737 | |
| 738 | stderr is dropped to avoid races if the process outputs to both stdout and |
| 739 | stderr. |
| 740 | """ |
| 741 | kwargs.setdefault('stderr', subprocess2.VOID) |
| 742 | kwargs.setdefault('stdout', subprocess2.PIPE) |
iannucci@chromium.org | 0d9e59c | 2016-01-09 08:08:41 +0000 | [diff] [blame] | 743 | kwargs.setdefault('shell', False) |
iannucci@chromium.org | 2198002 | 2014-04-11 04:51:49 +0000 | [diff] [blame] | 744 | cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 745 | proc = subprocess2.Popen(cmd, **kwargs) |
| 746 | return proc.stdout |
| 747 | |
| 748 | |
tandrii@chromium.org | 6c14310 | 2015-06-11 19:21:02 +0000 | [diff] [blame] | 749 | @contextlib.contextmanager |
| 750 | def run_stream_with_retcode(*cmd, **kwargs): |
| 751 | """Runs a git command as context manager yielding stdout as a PIPE. |
| 752 | |
| 753 | stderr is dropped to avoid races if the process outputs to both stdout and |
| 754 | stderr. |
| 755 | |
| 756 | Raises subprocess2.CalledProcessError on nonzero return code. |
| 757 | """ |
| 758 | kwargs.setdefault('stderr', subprocess2.VOID) |
| 759 | kwargs.setdefault('stdout', subprocess2.PIPE) |
iannucci@chromium.org | 0d9e59c | 2016-01-09 08:08:41 +0000 | [diff] [blame] | 760 | kwargs.setdefault('shell', False) |
tandrii@chromium.org | 6c14310 | 2015-06-11 19:21:02 +0000 | [diff] [blame] | 761 | cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd |
| 762 | try: |
| 763 | proc = subprocess2.Popen(cmd, **kwargs) |
| 764 | yield proc.stdout |
| 765 | finally: |
| 766 | retcode = proc.wait() |
| 767 | if retcode != 0: |
| 768 | raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), |
| 769 | None, None) |
| 770 | |
| 771 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 772 | def run_with_stderr(*cmd, **kwargs): |
| 773 | """Runs a git command. |
| 774 | |
| 775 | Returns (stdout, stderr) as a pair of strings. |
| 776 | |
| 777 | kwargs |
| 778 | autostrip (bool) - Strip the output. Defaults to True. |
| 779 | indata (str) - Specifies stdin data for the process. |
| 780 | """ |
| 781 | kwargs.setdefault('stdin', subprocess2.PIPE) |
| 782 | kwargs.setdefault('stdout', subprocess2.PIPE) |
| 783 | kwargs.setdefault('stderr', subprocess2.PIPE) |
iannucci@chromium.org | 0d9e59c | 2016-01-09 08:08:41 +0000 | [diff] [blame] | 784 | kwargs.setdefault('shell', False) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 785 | autostrip = kwargs.pop('autostrip', True) |
| 786 | indata = kwargs.pop('indata', None) |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 787 | decode = kwargs.pop('decode', True) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 788 | |
iannucci@chromium.org | 2198002 | 2014-04-11 04:51:49 +0000 | [diff] [blame] | 789 | cmd = (GIT_EXE, '-c', 'color.ui=never') + cmd |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 790 | proc = subprocess2.Popen(cmd, **kwargs) |
| 791 | ret, err = proc.communicate(indata) |
| 792 | retcode = proc.wait() |
| 793 | if retcode != 0: |
| 794 | raise subprocess2.CalledProcessError(retcode, cmd, os.getcwd(), ret, err) |
| 795 | |
| 796 | if autostrip: |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 797 | ret = (ret or b'').strip() |
| 798 | err = (err or b'').strip() |
| 799 | |
| 800 | if decode: |
| 801 | ret = ret.decode('utf-8', 'replace') |
| 802 | err = err.decode('utf-8', 'replace') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 803 | |
| 804 | return ret, err |
| 805 | |
| 806 | |
| 807 | def set_branch_config(branch, option, value, scope='local'): |
| 808 | set_config('branch.%s.%s' % (branch, option), value, scope=scope) |
| 809 | |
| 810 | |
| 811 | def set_config(option, value, scope='local'): |
| 812 | run('config', '--' + scope, option, value) |
| 813 | |
agable@chromium.org | d629fb4 | 2014-10-01 09:40:10 +0000 | [diff] [blame] | 814 | |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 815 | def get_dirty_files(): |
| 816 | # Make sure index is up-to-date before running diff-index. |
| 817 | run_with_retcode('update-index', '--refresh', '-q') |
Eli Ribble | 54434e7 | 2019-05-24 00:41:15 +0000 | [diff] [blame] | 818 | return run('diff-index', '--ignore-submodules', '--name-status', 'HEAD') |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 819 | |
| 820 | |
| 821 | def is_dirty_git_tree(cmd): |
iannucci | e38699b | 2016-08-15 17:32:31 -0700 | [diff] [blame] | 822 | w = lambda s: sys.stderr.write(s+"\n") |
| 823 | |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 824 | dirty = get_dirty_files() |
| 825 | if dirty: |
iannucci | e38699b | 2016-08-15 17:32:31 -0700 | [diff] [blame] | 826 | w('Cannot %s with a dirty tree. Commit, freeze or stash your changes first.' |
| 827 | % cmd) |
| 828 | w('Uncommitted files: (git diff-index --name-status HEAD)') |
| 829 | w(dirty[:4096]) |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 830 | if len(dirty) > 4096: # pragma: no cover |
iannucci | e38699b | 2016-08-15 17:32:31 -0700 | [diff] [blame] | 831 | w('... (run "git diff-index --name-status HEAD" to see full output).') |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 832 | return True |
| 833 | return False |
| 834 | |
| 835 | |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 836 | def status(): |
| 837 | """Returns a parsed version of git-status. |
| 838 | |
| 839 | Returns a generator of (current_name, (lstat, rstat, src)) pairs where: |
| 840 | * current_name is the name of the file |
| 841 | * lstat is the left status code letter from git-status |
| 842 | * rstat is the left status code letter from git-status |
| 843 | * src is the current name of the file, or the original name of the file |
| 844 | if lstat == 'R' |
| 845 | """ |
| 846 | stat_entry = collections.namedtuple('stat_entry', 'lstat rstat src') |
| 847 | |
| 848 | def tokenizer(stream): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 849 | acc = BytesIO() |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 850 | c = None |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 851 | while c != b'': |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 852 | c = stream.read(1) |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 853 | if c in (None, b'', b'\0'): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 854 | if len(acc.getvalue()): |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 855 | yield acc.getvalue() |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 856 | acc = BytesIO() |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 857 | else: |
| 858 | acc.write(c) |
| 859 | |
| 860 | def parser(tokens): |
| 861 | while True: |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 862 | try: |
| 863 | status_dest = next(tokens).decode('utf-8') |
| 864 | except StopIteration: |
| 865 | return |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 866 | stat, dest = status_dest[:2], status_dest[3:] |
| 867 | lstat, rstat = stat |
| 868 | if lstat == 'R': |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 869 | src = next(tokens).decode('utf-8') |
agable | 02b3c98 | 2016-06-22 07:51:22 -0700 | [diff] [blame] | 870 | else: |
| 871 | src = dest |
| 872 | yield (dest, stat_entry(lstat, rstat, src)) |
| 873 | |
| 874 | return parser(tokenizer(run_stream('status', '-z', bufsize=-1))) |
| 875 | |
| 876 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 877 | def squash_current_branch(header=None, merge_base=None): |
Alan Cutter | 0001782 | 2016-12-20 17:39:59 +1100 | [diff] [blame] | 878 | header = header or 'git squash commit for %s.' % current_branch() |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 879 | merge_base = merge_base or get_or_create_merge_base(current_branch()) |
| 880 | log_msg = header + '\n' |
| 881 | if log_msg: |
| 882 | log_msg += '\n' |
| 883 | log_msg += run('log', '--reverse', '--format=%H%n%B', '%s..HEAD' % merge_base) |
| 884 | run('reset', '--soft', merge_base) |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 885 | |
| 886 | if not get_dirty_files(): |
| 887 | # Sometimes the squash can result in the same tree, meaning that there is |
| 888 | # nothing to commit at this point. |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 889 | print('Nothing to commit; squashed branch is empty') |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 890 | return False |
Edward Lemur | 71681bf | 2019-10-09 23:46:20 +0000 | [diff] [blame] | 891 | run('commit', '--no-verify', '-a', '-F', '-', indata=log_msg.encode('utf-8')) |
sbc@chromium.org | 71437c0 | 2015-04-09 19:29:40 +0000 | [diff] [blame] | 892 | return True |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 893 | |
| 894 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 895 | def tags(*args): |
| 896 | return run('tag', *args).splitlines() |
| 897 | |
| 898 | |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 899 | def thaw(): |
| 900 | took_action = False |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 901 | for sha in run_stream('rev-list', 'HEAD').readlines(): |
| 902 | sha = sha.strip().decode('utf-8') |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 903 | msg = run('show', '--format=%f%b', '-s', 'HEAD') |
| 904 | match = FREEZE_MATCHER.match(msg) |
| 905 | if not match: |
| 906 | if not took_action: |
| 907 | return 'Nothing to thaw.' |
| 908 | break |
| 909 | |
| 910 | run('reset', '--' + FREEZE_SECTIONS[match.group(1)], sha) |
| 911 | took_action = True |
| 912 | |
| 913 | |
| 914 | def topo_iter(branch_tree, top_down=True): |
| 915 | """Generates (branch, parent) in topographical order for a branch tree. |
| 916 | |
| 917 | Given a tree: |
| 918 | |
| 919 | A1 |
| 920 | B1 B2 |
| 921 | C1 C2 C3 |
| 922 | D1 |
| 923 | |
| 924 | branch_tree would look like: { |
| 925 | 'D1': 'C3', |
| 926 | 'C3': 'B2', |
| 927 | 'B2': 'A1', |
| 928 | 'C1': 'B1', |
| 929 | 'C2': 'B1', |
| 930 | 'B1': 'A1', |
| 931 | } |
| 932 | |
| 933 | It is OK to have multiple 'root' nodes in your graph. |
| 934 | |
| 935 | if top_down is True, items are yielded from A->D. Otherwise they're yielded |
| 936 | from D->A. Within a layer the branches will be yielded in sorted order. |
| 937 | """ |
| 938 | branch_tree = branch_tree.copy() |
| 939 | |
| 940 | # TODO(iannucci): There is probably a more efficient way to do these. |
| 941 | if top_down: |
| 942 | while branch_tree: |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 943 | this_pass = [(b, p) for b, p in branch_tree.items() |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 944 | if p not in branch_tree] |
| 945 | assert this_pass, "Branch tree has cycles: %r" % branch_tree |
| 946 | for branch, parent in sorted(this_pass): |
| 947 | yield branch, parent |
| 948 | del branch_tree[branch] |
| 949 | else: |
| 950 | parent_to_branches = collections.defaultdict(set) |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 951 | for branch, parent in branch_tree.items(): |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 952 | parent_to_branches[parent].add(branch) |
| 953 | |
| 954 | while branch_tree: |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 955 | this_pass = [(b, p) for b, p in branch_tree.items() |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 956 | if not parent_to_branches[b]] |
| 957 | assert this_pass, "Branch tree has cycles: %r" % branch_tree |
| 958 | for branch, parent in sorted(this_pass): |
| 959 | yield branch, parent |
| 960 | parent_to_branches[parent].discard(branch) |
| 961 | del branch_tree[branch] |
| 962 | |
| 963 | |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 964 | def tree(treeref, recurse=False): |
| 965 | """Returns a dict representation of a git tree object. |
| 966 | |
| 967 | Args: |
| 968 | treeref (str) - a git ref which resolves to a tree (commits count as trees). |
qyearsley | 12fa6ff | 2016-08-24 09:18:40 -0700 | [diff] [blame] | 969 | recurse (bool) - include all of the tree's descendants too. File names will |
iannucci@chromium.org | aa74cf6 | 2013-11-19 20:00:49 +0000 | [diff] [blame] | 970 | take the form of 'some/path/to/file'. |
| 971 | |
| 972 | Return format: |
| 973 | { 'file_name': (mode, type, ref) } |
| 974 | |
| 975 | mode is an integer where: |
| 976 | * 0040000 - Directory |
| 977 | * 0100644 - Regular non-executable file |
| 978 | * 0100664 - Regular non-executable group-writeable file |
| 979 | * 0100755 - Regular executable file |
| 980 | * 0120000 - Symbolic link |
| 981 | * 0160000 - Gitlink |
| 982 | |
| 983 | type is a string where it's one of 'blob', 'commit', 'tree', 'tag'. |
| 984 | |
| 985 | ref is the hex encoded hash of the entry. |
| 986 | """ |
| 987 | ret = {} |
| 988 | opts = ['ls-tree', '--full-tree'] |
| 989 | if recurse: |
| 990 | opts.append('-r') |
| 991 | opts.append(treeref) |
| 992 | try: |
| 993 | for line in run(*opts).splitlines(): |
| 994 | mode, typ, ref, name = line.split(None, 3) |
| 995 | ret[name] = (mode, typ, ref) |
| 996 | except subprocess2.CalledProcessError: |
| 997 | return None |
| 998 | return ret |
| 999 | |
| 1000 | |
Mun Yong Jang | 781e71e | 2017-10-25 15:46:20 -0700 | [diff] [blame] | 1001 | def get_remote_url(remote='origin'): |
| 1002 | try: |
| 1003 | return run('config', 'remote.%s.url' % remote) |
| 1004 | except subprocess2.CalledProcessError: |
| 1005 | return None |
| 1006 | |
| 1007 | |
iannucci@chromium.org | 8bc9b5c | 2014-03-12 01:36:18 +0000 | [diff] [blame] | 1008 | def upstream(branch): |
| 1009 | try: |
| 1010 | return run('rev-parse', '--abbrev-ref', '--symbolic-full-name', |
| 1011 | branch+'@{upstream}') |
| 1012 | except subprocess2.CalledProcessError: |
| 1013 | return None |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 1014 | |
agable@chromium.org | d629fb4 | 2014-10-01 09:40:10 +0000 | [diff] [blame] | 1015 | |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 1016 | def get_git_version(): |
| 1017 | """Returns a tuple that contains the numeric components of the current git |
| 1018 | version.""" |
| 1019 | version_string = run('--version') |
| 1020 | version_match = re.search(r'(\d+.)+(\d+)', version_string) |
| 1021 | version = version_match.group() if version_match else '' |
| 1022 | |
| 1023 | return tuple(int(x) for x in version.split('.')) |
| 1024 | |
| 1025 | |
calamity@chromium.org | 745ffa6 | 2014-09-08 01:03:19 +0000 | [diff] [blame] | 1026 | def get_branches_info(include_tracking_status): |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 1027 | format_string = ( |
| 1028 | '--format=%(refname:short):%(objectname:short):%(upstream:short):') |
| 1029 | |
| 1030 | # This is not covered by the depot_tools CQ which only has git version 1.8. |
calamity@chromium.org | 745ffa6 | 2014-09-08 01:03:19 +0000 | [diff] [blame] | 1031 | if (include_tracking_status and |
| 1032 | get_git_version() >= MIN_UPSTREAM_TRACK_GIT_VERSION): # pragma: no cover |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 1033 | format_string += '%(upstream:track)' |
| 1034 | |
| 1035 | info_map = {} |
| 1036 | data = run('for-each-ref', format_string, 'refs/heads') |
calamity@chromium.org | 745ffa6 | 2014-09-08 01:03:19 +0000 | [diff] [blame] | 1037 | BranchesInfo = collections.namedtuple( |
| 1038 | 'BranchesInfo', 'hash upstream ahead behind') |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 1039 | for line in data.splitlines(): |
| 1040 | (branch, branch_hash, upstream_branch, tracking_status) = line.split(':') |
| 1041 | |
| 1042 | ahead_match = re.search(r'ahead (\d+)', tracking_status) |
| 1043 | ahead = int(ahead_match.group(1)) if ahead_match else None |
| 1044 | |
| 1045 | behind_match = re.search(r'behind (\d+)', tracking_status) |
| 1046 | behind = int(behind_match.group(1)) if behind_match else None |
| 1047 | |
calamity@chromium.org | 745ffa6 | 2014-09-08 01:03:19 +0000 | [diff] [blame] | 1048 | info_map[branch] = BranchesInfo( |
calamity@chromium.org | 9d2c880 | 2014-09-03 02:04:46 +0000 | [diff] [blame] | 1049 | hash=branch_hash, upstream=upstream_branch, ahead=ahead, behind=behind) |
| 1050 | |
| 1051 | # Set None for upstreams which are not branches (e.g empty upstream, remotes |
| 1052 | # and deleted upstream branches). |
| 1053 | missing_upstreams = {} |
| 1054 | for info in info_map.values(): |
| 1055 | if info.upstream not in info_map and info.upstream not in missing_upstreams: |
| 1056 | missing_upstreams[info.upstream] = None |
| 1057 | |
Edward Lemur | 12a537f | 2019-10-03 21:57:15 +0000 | [diff] [blame] | 1058 | result = info_map.copy() |
| 1059 | result.update(missing_upstreams) |
| 1060 | return result |
sammc@chromium.org | 900a33f | 2015-09-29 06:57:09 +0000 | [diff] [blame] | 1061 | |
| 1062 | |
| 1063 | def make_workdir_common(repository, new_workdir, files_to_symlink, |
scottmg@chromium.org | d4218d4 | 2015-10-07 23:49:20 +0000 | [diff] [blame] | 1064 | files_to_copy, symlink=None): |
| 1065 | if not symlink: |
| 1066 | symlink = os.symlink |
sammc@chromium.org | 900a33f | 2015-09-29 06:57:09 +0000 | [diff] [blame] | 1067 | os.makedirs(new_workdir) |
| 1068 | for entry in files_to_symlink: |
scottmg@chromium.org | d4218d4 | 2015-10-07 23:49:20 +0000 | [diff] [blame] | 1069 | clone_file(repository, new_workdir, entry, symlink) |
sammc@chromium.org | 900a33f | 2015-09-29 06:57:09 +0000 | [diff] [blame] | 1070 | for entry in files_to_copy: |
| 1071 | clone_file(repository, new_workdir, entry, shutil.copy) |
| 1072 | |
| 1073 | |
| 1074 | def make_workdir(repository, new_workdir): |
| 1075 | GIT_DIRECTORY_WHITELIST = [ |
| 1076 | 'config', |
| 1077 | 'info', |
| 1078 | 'hooks', |
| 1079 | 'logs/refs', |
| 1080 | 'objects', |
| 1081 | 'packed-refs', |
| 1082 | 'refs', |
| 1083 | 'remotes', |
| 1084 | 'rr-cache', |
sammc@chromium.org | 900a33f | 2015-09-29 06:57:09 +0000 | [diff] [blame] | 1085 | ] |
| 1086 | make_workdir_common(repository, new_workdir, GIT_DIRECTORY_WHITELIST, |
| 1087 | ['HEAD']) |
| 1088 | |
| 1089 | |
| 1090 | def clone_file(repository, new_workdir, link, operation): |
| 1091 | if not os.path.exists(os.path.join(repository, link)): |
| 1092 | return |
| 1093 | link_dir = os.path.dirname(os.path.join(new_workdir, link)) |
| 1094 | if not os.path.exists(link_dir): |
| 1095 | os.makedirs(link_dir) |
Henrique Ferreiro | fd4ad24 | 2018-01-10 12:19:18 +0100 | [diff] [blame] | 1096 | src = os.path.join(repository, link) |
| 1097 | if os.path.islink(src): |
Henrique Ferreiro | aea45d2 | 2018-02-19 09:48:36 +0100 | [diff] [blame] | 1098 | src = os.path.realpath(src) |
Henrique Ferreiro | fd4ad24 | 2018-01-10 12:19:18 +0100 | [diff] [blame] | 1099 | operation(src, os.path.join(new_workdir, link)) |