summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-04-07 17:10:46 +0200
committerThomas Haller <thaller@redhat.com>2023-04-07 17:23:13 +0200
commitfe4e5c24e408536501b319a59384eee515caf8a5 (patch)
treeae6905cf0f6f2d8b53bf7e8258ba5e93139954d5
parentee99a868f52713cca42ae78275c54cb1e30cc9fc (diff)
downloadNetworkManager-fe4e5c24e408536501b319a59384eee515caf8a5.tar.gz
find-backports: improve git_ref_exists() to cache also the hashes themselves
git_ref_exists() memoizes the result. But while it looks up the SHA sum for "ref", it also can cache the result for the SHA sum itself.
-rwxr-xr-xcontrib/scripts/find-backports50
1 files changed, 33 insertions, 17 deletions
diff --git a/contrib/scripts/find-backports b/contrib/scripts/find-backports
index 6d2500c946..ec4103e516 100755
--- a/contrib/scripts/find-backports
+++ b/contrib/scripts/find-backports
@@ -56,29 +56,45 @@ def _keys_to_dict(itr):
@memoize
-def git_ref_exists_plain(ref):
- try:
- subprocess.check_output(["git", "show-ref", "-q", "--verify", str(ref)])
- except subprocess.CalledProcessError:
- return False
- return True
+def git_ref_exists_full_path(ref):
+ val = git_ref_exists(ref)
+ if val:
+ try:
+ subprocess.check_output(["git", "show-ref", "-q", "--verify", str(ref)])
+ except subprocess.CalledProcessError:
+ pass
+ else:
+ return val
+ return None
-@memoize
-def git_ref_exists(ref):
+def _git_ref_exists_eval(ref):
try:
out = subprocess.check_output(
- ["git", "rev-parse", "--verify", str(ref) + "^{commit}"], stderr=FNULL
+ ["git", "rev-parse", "--verify", str(ref) + "^{commit}"],
+ stderr=FNULL,
)
except subprocess.CalledProcessError:
return None
- try:
- o = out.decode("ascii").strip()
- if len(o) == 40:
- return o
- except Exception:
- pass
- raise Exception("git-rev-parse for '%s' returned unexpected output %s" % (ref, out))
+ o = out.decode("ascii").strip()
+ if len(o) == 40:
+ return o
+ raise Exception(f"git-rev-parse for '{ref}' returned unexpected output {out}")
+
+
+_git_ref_exists_cache = {}
+
+
+def git_ref_exists(ref):
+ val = _git_ref_exists_cache.get(ref, False)
+
+ if val is False:
+ val = _git_ref_exists_eval(ref)
+ _git_ref_exists_cache[ref] = val
+ if val and ref != val:
+ _git_ref_exists_cache[val] = val
+
+ return val
@memoize
@@ -275,7 +291,7 @@ if __name__ == "__main__":
if not ref_head:
die('Ref "%s" does not exist' % (ref_head0))
- if not git_ref_exists_plain("refs/notes/bugs"):
+ if not git_ref_exists_full_path("refs/notes/bugs"):
die(
"Notes refs/notes/bugs not found. Read CONTRIBUTING.md file for how to setup the notes"
)