blob: f581c9b626e1284853cb30299d3105b8100ee2a5 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
agable@chromium.org5a306a22014-02-24 22:13:59 +00002# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""A git command for managing a local cache of git repositories."""
7
Gavin Mak42674f52023-08-24 20:39:59 +00008from __future__ import print_function
9
Andrii Shyshkalov4f56f232017-11-23 02:19:25 -080010import contextlib
agable@chromium.org5a306a22014-02-24 22:13:59 +000011import logging
12import optparse
13import os
szager@chromium.org174766f2014-05-13 21:27:46 +000014import re
John Budorick47ec0692019-05-01 15:04:28 +000015import subprocess
16import sys
agable@chromium.org5a306a22014-02-24 22:13:59 +000017import tempfile
szager@chromium.org1132f5f2014-08-23 01:57:59 +000018import threading
pgervais@chromium.orgf3726102014-04-17 17:24:15 +000019import time
Gavin Mak42674f52023-08-24 20:39:59 +000020
21try:
22 import urlparse
23except ImportError: # For Py3 compatibility
24 import urllib.parse as urlparse
Raul Tambreb946b232019-03-26 14:48:46 +000025
hinoka@google.com563559c2014-04-02 00:36:24 +000026from download_from_google_storage import Gsutil
agable@chromium.org5a306a22014-02-24 22:13:59 +000027import gclient_utils
Josip Sokcevic14a83ae2020-05-21 01:36:34 +000028import lockfile
Edward Lesmescb047442021-05-06 20:18:49 +000029import metrics
agable@chromium.org5a306a22014-02-24 22:13:59 +000030import subcommand
31
szager@chromium.org301a7c32014-06-16 17:13:50 +000032# Analogous to gc.autopacklimit git config.
33GC_AUTOPACKLIMIT = 50
Takuto Ikuta9fce2132017-12-14 10:44:28 +090034
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +000035GIT_CACHE_CORRUPT_MESSAGE = 'WARNING: The Git cache is corrupt.'
36
Josip Sokcevic604f1602021-10-15 15:45:10 +000037# gsutil creates many processes and threads. Creating too many gsutil cp
38# processes may result in running out of resources, and may perform worse due to
39# contextr switching. This limits how many concurrent gsutil cp processes
40# git_cache runs.
41GSUTIL_CP_SEMAPHORE = threading.Semaphore(2)
42
szager@chromium.org848fd492014-04-09 19:06:44 +000043try:
Quinten Yearsleyb2cc4a92016-12-15 13:53:26 -080044 # pylint: disable=undefined-variable
szager@chromium.org848fd492014-04-09 19:06:44 +000045 WinErr = WindowsError
46except NameError:
47 class WinErr(Exception):
48 pass
agable@chromium.org5a306a22014-02-24 22:13:59 +000049
hinokadcd84042016-06-09 14:26:17 -070050class ClobberNeeded(Exception):
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +000051 pass
agable@chromium.org5a306a22014-02-24 22:13:59 +000052
dnj4625b5a2016-11-10 18:23:26 -080053
54def exponential_backoff_retry(fn, excs=(Exception,), name=None, count=10,
55 sleep_time=0.25, printerr=None):
56 """Executes |fn| up to |count| times, backing off exponentially.
57
58 Args:
59 fn (callable): The function to execute. If this raises a handled
60 exception, the function will retry with exponential backoff.
61 excs (tuple): A tuple of Exception types to handle. If one of these is
62 raised by |fn|, a retry will be attempted. If |fn| raises an Exception
63 that is not in this list, it will immediately pass through. If |excs|
64 is empty, the Exception base class will be used.
65 name (str): Optional operation name to print in the retry string.
66 count (int): The number of times to try before allowing the exception to
67 pass through.
68 sleep_time (float): The initial number of seconds to sleep in between
69 retries. This will be doubled each retry.
70 printerr (callable): Function that will be called with the error string upon
71 failures. If None, |logging.warning| will be used.
72
73 Returns: The return value of the successful fn.
74 """
75 printerr = printerr or logging.warning
Edward Lesmes451e8ba2019-10-01 22:15:33 +000076 for i in range(count):
dnj4625b5a2016-11-10 18:23:26 -080077 try:
78 return fn()
79 except excs as e:
80 if (i+1) >= count:
81 raise
82
83 printerr('Retrying %s in %.2f second(s) (%d / %d attempts): %s' % (
84 (name or 'operation'), sleep_time, (i+1), count, e))
85 time.sleep(sleep_time)
86 sleep_time *= 2
87
88
szager@chromium.org848fd492014-04-09 19:06:44 +000089class Mirror(object):
90
91 git_exe = 'git.bat' if sys.platform.startswith('win') else 'git'
92 gsutil_exe = os.path.join(
hinoka@chromium.orgb091aa52014-12-20 01:47:31 +000093 os.path.dirname(os.path.abspath(__file__)), 'gsutil.py')
Vadim Shtayura08049e22017-10-11 00:14:52 +000094 cachepath_lock = threading.Lock()
szager@chromium.org848fd492014-04-09 19:06:44 +000095
Robert Iannuccia19649b2018-06-29 16:31:45 +000096 UNSET_CACHEPATH = object()
97
98 # Used for tests
99 _GIT_CONFIG_LOCATION = []
100
szager@chromium.org66c8b852015-09-22 23:19:07 +0000101 @staticmethod
102 def parse_fetch_spec(spec):
103 """Parses and canonicalizes a fetch spec.
104
105 Returns (fetchspec, value_regex), where value_regex can be used
106 with 'git config --replace-all'.
107 """
108 parts = spec.split(':', 1)
109 src = parts[0].lstrip('+').rstrip('/')
110 if not src.startswith('refs/'):
111 src = 'refs/heads/%s' % src
112 dest = parts[1].rstrip('/') if len(parts) > 1 else src
113 regex = r'\+%s:.*' % src.replace('*', r'\*')
114 return ('+%s:%s' % (src, dest), regex)
115
Edward Lesmes07a68342021-04-20 23:39:30 +0000116 def __init__(self, url, refs=None, commits=None, print_func=None):
szager@chromium.org848fd492014-04-09 19:06:44 +0000117 self.url = url
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +0000118 self.fetch_specs = {self.parse_fetch_spec(ref) for ref in (refs or [])}
Edward Lesmes07a68342021-04-20 23:39:30 +0000119 self.fetch_commits = set(commits or [])
szager@chromium.org848fd492014-04-09 19:06:44 +0000120 self.basedir = self.UrlToCacheDir(url)
121 self.mirror_path = os.path.join(self.GetCachePath(), self.basedir)
loislo@chromium.org0fb693f2014-12-25 15:28:22 +0000122 if print_func:
123 self.print = self.print_without_file
124 self.print_func = print_func
125 else:
126 self.print = print
127
dnj4625b5a2016-11-10 18:23:26 -0800128 def print_without_file(self, message, **_kwargs):
loislo@chromium.org0fb693f2014-12-25 15:28:22 +0000129 self.print_func(message)
szager@chromium.org848fd492014-04-09 19:06:44 +0000130
Andrii Shyshkalov4f56f232017-11-23 02:19:25 -0800131 @contextlib.contextmanager
132 def print_duration_of(self, what):
133 start = time.time()
134 try:
135 yield
136 finally:
137 self.print('%s took %.1f minutes' % (what, (time.time() - start) / 60.0))
138
hinoka@chromium.orgf8fa23d2014-06-05 01:00:04 +0000139 @property
140 def bootstrap_bucket(self):
Andrii Shyshkalov4b79c382019-04-15 23:48:35 +0000141 b = os.getenv('OVERRIDE_BOOTSTRAP_BUCKET')
142 if b:
143 return b
Gavin Mak42674f52023-08-24 20:39:59 +0000144 u = urlparse.urlparse(self.url)
Ryan Tseng3beabd02017-03-15 13:57:58 -0700145 if u.netloc == 'chromium.googlesource.com':
hinoka@chromium.orgf8fa23d2014-06-05 01:00:04 +0000146 return 'chromium-git-cache'
Ryan Tseng3beabd02017-03-15 13:57:58 -0700147 # Not recognized.
148 return None
hinoka@chromium.orgf8fa23d2014-06-05 01:00:04 +0000149
Karen Qiandcad7492019-04-26 03:11:16 +0000150 @property
151 def _gs_path(self):
152 return 'gs://%s/v2/%s' % (self.bootstrap_bucket, self.basedir)
153
szager@chromium.org174766f2014-05-13 21:27:46 +0000154 @classmethod
155 def FromPath(cls, path):
156 return cls(cls.CacheDirToUrl(path))
157
szager@chromium.org848fd492014-04-09 19:06:44 +0000158 @staticmethod
159 def UrlToCacheDir(url):
160 """Convert a git url to a normalized form for the cache dir path."""
Edward Lemure9024d02019-11-19 18:47:46 +0000161 if os.path.isdir(url):
162 # Ignore the drive letter in Windows
163 url = os.path.splitdrive(url)[1]
164 return url.replace('-', '--').replace(os.sep, '-')
165
Gavin Mak42674f52023-08-24 20:39:59 +0000166 parsed = urlparse.urlparse(url)
Edward Lemure9024d02019-11-19 18:47:46 +0000167 norm_url = parsed.netloc + parsed.path
szager@chromium.org848fd492014-04-09 19:06:44 +0000168 if norm_url.endswith('.git'):
169 norm_url = norm_url[:-len('.git')]
Dirk Prankedb589542019-04-12 21:07:01 +0000170
171 # Use the same dir for authenticated URLs and unauthenticated URLs.
172 norm_url = norm_url.replace('googlesource.com/a/', 'googlesource.com/')
173
szager@chromium.org848fd492014-04-09 19:06:44 +0000174 return norm_url.replace('-', '--').replace('/', '-').lower()
175
176 @staticmethod
szager@chromium.org174766f2014-05-13 21:27:46 +0000177 def CacheDirToUrl(path):
178 """Convert a cache dir path to its corresponding url."""
179 netpath = re.sub(r'\b-\b', '/', os.path.basename(path)).replace('--', '-')
180 return 'https://%s' % netpath
181
szager@chromium.org848fd492014-04-09 19:06:44 +0000182 @classmethod
183 def SetCachePath(cls, cachepath):
Vadim Shtayura08049e22017-10-11 00:14:52 +0000184 with cls.cachepath_lock:
185 setattr(cls, 'cachepath', cachepath)
szager@chromium.org848fd492014-04-09 19:06:44 +0000186
187 @classmethod
188 def GetCachePath(cls):
Vadim Shtayura08049e22017-10-11 00:14:52 +0000189 with cls.cachepath_lock:
190 if not hasattr(cls, 'cachepath'):
191 try:
192 cachepath = subprocess.check_output(
Robert Iannuccia19649b2018-06-29 16:31:45 +0000193 [cls.git_exe, 'config'] +
194 cls._GIT_CONFIG_LOCATION +
Edward Lesmes4c3eb702020-03-25 21:09:30 +0000195 ['cache.cachepath']).decode('utf-8', 'ignore').strip()
Vadim Shtayura08049e22017-10-11 00:14:52 +0000196 except subprocess.CalledProcessError:
Robert Iannuccia19649b2018-06-29 16:31:45 +0000197 cachepath = os.environ.get('GIT_CACHE_PATH', cls.UNSET_CACHEPATH)
Vadim Shtayura08049e22017-10-11 00:14:52 +0000198 setattr(cls, 'cachepath', cachepath)
Robert Iannuccia19649b2018-06-29 16:31:45 +0000199
200 ret = getattr(cls, 'cachepath')
201 if ret is cls.UNSET_CACHEPATH:
202 raise RuntimeError('No cache.cachepath git configuration or '
203 '$GIT_CACHE_PATH is set.')
204 return ret
szager@chromium.org848fd492014-04-09 19:06:44 +0000205
Karen Qianccd2b4d2019-05-03 22:25:59 +0000206 @staticmethod
207 def _GetMostRecentCacheDirectory(ls_out_set):
208 ready_file_pattern = re.compile(r'.*/(\d+).ready$')
209 ready_dirs = []
210
211 for name in ls_out_set:
212 m = ready_file_pattern.match(name)
213 # Given <path>/<number>.ready,
214 # we are interested in <path>/<number> directory
215 if m and (name[:-len('.ready')] + '/') in ls_out_set:
216 ready_dirs.append((int(m.group(1)), name[:-len('.ready')]))
217
218 if not ready_dirs:
219 return None
220
221 return max(ready_dirs)[1]
222
dnj4625b5a2016-11-10 18:23:26 -0800223 def Rename(self, src, dst):
224 # This is somehow racy on Windows.
225 # Catching OSError because WindowsError isn't portable and
226 # pylint complains.
227 exponential_backoff_retry(
228 lambda: os.rename(src, dst),
229 excs=(OSError,),
230 name='rename [%s] => [%s]' % (src, dst),
231 printerr=self.print)
232
Josip Sokcevic650f8532021-10-15 18:35:31 +0000233 def RunGit(self, cmd, print_stdout=True, **kwargs):
szager@chromium.org848fd492014-04-09 19:06:44 +0000234 """Run git in a subprocess."""
235 cwd = kwargs.setdefault('cwd', self.mirror_path)
Joanna Wangea99f9a2023-08-17 02:20:43 +0000236 if "--git-dir" not in cmd:
237 cmd = ['--git-dir', os.path.abspath(cwd)] + cmd
238
szager@chromium.org848fd492014-04-09 19:06:44 +0000239 kwargs.setdefault('print_stdout', False)
Josip Sokcevic650f8532021-10-15 18:35:31 +0000240 if print_stdout:
241 kwargs.setdefault('filter_fn', self.print)
szager@chromium.org848fd492014-04-09 19:06:44 +0000242 env = kwargs.get('env') or kwargs.setdefault('env', os.environ.copy())
243 env.setdefault('GIT_ASKPASS', 'true')
244 env.setdefault('SSH_ASKPASS', 'true')
245 self.print('running "git %s" in "%s"' % (' '.join(cmd), cwd))
246 gclient_utils.CheckCallAndFilter([self.git_exe] + cmd, **kwargs)
247
Joanna Wangea99f9a2023-08-17 02:20:43 +0000248 def config(self, reset_fetch_config=False):
Edward Lemur579c9862018-07-13 23:17:51 +0000249 if reset_fetch_config:
Edward Lemur2f38df62018-07-14 02:13:21 +0000250 try:
Joanna Wangea99f9a2023-08-17 02:20:43 +0000251 self.RunGit(['config', '--unset-all', 'remote.origin.fetch'])
Edward Lemur2f38df62018-07-14 02:13:21 +0000252 except subprocess.CalledProcessError as e:
253 # If exit code was 5, it means we attempted to unset a config that
254 # didn't exist. Ignore it.
255 if e.returncode != 5:
256 raise
Edward Lemur579c9862018-07-13 23:17:51 +0000257
szager@chromium.org301a7c32014-06-16 17:13:50 +0000258 # Don't run git-gc in a daemon. Bad things can happen if it gets killed.
hinokadcd84042016-06-09 14:26:17 -0700259 try:
Joanna Wangea99f9a2023-08-17 02:20:43 +0000260 self.RunGit(['config', 'gc.autodetach', '0'])
hinokadcd84042016-06-09 14:26:17 -0700261 except subprocess.CalledProcessError:
262 # Hard error, need to clobber.
263 raise ClobberNeeded()
szager@chromium.org301a7c32014-06-16 17:13:50 +0000264
265 # Don't combine pack files into one big pack file. It's really slow for
266 # repositories, and there's no way to track progress and make sure it's
267 # not stuck.
Ryan Tseng3beabd02017-03-15 13:57:58 -0700268 if self.supported_project():
Joanna Wangea99f9a2023-08-17 02:20:43 +0000269 self.RunGit(['config', 'gc.autopacklimit', '0'])
szager@chromium.org301a7c32014-06-16 17:13:50 +0000270
271 # Allocate more RAM for cache-ing delta chains, for better performance
272 # of "Resolving deltas".
Joanna Wangea99f9a2023-08-17 02:20:43 +0000273 self.RunGit([
274 'config', 'core.deltaBaseCacheLimit',
275 gclient_utils.DefaultDeltaBaseCacheLimit()
276 ])
szager@chromium.org301a7c32014-06-16 17:13:50 +0000277
Joanna Wangea99f9a2023-08-17 02:20:43 +0000278 self.RunGit(['config', 'remote.origin.url', self.url])
279 self.RunGit([
280 'config', '--replace-all', 'remote.origin.fetch',
281 '+refs/heads/*:refs/heads/*', r'\+refs/heads/\*:.*'
282 ])
szager@chromium.org66c8b852015-09-22 23:19:07 +0000283 for spec, value_regex in self.fetch_specs:
szager@chromium.org965c44f2014-08-19 21:19:19 +0000284 self.RunGit(
Joanna Wangea99f9a2023-08-17 02:20:43 +0000285 ['config', '--replace-all', 'remote.origin.fetch', spec, value_regex])
szager@chromium.org848fd492014-04-09 19:06:44 +0000286
287 def bootstrap_repo(self, directory):
Andrii Shyshkalov4f56f232017-11-23 02:19:25 -0800288 """Bootstrap the repo from Google Storage if possible.
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000289
290 More apt-ly named bootstrap_repo_from_cloud_if_possible_else_do_nothing().
291 """
Ryan Tseng3beabd02017-03-15 13:57:58 -0700292 if not self.bootstrap_bucket:
293 return False
szager@chromium.org848fd492014-04-09 19:06:44 +0000294
hinoka@chromium.org199bc5f2014-12-17 02:17:14 +0000295 gsutil = Gsutil(self.gsutil_exe, boto_path=None)
Yuwei Huanga1fbdff2019-02-01 21:51:15 +0000296
Karen Qian0cbd5a52019-04-29 20:14:50 +0000297 # Get the most recent version of the directory.
298 # This is determined from the most recent version of a .ready file.
299 # The .ready file is only uploaded when an entire directory has been
300 # uploaded to GS.
301 _, ls_out, ls_err = gsutil.check_call('ls', self._gs_path)
Karen Qianccd2b4d2019-05-03 22:25:59 +0000302 ls_out_set = set(ls_out.strip().splitlines())
303 latest_dir = self._GetMostRecentCacheDirectory(ls_out_set)
Yuwei Huanga1fbdff2019-02-01 21:51:15 +0000304
Karen Qianccd2b4d2019-05-03 22:25:59 +0000305 if not latest_dir:
Andrii Shyshkalov4f56f232017-11-23 02:19:25 -0800306 self.print('No bootstrap file for %s found in %s, stderr:\n %s' %
307 (self.mirror_path, self.bootstrap_bucket,
Karen Qian0cbd5a52019-04-29 20:14:50 +0000308 ' '.join((ls_err or '').splitlines(True))))
szager@chromium.org848fd492014-04-09 19:06:44 +0000309 return False
szager@chromium.org848fd492014-04-09 19:06:44 +0000310
szager@chromium.org848fd492014-04-09 19:06:44 +0000311 try:
Karen Qian0cbd5a52019-04-29 20:14:50 +0000312 # create new temporary directory locally
szager@chromium.org1cbf1042014-06-17 18:26:24 +0000313 tempdir = tempfile.mkdtemp(prefix='_cache_tmp', dir=self.GetCachePath())
Josip Sokcevica40c1e12021-08-18 20:38:32 +0000314 self.RunGit(['init', '--bare'], cwd=tempdir)
Karen Qian0cbd5a52019-04-29 20:14:50 +0000315 self.print('Downloading files in %s/* into %s.' %
316 (latest_dir, tempdir))
Andrii Shyshkalov4f56f232017-11-23 02:19:25 -0800317 with self.print_duration_of('download'):
Josip Sokcevic604f1602021-10-15 15:45:10 +0000318 with GSUTIL_CP_SEMAPHORE:
319 code = gsutil.call('-m', 'cp', '-r', latest_dir + "/*",
320 tempdir)
szager@chromium.org848fd492014-04-09 19:06:44 +0000321 if code:
szager@chromium.org848fd492014-04-09 19:06:44 +0000322 return False
Josip Sokcevica40c1e12021-08-18 20:38:32 +0000323 # Set HEAD to main.
324 self.RunGit(['symbolic-ref', 'HEAD', 'refs/heads/main'], cwd=tempdir)
Josip Sokcevic67e12282020-12-16 17:12:45 +0000325 # A quick validation that all references are valid.
Josip Sokcevic650f8532021-10-15 18:35:31 +0000326 self.RunGit(['for-each-ref'], print_stdout=False, cwd=tempdir)
Karen Qian0cbd5a52019-04-29 20:14:50 +0000327 except Exception as e:
328 self.print('Encountered error: %s' % str(e), file=sys.stderr)
329 gclient_utils.rmtree(tempdir)
szager@chromium.org848fd492014-04-09 19:06:44 +0000330 return False
Karen Qian0cbd5a52019-04-29 20:14:50 +0000331 # delete the old directory
332 if os.path.exists(directory):
333 gclient_utils.rmtree(directory)
334 self.Rename(tempdir, directory)
szager@chromium.org848fd492014-04-09 19:06:44 +0000335 return True
336
Andrii Shyshkalov46a672b2017-11-24 18:04:43 -0800337 def contains_revision(self, revision):
338 if not self.exists():
339 return False
340
341 if sys.platform.startswith('win'):
342 # Windows .bat scripts use ^ as escape sequence, which means we have to
343 # escape it with itself for every .bat invocation.
344 needle = '%s^^^^{commit}' % revision
345 else:
346 needle = '%s^{commit}' % revision
347 try:
348 # cat-file exits with 0 on success, that is git object of given hash was
349 # found.
350 self.RunGit(['cat-file', '-e', needle])
351 return True
352 except subprocess.CalledProcessError:
Josip Sokcevic35061442022-01-12 00:32:54 +0000353 self.print('Commit with hash "%s" not found' % revision, file=sys.stderr)
Andrii Shyshkalov46a672b2017-11-24 18:04:43 -0800354 return False
355
szager@chromium.org848fd492014-04-09 19:06:44 +0000356 def exists(self):
357 return os.path.isfile(os.path.join(self.mirror_path, 'config'))
358
Ryan Tseng3beabd02017-03-15 13:57:58 -0700359 def supported_project(self):
360 """Returns true if this repo is known to have a bootstrap zip file."""
Gavin Mak42674f52023-08-24 20:39:59 +0000361 u = urlparse.urlparse(self.url)
Ryan Tseng3beabd02017-03-15 13:57:58 -0700362 return u.netloc in [
363 'chromium.googlesource.com',
364 'chrome-internal.googlesource.com']
365
szager@chromium.org66c8b852015-09-22 23:19:07 +0000366 def _preserve_fetchspec(self):
367 """Read and preserve remote.origin.fetch from an existing mirror.
368
369 This modifies self.fetch_specs.
370 """
371 if not self.exists():
372 return
373 try:
374 config_fetchspecs = subprocess.check_output(
375 [self.git_exe, 'config', '--get-all', 'remote.origin.fetch'],
Edward Lesmes4c3eb702020-03-25 21:09:30 +0000376 cwd=self.mirror_path).decode('utf-8', 'ignore')
szager@chromium.org66c8b852015-09-22 23:19:07 +0000377 for fetchspec in config_fetchspecs.splitlines():
378 self.fetch_specs.add(self.parse_fetch_spec(fetchspec))
379 except subprocess.CalledProcessError:
Gavin Make6a62332020-12-04 21:57:10 +0000380 logging.warning(
381 'Tried and failed to preserve remote.origin.fetch from the '
382 'existing cache directory. You may need to manually edit '
383 '%s and "git cache fetch" again.' %
384 os.path.join(self.mirror_path, 'config'))
szager@chromium.org66c8b852015-09-22 23:19:07 +0000385
Edward Lesmes34f71ab2020-03-25 21:24:00 +0000386 def _ensure_bootstrapped(
387 self, depth, bootstrap, reset_fetch_config, force=False):
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000388 pack_dir = os.path.join(self.mirror_path, 'objects', 'pack')
389 pack_files = []
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000390 if os.path.isdir(pack_dir):
391 pack_files = [f for f in os.listdir(pack_dir) if f.endswith('.pack')]
Edward Lesmes34f71ab2020-03-25 21:24:00 +0000392 self.print('%s has %d .pack files, re-bootstrapping if >%d or ==0' %
Karen Qian0cbd5a52019-04-29 20:14:50 +0000393 (self.mirror_path, len(pack_files), GC_AUTOPACKLIMIT))
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000394
Aravind Vasudevan6eccb0e2023-03-06 17:28:15 +0000395 # master->main branch migration left the cache in some builders to have its
396 # HEAD still pointing to refs/heads/master. This causes bot_update to fail.
397 # If in this state, delete the cache and force bootstrap.
398 try:
399 with open(os.path.join(self.mirror_path, 'HEAD')) as f:
400 head_ref = f.read()
401 except FileNotFoundError:
402 head_ref = ''
403
404 # Check only when HEAD points to master.
405 if 'master' in head_ref:
406 # Some repos could still have master so verify if the ref exists first.
407 show_ref_master_cmd = subprocess.run(
408 [Mirror.git_exe, 'show-ref', '--verify', 'refs/heads/master'],
409 cwd=self.mirror_path)
410
411 if show_ref_master_cmd.returncode != 0:
412 # Remove mirror
413 gclient_utils.rmtree(self.mirror_path)
414
415 # force bootstrap
416 force = True
417
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000418 should_bootstrap = (force or
szager@chromium.org66c8b852015-09-22 23:19:07 +0000419 not self.exists() or
Edward Lesmes34f71ab2020-03-25 21:24:00 +0000420 len(pack_files) > GC_AUTOPACKLIMIT or
421 len(pack_files) == 0)
Karen Qian0cbd5a52019-04-29 20:14:50 +0000422
423 if not should_bootstrap:
424 if depth and os.path.exists(os.path.join(self.mirror_path, 'shallow')):
Gavin Make6a62332020-12-04 21:57:10 +0000425 logging.warning(
Karen Qian0cbd5a52019-04-29 20:14:50 +0000426 'Shallow fetch requested, but repo cache already exists.')
427 return
428
Edward Lesmes34f71ab2020-03-25 21:24:00 +0000429 if not self.exists():
John Budorick47ec0692019-05-01 15:04:28 +0000430 if os.path.exists(self.mirror_path):
431 # If the mirror path exists but self.exists() returns false, we're
432 # in an unexpected state. Nuke the previous mirror directory and
433 # start fresh.
434 gclient_utils.rmtree(self.mirror_path)
Karen Qian0cbd5a52019-04-29 20:14:50 +0000435 os.mkdir(self.mirror_path)
Edward Lesmes34f71ab2020-03-25 21:24:00 +0000436 elif not reset_fetch_config:
437 # Re-bootstrapping an existing mirror; preserve existing fetch spec.
438 self._preserve_fetchspec()
Karen Qian0cbd5a52019-04-29 20:14:50 +0000439
440 bootstrapped = (not depth and bootstrap and
441 self.bootstrap_repo(self.mirror_path))
442
443 if not bootstrapped:
444 if not self.exists() or not self.supported_project():
445 # Bootstrap failed due to:
446 # 1. No previous cache.
447 # 2. Project doesn't have a bootstrap folder.
Ryan Tseng3beabd02017-03-15 13:57:58 -0700448 # Start with a bare git dir.
Joanna Wangea99f9a2023-08-17 02:20:43 +0000449 self.RunGit(['init', '--bare'])
Josip Sokcevica4b36022022-06-09 19:59:33 +0000450 # Set appropriate symbolic-ref
Joanna Wangea99f9a2023-08-17 02:20:43 +0000451 remote_info = exponential_backoff_retry(lambda: subprocess.check_output(
452 [
453 self.git_exe, '--git-dir',
454 os.path.abspath(self.mirror_path), 'remote', 'show', self.url
455 ],
456 cwd=self.mirror_path).decode('utf-8', 'ignore').strip())
Josip Sokcevica4b36022022-06-09 19:59:33 +0000457 default_branch_regexp = re.compile(r'HEAD branch: (.*)$')
458 m = default_branch_regexp.search(remote_info, re.MULTILINE)
459 if m:
Joanna Wangea99f9a2023-08-17 02:20:43 +0000460 self.RunGit(['symbolic-ref', 'HEAD', 'refs/heads/' + m.groups()[0]])
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000461 else:
462 # Bootstrap failed, previous cache exists; warn and continue.
Gavin Make6a62332020-12-04 21:57:10 +0000463 logging.warning(
Andrii Shyshkalov4f56f232017-11-23 02:19:25 -0800464 'Git cache has a lot of pack files (%d). Tried to re-bootstrap '
Gavin Make6a62332020-12-04 21:57:10 +0000465 'but failed. Continuing with non-optimized repository.' %
466 len(pack_files))
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000467
Josip Sokcevic6afaa6c2020-05-08 18:20:17 +0000468 def _fetch(self,
Josip Sokcevic6afaa6c2020-05-08 18:20:17 +0000469 verbose,
470 depth,
471 no_fetch_tags,
472 reset_fetch_config,
473 prune=True):
Joanna Wangea99f9a2023-08-17 02:20:43 +0000474 self.config(reset_fetch_config)
Josip Sokcevic6afaa6c2020-05-08 18:20:17 +0000475
476 fetch_cmd = ['fetch']
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000477 if verbose:
Josip Sokcevic6afaa6c2020-05-08 18:20:17 +0000478 fetch_cmd.extend(['-v', '--progress'])
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000479 if depth:
Josip Sokcevic6afaa6c2020-05-08 18:20:17 +0000480 fetch_cmd.extend(['--depth', str(depth)])
danakjc41f72c2019-11-05 17:12:01 +0000481 if no_fetch_tags:
Josip Sokcevic6afaa6c2020-05-08 18:20:17 +0000482 fetch_cmd.append('--no-tags')
483 if prune:
484 fetch_cmd.append('--prune')
485 fetch_cmd.append('origin')
486
Joanna Wangea99f9a2023-08-17 02:20:43 +0000487 fetch_specs = subprocess.check_output([
488 self.git_exe, '--git-dir',
489 os.path.abspath(self.mirror_path), 'config', '--get-all',
490 'remote.origin.fetch'
491 ],
492 cwd=self.mirror_path).decode(
493 'utf-8',
494 'ignore').strip().splitlines()
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000495 for spec in fetch_specs:
496 try:
497 self.print('Fetching %s' % spec)
Andrii Shyshkalov4f56f232017-11-23 02:19:25 -0800498 with self.print_duration_of('fetch %s' % spec):
Joanna Wangea99f9a2023-08-17 02:20:43 +0000499 self.RunGit(fetch_cmd + [spec], retry=True)
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000500 except subprocess.CalledProcessError:
501 if spec == '+refs/heads/*:refs/heads/*':
hinokadcd84042016-06-09 14:26:17 -0700502 raise ClobberNeeded() # Corrupted cache.
Gavin Make6a62332020-12-04 21:57:10 +0000503 logging.warning('Fetch of %s failed' % spec)
Edward Lesmes07a68342021-04-20 23:39:30 +0000504 for commit in self.fetch_commits:
505 self.print('Fetching %s' % commit)
506 try:
507 with self.print_duration_of('fetch %s' % commit):
Joanna Wangea99f9a2023-08-17 02:20:43 +0000508 self.RunGit(['fetch', 'origin', commit], retry=True)
Edward Lesmes07a68342021-04-20 23:39:30 +0000509 except subprocess.CalledProcessError:
510 logging.warning('Fetch of %s failed' % commit)
hinoka@chromium.orgaa1e1a42014-06-26 21:58:51 +0000511
danakjc41f72c2019-11-05 17:12:01 +0000512 def populate(self,
513 depth=None,
514 no_fetch_tags=False,
515 shallow=False,
516 bootstrap=False,
517 verbose=False,
danakjc41f72c2019-11-05 17:12:01 +0000518 lock_timeout=0,
Edward Lemur579c9862018-07-13 23:17:51 +0000519 reset_fetch_config=False):
szager@chromium.orgb0a13a22014-06-18 00:52:25 +0000520 assert self.GetCachePath()
szager@chromium.org848fd492014-04-09 19:06:44 +0000521 if shallow and not depth:
522 depth = 10000
523 gclient_utils.safe_makedirs(self.GetCachePath())
524
Josip Sokcevic14a83ae2020-05-21 01:36:34 +0000525 with lockfile.lock(self.mirror_path, lock_timeout):
526 try:
527 self._ensure_bootstrapped(depth, bootstrap, reset_fetch_config)
Joanna Wangea99f9a2023-08-17 02:20:43 +0000528 self._fetch(verbose, depth, no_fetch_tags, reset_fetch_config)
Josip Sokcevic14a83ae2020-05-21 01:36:34 +0000529 except ClobberNeeded:
530 # This is a major failure, we need to clean and force a bootstrap.
531 gclient_utils.rmtree(self.mirror_path)
532 self.print(GIT_CACHE_CORRUPT_MESSAGE)
533 self._ensure_bootstrapped(depth,
534 bootstrap,
535 reset_fetch_config,
536 force=True)
Joanna Wangea99f9a2023-08-17 02:20:43 +0000537 self._fetch(verbose, depth, no_fetch_tags, reset_fetch_config)
szager@chromium.org848fd492014-04-09 19:06:44 +0000538
Joanna Wang5175d182022-12-07 17:27:57 +0000539 def update_bootstrap(self, prune=False, gc_aggressive=False):
Joanna Wang38d16732022-10-10 17:12:47 +0000540 # NOTE: There have been cases where repos were being recursively uploaded
541 # to google storage.
542 # E.g. `<host_url>-<repo>/<gen_number>/<host_url>-<repo>/` in GS and
543 # <host_url>-<repo>/<host_url>-<repo>/ on the bot.
544 # Check for recursed files on the bot here and remove them if found
545 # before we upload to GS.
546 # See crbug.com/1370443; keep this check until root cause is found.
547 recursed_dir = os.path.join(self.mirror_path,
Joanna Wang17cf81d2022-10-12 03:41:24 +0000548 self.mirror_path.split(os.path.sep)[-1])
Joanna Wang38d16732022-10-10 17:12:47 +0000549 if os.path.exists(recursed_dir):
550 self.print('Deleting unexpected directory: %s' % recursed_dir)
Josip Sokcevicd540d8b2022-10-12 18:43:49 +0000551 gclient_utils.rmtree(recursed_dir)
Joanna Wang38d16732022-10-10 17:12:47 +0000552
Karen Qiandcad7492019-04-26 03:11:16 +0000553 # The folder is <git number>
Joanna Wang5175d182022-12-07 17:27:57 +0000554 gen_number = subprocess.check_output([self.git_exe, 'number'],
555 cwd=self.mirror_path).decode(
556 'utf-8', 'ignore').strip()
Karen Qiandcad7492019-04-26 03:11:16 +0000557 gsutil = Gsutil(path=self.gsutil_exe, boto_path=None)
558
Karen Qianccd2b4d2019-05-03 22:25:59 +0000559 dest_prefix = '%s/%s' % (self._gs_path, gen_number)
Karen Qiandcad7492019-04-26 03:11:16 +0000560
Karen Qianccd2b4d2019-05-03 22:25:59 +0000561 # ls_out lists contents in the format: gs://blah/blah/123...
Joanna Wang5b5ee2d2022-10-12 17:18:22 +0000562 self.print('running "gsutil ls %s":' % self._gs_path)
Joanna Wang38d16732022-10-10 17:12:47 +0000563 ls_code, ls_out, ls_error = gsutil.check_call_with_retries(
564 'ls', self._gs_path)
565 if ls_code != 0:
566 self.print(ls_error)
567 else:
568 self.print(ls_out)
Karen Qiandcad7492019-04-26 03:11:16 +0000569
Karen Qianccd2b4d2019-05-03 22:25:59 +0000570 # Check to see if folder already exists in gs
571 ls_out_set = set(ls_out.strip().splitlines())
572 if (dest_prefix + '/' in ls_out_set and
573 dest_prefix + '.ready' in ls_out_set):
574 print('Cache %s already exists.' % dest_prefix)
Karen Qiandcad7492019-04-26 03:11:16 +0000575 return
576
Andrii Shyshkalov46b91c02020-10-27 17:25:47 +0000577 # Reduce the number of individual files to download & write on disk.
578 self.RunGit(['pack-refs', '--all'])
579
Andrii Shyshkalov199182f2019-04-26 16:01:20 +0000580 # Run Garbage Collect to compress packfile.
Andrii Shyshkalovdcfe55f2019-09-21 03:35:39 +0000581 gc_args = ['gc', '--prune=all']
582 if gc_aggressive:
Michael Moss77480942020-06-22 18:32:37 +0000583 # The default "gc --aggressive" is often too aggressive for some machines,
584 # since it attempts to create as many threads as there are CPU cores,
585 # while not limiting per-thread memory usage, which puts too much pressure
586 # on RAM on high-core machines, causing them to thrash. Using lower-level
587 # commands gives more control over those settings.
588
589 # This might not be strictly necessary, but it's fast and is normally run
590 # by 'gc --aggressive', so it shouldn't hurt.
591 self.RunGit(['reflog', 'expire', '--all'])
592
593 # These are the default repack settings for 'gc --aggressive'.
594 gc_args = ['repack', '-d', '-l', '-f', '--depth=50', '--window=250', '-A',
595 '--unpack-unreachable=all']
596 # A 1G memory limit seems to provide comparable pack results as the
597 # default, even for our largest repos, while preventing runaway memory (at
598 # least on current Chromium builders which have about 4G RAM per core).
599 gc_args.append('--window-memory=1g')
600 # NOTE: It might also be possible to avoid thrashing with a larger window
601 # (e.g. "--window-memory=2g") by limiting the number of threads created
602 # (e.g. "--threads=[cores/2]"). Some limited testing didn't show much
603 # difference in outcomes on our current repos, but it might be worth
604 # trying if the repos grow much larger and the packs don't seem to be
605 # getting compressed enough.
Andrii Shyshkalovdcfe55f2019-09-21 03:35:39 +0000606 self.RunGit(gc_args)
Andrii Shyshkalov199182f2019-04-26 16:01:20 +0000607
Joanna Wang38d16732022-10-10 17:12:47 +0000608 self.print('running "gsutil -m rsync -r -d %s %s"' %
Joanna Wang2c54a192022-10-05 01:01:40 +0000609 (self.mirror_path, dest_prefix))
Joanna Wang38d16732022-10-10 17:12:47 +0000610 gsutil.call('-m', 'rsync', '-r', '-d', self.mirror_path, dest_prefix)
Karen Qiandcad7492019-04-26 03:11:16 +0000611
Karen Qianccd2b4d2019-05-03 22:25:59 +0000612 # Create .ready file and upload
Karen Qiandcad7492019-04-26 03:11:16 +0000613 _, ready_file_name = tempfile.mkstemp(suffix='.ready')
614 try:
Joanna Wang2c54a192022-10-05 01:01:40 +0000615 self.print('running "gsutil cp %s %s.ready"' %
616 (ready_file_name, dest_prefix))
Karen Qianccd2b4d2019-05-03 22:25:59 +0000617 gsutil.call('cp', ready_file_name, '%s.ready' % (dest_prefix))
Karen Qiandcad7492019-04-26 03:11:16 +0000618 finally:
619 os.remove(ready_file_name)
hinoka@chromium.orgc8444f32014-06-18 23:18:17 +0000620
Karen Qianccd2b4d2019-05-03 22:25:59 +0000621 # remove all other directory/.ready files in the same gs_path
622 # except for the directory/.ready file previously created
623 # which can be used for bootstrapping while the current one is
624 # being uploaded
625 if not prune:
626 return
627 prev_dest_prefix = self._GetMostRecentCacheDirectory(ls_out_set)
628 if not prev_dest_prefix:
629 return
630 for path in ls_out_set:
Aravind Vasudevanc5f0cbb2022-01-24 23:56:57 +0000631 if path in (prev_dest_prefix + '/', prev_dest_prefix + '.ready'):
Karen Qianccd2b4d2019-05-03 22:25:59 +0000632 continue
633 if path.endswith('.ready'):
634 gsutil.call('rm', path)
635 continue
636 gsutil.call('-m', 'rm', '-r', path)
637
638
szager@chromium.orgcdfcd7c2014-06-10 23:40:46 +0000639 @staticmethod
640 def DeleteTmpPackFiles(path):
641 pack_dir = os.path.join(path, 'objects', 'pack')
szager@chromium.org33418492014-06-18 19:03:39 +0000642 if not os.path.isdir(pack_dir):
643 return
szager@chromium.orgcdfcd7c2014-06-10 23:40:46 +0000644 pack_files = [f for f in os.listdir(pack_dir) if
645 f.startswith('.tmp-') or f.startswith('tmp_pack_')]
646 for f in pack_files:
647 f = os.path.join(pack_dir, f)
648 try:
649 os.remove(f)
Gavin Make6a62332020-12-04 21:57:10 +0000650 logging.warning('Deleted stale temporary pack file %s' % f)
szager@chromium.orgcdfcd7c2014-06-10 23:40:46 +0000651 except OSError:
Gavin Make6a62332020-12-04 21:57:10 +0000652 logging.warning('Unable to delete temporary pack file %s' % f)
szager@chromium.org174766f2014-05-13 21:27:46 +0000653
szager@chromium.org848fd492014-04-09 19:06:44 +0000654
agable@chromium.org5a306a22014-02-24 22:13:59 +0000655@subcommand.usage('[url of repo to check for caching]')
Edward Lesmescb047442021-05-06 20:18:49 +0000656@metrics.collector.collect_metrics('git cache exists')
agable@chromium.org5a306a22014-02-24 22:13:59 +0000657def CMDexists(parser, args):
658 """Check to see if there already is a cache of the given repo."""
szager@chromium.org848fd492014-04-09 19:06:44 +0000659 _, args = parser.parse_args(args)
agable@chromium.org5a306a22014-02-24 22:13:59 +0000660 if not len(args) == 1:
661 parser.error('git cache exists only takes exactly one repo url.')
662 url = args[0]
szager@chromium.org848fd492014-04-09 19:06:44 +0000663 mirror = Mirror(url)
664 if mirror.exists():
665 print(mirror.mirror_path)
agable@chromium.org5a306a22014-02-24 22:13:59 +0000666 return 0
667 return 1
668
669
hinoka@google.com563559c2014-04-02 00:36:24 +0000670@subcommand.usage('[url of repo to create a bootstrap zip file]')
Edward Lesmescb047442021-05-06 20:18:49 +0000671@metrics.collector.collect_metrics('git cache update-bootstrap')
hinoka@google.com563559c2014-04-02 00:36:24 +0000672def CMDupdate_bootstrap(parser, args):
673 """Create and uploads a bootstrap tarball."""
674 # Lets just assert we can't do this on Windows.
675 if sys.platform.startswith('win'):
szager@chromium.org848fd492014-04-09 19:06:44 +0000676 print('Sorry, update bootstrap will not work on Windows.', file=sys.stderr)
hinoka@google.com563559c2014-04-02 00:36:24 +0000677 return 1
678
Robert Iannucci0081c0f2019-09-29 08:30:54 +0000679 parser.add_option('--skip-populate', action='store_true',
680 help='Skips "populate" step if mirror already exists.')
Andrii Shyshkalovdcfe55f2019-09-21 03:35:39 +0000681 parser.add_option('--gc-aggressive', action='store_true',
682 help='Run aggressive repacking of the repo.')
hinoka@chromium.orgc8444f32014-06-18 23:18:17 +0000683 parser.add_option('--prune', action='store_true',
Andrii Shyshkalov7a2205c2019-04-26 05:14:36 +0000684 help='Prune all other cached bundles of the same repo.')
hinoka@chromium.orgc8444f32014-06-18 23:18:17 +0000685
hinoka@google.com563559c2014-04-02 00:36:24 +0000686 populate_args = args[:]
Robert Iannucci0081c0f2019-09-29 08:30:54 +0000687 options, args = parser.parse_args(args)
688 url = args[0]
689 mirror = Mirror(url)
690 if not options.skip_populate or not mirror.exists():
691 CMDpopulate(parser, populate_args)
692 else:
693 print('Skipped populate step.')
hinoka@google.com563559c2014-04-02 00:36:24 +0000694
695 # Get the repo directory.
Andrii Shyshkalovc50b0962019-11-21 23:03:18 +0000696 _, args2 = parser.parse_args(args)
697 url = args2[0]
szager@chromium.org848fd492014-04-09 19:06:44 +0000698 mirror = Mirror(url)
Joanna Wang5175d182022-12-07 17:27:57 +0000699 mirror.update_bootstrap(options.prune, options.gc_aggressive)
szager@chromium.org848fd492014-04-09 19:06:44 +0000700 return 0
hinoka@google.com563559c2014-04-02 00:36:24 +0000701
702
agable@chromium.org5a306a22014-02-24 22:13:59 +0000703@subcommand.usage('[url of repo to add to or update in cache]')
Edward Lesmescb047442021-05-06 20:18:49 +0000704@metrics.collector.collect_metrics('git cache populate')
agable@chromium.org5a306a22014-02-24 22:13:59 +0000705def CMDpopulate(parser, args):
706 """Ensure that the cache has all up-to-date objects for the given repo."""
707 parser.add_option('--depth', type='int',
708 help='Only cache DEPTH commits of history')
danakjc41f72c2019-11-05 17:12:01 +0000709 parser.add_option(
710 '--no-fetch-tags',
711 action='store_true',
712 help=('Don\'t fetch tags from the server. This can speed up '
713 'fetch considerably when there are many tags.'))
agable@chromium.org5a306a22014-02-24 22:13:59 +0000714 parser.add_option('--shallow', '-s', action='store_true',
715 help='Only cache 10000 commits of history')
716 parser.add_option('--ref', action='append',
717 help='Specify additional refs to be fetched')
Edward Lesmes07a68342021-04-20 23:39:30 +0000718 parser.add_option('--commit', action='append',
719 help='Specify additional commits to be fetched')
pgervais@chromium.orgb9f27512014-08-08 15:52:33 +0000720 parser.add_option('--no_bootstrap', '--no-bootstrap',
721 action='store_true',
hinoka@google.com563559c2014-04-02 00:36:24 +0000722 help='Don\'t bootstrap from Google Storage')
Josip Sokcevic14a83ae2020-05-21 01:36:34 +0000723 parser.add_option('--ignore_locks',
724 '--ignore-locks',
Vadim Shtayura08049e22017-10-11 00:14:52 +0000725 action='store_true',
Josip Sokcevic14a83ae2020-05-21 01:36:34 +0000726 help='NOOP. This flag will be removed in the future.')
Robert Iannucci09315982019-10-05 08:12:03 +0000727 parser.add_option('--break-locks',
728 action='store_true',
729 help='Break any existing lock instead of just ignoring it')
Edward Lemur579c9862018-07-13 23:17:51 +0000730 parser.add_option('--reset-fetch-config', action='store_true', default=False,
731 help='Reset the fetch config before populating the cache.')
hinoka@google.com563559c2014-04-02 00:36:24 +0000732
agable@chromium.org5a306a22014-02-24 22:13:59 +0000733 options, args = parser.parse_args(args)
agable@chromium.org5a306a22014-02-24 22:13:59 +0000734 if not len(args) == 1:
735 parser.error('git cache populate only takes exactly one repo url.')
Josip Sokcevic14a83ae2020-05-21 01:36:34 +0000736 if options.ignore_locks:
737 print('ignore_locks is no longer used. Please remove its usage.')
738 if options.break_locks:
739 print('break_locks is no longer used. Please remove its usage.')
agable@chromium.org5a306a22014-02-24 22:13:59 +0000740 url = args[0]
741
Edward Lesmes07a68342021-04-20 23:39:30 +0000742 mirror = Mirror(url, refs=options.ref, commits=options.commit)
szager@chromium.org848fd492014-04-09 19:06:44 +0000743 kwargs = {
danakjc41f72c2019-11-05 17:12:01 +0000744 'no_fetch_tags': options.no_fetch_tags,
szager@chromium.org848fd492014-04-09 19:06:44 +0000745 'verbose': options.verbose,
746 'shallow': options.shallow,
747 'bootstrap': not options.no_bootstrap,
Vadim Shtayura08049e22017-10-11 00:14:52 +0000748 'lock_timeout': options.timeout,
Edward Lemur579c9862018-07-13 23:17:51 +0000749 'reset_fetch_config': options.reset_fetch_config,
szager@chromium.org848fd492014-04-09 19:06:44 +0000750 }
agable@chromium.org5a306a22014-02-24 22:13:59 +0000751 if options.depth:
szager@chromium.org848fd492014-04-09 19:06:44 +0000752 kwargs['depth'] = options.depth
753 mirror.populate(**kwargs)
agable@chromium.org5a306a22014-02-24 22:13:59 +0000754
755
szager@chromium.orgf3145112014-08-07 21:02:36 +0000756@subcommand.usage('Fetch new commits into cache and current checkout')
Edward Lesmescb047442021-05-06 20:18:49 +0000757@metrics.collector.collect_metrics('git cache fetch')
szager@chromium.orgf3145112014-08-07 21:02:36 +0000758def CMDfetch(parser, args):
759 """Update mirror, and fetch in cwd."""
760 parser.add_option('--all', action='store_true', help='Fetch all remotes')
szager@chromium.org66c8b852015-09-22 23:19:07 +0000761 parser.add_option('--no_bootstrap', '--no-bootstrap',
762 action='store_true',
763 help='Don\'t (re)bootstrap from Google Storage')
danakjc41f72c2019-11-05 17:12:01 +0000764 parser.add_option(
765 '--no-fetch-tags',
766 action='store_true',
767 help=('Don\'t fetch tags from the server. This can speed up '
768 'fetch considerably when there are many tags.'))
szager@chromium.orgf3145112014-08-07 21:02:36 +0000769 options, args = parser.parse_args(args)
770
771 # Figure out which remotes to fetch. This mimics the behavior of regular
772 # 'git fetch'. Note that in the case of "stacked" or "pipelined" branches,
773 # this will NOT try to traverse up the branching structure to find the
774 # ultimate remote to update.
775 remotes = []
776 if options.all:
777 assert not args, 'fatal: fetch --all does not take a repository argument'
Edward Lesmes4c3eb702020-03-25 21:09:30 +0000778 remotes = subprocess.check_output([Mirror.git_exe, 'remote'])
779 remotes = remotes.decode('utf-8', 'ignore').splitlines()
szager@chromium.orgf3145112014-08-07 21:02:36 +0000780 elif args:
781 remotes = args
782 else:
783 current_branch = subprocess.check_output(
Edward Lesmes4c3eb702020-03-25 21:09:30 +0000784 [Mirror.git_exe, 'rev-parse', '--abbrev-ref', 'HEAD'])
785 current_branch = current_branch.decode('utf-8', 'ignore').strip()
szager@chromium.orgf3145112014-08-07 21:02:36 +0000786 if current_branch != 'HEAD':
787 upstream = subprocess.check_output(
Edward Lesmes4c3eb702020-03-25 21:09:30 +0000788 [Mirror.git_exe, 'config', 'branch.%s.remote' % current_branch])
789 upstream = upstream.decode('utf-8', 'ignore').strip()
szager@chromium.orgf3145112014-08-07 21:02:36 +0000790 if upstream and upstream != '.':
791 remotes = [upstream]
792 if not remotes:
793 remotes = ['origin']
794
795 cachepath = Mirror.GetCachePath()
796 git_dir = os.path.abspath(subprocess.check_output(
Edward Lesmes4c3eb702020-03-25 21:09:30 +0000797 [Mirror.git_exe, 'rev-parse', '--git-dir']).decode('utf-8', 'ignore'))
szager@chromium.orgf3145112014-08-07 21:02:36 +0000798 git_dir = os.path.abspath(git_dir)
799 if git_dir.startswith(cachepath):
800 mirror = Mirror.FromPath(git_dir)
szager@chromium.orgdbb6f822016-02-02 22:59:30 +0000801 mirror.populate(
danakjc41f72c2019-11-05 17:12:01 +0000802 bootstrap=not options.no_bootstrap,
803 no_fetch_tags=options.no_fetch_tags,
804 lock_timeout=options.timeout)
szager@chromium.orgf3145112014-08-07 21:02:36 +0000805 return 0
806 for remote in remotes:
807 remote_url = subprocess.check_output(
Edward Lesmes4c3eb702020-03-25 21:09:30 +0000808 [Mirror.git_exe, 'config', 'remote.%s.url' % remote])
809 remote_url = remote_url.decode('utf-8', 'ignore').strip()
szager@chromium.orgf3145112014-08-07 21:02:36 +0000810 if remote_url.startswith(cachepath):
811 mirror = Mirror.FromPath(remote_url)
812 mirror.print = lambda *args: None
813 print('Updating git cache...')
szager@chromium.orgdbb6f822016-02-02 22:59:30 +0000814 mirror.populate(
danakjc41f72c2019-11-05 17:12:01 +0000815 bootstrap=not options.no_bootstrap,
816 no_fetch_tags=options.no_fetch_tags,
817 lock_timeout=options.timeout)
szager@chromium.orgf3145112014-08-07 21:02:36 +0000818 subprocess.check_call([Mirror.git_exe, 'fetch', remote])
819 return 0
820
821
Josip Sokcevic14a83ae2020-05-21 01:36:34 +0000822@subcommand.usage('do not use - it is a noop.')
Edward Lesmescb047442021-05-06 20:18:49 +0000823@metrics.collector.collect_metrics('git cache unlock')
Vadim Shtayura08049e22017-10-11 00:14:52 +0000824def CMDunlock(parser, args):
Josip Sokcevic14a83ae2020-05-21 01:36:34 +0000825 """This command does nothing."""
826 print('This command does nothing and will be removed in the future.')
Vadim Shtayura08049e22017-10-11 00:14:52 +0000827
828
agable@chromium.org5a306a22014-02-24 22:13:59 +0000829class OptionParser(optparse.OptionParser):
830 """Wrapper class for OptionParser to handle global options."""
831
832 def __init__(self, *args, **kwargs):
833 optparse.OptionParser.__init__(self, *args, prog='git cache', **kwargs)
834 self.add_option('-c', '--cache-dir',
Robert Iannuccia19649b2018-06-29 16:31:45 +0000835 help=(
836 'Path to the directory containing the caches. Normally '
837 'deduced from git config cache.cachepath or '
838 '$GIT_CACHE_PATH.'))
szager@chromium.org2c391af2014-05-23 09:07:15 +0000839 self.add_option('-v', '--verbose', action='count', default=1,
agable@chromium.org5a306a22014-02-24 22:13:59 +0000840 help='Increase verbosity (can be passed multiple times)')
szager@chromium.org2c391af2014-05-23 09:07:15 +0000841 self.add_option('-q', '--quiet', action='store_true',
842 help='Suppress all extraneous output')
Vadim Shtayura08049e22017-10-11 00:14:52 +0000843 self.add_option('--timeout', type='int', default=0,
844 help='Timeout for acquiring cache lock, in seconds')
agable@chromium.org5a306a22014-02-24 22:13:59 +0000845
846 def parse_args(self, args=None, values=None):
Edward Lesmescb047442021-05-06 20:18:49 +0000847 # Create an optparse.Values object that will store only the actual passed
848 # options, without the defaults.
849 actual_options = optparse.Values()
850 _, args = optparse.OptionParser.parse_args(self, args, actual_options)
851 # Create an optparse.Values object with the default options.
852 options = optparse.Values(self.get_default_values().__dict__)
853 # Update it with the options passed by the user.
854 options._update_careful(actual_options.__dict__)
855 # Store the options passed by the user in an _actual_options attribute.
856 # We store only the keys, and not the values, since the values can contain
857 # arbitrary information, which might be PII.
858 metrics.collector.add('arguments', list(actual_options.__dict__.keys()))
859
szager@chromium.org2c391af2014-05-23 09:07:15 +0000860 if options.quiet:
861 options.verbose = 0
862
863 levels = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG]
864 logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
agable@chromium.org5a306a22014-02-24 22:13:59 +0000865
866 try:
szager@chromium.org848fd492014-04-09 19:06:44 +0000867 global_cache_dir = Mirror.GetCachePath()
868 except RuntimeError:
869 global_cache_dir = None
870 if options.cache_dir:
871 if global_cache_dir and (
872 os.path.abspath(options.cache_dir) !=
873 os.path.abspath(global_cache_dir)):
Gavin Make6a62332020-12-04 21:57:10 +0000874 logging.warning('Overriding globally-configured cache directory.')
szager@chromium.org848fd492014-04-09 19:06:44 +0000875 Mirror.SetCachePath(options.cache_dir)
agable@chromium.org5a306a22014-02-24 22:13:59 +0000876
agable@chromium.org5a306a22014-02-24 22:13:59 +0000877 return options, args
878
879
880def main(argv):
881 dispatcher = subcommand.CommandDispatcher(__name__)
882 return dispatcher.execute(OptionParser(), argv)
883
884
885if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +0000886 try:
Edward Lesmescb047442021-05-06 20:18:49 +0000887 with metrics.collector.print_notice_and_exit():
888 sys.exit(main(sys.argv[1:]))
sbc@chromium.org013731e2015-02-26 18:28:43 +0000889 except KeyboardInterrupt:
890 sys.stderr.write('interrupted\n')
Edward Lemurdf746d02019-07-27 00:42:46 +0000891 sys.exit(1)