X-Git-Url: http://git.scottworley.com/git-cache/blobdiff_plain/347be7cf3626da2bee6487ef25b2d9dc66988a43..513b354cd4a557c53273e0bacc9be812407f2a4a:/git_cache.py diff --git a/git_cache.py b/git_cache.py index 594fb8a..aab5b07 100644 --- a/git_cache.py +++ b/git_cache.py @@ -12,6 +12,8 @@ import sys from typing import Tuple +import backoff + Path = str # eg: "/home/user/.cache/git-cache/v1" Repo = str # eg: "https://github.com/NixOS/nixpkgs.git" Ref = str # eg: "master" or "v1.0.0" @@ -36,17 +38,26 @@ def verify_ancestry(repo: Repo, ref: Ref, rev: Rev) -> None: rev, ref], check=True) +@backoff.on_exception( + backoff.expo, + subprocess.CalledProcessError, + max_time=lambda: int(os.environ.get('BACKOFF_MAX_TIME', '30'))) +def _git_fetch(cachedir: Path, repo: Repo, ref: Ref) -> None: + # We don't use --force here because we want to abort and freak out if forced + # updates are happening. + subprocess.run(['git', '-C', cachedir, 'fetch', repo, + '%s:%s' % (ref, ref)], check=True) + + def fetch(repo: Repo, ref: Ref) -> Tuple[Path, Rev]: cachedir = git_cachedir(repo) if not os.path.exists(cachedir): logging.debug("Initializing git repo") - subprocess.run(['git', 'init', '--bare', cachedir], check=True) + subprocess.run(['git', 'init', '--bare', cachedir], + check=True, stdout=sys.stderr) logging.debug('Fetching ref "%s" from %s', ref, repo) - # We don't use --force here because we want to abort and freak out if forced - # updates are happening. - subprocess.run(['git', '-C', cachedir, 'fetch', repo, - '%s:%s' % (ref, ref)], check=True) + _git_fetch(cachedir, repo, ref) with open(os.path.join(cachedir, 'refs', 'heads', ref)) as rev_file: rev = Rev(rev_file.read(999).strip())