@@ -2,6 +2,7 @@ const fs = require('fs');
22const { URL } = require ( 'url' ) ;
33const { default : GitHubClient , auth} = require ( '@github-graph/api' ) ;
44const ask = require ( 'interrogator' ) ;
5+ const chalk = require ( 'chalk' ) ;
56const git = require ( '../packages/http' ) ;
67const gitObj = require ( '../packages/objects' ) ;
78
@@ -66,7 +67,7 @@ async function pullRepo() {
6667 const mode =
6768 process . argv [ 4 ] ?? ( await ask . list ( `mode?` , [ `commits` , `tree` ] ) ) ;
6869 if ( ! [ `commits` , `tree` ] . includes ( mode ) ) {
69- console . error ( `Invalid mode` ) ;
70+ console . error ( chalk . red ( `Invalid mode` ) ) ;
7071 return process . exit ( 1 ) ;
7172 }
7273 const headCommit = process . argv [ 5 ] ;
@@ -77,13 +78,34 @@ async function pullRepo() {
7778 const repoURL = new URL ( `https://github.com/${ repo . owner } /${ repo . name } .git` ) ;
7879
7980 const start = Date . now ( ) ;
80- console . warn ( `git_init` , `Git init request ${ repoURL . href } ` ) ;
81+ console . warn ( chalk . cyan ( `git_init` ) , `Git init request ${ repoURL . href } ` ) ;
8182 const { capabilities : serverCapabilities } = await git . initialRequest ( repoURL , {
8283 http,
8384 agent : 'rollingversions.com' ,
8485 } ) ;
8586
86- console . warn ( `git_lsrefs` , `Git ls refs request ${ repoURL . href } ` ) ;
87+ const pullObjects = async ( want ) => {
88+ const fetchResponse = await git . fetchObjects (
89+ repoURL ,
90+ { want : [ ...new Set ( want ) ] } ,
91+ {
92+ http,
93+ agent : 'rollingversions.com' ,
94+ serverCapabilities,
95+ } ,
96+ ) ;
97+
98+ const entries = new Map ( ) ;
99+ await new Promise ( ( resolve , reject ) => {
100+ fetchResponse
101+ . on ( `data` , ( entry ) => entries . set ( entry . hash , entry ) )
102+ . on ( `error` , reject )
103+ . on ( `end` , ( ) => resolve ( ) ) ;
104+ } ) ;
105+ return entries ;
106+ } ;
107+
108+ console . warn ( chalk . cyan ( `git_lsrefs` ) , `Git ls refs request ${ repoURL . href } ` ) ;
87109 const remoteRefs = headCommit
88110 ? [ { objectID : headCommit } ]
89111 : await git . lsRefs (
@@ -106,20 +128,21 @@ async function pullRepo() {
106128 console . warn ( `ref:` , ref ) ;
107129 }
108130
109- console . warn ( `git_fetch_objects` , `Git fetch request ${ repoURL . href } ` ) ;
131+ console . warn (
132+ chalk . cyan ( `git_fetch_objects` ) ,
133+ `Git fetch request ${ repoURL . href } ` ,
134+ ) ;
110135 const fetchResponse = await git . fetchObjects (
111136 repoURL ,
112137
113138 mode === 'tree'
114139 ? {
115140 want : [ ...new Set ( remoteRefs . map ( ( ref ) => ref . objectID ) ) ] ,
116- have : [ ] ,
117- filter : [ git . blobNone ( 0 ) ] ,
141+ filter : [ git . blobNone ( ) ] ,
118142 deepen : 1 ,
119143 }
120144 : {
121145 want : [ ...new Set ( remoteRefs . map ( ( ref ) => ref . objectID ) ) ] ,
122- have : [ ] ,
123146 filter : [ git . treeDepth ( 0 ) ] ,
124147 } ,
125148 {
@@ -135,33 +158,64 @@ async function pullRepo() {
135158 . on ( `data` , ( entry ) => {
136159 if ( gitObj . objectIsCommit ( entry . body ) ) {
137160 const commit = gitObj . decodeObject ( entry . body ) ;
138- console . warn ( `${ entry . hash } ${ commit . body . message } ` ) ;
161+ console . warn ( `${ chalk . magenta ( entry . hash ) } ${ commit . body . message } ` ) ;
139162 headTreeSha = commit . body . tree ;
140163 rootHash = commit . body . tree ;
141- }
142- if ( gitObj . objectIsTree ( entry . body ) ) {
164+ } else if ( gitObj . objectIsTree ( entry . body ) ) {
143165 const tree = gitObj . decodeObject ( entry . body ) ;
144166 trees . set ( entry . hash , tree . body ) ;
167+ } else {
168+ const obj = gitObj . decodeObject ( entry . body ) ;
169+ console . error (
170+ chalk . red ( `Unexpected object ${ entry . hash } of type ${ obj . type } ` ) ,
171+ ) ;
145172 }
146173 } )
147174 . on ( `error` , reject )
148175 . on ( `end` , ( ) => resolve ( ) ) ;
149176 } ) ;
150177 if ( mode === `tree` ) {
151- const printTree = ( hash , path ) => {
178+ const packages = [ ] ;
179+ const walkTree = ( hash , parentPath ) => {
152180 const tree = trees . get ( hash ) ;
153181 for ( const [ name , { mode, hash} ] of Object . entries ( tree ) ) {
154- console . log ( `${ path } /${ name } ${ gitObj . Mode [ mode ] } ${ hash } ` ) ;
182+ const path = `${ parentPath } /${ name } ` ;
183+ const modeName = gitObj . Mode [ mode ] ;
184+ const modeColor =
185+ { file : chalk . yellow , tree : chalk . blue } [ modeName ] ?? chalk . red ;
186+ console . log ( `${ path } ${ modeColor ( gitObj . Mode [ mode ] ) } ${ hash } ` ) ;
155187
156188 if ( mode === gitObj . Mode . tree ) {
157- printTree ( hash , `${ path } /${ name } ` ) ;
189+ walkTree ( hash , path ) ;
190+ } else if (
191+ mode === gitObj . Mode . file &&
192+ ( name === `rolling-package.toml` || name === 'package.json' )
193+ ) {
194+ packages . push ( { path, hash} ) ;
158195 }
159196 }
160197 } ;
161- printTree ( rootHash , `` ) ;
198+ walkTree ( rootHash , `` ) ;
199+ if ( packages . length ) {
200+ const packageObjects = await pullObjects ( packages . map ( ( p ) => p . hash ) ) ;
201+ for ( const { path, hash} of packages ) {
202+ const entry = packageObjects . get ( hash ) ;
203+ if ( entry ) {
204+ const obj = gitObj . decodeObject ( entry . body ) ;
205+ console . log ( `${ chalk . magenta ( path ) } ${ hash } ${ obj . type } ` ) ;
206+ console . log ( `` ) ;
207+ console . log ( Buffer . from ( obj . body ) . toString ( `utf8` ) ) ;
208+ console . log ( `` ) ;
209+ console . log ( `` ) ;
210+ } else {
211+ console . error ( chalk . red ( `${ path } ${ hash } NOT FOUND!!!` ) ) ;
212+ }
213+ }
214+ }
162215 }
216+
163217 const end = Date . now ( ) ;
164- console . log ( `Pull duration: ${ end - start } ` ) ;
218+ console . log ( chalk . green ( `Pull duration: ${ end - start } ` ) ) ;
165219}
166220
167221pullRepo ( ) . catch ( ( ex ) => {
0 commit comments