git_cache.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. #!/usr/bin/env python
  2. # 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. """A git command for managing a local cache of git repositories."""
  6. from __future__ import print_function
  7. import contextlib
  8. import errno
  9. import logging
  10. import optparse
  11. import os
  12. import re
  13. import subprocess
  14. import sys
  15. import tempfile
  16. import threading
  17. import time
  18. try:
  19. import urlparse
  20. except ImportError: # For Py3 compatibility
  21. import urllib.parse as urlparse
  22. from download_from_google_storage import Gsutil
  23. import gclient_utils
  24. import lockfile
  25. import subcommand
  26. # Analogous to gc.autopacklimit git config.
  27. GC_AUTOPACKLIMIT = 50
  28. GIT_CACHE_CORRUPT_MESSAGE = 'WARNING: The Git cache is corrupt.'
  29. try:
  30. # pylint: disable=undefined-variable
  31. WinErr = WindowsError
  32. except NameError:
  33. class WinErr(Exception):
  34. pass
  35. class ClobberNeeded(Exception):
  36. pass
  37. def exponential_backoff_retry(fn, excs=(Exception,), name=None, count=10,
  38. sleep_time=0.25, printerr=None):
  39. """Executes |fn| up to |count| times, backing off exponentially.
  40. Args:
  41. fn (callable): The function to execute. If this raises a handled
  42. exception, the function will retry with exponential backoff.
  43. excs (tuple): A tuple of Exception types to handle. If one of these is
  44. raised by |fn|, a retry will be attempted. If |fn| raises an Exception
  45. that is not in this list, it will immediately pass through. If |excs|
  46. is empty, the Exception base class will be used.
  47. name (str): Optional operation name to print in the retry string.
  48. count (int): The number of times to try before allowing the exception to
  49. pass through.
  50. sleep_time (float): The initial number of seconds to sleep in between
  51. retries. This will be doubled each retry.
  52. printerr (callable): Function that will be called with the error string upon
  53. failures. If None, |logging.warning| will be used.
  54. Returns: The return value of the successful fn.
  55. """
  56. printerr = printerr or logging.warning
  57. for i in range(count):
  58. try:
  59. return fn()
  60. except excs as e:
  61. if (i+1) >= count:
  62. raise
  63. printerr('Retrying %s in %.2f second(s) (%d / %d attempts): %s' % (
  64. (name or 'operation'), sleep_time, (i+1), count, e))
  65. time.sleep(sleep_time)
  66. sleep_time *= 2
  67. class Mirror(object):
  68. git_exe = 'git.bat' if sys.platform.startswith('win') else 'git'
  69. gsutil_exe = os.path.join(
  70. os.path.dirname(os.path.abspath(__file__)), 'gsutil.py')
  71. cachepath_lock = threading.Lock()
  72. UNSET_CACHEPATH = object()
  73. # Used for tests
  74. _GIT_CONFIG_LOCATION = []
  75. @staticmethod
  76. def parse_fetch_spec(spec):
  77. """Parses and canonicalizes a fetch spec.
  78. Returns (fetchspec, value_regex), where value_regex can be used
  79. with 'git config --replace-all'.
  80. """
  81. parts = spec.split(':', 1)
  82. src = parts[0].lstrip('+').rstrip('/')
  83. if not src.startswith('refs/'):
  84. src = 'refs/heads/%s' % src
  85. dest = parts[1].rstrip('/') if len(parts) > 1 else src
  86. regex = r'\+%s:.*' % src.replace('*', r'\*')
  87. return ('+%s:%s' % (src, dest), regex)
  88. def __init__(self, url, refs=None, print_func=None):
  89. self.url = url
  90. self.fetch_specs = set([self.parse_fetch_spec(ref) for ref in (refs or [])])
  91. self.basedir = self.UrlToCacheDir(url)
  92. self.mirror_path = os.path.join(self.GetCachePath(), self.basedir)
  93. if print_func:
  94. self.print = self.print_without_file
  95. self.print_func = print_func
  96. else:
  97. self.print = print
  98. def print_without_file(self, message, **_kwargs):
  99. self.print_func(message)
  100. @contextlib.contextmanager
  101. def print_duration_of(self, what):
  102. start = time.time()
  103. try:
  104. yield
  105. finally:
  106. self.print('%s took %.1f minutes' % (what, (time.time() - start) / 60.0))
  107. @property
  108. def bootstrap_bucket(self):
  109. b = os.getenv('OVERRIDE_BOOTSTRAP_BUCKET')
  110. if b:
  111. return b
  112. u = urlparse.urlparse(self.url)
  113. if u.netloc == 'chromium.googlesource.com':
  114. return 'chromium-git-cache'
  115. # TODO(tandrii): delete once LUCI migration is completed.
  116. # Only public hosts will be supported going forward.
  117. elif u.netloc == 'chrome-internal.googlesource.com':
  118. return 'chrome-git-cache'
  119. # Not recognized.
  120. return None
  121. @property
  122. def _gs_path(self):
  123. return 'gs://%s/v2/%s' % (self.bootstrap_bucket, self.basedir)
  124. @classmethod
  125. def FromPath(cls, path):
  126. return cls(cls.CacheDirToUrl(path))
  127. @staticmethod
  128. def UrlToCacheDir(url):
  129. """Convert a git url to a normalized form for the cache dir path."""
  130. if os.path.isdir(url):
  131. # Ignore the drive letter in Windows
  132. url = os.path.splitdrive(url)[1]
  133. return url.replace('-', '--').replace(os.sep, '-')
  134. parsed = urlparse.urlparse(url)
  135. norm_url = parsed.netloc + parsed.path
  136. if norm_url.endswith('.git'):
  137. norm_url = norm_url[:-len('.git')]
  138. # Use the same dir for authenticated URLs and unauthenticated URLs.
  139. norm_url = norm_url.replace('googlesource.com/a/', 'googlesource.com/')
  140. return norm_url.replace('-', '--').replace('/', '-').lower()
  141. @staticmethod
  142. def CacheDirToUrl(path):
  143. """Convert a cache dir path to its corresponding url."""
  144. netpath = re.sub(r'\b-\b', '/', os.path.basename(path)).replace('--', '-')
  145. return 'https://%s' % netpath
  146. @classmethod
  147. def SetCachePath(cls, cachepath):
  148. with cls.cachepath_lock:
  149. setattr(cls, 'cachepath', cachepath)
  150. @classmethod
  151. def GetCachePath(cls):
  152. with cls.cachepath_lock:
  153. if not hasattr(cls, 'cachepath'):
  154. try:
  155. cachepath = subprocess.check_output(
  156. [cls.git_exe, 'config'] +
  157. cls._GIT_CONFIG_LOCATION +
  158. ['cache.cachepath']).decode('utf-8', 'ignore').strip()
  159. except subprocess.CalledProcessError:
  160. cachepath = os.environ.get('GIT_CACHE_PATH', cls.UNSET_CACHEPATH)
  161. setattr(cls, 'cachepath', cachepath)
  162. ret = getattr(cls, 'cachepath')
  163. if ret is cls.UNSET_CACHEPATH:
  164. raise RuntimeError('No cache.cachepath git configuration or '
  165. '$GIT_CACHE_PATH is set.')
  166. return ret
  167. @staticmethod
  168. def _GetMostRecentCacheDirectory(ls_out_set):
  169. ready_file_pattern = re.compile(r'.*/(\d+).ready$')
  170. ready_dirs = []
  171. for name in ls_out_set:
  172. m = ready_file_pattern.match(name)
  173. # Given <path>/<number>.ready,
  174. # we are interested in <path>/<number> directory
  175. if m and (name[:-len('.ready')] + '/') in ls_out_set:
  176. ready_dirs.append((int(m.group(1)), name[:-len('.ready')]))
  177. if not ready_dirs:
  178. return None
  179. return max(ready_dirs)[1]
  180. def Rename(self, src, dst):
  181. # This is somehow racy on Windows.
  182. # Catching OSError because WindowsError isn't portable and
  183. # pylint complains.
  184. exponential_backoff_retry(
  185. lambda: os.rename(src, dst),
  186. excs=(OSError,),
  187. name='rename [%s] => [%s]' % (src, dst),
  188. printerr=self.print)
  189. def RunGit(self, cmd, **kwargs):
  190. """Run git in a subprocess."""
  191. cwd = kwargs.setdefault('cwd', self.mirror_path)
  192. kwargs.setdefault('print_stdout', False)
  193. kwargs.setdefault('filter_fn', self.print)
  194. env = kwargs.get('env') or kwargs.setdefault('env', os.environ.copy())
  195. env.setdefault('GIT_ASKPASS', 'true')
  196. env.setdefault('SSH_ASKPASS', 'true')
  197. self.print('running "git %s" in "%s"' % (' '.join(cmd), cwd))
  198. gclient_utils.CheckCallAndFilter([self.git_exe] + cmd, **kwargs)
  199. def config(self, cwd=None, reset_fetch_config=False):
  200. if cwd is None:
  201. cwd = self.mirror_path
  202. if reset_fetch_config:
  203. try:
  204. self.RunGit(['config', '--unset-all', 'remote.origin.fetch'], cwd=cwd)
  205. except subprocess.CalledProcessError as e:
  206. # If exit code was 5, it means we attempted to unset a config that
  207. # didn't exist. Ignore it.
  208. if e.returncode != 5:
  209. raise
  210. # Don't run git-gc in a daemon. Bad things can happen if it gets killed.
  211. try:
  212. self.RunGit(['config', 'gc.autodetach', '0'], cwd=cwd)
  213. except subprocess.CalledProcessError:
  214. # Hard error, need to clobber.
  215. raise ClobberNeeded()
  216. # Don't combine pack files into one big pack file. It's really slow for
  217. # repositories, and there's no way to track progress and make sure it's
  218. # not stuck.
  219. if self.supported_project():
  220. self.RunGit(['config', 'gc.autopacklimit', '0'], cwd=cwd)
  221. # Allocate more RAM for cache-ing delta chains, for better performance
  222. # of "Resolving deltas".
  223. self.RunGit(['config', 'core.deltaBaseCacheLimit',
  224. gclient_utils.DefaultDeltaBaseCacheLimit()], cwd=cwd)
  225. self.RunGit(['config', 'remote.origin.url', self.url], cwd=cwd)
  226. self.RunGit(['config', '--replace-all', 'remote.origin.fetch',
  227. '+refs/heads/*:refs/heads/*', r'\+refs/heads/\*:.*'], cwd=cwd)
  228. for spec, value_regex in self.fetch_specs:
  229. self.RunGit(
  230. ['config', '--replace-all', 'remote.origin.fetch', spec, value_regex],
  231. cwd=cwd)
  232. def bootstrap_repo(self, directory):
  233. """Bootstrap the repo from Google Storage if possible.
  234. More apt-ly named bootstrap_repo_from_cloud_if_possible_else_do_nothing().
  235. """
  236. if not self.bootstrap_bucket:
  237. return False
  238. gsutil = Gsutil(self.gsutil_exe, boto_path=None)
  239. # Get the most recent version of the directory.
  240. # This is determined from the most recent version of a .ready file.
  241. # The .ready file is only uploaded when an entire directory has been
  242. # uploaded to GS.
  243. _, ls_out, ls_err = gsutil.check_call('ls', self._gs_path)
  244. ls_out_set = set(ls_out.strip().splitlines())
  245. latest_dir = self._GetMostRecentCacheDirectory(ls_out_set)
  246. if not latest_dir:
  247. self.print('No bootstrap file for %s found in %s, stderr:\n %s' %
  248. (self.mirror_path, self.bootstrap_bucket,
  249. ' '.join((ls_err or '').splitlines(True))))
  250. return False
  251. try:
  252. # create new temporary directory locally
  253. tempdir = tempfile.mkdtemp(prefix='_cache_tmp', dir=self.GetCachePath())
  254. self.RunGit(['init', '--bare'], cwd=tempdir)
  255. self.print('Downloading files in %s/* into %s.' %
  256. (latest_dir, tempdir))
  257. with self.print_duration_of('download'):
  258. code = gsutil.call('-m', 'cp', '-r', latest_dir + "/*",
  259. tempdir)
  260. if code:
  261. return False
  262. except Exception as e:
  263. self.print('Encountered error: %s' % str(e), file=sys.stderr)
  264. gclient_utils.rmtree(tempdir)
  265. return False
  266. # delete the old directory
  267. if os.path.exists(directory):
  268. gclient_utils.rmtree(directory)
  269. self.Rename(tempdir, directory)
  270. return True
  271. def contains_revision(self, revision):
  272. if not self.exists():
  273. return False
  274. if sys.platform.startswith('win'):
  275. # Windows .bat scripts use ^ as escape sequence, which means we have to
  276. # escape it with itself for every .bat invocation.
  277. needle = '%s^^^^{commit}' % revision
  278. else:
  279. needle = '%s^{commit}' % revision
  280. try:
  281. # cat-file exits with 0 on success, that is git object of given hash was
  282. # found.
  283. self.RunGit(['cat-file', '-e', needle])
  284. return True
  285. except subprocess.CalledProcessError:
  286. return False
  287. def exists(self):
  288. return os.path.isfile(os.path.join(self.mirror_path, 'config'))
  289. def supported_project(self):
  290. """Returns true if this repo is known to have a bootstrap zip file."""
  291. u = urlparse.urlparse(self.url)
  292. return u.netloc in [
  293. 'chromium.googlesource.com',
  294. 'chrome-internal.googlesource.com']
  295. def _preserve_fetchspec(self):
  296. """Read and preserve remote.origin.fetch from an existing mirror.
  297. This modifies self.fetch_specs.
  298. """
  299. if not self.exists():
  300. return
  301. try:
  302. config_fetchspecs = subprocess.check_output(
  303. [self.git_exe, 'config', '--get-all', 'remote.origin.fetch'],
  304. cwd=self.mirror_path).decode('utf-8', 'ignore')
  305. for fetchspec in config_fetchspecs.splitlines():
  306. self.fetch_specs.add(self.parse_fetch_spec(fetchspec))
  307. except subprocess.CalledProcessError:
  308. logging.warn('Tried and failed to preserve remote.origin.fetch from the '
  309. 'existing cache directory. You may need to manually edit '
  310. '%s and "git cache fetch" again.'
  311. % os.path.join(self.mirror_path, 'config'))
  312. def _ensure_bootstrapped(
  313. self, depth, bootstrap, reset_fetch_config, force=False):
  314. pack_dir = os.path.join(self.mirror_path, 'objects', 'pack')
  315. pack_files = []
  316. if os.path.isdir(pack_dir):
  317. pack_files = [f for f in os.listdir(pack_dir) if f.endswith('.pack')]
  318. self.print('%s has %d .pack files, re-bootstrapping if >%d or ==0' %
  319. (self.mirror_path, len(pack_files), GC_AUTOPACKLIMIT))
  320. should_bootstrap = (force or
  321. not self.exists() or
  322. len(pack_files) > GC_AUTOPACKLIMIT or
  323. len(pack_files) == 0)
  324. if not should_bootstrap:
  325. if depth and os.path.exists(os.path.join(self.mirror_path, 'shallow')):
  326. logging.warn(
  327. 'Shallow fetch requested, but repo cache already exists.')
  328. return
  329. if not self.exists():
  330. if os.path.exists(self.mirror_path):
  331. # If the mirror path exists but self.exists() returns false, we're
  332. # in an unexpected state. Nuke the previous mirror directory and
  333. # start fresh.
  334. gclient_utils.rmtree(self.mirror_path)
  335. os.mkdir(self.mirror_path)
  336. elif not reset_fetch_config:
  337. # Re-bootstrapping an existing mirror; preserve existing fetch spec.
  338. self._preserve_fetchspec()
  339. bootstrapped = (not depth and bootstrap and
  340. self.bootstrap_repo(self.mirror_path))
  341. if not bootstrapped:
  342. if not self.exists() or not self.supported_project():
  343. # Bootstrap failed due to:
  344. # 1. No previous cache.
  345. # 2. Project doesn't have a bootstrap folder.
  346. # Start with a bare git dir.
  347. self.RunGit(['init', '--bare'], cwd=self.mirror_path)
  348. else:
  349. # Bootstrap failed, previous cache exists; warn and continue.
  350. logging.warn(
  351. 'Git cache has a lot of pack files (%d). Tried to re-bootstrap '
  352. 'but failed. Continuing with non-optimized repository.'
  353. % len(pack_files))
  354. def _fetch(self,
  355. rundir,
  356. verbose,
  357. depth,
  358. no_fetch_tags,
  359. reset_fetch_config,
  360. prune=True):
  361. self.config(rundir, reset_fetch_config)
  362. fetch_cmd = ['fetch']
  363. if verbose:
  364. fetch_cmd.extend(['-v', '--progress'])
  365. if depth:
  366. fetch_cmd.extend(['--depth', str(depth)])
  367. if no_fetch_tags:
  368. fetch_cmd.append('--no-tags')
  369. if prune:
  370. fetch_cmd.append('--prune')
  371. fetch_cmd.append('origin')
  372. fetch_specs = subprocess.check_output(
  373. [self.git_exe, 'config', '--get-all', 'remote.origin.fetch'],
  374. cwd=rundir).decode('utf-8', 'ignore').strip().splitlines()
  375. for spec in fetch_specs:
  376. try:
  377. self.print('Fetching %s' % spec)
  378. with self.print_duration_of('fetch %s' % spec):
  379. self.RunGit(fetch_cmd + [spec], cwd=rundir, retry=True)
  380. except subprocess.CalledProcessError:
  381. if spec == '+refs/heads/*:refs/heads/*':
  382. raise ClobberNeeded() # Corrupted cache.
  383. logging.warn('Fetch of %s failed' % spec)
  384. def populate(self,
  385. depth=None,
  386. no_fetch_tags=False,
  387. shallow=False,
  388. bootstrap=False,
  389. verbose=False,
  390. lock_timeout=0,
  391. reset_fetch_config=False):
  392. assert self.GetCachePath()
  393. if shallow and not depth:
  394. depth = 10000
  395. gclient_utils.safe_makedirs(self.GetCachePath())
  396. with lockfile.lock(self.mirror_path, lock_timeout):
  397. try:
  398. self._ensure_bootstrapped(depth, bootstrap, reset_fetch_config)
  399. self._fetch(self.mirror_path, verbose, depth, no_fetch_tags,
  400. reset_fetch_config)
  401. except ClobberNeeded:
  402. # This is a major failure, we need to clean and force a bootstrap.
  403. gclient_utils.rmtree(self.mirror_path)
  404. self.print(GIT_CACHE_CORRUPT_MESSAGE)
  405. self._ensure_bootstrapped(depth,
  406. bootstrap,
  407. reset_fetch_config,
  408. force=True)
  409. self._fetch(self.mirror_path, verbose, depth, no_fetch_tags,
  410. reset_fetch_config)
  411. def update_bootstrap(self, prune=False, gc_aggressive=False):
  412. # The folder is <git number>
  413. gen_number = subprocess.check_output(
  414. [self.git_exe, 'number', 'master'],
  415. cwd=self.mirror_path).decode('utf-8', 'ignore').strip()
  416. gsutil = Gsutil(path=self.gsutil_exe, boto_path=None)
  417. src_name = self.mirror_path
  418. dest_prefix = '%s/%s' % (self._gs_path, gen_number)
  419. # ls_out lists contents in the format: gs://blah/blah/123...
  420. _, ls_out, _ = gsutil.check_call('ls', self._gs_path)
  421. # Check to see if folder already exists in gs
  422. ls_out_set = set(ls_out.strip().splitlines())
  423. if (dest_prefix + '/' in ls_out_set and
  424. dest_prefix + '.ready' in ls_out_set):
  425. print('Cache %s already exists.' % dest_prefix)
  426. return
  427. # Run Garbage Collect to compress packfile.
  428. gc_args = ['gc', '--prune=all']
  429. if gc_aggressive:
  430. gc_args.append('--aggressive')
  431. self.RunGit(gc_args)
  432. gsutil.call('-m', 'cp', '-r', src_name, dest_prefix)
  433. # Create .ready file and upload
  434. _, ready_file_name = tempfile.mkstemp(suffix='.ready')
  435. try:
  436. gsutil.call('cp', ready_file_name, '%s.ready' % (dest_prefix))
  437. finally:
  438. os.remove(ready_file_name)
  439. # remove all other directory/.ready files in the same gs_path
  440. # except for the directory/.ready file previously created
  441. # which can be used for bootstrapping while the current one is
  442. # being uploaded
  443. if not prune:
  444. return
  445. prev_dest_prefix = self._GetMostRecentCacheDirectory(ls_out_set)
  446. if not prev_dest_prefix:
  447. return
  448. for path in ls_out_set:
  449. if (path == prev_dest_prefix + '/' or
  450. path == prev_dest_prefix + '.ready'):
  451. continue
  452. if path.endswith('.ready'):
  453. gsutil.call('rm', path)
  454. continue
  455. gsutil.call('-m', 'rm', '-r', path)
  456. @staticmethod
  457. def DeleteTmpPackFiles(path):
  458. pack_dir = os.path.join(path, 'objects', 'pack')
  459. if not os.path.isdir(pack_dir):
  460. return
  461. pack_files = [f for f in os.listdir(pack_dir) if
  462. f.startswith('.tmp-') or f.startswith('tmp_pack_')]
  463. for f in pack_files:
  464. f = os.path.join(pack_dir, f)
  465. try:
  466. os.remove(f)
  467. logging.warn('Deleted stale temporary pack file %s' % f)
  468. except OSError:
  469. logging.warn('Unable to delete temporary pack file %s' % f)
  470. @subcommand.usage('[url of repo to check for caching]')
  471. def CMDexists(parser, args):
  472. """Check to see if there already is a cache of the given repo."""
  473. _, args = parser.parse_args(args)
  474. if not len(args) == 1:
  475. parser.error('git cache exists only takes exactly one repo url.')
  476. url = args[0]
  477. mirror = Mirror(url)
  478. if mirror.exists():
  479. print(mirror.mirror_path)
  480. return 0
  481. return 1
  482. @subcommand.usage('[url of repo to create a bootstrap zip file]')
  483. def CMDupdate_bootstrap(parser, args):
  484. """Create and uploads a bootstrap tarball."""
  485. # Lets just assert we can't do this on Windows.
  486. if sys.platform.startswith('win'):
  487. print('Sorry, update bootstrap will not work on Windows.', file=sys.stderr)
  488. return 1
  489. parser.add_option('--skip-populate', action='store_true',
  490. help='Skips "populate" step if mirror already exists.')
  491. parser.add_option('--gc-aggressive', action='store_true',
  492. help='Run aggressive repacking of the repo.')
  493. parser.add_option('--prune', action='store_true',
  494. help='Prune all other cached bundles of the same repo.')
  495. populate_args = args[:]
  496. options, args = parser.parse_args(args)
  497. url = args[0]
  498. mirror = Mirror(url)
  499. if not options.skip_populate or not mirror.exists():
  500. CMDpopulate(parser, populate_args)
  501. else:
  502. print('Skipped populate step.')
  503. # Get the repo directory.
  504. _, args2 = parser.parse_args(args)
  505. url = args2[0]
  506. mirror = Mirror(url)
  507. mirror.update_bootstrap(options.prune, options.gc_aggressive)
  508. return 0
  509. @subcommand.usage('[url of repo to add to or update in cache]')
  510. def CMDpopulate(parser, args):
  511. """Ensure that the cache has all up-to-date objects for the given repo."""
  512. parser.add_option('--depth', type='int',
  513. help='Only cache DEPTH commits of history')
  514. parser.add_option(
  515. '--no-fetch-tags',
  516. action='store_true',
  517. help=('Don\'t fetch tags from the server. This can speed up '
  518. 'fetch considerably when there are many tags.'))
  519. parser.add_option('--shallow', '-s', action='store_true',
  520. help='Only cache 10000 commits of history')
  521. parser.add_option('--ref', action='append',
  522. help='Specify additional refs to be fetched')
  523. parser.add_option('--no_bootstrap', '--no-bootstrap',
  524. action='store_true',
  525. help='Don\'t bootstrap from Google Storage')
  526. parser.add_option('--ignore_locks',
  527. '--ignore-locks',
  528. action='store_true',
  529. help='NOOP. This flag will be removed in the future.')
  530. parser.add_option('--break-locks',
  531. action='store_true',
  532. help='Break any existing lock instead of just ignoring it')
  533. parser.add_option('--reset-fetch-config', action='store_true', default=False,
  534. help='Reset the fetch config before populating the cache.')
  535. options, args = parser.parse_args(args)
  536. if not len(args) == 1:
  537. parser.error('git cache populate only takes exactly one repo url.')
  538. if options.ignore_lock:
  539. print('ignore_lock is no longer used. Please remove its usage.')
  540. url = args[0]
  541. mirror = Mirror(url, refs=options.ref)
  542. kwargs = {
  543. 'no_fetch_tags': options.no_fetch_tags,
  544. 'verbose': options.verbose,
  545. 'shallow': options.shallow,
  546. 'bootstrap': not options.no_bootstrap,
  547. 'lock_timeout': options.timeout,
  548. 'reset_fetch_config': options.reset_fetch_config,
  549. }
  550. if options.depth:
  551. kwargs['depth'] = options.depth
  552. mirror.populate(**kwargs)
  553. @subcommand.usage('Fetch new commits into cache and current checkout')
  554. def CMDfetch(parser, args):
  555. """Update mirror, and fetch in cwd."""
  556. parser.add_option('--all', action='store_true', help='Fetch all remotes')
  557. parser.add_option('--no_bootstrap', '--no-bootstrap',
  558. action='store_true',
  559. help='Don\'t (re)bootstrap from Google Storage')
  560. parser.add_option(
  561. '--no-fetch-tags',
  562. action='store_true',
  563. help=('Don\'t fetch tags from the server. This can speed up '
  564. 'fetch considerably when there are many tags.'))
  565. options, args = parser.parse_args(args)
  566. # Figure out which remotes to fetch. This mimics the behavior of regular
  567. # 'git fetch'. Note that in the case of "stacked" or "pipelined" branches,
  568. # this will NOT try to traverse up the branching structure to find the
  569. # ultimate remote to update.
  570. remotes = []
  571. if options.all:
  572. assert not args, 'fatal: fetch --all does not take a repository argument'
  573. remotes = subprocess.check_output([Mirror.git_exe, 'remote'])
  574. remotes = remotes.decode('utf-8', 'ignore').splitlines()
  575. elif args:
  576. remotes = args
  577. else:
  578. current_branch = subprocess.check_output(
  579. [Mirror.git_exe, 'rev-parse', '--abbrev-ref', 'HEAD'])
  580. current_branch = current_branch.decode('utf-8', 'ignore').strip()
  581. if current_branch != 'HEAD':
  582. upstream = subprocess.check_output(
  583. [Mirror.git_exe, 'config', 'branch.%s.remote' % current_branch])
  584. upstream = upstream.decode('utf-8', 'ignore').strip()
  585. if upstream and upstream != '.':
  586. remotes = [upstream]
  587. if not remotes:
  588. remotes = ['origin']
  589. cachepath = Mirror.GetCachePath()
  590. git_dir = os.path.abspath(subprocess.check_output(
  591. [Mirror.git_exe, 'rev-parse', '--git-dir']).decode('utf-8', 'ignore'))
  592. git_dir = os.path.abspath(git_dir)
  593. if git_dir.startswith(cachepath):
  594. mirror = Mirror.FromPath(git_dir)
  595. mirror.populate(
  596. bootstrap=not options.no_bootstrap,
  597. no_fetch_tags=options.no_fetch_tags,
  598. lock_timeout=options.timeout)
  599. return 0
  600. for remote in remotes:
  601. remote_url = subprocess.check_output(
  602. [Mirror.git_exe, 'config', 'remote.%s.url' % remote])
  603. remote_url = remote_url.decode('utf-8', 'ignore').strip()
  604. if remote_url.startswith(cachepath):
  605. mirror = Mirror.FromPath(remote_url)
  606. mirror.print = lambda *args: None
  607. print('Updating git cache...')
  608. mirror.populate(
  609. bootstrap=not options.no_bootstrap,
  610. no_fetch_tags=options.no_fetch_tags,
  611. lock_timeout=options.timeout)
  612. subprocess.check_call([Mirror.git_exe, 'fetch', remote])
  613. return 0
  614. @subcommand.usage('do not use - it is a noop.')
  615. def CMDunlock(parser, args):
  616. """This command does nothing."""
  617. print('This command does nothing and will be removed in the future.')
  618. class OptionParser(optparse.OptionParser):
  619. """Wrapper class for OptionParser to handle global options."""
  620. def __init__(self, *args, **kwargs):
  621. optparse.OptionParser.__init__(self, *args, prog='git cache', **kwargs)
  622. self.add_option('-c', '--cache-dir',
  623. help=(
  624. 'Path to the directory containing the caches. Normally '
  625. 'deduced from git config cache.cachepath or '
  626. '$GIT_CACHE_PATH.'))
  627. self.add_option('-v', '--verbose', action='count', default=1,
  628. help='Increase verbosity (can be passed multiple times)')
  629. self.add_option('-q', '--quiet', action='store_true',
  630. help='Suppress all extraneous output')
  631. self.add_option('--timeout', type='int', default=0,
  632. help='Timeout for acquiring cache lock, in seconds')
  633. def parse_args(self, args=None, values=None):
  634. options, args = optparse.OptionParser.parse_args(self, args, values)
  635. if options.quiet:
  636. options.verbose = 0
  637. levels = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG]
  638. logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)])
  639. try:
  640. global_cache_dir = Mirror.GetCachePath()
  641. except RuntimeError:
  642. global_cache_dir = None
  643. if options.cache_dir:
  644. if global_cache_dir and (
  645. os.path.abspath(options.cache_dir) !=
  646. os.path.abspath(global_cache_dir)):
  647. logging.warn('Overriding globally-configured cache directory.')
  648. Mirror.SetCachePath(options.cache_dir)
  649. return options, args
  650. def main(argv):
  651. dispatcher = subcommand.CommandDispatcher(__name__)
  652. return dispatcher.execute(OptionParser(), argv)
  653. if __name__ == '__main__':
  654. try:
  655. sys.exit(main(sys.argv[1:]))
  656. except KeyboardInterrupt:
  657. sys.stderr.write('interrupted\n')
  658. sys.exit(1)