#!/usr/bin/env python3 import os import os.path import subprocess GIT_ROOT = 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"] 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"] 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 push_all(repo): print(f"[>] Pushing {repo}") subprocess.run(["git", "push", "origin"], cwd=GIT_ROOT+repo) #subprocess.run([f"{GIT_ROOT}.hooks/post-update"], cwd=GIT_ROOT+repo) if __name__ == "__main__": for element in os.listdir(GIT_ROOT): if element.endswith(".git"): if is_mirror(element): direction = get_direction(element) if direction == "up": push_all(element) if direction == "down": fetch_all(element)