#!/usr/bin/env python import os import subprocess def check(args, input=None): return subprocess.check_output(args, text=True, input=input).strip() # config repo and branch REPO = "/srv/git/git-hooks" BRANCH = "main" # cd into repo os.chdir(REPO) # list all directories dirs = check(["git", "ls-tree", "--name-only", BRANCH]).splitlines() for d in dirs: # list all files files = check(["git", "ls-tree", "--name-only", f"{BRANCH}:{d}"]).splitlines() for f in files: # print the hook and write it to the git repo file = check(["git", "--no-pager", "show", f"{BRANCH}:{d}/{f}"]) hookpath = f"/srv/git/{d}/hooks/{f}" with open(hookpath, "w") as fd: fd.write(file) # make hook executable check(["chmod", "+x", hookpath])