import os, pathlib, subprocess, sys, json, datetime
root = pathlib.Path(sys.argv[1]) / datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
root.mkdir(parents=True)
origin = 'https://github.com/pingdotgg/t3code.git'
branch = 'factory/issue-44417' # Existing PR; successful read-only reopening, no forge writes.
env = dict(os.environ, GIT_AUTHOR_DATE='2026-11-20T12:00:00Z', GIT_COMMITTER_DATE='2026-11-20T12:00:00Z')
def git(cwd, *args):
    return subprocess.check_output(['git','-C',str(cwd),*args], env=env, stderr=subprocess.STDOUT).decode().strip()
for side in ['base','head']:
    for kind in ['real-hook','noop-shim']:
        parent=root/side/kind
        repo=parent/'push-hook-repro'
        repo.mkdir(parents=True)
        bare=parent/'remote.git'
        subprocess.run(['git','init','--bare',str(bare)],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
        git(repo,'init','-b','main')
        git(repo,'config','user.name','Repro')
        git(repo,'config','user.email','repro@example.com')
        git(repo,'config','core.hooksPath','.vite-hooks/_')
        git(repo,'config',f'url.{bare.as_uri()}.pushInsteadOf',origin)
        git(repo,'remote','add','origin',origin)
        hooks=repo/'.vite-hooks'
        (hooks/'_').mkdir(parents=True)
        for name in ['pre-push','pre-commit','reference-transaction']:
            p=hooks/'_'/name
            p.write_text('#!/usr/bin/env sh\n. "$(dirname "$0")/h"\n')
            p.chmod(0o755)
        (hooks/'_'/'h').write_text('#!/usr/bin/env sh\nn=$(basename "$0")\ns=$(dirname "$(dirname "$0")")/$n\n[ ! -f "$s" ] && exit 0\nsh -e "$s" "$@"\n')
        (hooks/'pre-commit').write_text('#!/usr/bin/env sh\nexit 0\n')
        (repo/'package.json').write_text(json.dumps({'name':'push-hook-repro','private':True,'scripts':{'prepare':'vp config --no-agent'}})+'\n')
        (repo/'README.md').write_text('Push hook reproduction\n')
        git(repo,'add','.')
        git(repo,'commit','-m','Initial')
        git(repo,'push','-u','origin','main')
        git(repo,'symbolic-ref','refs/remotes/origin/HEAD','refs/remotes/origin/main')
        git(repo,'switch','-c',branch)
        if kind=='real-hook':
            (hooks/'pre-push').write_text('#!/usr/bin/env sh\nprintf "pre-push ran\\n" >> .git/push-hook-ran\n')
        (repo/'README.md').write_text('Push hook reproduction\nFeature change\n')
        git(repo,'add','.')
        git(repo,'commit','-m','Repro change')
print(root)
