From d2aa3b61cf55e59268efcab4914b0a38312438e8 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 8 Feb 2024 15:43:46 +0100 Subject: [PATCH] fix: properly recur in subdirs, handle paths --- fetch-mirrors | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/fetch-mirrors b/fetch-mirrors index 936bcc3..99ee223 100755 --- a/fetch-mirrors +++ b/fetch-mirrors @@ -6,38 +6,41 @@ from pathlib import Path GIT_ROOT = Path(os.environ.get("GIT_ROOT_DIR") or "/srv/git/") -def is_mirror(repo): - cmd = ["git", "config", "-f", f"{GIT_ROOT}{repo}/config", "remote.origin.mirror"] +def is_mirror(repo_path): + cmd = ["git", "config", "-f", f"{repo_path}/config", "remote.origin.mirror"] proc = subprocess.run(cmd, stdout=subprocess.PIPE) if proc.stdout.decode().strip() == "true": return True return False -def get_direction(repo): - cmd = ["git", "config", "-f", f"{GIT_ROOT}{repo}/config", "remote.origin.direction"] +def get_direction(repo_path): + cmd = ["git", "config", "-f", f"{repo_path}/config", "remote.origin.direction"] proc = subprocess.run(cmd, stdout=subprocess.PIPE) return proc.stdout.decode().strip() -def fetch_all(repo): - print(f"[<] Fetching {repo}") - subprocess.run(["git", "fetch", "--all"], cwd=GIT_ROOT+repo) - if os.path.isfile(f"{GIT_ROOT}{repo}/hooks/post-receive"): - subprocess.run([f"{GIT_ROOT}{repo}/hooks/post-receive"]) +def fetch_all(repo_path): + print(f"[<] Fetching {repo_path}") + subprocess.run(["git", "fetch", "--all"], cwd=repo_path) + if os.path.isfile(f"{repo_path}/hooks/post-receive"): + subprocess.run([f"{repo_path}/hooks/post-receive"]) -def push_all(repo): - print(f"[>] Pushing {repo}") - subprocess.run(["git", "push", "origin"], cwd=GIT_ROOT+repo) +def push_all(repo_path): + print(f"[>] Pushing {repo_path}") + subprocess.run(["git", "push", "origin"], cwd=repo_path) #subprocess.run([f"{GIT_ROOT}.hooks/post-update"], cwd=GIT_ROOT+repo) def run_on_all_repos(root: Path): for element in os.listdir(root): - if element.endswith(".git"): - if is_mirror(element): - direction = get_direction(element) + full_path = root / element + if element.startswith("."): + pass # ignore hidden files + elif element.endswith(".git"): + if is_mirror(full_path): + direction = get_direction(full_path) if direction == "up": - push_all(element) + push_all(full_path) if direction == "down": - fetch_all(element) + fetch_all(full_path) elif os.path.isdir(root / element): run_on_all_repos(root / element)