Use URL and prefix (directory) as git cache keys

Using URL instead of git revisions should ensure that the cache
doesn't grow indefinitely. With git revisions, the cache would be soon
full of mostly old revisions. The advantage using git revision is
that, for example, different branches of a repo can be cached
simultaneously. I hope, this is not the common usage pattern. And if
it is, one can always set XDG_CACHE_HOME variable to use different
caches for different branches.
This commit is contained in:
Michal Sojka 2025-04-18 12:13:32 +02:00
parent c619c573f2
commit a6c2ff98fb

View file

@ -444,21 +444,18 @@ def ros2nix(args):
merge_base = merge_base_to_upstream(head)
head = check_output(f"git rev-list {merge_base} -1 -- .".split())
def cache_key(prefix, rev):
def cache_key(url, prefix):
if args.use_per_package_src:
return f"{prefix}-{rev}"
return rev
return f"{url}?dir={prefix}"
return url
# Latest commit present in the upstream repo. If
# the local repository doesn't have additional
# commits, it is the same as HEAD. Should work
# even with detached HEAD.
upstream_rev = merge_base_to_upstream(head)
if cache_key(prefix, upstream_rev) in git_cache:
info = git_cache[cache_key(prefix, upstream_rev)]
upstream_rev = info["rev"]
else:
info = git_cache.get(cache_key(url, prefix))
if info is None or info["rev"] != upstream_rev:
info = json.loads(
subprocess.check_output(
["nix-prefetch-git", "--quiet"]
@ -470,7 +467,7 @@ def ros2nix(args):
+ [toplevel, upstream_rev],
).decode()
)
git_cache[cache_key(prefix, upstream_rev)] = {k : info[k] for k in ["rev", "sha256"]}
git_cache[cache_key(url, prefix)] = {k : info[k] for k in ["rev", "sha256"]}
match = re.match("https://github.com/(?P<owner>[^/]*)/(?P<repo>.*?)(.git|/.*)?$", url)
sparse_checkout = f"""sparseCheckout = ["{prefix}"];