import subprocess,os,shutil,json,tarfile,io
K='/workspace/kit-wt/auth-kit/versions'
D='/workspace/kit-wt-d'; L='/workspace/atopaid-wt'
P='https://github.com/sdzrxwfxd2-cloud/'
def archive(repo,ref,paths,dest,exclude=()):
    os.makedirs(dest,exist_ok=True)
    raw=subprocess.check_output(['git','-C',repo,'archive',ref,'--']+paths)
    with tarfile.open(fileobj=io.BytesIO(raw)) as t:
        for m in t.getmembers():
            if any(m.name.startswith(e) or e in m.name for e in exclude): continue
            t.extract(m,dest,filter='data')
V=[
 dict(id='01-v3-12nov',title='v3 (12 Nov): first full design, runway behind the sign-in, versions A and B',repo='design',branch='design/auth-signin-signup-12nov',pr='go-around-design/pull/9',link='https://v3-12nov.go-around-design-previews.pages.dev/',paths=['components/auth-signin-signup/README.md','components/auth-signin-signup/v3/README.md','components/auth-signin-signup/code','components/auth-signin-signup/docs','components/auth-signin-signup/flow-map.svg'],exclude=['/tick/','.mp4']),
 dict(id='02-auth-touch-19nov',title='Auth touch (19 Nov): Google and Passkey on one row, email opens inline',repo='design',branch='design/auth-touch-19nov',pr='go-around-design/pull/18',link='https://go-around-design-previews.pages.dev/auth-touch-19nov/components/pilot-ato-wired-19nov/auth/',paths=['components/pilot-ato-wired-19nov/auth','components/pilot-ato-wired-19nov/shared/ga.css','components/pilot-ato-wired-19nov/shared/ga.js','components/pilot-ato-wired-19nov/README.md'],exclude=['SUMMARY.png','/shots/']),
 dict(id='03-v10-rebuild-19nov',title='v10 rebuild (19 Nov): the one with the member pass cards and the Welcome pass',repo='design',branch='design/auth-v10-touch-19nov',pr='go-around-design/pull/20',link='https://go-around-design-previews.pages.dev/auth-v10-touch-19nov/components/pilot-ato-wired-19nov/auth-v10/',paths=['components/pilot-ato-wired-19nov/auth-v10','components/pilot-ato-wired-19nov/auth','components/pilot-ato-wired-19nov/user/user.css','components/pilot-ato-wired-19nov/user/user.js','components/pilot-ato-wired-19nov/shared/ga.css','components/pilot-ato-wired-19nov/shared/ga.js','components/pilot-ato-wired-19nov/fixtures','components/pilot-ato-wired-19nov/README.md'],exclude=['SUMMARY.png','/shots/']),
 dict(id='04-combined-a-20nov',title='Combined A (20 Nov): sign in + create account, led by the Designer layout',repo='design',branch='design/auth-combined-a-20nov',pr='go-around-design/pull/21',link='https://go-around-design-previews.pages.dev/a/components/pilot-ato-wired-19nov/auth-combined/',paths=['components/pilot-ato-wired-19nov/auth-combined','components/pilot-ato-wired-19nov/auth-v10','components/pilot-ato-wired-19nov/auth','components/pilot-ato-wired-19nov/user/user.css','components/pilot-ato-wired-19nov/user/user.js','components/pilot-ato-wired-19nov/shared/ga.css','components/pilot-ato-wired-19nov/shared/ga.js','components/pilot-ato-wired-19nov/fixtures','components/pilot-ato-wired-19nov/README.md'],exclude=['SUMMARY.png','/shots/']),
 dict(id='05-combined-b-20nov',title='Combined B (20 Nov): sign in + create account, led by the v10 layout',repo='design',branch='design/auth-combined-b-20nov',pr='go-around-design/pull/22',link='https://go-around-design-previews.pages.dev/b/components/pilot-ato-wired-19nov/auth-combined/',paths=['components/pilot-ato-wired-19nov/auth-combined','components/pilot-ato-wired-19nov/auth-v10','components/pilot-ato-wired-19nov/auth','components/pilot-ato-wired-19nov/user/user.css','components/pilot-ato-wired-19nov/user/user.js','components/pilot-ato-wired-19nov/shared/ga.css','components/pilot-ato-wired-19nov/shared/ga.js','components/pilot-ato-wired-19nov/fixtures','components/pilot-ato-wired-19nov/README.md'],exclude=['SUMMARY.png','/shots/']),
 dict(id='06-topbar-unify-19nov',title='Top bar unify (19 Nov): one top bar on every page, Sign in included',repo='design',branch='design/topbar-unify-19nov',pr='go-around-design/pull/19',link=None,paths=['components/pilot-ato-wired-19nov/shared/ga.css','components/pilot-ato-wired-19nov/shared/ga.js','components/pilot-ato-wired-19nov/auth','components/ato-page/public-option-a-13nov/option-a/shared/ga-bar.css','components/ato-page/public-option-a-13nov/option-a/shared/topbar.js'],exclude=['SUMMARY.png','/shots/']),
 dict(id='07-launch-ours-ministeps-v10-19nov',title='Launch repo: ours, mini-steps (v10) (19 Nov)',repo='launch',branch='design/signup-ours-ministeps-v10-19nov',pr=None,link=None,paths=['site/login','site/signup'],exclude=[]),
 dict(id='08-launch-v11-before-19nov',title='Launch repo: v11, no refund line, tick (MT’s v11, before my touches) (19 Nov)',repo='launch',branch='design/signup-v11-no-refund-tick-19nov',pr=None,link='https://v11-before.go-around-design-previews.pages.dev/signup/',paths=['site/login','site/signup'],exclude=[]),
 dict(id='09-launch-v11-designer-touch-20nov',title='Launch repo: v11 with the Designer’s touches (runway lights, progress flare, plan passes, label overflow fix) (20 Nov)',repo='launch',branch='design/signup-v11-designer-touch-20nov',pr='go-around-launch-11nov/pull/8',link='https://v11-after.go-around-design-previews.pages.dev/signup/',paths=['site/login','site/signup'],exclude=[]),
 dict(id='10-launch-v12-mt-edits-20nov',title='Launch repo: v12 Version 1, MT’s edits (20 Nov)',repo='launch',branch='design/signup-v12-mt-edits-20nov',pr='go-around-launch-11nov/pull/9',link='https://v12-v1.go-around-design-previews.pages.dev/login/',paths=['site/login','site/signup'],exclude=[]),
 dict(id='11-launch-v12-designer-vision-20nov',title='Launch repo: v12 Version 2, the Designer’s vision (20 Nov)',repo='launch',branch='design/signup-v12-designer-vision-20nov',pr='go-around-launch-11nov/pull/10',link='https://v12-v2.go-around-design-previews.pages.dev/login/',paths=['site/login','site/signup'],exclude=[]),
]
meta=[]
for v in V:
    repo=D if v['repo']=='design' else L
    ref='origin/'+v['branch']
    sha=subprocess.check_output(['git','-C',repo,'rev-parse',ref]).decode().strip()
    dest=f"{K}/{v['id']}"
    shutil.rmtree(dest,ignore_errors=True)
    archive(repo,ref,v['paths'],dest+'/source',v['exclude'])
    v['sha']=sha; meta.append({k:v[k] for k in ['id','title','repo','branch','pr','link','sha']})
    print(v['id'],sha[:8],subprocess.check_output(['du','-sh',dest]).decode().split()[0])
json.dump(meta,open(K+'/versions.json','w'),indent=1)
