2024-01-12 09:46:11 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
2024-02-08 14:55:36 +01:00
|
|
|
from pathlib import Path
|
2024-01-12 09:46:11 +01:00
|
|
|
|
2024-02-08 14:55:36 +01:00
|
|
|
GIT_ROOT = Path(os.environ.get("GIT_ROOT_DIR") or "/srv/git/")
|
2024-01-12 09:46:11 +01:00
|
|
|
|
2024-02-08 15:43:46 +01:00
|
|
|
def is_mirror(repo_path):
|
|
|
|
cmd = ["git", "config", "-f", f"{repo_path}/config", "remote.origin.mirror"]
|
2024-01-12 09:46:11 +01:00
|
|
|
proc = subprocess.run(cmd, stdout=subprocess.PIPE)
|
|
|
|
if proc.stdout.decode().strip() == "true":
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2024-02-08 15:43:46 +01:00
|
|
|
def get_direction(repo_path):
|
|
|
|
cmd = ["git", "config", "-f", f"{repo_path}/config", "remote.origin.direction"]
|
2024-01-12 09:46:11 +01:00
|
|
|
proc = subprocess.run(cmd, stdout=subprocess.PIPE)
|
|
|
|
return proc.stdout.decode().strip()
|
|
|
|
|
2024-02-08 15:43:46 +01:00
|
|
|
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"])
|
2024-01-12 09:46:11 +01:00
|
|
|
|
2024-02-08 15:43:46 +01:00
|
|
|
def push_all(repo_path):
|
|
|
|
print(f"[>] Pushing {repo_path}")
|
|
|
|
subprocess.run(["git", "push", "origin"], cwd=repo_path)
|
2024-01-12 09:46:11 +01:00
|
|
|
#subprocess.run([f"{GIT_ROOT}.hooks/post-update"], cwd=GIT_ROOT+repo)
|
|
|
|
|
2024-02-08 14:55:36 +01:00
|
|
|
def run_on_all_repos(root: Path):
|
|
|
|
for element in os.listdir(root):
|
2024-02-08 15:43:46 +01:00
|
|
|
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)
|
2024-01-12 09:46:11 +01:00
|
|
|
if direction == "up":
|
2024-02-08 15:43:46 +01:00
|
|
|
push_all(full_path)
|
2024-01-12 09:46:11 +01:00
|
|
|
if direction == "down":
|
2024-02-08 15:43:46 +01:00
|
|
|
fetch_all(full_path)
|
2024-02-08 14:55:36 +01:00
|
|
|
elif os.path.isdir(root / element):
|
|
|
|
run_on_all_repos(root / element)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run_on_all_repos(GIT_ROOT)
|
|
|
|
|