Skip to content

Fix git tag pruning when fetching with specific refspecs #5254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 24, 2025

This PR addresses a bug where --prune-tags doesn't work properly when git fetch is executed with specific refspecs (like in PR builds), causing conflicts when old and new tags have conflicting filesystem paths.

Problem

When PR builds fetch with refspecs like +refs/heads/*:refs/remotes/origin/* +refs/pull/<number>/merge:refs/remotes/pull/<number>/merge, git doesn't prune conflicting tag refs even with the --prune-tags flag, leading to errors like:

error: cannot lock ref 'refs/tags/one': 'refs/tags/one/two' exists; cannot create 'refs/tags/one'
! [new tag]               one              -> one  (unable to update local ref)

This happens because git's --prune-tags flag only works on refs that match the provided refspecs. Since PR build refspecs target branches and pull request refs (not tags), tag pruning doesn't happen even though new tags are still fetched.

Solution

Add git remote prune origin before fetching when we have specific refspecs and fetchTags is enabled, to clean up conflicting tag refs that would prevent new tags from being created.

Changes Made

  1. Added GitRemotePrune method to both GitCommandManager (interface + implementation) and GitCliManager
  2. Modified GitFetch methods in both classes to call GitRemotePrune before fetch when:
    • Specific refspecs are provided (refSpec is not null and has items)
    • Tag fetching is enabled (fetchTags is true)
    • Tag pruning is enabled (same conditions as existing --prune-tags)
  3. Added test to verify GitRemotePrune is called for PR builds

Behavior

  • Normal branch builds: No change - uses existing fetch behavior
  • PR builds with tags: Now runs git remote prune origin before git fetch to clean up conflicting tag refs
  • Error handling: If git remote prune fails, it logs the exit code but continues with fetch (non-breaking)

This is a minimal, surgical fix that addresses the specific git behavior while maintaining full backward compatibility.

Fixes #5170.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 03nvsblobprodwcus014.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 055vsblobprodwcus02.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 1g1vsblobprodwcus01.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 3eavsblobprodwcus03.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 3zgvsblobprodwcus024.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 40qvsblobprodwcus022.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 4fdvsblobprodwcus012.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 5tbvsblobprodwcus06.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 61cvsblobprodwcus010.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 73dvsblobprodwcus07.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 8usvsblobprodwcus011.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • 9owvsblobprodwcus020.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • arpvsblobprodwcus017.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • crcvsblobprodwcus021.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • fslvsblobprodwcus015.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • grnvsblobprodwcus04.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • uubvsblobprodwcus018.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • vwmvsblobprodwcus016.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: sanjuyadav24 <185911972+sanjuyadav24@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [BUG]: checkout not pruning tags when fetching a specific ref Fix git tag pruning when fetching with specific refspecs Jun 24, 2025
@Copilot Copilot AI requested a review from sanjuyadav24 June 24, 2025 17:16
Copilot finished work on behalf of sanjuyadav24 June 24, 2025 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: checkout not pruning tags when fetching a specific ref
2 participants