"""Synthetic Git snapshot lab. No arguments, real repositories, secrets or network.
Creates and removes its own temporary repositories; writes dummy commits only there.
"""
import json, os, pathlib, subprocess, tempfile, platform
GIT='/usr/bin/git'

def main():
 results=[]
 with tempfile.TemporaryDirectory(prefix='commit-snapshot-demo-') as temp:
  root=pathlib.Path(temp);home=root/'isolated-home';home.mkdir();hooks=root/'empty-hooks';hooks.mkdir()
  env={'PATH':'/usr/bin:/bin','HOME':str(home),'GIT_CONFIG_NOSYSTEM':'1','GIT_CONFIG_GLOBAL':os.devnull,
       'GIT_AUTHOR_NAME':'Synthetic Demo','GIT_AUTHOR_EMAIL':'demo@example.invalid',
       'GIT_COMMITTER_NAME':'Synthetic Demo','GIT_COMMITTER_EMAIL':'demo@example.invalid',
       'GIT_TERMINAL_PROMPT':'0','LC_ALL':'C'}
  def git(repo,*args):
   r=subprocess.run([GIT,'-c','core.hooksPath='+str(hooks),'-c','commit.gpgsign=false',*args],cwd=repo,env=env,text=True,capture_output=True,check=True)
   return r.stdout.strip()
  def repo(name):
   p=root/name;p.mkdir();git(p,'init','-q');(p/'config.txt').write_text('token=PLACEHOLDER\n');(p/'other.txt').write_text('baseline\n');git(p,'add','--','config.txt','other.txt');git(p,'commit','-qm','dummy baseline');return p
  # Working tree was cleaned, but a prior staged version remains.
  p=repo('plain');(p/'config.txt').write_text('token=DEMO_ONLY_NOT_A_CREDENTIAL\n');git(p,'add','--','config.txt')
  assert git(p,'diff','--','config.txt')==''
  assert 'DEMO_ONLY' in git(p,'diff','--cached','--','config.txt')
  results.append({'case':'plain diff empty after add','staged_dummy_still_present':True})
  (p/'config.txt').write_text('token=PLACEHOLDER\n')
  assert 'DEMO_ONLY' not in (p/'config.txt').read_text();assert 'DEMO_ONLY' in git(p,'show',':config.txt')
  results.append({'case':'working copy cleaned after add','index_still_contains_dummy':True})
  git(p,'commit','-qm','dummy plain commit');assert 'DEMO_ONLY' in git(p,'show','HEAD:config.txt')
  results.append({'case':'plain commit without paths','committed_previous_index':True})
  # A reviewed staged change does not freeze later working-copy edits with -a.
  p=repo('all');(p/'config.txt').write_text('token=REVIEWED_PLACEHOLDER\n');git(p,'add','--','config.txt');review=git(p,'show',':config.txt')
  (p/'config.txt').write_text('token=DEMO_ONLY_LATER_EDIT\n');(p/'new.txt').write_text('untracked dummy\n');git(p,'commit','-qam','dummy all commit')
  assert git(p,'show','HEAD:config.txt')!=review and 'DEMO_ONLY_LATER_EDIT' in git(p,'show','HEAD:config.txt')
  results.append({'case':'commit -a after staged review','later_tracked_edit_committed':True})
  assert 'new.txt' not in git(p,'ls-tree','--name-only','HEAD').splitlines()
  results.append({'case':'commit -a untracked path','untracked_file_not_committed':True})
  # Explicit path commit has another content selection rule.
  p=repo('path');(p/'config.txt').write_text('token=STAGED_PLACEHOLDER\n');(p/'other.txt').write_text('staged unrelated\n');git(p,'add','--','config.txt','other.txt')
  (p/'config.txt').write_text('token=DEMO_ONLY_PATH_EDIT\n');git(p,'commit','-qm','dummy path commit','--','config.txt')
  assert 'DEMO_ONLY_PATH_EDIT' in git(p,'show','HEAD:config.txt');assert git(p,'show','HEAD:other.txt')=='baseline';assert git(p,'show',':other.txt')=='staged unrelated'
  results.append({'case':'explicit path commit','working_path_committed_other_staging_preserved':True})
  # Bounded correction of the staged fixture before ordinary commit.
  p=repo('restage');(p/'config.txt').write_text('token=DEMO_ONLY_STAGED\n');git(p,'add','--','config.txt');(p/'config.txt').write_text('token=REVIEWED_PLACEHOLDER\n');git(p,'add','--','config.txt');review=git(p,'show',':config.txt');git(p,'commit','-qm','reviewed dummy commit');assert git(p,'show','HEAD:config.txt')==review
  results.append({'case':'review after explicit restage, plain commit','committed_reviewed_bytes':True})
  assert all(not git(root/n,'remote') for n in ['plain','all','path','restage'])
  output={'passed':True,'git_version':git(root,'--version'),'python':platform.python_version(),'cases':results,'no_remotes':True,'limits':'Only synthetic ordinary local repositories; no hooks, external filters, partial staging UI, service or real credential scan tested.'}
 print(json.dumps(output,indent=2))
if __name__=='__main__':main()
