"""Capture the real Git fixtures after the live-server run; preserves exit codes and stderr."""
import pathlib,subprocess,sys,sqlite3,json
r=pathlib.Path(sys.argv[1]);text=[]
def git(path,*args):
 p=subprocess.run(['git','-C',str(path),*args],capture_output=True,text=True)
 text.append('$ git -C '+str(path)+' '+' '.join(args));text.append('exit='+str(p.returncode));text.append(p.stdout+p.stderr)
fixture=r/'server/fixture';git(fixture/'demo','show-ref');git(fixture/'demo','config','--local','--list');git(fixture/'probe','symbolic-ref','HEAD');git(fixture/'probe','rev-parse','--symbolic-full-name','--verify','--end-of-options','@');git(fixture/'probe','config','--local','--list');git(fixture/'source','remote','-v');git(fixture/'source','config','--local','--list')
db=sqlite3.connect(r/'server/userdata/statev2.sqlite')
for (payload,) in db.execute("select payload_json from orchestration_events where event_type='thread.created'"):
 x=json.loads(payload)
 if x.get('creationSource')=='provider' and x.get('worktreePath'):
  git(pathlib.Path(x['worktreePath']),'rev-parse','--symbolic-full-name','--verify','--end-of-options','HEAD');git(pathlib.Path(x['worktreePath']),'symbolic-ref','HEAD')
(r/'git-state.txt').write_text('\n'.join(text).replace(str(r),'/proof'))
