Simplify clickusagemd pre-push hook detection.
This commit is contained in:
parent
27ab8a24d9
commit
c777cb0a1f
|
@ -1,7 +1,7 @@
|
|||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
PRE_PUSH_SCRIPT = """#!/bin/sh
|
||||
#:clickusagemd:
|
||||
|
||||
set -e
|
||||
|
||||
|
@ -25,10 +25,20 @@ def is_gitdir(func):
|
|||
return path_check
|
||||
|
||||
|
||||
def is_clickusagemd_hook(path: Path) -> bool:
|
||||
githook = path / "pre-push"
|
||||
if githook.exists():
|
||||
with githook.open("rt") as fd:
|
||||
file_contents = fd.read()
|
||||
if ":clickusagemd:" in file_contents:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@is_gitdir
|
||||
def install_hook(path: Path) -> bool:
|
||||
githook = path / "pre-push"
|
||||
if not githook.exists():
|
||||
if not githook.exists() or is_clickusagemd_hook(path):
|
||||
with githook.open("wt") as fd:
|
||||
fd.write(PRE_PUSH_SCRIPT)
|
||||
fd.close()
|
||||
|
@ -43,20 +53,11 @@ def install_hook(path: Path) -> bool:
|
|||
@is_gitdir
|
||||
def uninstall_hook(path: Path) -> bool:
|
||||
githook = path / "pre-push"
|
||||
if githook.exists():
|
||||
sha256_hook = hashlib.new("sha256")
|
||||
with githook.open("rb") as fd:
|
||||
sha256_hook.update(fd.read())
|
||||
sha256_const = hashlib.new("sha256")
|
||||
sha256_const.update(PRE_PUSH_SCRIPT.encode("utf-8"))
|
||||
|
||||
if sha256_const.digest() == sha256_hook.digest():
|
||||
githook.unlink()
|
||||
print(f"Hook `{githook}' uninstalled.")
|
||||
return True
|
||||
else:
|
||||
print("The file seems not to be the a clickusagemd hook.")
|
||||
if githook.exists() and is_clickusagemd_hook(path):
|
||||
githook.unlink()
|
||||
print(f"Hook `{githook}' uninstalled.")
|
||||
return True
|
||||
else:
|
||||
print("The clickusagemd hook is not installed.")
|
||||
print(f"The `{githook}' file was not uninstalled.")
|
||||
|
||||
return False
|
||||
|
|
Loading…
Reference in New Issue