Fix refspec for shallow clones to use single-branch instead of wildcard #2229
      
        
          +157
        
        
          −16
        
        
          
        
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Problem
When performing a shallow clone with
gix clone --depth 1, gitoxide was fetching all branches using the wildcard refspec+refs/heads/*:refs/remotes/origin/*, resulting in significantly larger repositories compared to Git. For example, cloning the gitoxide repository itself produced a ~130MB.gitdirectory with gitoxide vs ~70MB with Git.Root Cause
The refspec in
gix/src/clone/fetch/mod.rswas hardcoded to always use the wildcard pattern+refs/heads/*:refs/remotes/origin/*, regardless of whether the clone was shallow or not. This meant all branches were fetched and stored, even though shallow clones typically only need a single branch.Solution
Modified the clone logic to detect shallow clones and automatically use single-branch refspecs when appropriate, matching Git's behavior:
ref_nameis specified: Uses a refspec targeting that specific branch (e.g.,+refs/heads/feature:refs/remotes/origin/feature)ref_nameis not specified: Attempts to detect the remote's default branch:init.defaultBranchconfig or defaults to "main"+refs/heads/main:refs/remotes/origin/maininstead of wildcardThis only applies when no custom
configure_remotecallback is provided, preserving flexibility for advanced use cases.Results
.git/configfor future fetchesNote: The remaining size difference with Git (49MB vs 6.6MB) is due to tag fetching behavior - gitoxide currently fetches all tags by default while Git doesn't for shallow clones. This is a separate behavioral difference that could be addressed in a future PR.
Fixes #2227
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.