import subprocess, pathlib, sys, json
root=pathlib.Path(sys.argv[1]); root.mkdir(parents=True, exist_ok=True)
def git(*args):
 return subprocess.check_output(['git',*args], cwd=root,text=True).strip()
git('init','-q','--initial-branch=main')
git('remote','add','origin','https://github.com/pingdotgg/t3code.git')
branch='perf/server-test-shards'
git('config','remote.origin.fetch',f'+refs/heads/{branch}:refs/remotes/origin/{branch}')
git('fetch','--depth=1','--no-tags','origin',branch)
git('reset','--hard',f'origin/{branch}')
git('checkout','-b',branch,'--track',f'origin/{branch}')
(root/'LOCAL-WORK.txt').write_text('Unpublished local work. This file and commit must survive PR worktree checkout.\n')
git('add','LOCAL-WORK.txt')
git('-c','user.name=Scenario','-c','user.email=scenario@example.invalid','commit','-qm','Unpublished local work: retain this commit')
sha=git('rev-parse','HEAD'); git('checkout','main')
(root.parent/(root.name+'-seed.json')).write_text(json.dumps({'branch':branch,'unpublished':sha,'published':git('rev-parse',f'origin/{branch}')},indent=2))
print(sha)
