1010using System . Collections . Generic ;
1111using System . IO ;
1212using System . Linq ;
13+ using System . Threading ;
1314
1415namespace FastFetch
1516{
@@ -30,7 +31,6 @@ public class FetchHelper
3031 protected readonly bool SkipConfigUpdate ;
3132
3233 private const string AreaPath = nameof ( FetchHelper ) ;
33- private const int CommitDepth = 1 ;
3434
3535 public FetchHelper (
3636 ITracer tracer ,
@@ -132,15 +132,24 @@ public static bool TryLoadFileList(Enlistment enlistment, string filesInput, Lis
132132 /// <param name="branchOrCommit">A specific branch to filter for, or null for all branches returned from info/refs</param>
133133 public virtual void FastFetch ( string branchOrCommit , bool isBranch )
134134 {
135- int matchedBlobs ;
136- int downloadedBlobs ;
137- this . FastFetchWithStats ( branchOrCommit , isBranch , out matchedBlobs , out downloadedBlobs ) ;
135+ int matchedBlobCount ;
136+ int downloadedBlobCount ;
137+ int readFileCount ;
138+
139+ this . FastFetchWithStats ( branchOrCommit , isBranch , false , out matchedBlobCount , out downloadedBlobCount , out readFileCount ) ;
138140 }
139141
140- public void FastFetchWithStats ( string branchOrCommit , bool isBranch , out int matchedBlobs , out int downloadedBlobs )
142+ public void FastFetchWithStats (
143+ string branchOrCommit ,
144+ bool isBranch ,
145+ bool readFilesAfterDownload ,
146+ out int matchedBlobCount ,
147+ out int downloadedBlobCount ,
148+ out int readFileCount )
141149 {
142- matchedBlobs = 0 ;
143- downloadedBlobs = 0 ;
150+ matchedBlobCount = 0 ;
151+ downloadedBlobCount = 0 ;
152+ readFileCount = 0 ;
144153
145154 if ( string . IsNullOrWhiteSpace ( branchOrCommit ) )
146155 {
@@ -169,16 +178,13 @@ public void FastFetchWithStats(string branchOrCommit, bool isBranch, out int mat
169178 }
170179
171180 this . DownloadMissingCommit ( commitToFetch , this . GitObjects ) ;
172-
173- // Dummy output queue since we don't need to checkout available blobs
174- BlockingCollection < string > availableBlobs = new BlockingCollection < string > ( ) ;
175181
176182 // Configure pipeline
177183 // LsTreeHelper output => FindMissingBlobs => BatchDownload => IndexPack
178184 string shallowFile = Path . Combine ( this . Enlistment . WorkingDirectoryRoot , GVFSConstants . DotGit . Shallow ) ;
179185
180186 string previousCommit = null ;
181-
187+
182188 // Use the shallow file to find a recent commit to diff against to try and reduce the number of SHAs to check
183189 DiffHelper blobEnumerator = new DiffHelper ( this . Tracer , this . Enlistment , this . FileList , this . FolderList ) ;
184190 if ( File . Exists ( shallowFile ) )
@@ -192,21 +198,32 @@ public void FastFetchWithStats(string branchOrCommit, bool isBranch, out int mat
192198 }
193199 }
194200
195- blobEnumerator . PerformDiff ( previousCommit , commitToFetch ) ;
196- this . HasFailures |= blobEnumerator . HasFailures ;
201+ new Thread (
202+ ( ) =>
203+ {
204+ blobEnumerator . PerformDiff ( previousCommit , commitToFetch ) ;
205+ this . HasFailures |= blobEnumerator . HasFailures ;
206+ } ) . Start ( ) ;
207+
208+ BlockingCollection < string > availableBlobs = new BlockingCollection < string > ( ) ;
197209
198210 FindMissingBlobsJob blobFinder = new FindMissingBlobsJob ( this . SearchThreadCount , blobEnumerator . RequiredBlobs , availableBlobs , this . Tracer , this . Enlistment ) ;
199- BatchObjectDownloadJob downloader = new BatchObjectDownloadJob ( this . DownloadThreadCount , this . ChunkSize , blobFinder . DownloadQueue , availableBlobs , this . Tracer , this . Enlistment , this . ObjectRequestor , this . GitObjects ) ;
211+ BatchObjectDownloadJob downloader = new BatchObjectDownloadJob ( this . DownloadThreadCount , this . ChunkSize , blobFinder . MissingBlobs , availableBlobs , this . Tracer , this . Enlistment , this . ObjectRequestor , this . GitObjects ) ;
200212 IndexPackJob packIndexer = new IndexPackJob ( this . IndexThreadCount , downloader . AvailablePacks , availableBlobs , this . Tracer , this . GitObjects ) ;
213+ ReadFilesJob readFiles = new ReadFilesJob ( Environment . ProcessorCount * 2 , blobEnumerator . FileAddOperations , availableBlobs , this . Tracer ) ;
201214
202215 blobFinder . Start ( ) ;
203216 downloader . Start ( ) ;
204-
217+
218+ if ( readFilesAfterDownload )
219+ {
220+ readFiles . Start ( ) ;
221+ }
222+
205223 // If indexing happens during searching, searching progressively gets slower, so wait on searching before indexing.
206224 blobFinder . WaitForCompletion ( ) ;
207225 this . HasFailures |= blobFinder . HasFailures ;
208226
209- // Index regardless of failures, it'll shorten the next fetch.
210227 packIndexer . Start ( ) ;
211228
212229 downloader . WaitForCompletion ( ) ;
@@ -215,8 +232,17 @@ public void FastFetchWithStats(string branchOrCommit, bool isBranch, out int mat
215232 packIndexer . WaitForCompletion ( ) ;
216233 this . HasFailures |= packIndexer . HasFailures ;
217234
218- matchedBlobs = blobFinder . AvailableBlobCount + blobFinder . MissingBlobCount ;
219- downloadedBlobs = blobFinder . MissingBlobCount ;
235+ availableBlobs . CompleteAdding ( ) ;
236+
237+ if ( readFilesAfterDownload )
238+ {
239+ readFiles . WaitForCompletion ( ) ;
240+ this . HasFailures |= readFiles . HasFailures ;
241+ }
242+
243+ matchedBlobCount = blobFinder . AvailableBlobCount + blobFinder . MissingBlobCount ;
244+ downloadedBlobCount = blobFinder . MissingBlobCount ;
245+ readFileCount = readFiles . ReadFileCount ;
220246
221247 if ( ! this . SkipConfigUpdate && ! this . HasFailures )
222248 {
@@ -313,15 +339,14 @@ protected void DownloadMissingCommit(string commitSha, GitObjects gitObjects)
313339 {
314340 EventMetadata startMetadata = new EventMetadata ( ) ;
315341 startMetadata . Add ( "CommitSha" , commitSha ) ;
316- startMetadata . Add ( "CommitDepth" , CommitDepth ) ;
317342
318343 using ( ITracer activity = this . Tracer . StartActivity ( "DownloadTrees" , EventLevel . Informational , Keywords . Telemetry , startMetadata ) )
319344 {
320345 using ( FastFetchLibGit2Repo repo = new FastFetchLibGit2Repo ( this . Tracer , this . Enlistment . WorkingDirectoryRoot ) )
321346 {
322347 if ( ! repo . ObjectExists ( commitSha ) )
323348 {
324- if ( ! gitObjects . TryEnsureCommitIsLocal ( commitSha , commitDepth : CommitDepth ) )
349+ if ( ! gitObjects . TryDownloadCommit ( commitSha ) )
325350 {
326351 EventMetadata metadata = new EventMetadata ( ) ;
327352 metadata . Add ( "ObjectsEndpointUrl" , this . ObjectRequestor . CacheServer . ObjectsEndpointUrl ) ;
0 commit comments