Skip to content

Conversation

@pstreef
Copy link
Contributor

@pstreef pstreef commented Jan 6, 2026

Problem

When cloning repositories with many refs (250K+ tags/branches), JGit sends "have" lines for all local objects during fetch negotiation. This creates massive request payloads that cause RSocket fragmentation issues.

Solution

Add shallow clone support (--depth 1) which:

  • Only fetches the tip commit(s)
  • Sends zero "have" lines on initial clone (no local objects)
  • Keeps subsequent fetch payloads tiny (only 1-2 commits to report)

API

Git.cloneRepository()
    .setURI("https://github.com/example/repo.git")
    .setDepth(1)
    .call();

Changes

File Description
Transport.java Add depth, deepenSince, deepenNots fields with accessors
CloneCommand.java Add setDepth(), setShallowSince(), addShallowExclude()
FetchCommand.java Add setDepth(), setShallowSince(), addShallowExclude()
BasePackFetchConnection.java Send deepen commands, handle shallow-info response, write .git/shallow
FetchProcess.java Skip completeness check for shallow clones
GitProtocolConstants.java Add SECTION_SHALLOW_INFO constant

Testing

Tested manually against GitHub:

Clone completed in 2921ms
HEAD ref: 4b235690d4ecc37b3ebedc0fe52235ab95eaff11
src/ exists: true
Shallow file exists: true
SUCCESS: Shallow clone worked!

Notes

This is a simplified implementation compared to official JGit 6.x:

  • Supports initial shallow clone (primary use case)
  • Does not support --unshallow or deepening existing shallow clones
  • Writes directly to .git/shallow file (vs ObjectDatabase API)

Adds API methods to perform shallow clones which only fetch a limited
commit history. This is useful for repositories with many refs (250K+
tags/branches) where full clones cause large request payloads.

Changes:
- Transport: Add depth field with getter/setter
- CloneCommand: Add setDepth()
- FetchCommand: Add setDepth()
- BasePackFetchConnection: Send deepen command, handle shallow-info
  response section, write .git/shallow file
- FetchProcess: Skip completeness check for shallow clones
- GitProtocolConstants: Add SECTION_SHALLOW_INFO constant

Usage:
  Git.cloneRepository()
      .setURI("https://github.com/example/repo.git")
      .setDepth(1)
      .call();
@pstreef pstreef force-pushed the feat/shallow-clone branch from 88a3de9 to f0c72f7 Compare January 6, 2026 14:58
@pstreef
Copy link
Contributor Author

pstreef commented Jan 13, 2026

We wont be adding this at this time.

@pstreef pstreef closed this Jan 13, 2026
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants