From a6c2ff98fb97b71a0fef14fb8edf6dc48bfc008a Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 18 Apr 2025 12:13:32 +0200 Subject: [PATCH] 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. --- ros2nix/ros2nix.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/ros2nix/ros2nix.py b/ros2nix/ros2nix.py index 157ef24..499c421 100755 --- a/ros2nix/ros2nix.py +++ b/ros2nix/ros2nix.py @@ -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[^/]*)/(?P.*?)(.git|/.*)?$", url) sparse_checkout = f"""sparseCheckout = ["{prefix}"];