Skip to content

Commit d10c117

Browse files
Disable Shallow Fetch for AutoManagedVHD-Based Full Clones (#5411)
* Override fetchDepth for full-clones via AutoManagedVhd * Add an AgentKnob to disable this behavior * Debug to output log --------- Co-authored-by: Semih Okur <seokur@microsoft.com>
1 parent 023b60d commit d10c117

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Agent.Plugins/GitSourceProvider.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,31 @@ public async Task GetSourceAsync(
618618
}
619619
}
620620

621+
if (File.Exists(Path.Combine(targetPath, ".autoManagedVhd"))
622+
&& !AgentKnobs.DisableAutoManagedVhdShallowOverride.GetValue(executionContext).AsBoolean())
623+
{
624+
// The existing working directory comes from an auto-managed VHD and is a full,
625+
// non-shallow clone of the repository. Some pipelines enable shallow fetch, but
626+
// Git cannot convert an existing full clone into a shallow one in-place.
627+
//
628+
// Technical reason:
629+
// A full clone already has complete commit history and object reachability.
630+
// When a fetch is issued with a non-zero --depth against a full clone, Git
631+
// does *not* rewrite the local history to match the requested shallow boundary.
632+
// Instead, to honor the depth constraint, Git falls back to creating a brand-new
633+
// shallow clone in an empty directory.
634+
//
635+
// That behavior causes the agent to discard the VHD-provided clone and re-clone
636+
// from scratch—defeating the whole purpose of using AutoManagedVHDs for fast sync.
637+
//
638+
// To avoid this, force a normal full fetch by disabling shallow behavior:
639+
// clean = false → preserve the VHD clone
640+
// fetchDepth = 0 → perform a standard "sync" fetch
641+
clean = false;
642+
fetchDepth = 0;
643+
executionContext.Output($"Detected an Auto Managed VHD at {targetPath}. Setting clean to false and fetchDepth to 0.");
644+
}
645+
621646
// When repo.clean is selected for a git repo, execute git clean -ffdx and git reset --hard HEAD on the current repo.
622647
// This will help us save the time to reclone the entire repo.
623648
// If any git commands exit with non-zero return code or any exception happened during git.exe invoke, fall back to delete the repo folder.

src/Agent.Sdk/Knob/AgentKnobs.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ public class AgentKnobs
231231
new EnvironmentKnobSource("VSTS_FETCHBYCOMMITFORFULLCLONE"),
232232
new BuiltInDefaultKnobSource("false"));
233233

234+
public static readonly Knob DisableAutoManagedVhdShallowOverride = new Knob(
235+
nameof(DisableAutoManagedVhdShallowOverride),
236+
"If true, the agent will NOT override shallow-fetch settings when an AutoManagedVHD full clone is detected.",
237+
new RuntimeKnobSource("VSTS.DisableAutoManagedVhdShallowOverride"),
238+
new EnvironmentKnobSource("VSTS_DISABLEAUTOMANAGEDVHD_SHALLOW_OVERRIDE"),
239+
new BuiltInDefaultKnobSource("false"));
240+
234241
// Agent logging
235242
public static readonly Knob AgentPerflog = new Knob(
236243
nameof(AgentPerflog),

0 commit comments

Comments
 (0)