#!/usr/bin/python3
# Both sides run this identical observer. No synthetic command responses.
import os,sys,subprocess,time,json
real=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'real-git')
# PRoot preserves the bound filename; find the real binary beside the driver through env.
real=os.environ['PROOF_REAL_GIT']
a=sys.argv[1:]
if 'for-each-ref' not in a or '--format=%(refname)' not in a:
    os.execv(real,['git',*a])
start=time.perf_counter_ns()
r=subprocess.run([real,*a],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
elapsed=(time.perf_counter_ns()-start)/1e6
row={'ms':elapsed,'code':r.returncode,'patterns':a[a.index('--format=%(refname)')+1:],'bytes':len(r.stdout),'refs':r.stdout.decode().splitlines()}
with open(os.environ['MEASURE_LOG'],'a') as f: f.write(json.dumps(row)+'\n')
sys.stdout.buffer.write(r.stdout)
sys.stderr.buffer.write(r.stderr)
sys.exit(r.returncode)
