]> git.scottworley.com Git - git-cache/blame - git_cache.py
Start on 1.4.0
[git-cache] / git_cache.py
CommitLineData
bef7ce53
SW
1# It would be nice if we could share the nix git cache, but as of the
2# time of writing it is transitioning from gitv2 (deprecated) to gitv3
3# (not ready yet), and trying to straddle them both is too far into nix
4# implementation details for my comfort. So we re-implement here half of
5# nix's builtins.fetchGit. :(
6
7import hashlib
8import logging
9import os
10import subprocess
347be7cf 11import sys
bef7ce53
SW
12
13from typing import Tuple
14
f36d5c6f
SW
15import backoff
16
bef7ce53
SW
17Path = str # eg: "/home/user/.cache/git-cache/v1"
18Repo = str # eg: "https://github.com/NixOS/nixpkgs.git"
19Ref = str # eg: "master" or "v1.0.0"
20Rev = str # eg: "53a27350551844e1ed1a9257690294767389ef0d"
21
22
23def git_cachedir(repo: Repo) -> Path:
24 # Use xdg module when it's less painful to have as a dependency
25 XDG_CACHE_HOME = Path(
26 os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache')))
27
28 return Path(os.path.join(
29 XDG_CACHE_HOME,
30 'git-cache/v1',
31 hashlib.sha256(repo.encode()).hexdigest()))
32
33
eb638847 34def is_ancestor(repo: Repo, ref: Ref, rev: Rev) -> bool:
bef7ce53 35 cachedir = git_cachedir(repo)
eb638847
SW
36 logging.debug('Checking if rev %s is an ancestor of ref "%s"', rev, ref)
37 process = subprocess.run(
38 ['git', '-C', cachedir, 'merge-base', '--is-ancestor', rev, ref], check=False)
39 return process.returncode == 0
40
41
42def verify_ancestry(repo: Repo, ref: Ref, rev: Rev) -> None:
43 if not is_ancestor(repo, ref, rev):
44 raise Exception('Rev %s is not an ancestor of ref "%s"' % (rev, ref))
bef7ce53
SW
45
46
f36d5c6f
SW
47@backoff.on_exception(
48 backoff.expo,
49 subprocess.CalledProcessError,
50 max_time=lambda: int(os.environ.get('BACKOFF_MAX_TIME', '30')))
51def _git_fetch(cachedir: Path, repo: Repo, ref: Ref) -> None:
52 # We don't use --force here because we want to abort and freak out if forced
53 # updates are happening.
54 subprocess.run(['git', '-C', cachedir, 'fetch', repo,
55 '%s:%s' % (ref, ref)], check=True)
56
57
bef7ce53
SW
58def fetch(repo: Repo, ref: Ref) -> Tuple[Path, Rev]:
59 cachedir = git_cachedir(repo)
60 if not os.path.exists(cachedir):
61 logging.debug("Initializing git repo")
513b354c
SW
62 subprocess.run(['git', 'init', '--bare', cachedir],
63 check=True, stdout=sys.stderr)
bef7ce53
SW
64
65 logging.debug('Fetching ref "%s" from %s', ref, repo)
f36d5c6f 66 _git_fetch(cachedir, repo, ref)
bef7ce53
SW
67
68 with open(os.path.join(cachedir, 'refs', 'heads', ref)) as rev_file:
69 rev = Rev(rev_file.read(999).strip())
70 verify_ancestry(repo, ref, rev)
71
72 return cachedir, rev
73
74
75def ensure_rev_available(repo: Repo, ref: Ref, rev: Rev) -> Path:
76 cachedir = git_cachedir(repo)
eb638847
SW
77 if os.path.exists(cachedir) and is_ancestor(repo, ref, rev):
78 return cachedir
bef7ce53
SW
79
80 logging.debug(
81 'We do not have rev %s. We will fetch ref "%s" and hope it appears.',
82 rev, ref)
83 fetch(repo, ref)
84 logging.debug('Verifying that fetch retrieved rev %s', rev)
85 subprocess.run(['git', '-C', cachedir, 'cat-file', '-e', rev], check=True)
eb638847 86 verify_ancestry(repo, ref, rev)
bef7ce53
SW
87
88 return cachedir
347be7cf
SW
89
90
91def _main() -> None:
92 if len(sys.argv) == 3:
93 print('{1} {0}'.format(*fetch(Repo(sys.argv[1]), Ref(sys.argv[2]))))
94 elif len(sys.argv) == 4:
95 print(ensure_rev_available(
96 Repo(sys.argv[1]), Ref(sys.argv[2]), Rev(sys.argv[3])))
97 else:
98 usage = '''usage: git-cache repo ref [rev]
99example: git-cache https://github.com/NixOS/nixpkgs.git master'''
100 print(usage, file=sys.stderr)
101 sys.exit(1)