From 558a7d3db2ff04b81d4886883e9c5e740f757f06 Mon Sep 17 00:00:00 2001 From: Matthew John Cheetham Date: Wed, 18 Jun 2025 12:08:49 +0100 Subject: [PATCH] Revert "Always index locally for prefetch packs" This reverts commit 317d1c2ffe68246b3f4386b42084ef03ce354cba. --- GVFS/GVFS.Common/Git/GitObjects.cs | 79 +++++++++++++++++------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/GVFS/GVFS.Common/Git/GitObjects.cs b/GVFS/GVFS.Common/Git/GitObjects.cs index b0b30faad9..c2fe88b805 100644 --- a/GVFS/GVFS.Common/Git/GitObjects.cs +++ b/GVFS/GVFS.Common/Git/GitObjects.cs @@ -706,53 +706,64 @@ private LooseObjectToWrite GetLooseObjectDestination(string sha) bytesDownloaded += packLength; - // We can't trust the index file from the server, so we will always build our own. - // We still need to consume and handle any exceptions from the index stream though. - var canContinue = true; - GitProcess.Result result; - if (this.TryBuildIndex(activity, packTempPath, out result, gitProcess)) + // We will try to build an index if the server does not send one + if (pack.IndexStream == null) { - tempPacks.Add(new TempPrefetchPackAndIdx(pack.Timestamp, packName, packTempPath, packFlushTask, idxName, idxTempPath, idxFlushTask: null)); - if (pack.IndexStream != null) + GitProcess.Result result; + if (!this.TryBuildIndex(activity, packTempPath, out result, gitProcess)) { - try - { - bytesDownloaded += pack.IndexStream.Length; - if (pack.IndexStream.CanSeek) - { - pack.IndexStream.Seek(0, SeekOrigin.End); - } - else - { - pack.IndexStream.CopyTo(Stream.Null); - } - } - catch (Exception e) + if (packFlushTask != null) { - canContinue = false; - EventMetadata metadata = CreateEventMetadata(e); - activity.RelatedWarning(metadata, "Failed to read to end of index stream"); + packFlushTask.Wait(); } + + // Move whatever has been successfully downloaded so far + Exception moveException; + this.TryFlushAndMoveTempPacks(tempPacks, ref latestTimestamp, out moveException); + + return new RetryWrapper.CallbackResult(null, true); } + + tempPacks.Add(new TempPrefetchPackAndIdx(pack.Timestamp, packName, packTempPath, packFlushTask, idxName, idxTempPath, idxFlushTask: null)); } else { - canContinue = false; - } - - if (!canContinue) - { - if (packFlushTask != null) + Task indexFlushTask; + if (this.TryWriteTempFile(activity, pack.IndexStream, idxTempPath, out indexLength, out indexFlushTask)) { - packFlushTask.Wait(); + tempPacks.Add(new TempPrefetchPackAndIdx(pack.Timestamp, packName, packTempPath, packFlushTask, idxName, idxTempPath, indexFlushTask)); } + else + { + bytesDownloaded += indexLength; + + // Try to build the index manually, then retry the prefetch + GitProcess.Result result; + if (this.TryBuildIndex(activity, packTempPath, out result, gitProcess)) + { + // If we were able to recreate the failed index + // we can start the prefetch at the next timestamp + tempPacks.Add(new TempPrefetchPackAndIdx(pack.Timestamp, packName, packTempPath, packFlushTask, idxName, idxTempPath, idxFlushTask: null)); + } + else + { + if (packFlushTask != null) + { + packFlushTask.Wait(); + } + } - // Move whatever has been successfully downloaded so far - Exception moveException; - this.TryFlushAndMoveTempPacks(tempPacks, ref latestTimestamp, out moveException); + // Move whatever has been successfully downloaded so far + Exception moveException; + this.TryFlushAndMoveTempPacks(tempPacks, ref latestTimestamp, out moveException); - return new RetryWrapper.CallbackResult(null, true); + // The download stream will not be in a good state if the index download fails. + // So we have to restart the prefetch + return new RetryWrapper.CallbackResult(null, true); + } } + + bytesDownloaded += indexLength; } Exception exception = null;