27

How I can do

git clone --depth 1 ...

with JGIT library?

Timothy Klim
  • 1,257
  • 15
  • 25

2 Answers2

17

You can't, JGit doesn't yet support shallow clones on the client side (it does on the server side, though).

sschuberth
  • 28,386
  • 6
  • 101
  • 146
Matthias Sohn
  • 339
  • 2
  • 4
  • 2
    this bug indicates that this is available now. Is it the case? https://bugs.eclipse.org/bugs/show_bug.cgi?id=301627 – maxmelbin Oct 17 '12 at 04:31
  • 1
    Anyone found a way to do this? – Doug Dec 02 '12 at 08:01
  • 2
    Hmm, Jgit still doesn't support shallow clone as of this post. If you're like me and wondered how the Jenkins plugin offers a shallow option, then you also wouldn't be surprised to hear that those guys implemented an abstraction that *falls back* to jgit when linus git isn't available. I don't know how to use this outside of jenkins but here's the part with the abstraction: https://github.com/jenkinsci/git-client-plugin/blob/master/src/main/java/org/jenkinsci/plugins/gitclient/CloneCommand.java. – nelsonjchen Jul 05 '14 at 05:34
  • 2
    To clarify @maxmelbin 's comment: the mentioned bug is about the server implementation inside JGit, the server now supports shallow fetch if requested (see comment #13). `git clone --depth` however is **not** implemented yet, see [bug 475615: Support "git clone --depth" in CloneCommand](https://bugs.eclipse.org/bugs/show_bug.cgi?id=475615) – Rüdiger Herrmann Nov 26 '15 at 08:31
  • There is a WIP implementation of this [on GitHub](https://github.com/timeraider4u/jgit/tree/depth). – sdgfsdh Apr 04 '17 at 09:35
  • @sdgfsdh do you have an example? – supritshah1289 Jun 12 '18 at 00:59
  • By now https://bugs.eclipse.org/bugs/show_bug.cgi?id=475615 has been resolved and JGit 6.3.0 does support shallow clones / fetches on the client side. – sschuberth Sep 14 '22 at 04:54
  • Some 11 years after the question was originally posed, it looks like an implementation now exists, in the (as yet unreleased) 6.5. Here's the PR: https://git.eclipse.org/r/c/jgit/jgit/+/196477 – Marty Pitt Feb 17 '23 at 09:35
2

According to the Release notes for version 6.5.0, this has been released on March 15, 2023.

You can now use any of

  • .setDepth(Integer)
  • .setShallowSince(Instant)
  • .setShallowSince(OffsetDateTime)

for example like so:

Git.cloneRepository().setDepth(1)
jonny
  • 4,264
  • 4
  • 22
  • 29