33 parseFetchResponse ,
44 FetchCommand ,
55 FetchResponseEntryObject ,
6+ FetchCommandOutputOptions ,
7+ parseFetchResponseV2 ,
8+ FetchCommandOutputOptionsV2 ,
69} from '@rollingversions/git-protocol' ;
710import { ContextWithServerCapabilities } from './Context' ;
811
@@ -16,7 +19,12 @@ export default async function fetchObjects<
1619> (
1720 repoURL : URL ,
1821 command : FetchCommand ,
19- ctx : ContextWithServerCapabilities < THeaders > ,
22+ {
23+ raw,
24+ store,
25+ storeMode,
26+ ...ctx
27+ } : ContextWithServerCapabilities < THeaders > & FetchCommandOutputOptions ,
2028) {
2129 const url = new URL (
2230 `${
@@ -56,5 +64,64 @@ export default async function fetchObjects<
5664 ) } `,
5765 ) ;
5866 }
59- return parseFetchResponse ( response . body ) ;
67+ return parseFetchResponse ( response . body , { raw, store, storeMode} ) ;
68+ }
69+
70+ export async function * fetchObjectsV2 <
71+ THeaders extends { set ( name : string , value : string ) : unknown }
72+ > (
73+ repoURL : URL ,
74+ command : FetchCommand ,
75+ {
76+ onProgress,
77+ store,
78+ storeMode,
79+ ...ctx
80+ } : ContextWithServerCapabilities < THeaders > & FetchCommandOutputOptionsV2 ,
81+ ) {
82+ const url = new URL (
83+ `${
84+ repoURL . href . endsWith ( '.git' ) ? repoURL . href : `${ repoURL . href } .git`
85+ } /git-upload-pack`,
86+ ) ;
87+ const headers = ctx . http . createHeaders ( url ) ;
88+ headers . set ( 'accept' , 'application/x-git-upload-pack-result' ) ;
89+ headers . set ( 'content-type' , 'application/x-git-upload-pack-request' ) ;
90+ headers . set ( 'git-protocol' , 'version=2' ) ;
91+ headers . set ( 'user-agent' , ctx . agent ) ;
92+
93+ const response = await ctx . http . post (
94+ url ,
95+ headers ,
96+ composeFetchCommand (
97+ command ,
98+ new Map (
99+ [
100+ [ 'agent' , ctx . agent ] as const ,
101+ ...defaultCapabilities ,
102+ ] . filter ( ( [ key ] ) => ctx . serverCapabilities . has ( key ) ) ,
103+ ) ,
104+ ) ,
105+ ) ;
106+ if ( response . statusCode !== 200 ) {
107+ const body = await new Promise < Buffer > ( ( resolve , reject ) => {
108+ const body : Buffer [ ] = [ ] ;
109+ response . body
110+ . on ( `data` , ( chunk ) => body . push ( chunk ) )
111+ . on ( `error` , reject )
112+ . on ( `end` , ( ) => resolve ( Buffer . concat ( body ) ) ) ;
113+ } ) ;
114+ throw new Error (
115+ `Git server responded with status ${ response . statusCode } : ${ body . toString (
116+ `utf8` ,
117+ ) } `,
118+ ) ;
119+ }
120+ for await ( const entry of parseFetchResponseV2 ( response . body , {
121+ onProgress,
122+ store,
123+ storeMode,
124+ } ) ) {
125+ yield entry ;
126+ }
60127}
0 commit comments