mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-09 02:28:18 +02:00
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.
This commit is contained in:
parent
ee99a868f5
commit
fe4e5c24e4
1 changed files with 33 additions and 17 deletions
|
|
@ -56,29 +56,45 @@ def _keys_to_dict(itr):
|
||||||
|
|
||||||
|
|
||||||
@memoize
|
@memoize
|
||||||
def git_ref_exists_plain(ref):
|
def git_ref_exists_full_path(ref):
|
||||||
try:
|
val = git_ref_exists(ref)
|
||||||
subprocess.check_output(["git", "show-ref", "-q", "--verify", str(ref)])
|
if val:
|
||||||
except subprocess.CalledProcessError:
|
try:
|
||||||
return False
|
subprocess.check_output(["git", "show-ref", "-q", "--verify", str(ref)])
|
||||||
return True
|
except subprocess.CalledProcessError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return val
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@memoize
|
def _git_ref_exists_eval(ref):
|
||||||
def git_ref_exists(ref):
|
|
||||||
try:
|
try:
|
||||||
out = subprocess.check_output(
|
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:
|
except subprocess.CalledProcessError:
|
||||||
return None
|
return None
|
||||||
try:
|
o = out.decode("ascii").strip()
|
||||||
o = out.decode("ascii").strip()
|
if len(o) == 40:
|
||||||
if len(o) == 40:
|
return o
|
||||||
return o
|
raise Exception(f"git-rev-parse for '{ref}' returned unexpected output {out}")
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
raise Exception("git-rev-parse for '%s' returned unexpected output %s" % (ref, 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
|
@memoize
|
||||||
|
|
@ -275,7 +291,7 @@ if __name__ == "__main__":
|
||||||
if not ref_head:
|
if not ref_head:
|
||||||
die('Ref "%s" does not exist' % (ref_head0))
|
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(
|
die(
|
||||||
"Notes refs/notes/bugs not found. Read CONTRIBUTING.md file for how to setup the notes"
|
"Notes refs/notes/bugs not found. Read CONTRIBUTING.md file for how to setup the notes"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue