Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit 2a4d1d7

Browse files
authored
Merge pull request #387 from ajoberstar/shallow-fetch
(minor) Add fetch depth option
2 parents 61c477a + d3af195 commit 2a4d1d7

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

docs/modules/ROOT/pages/grgit-fetch.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ grgit.fetch()
1313

1414
[source, groovy]
1515
----
16-
grgit.fetch(remote: '<name or uri>', refSpecs: [<refspec>, ...], prune: <boolean>, tagMode: <mode>)
16+
grgit.fetch(remote: '<name or uri>', refSpecs: [<refspec>, ...], prune: <boolean>, depth: <int>, tagMode: <mode>)
1717
----
1818

1919
[source, groovy]
@@ -22,6 +22,7 @@ grgit.fetch {
2222
remote = '<name or uri>'
2323
refspecs = [<refspec>, ...]
2424
prune = <boolean>
25+
depth = <int>
2526
tagMode = <mode>
2627
}
2728
----
@@ -43,6 +44,7 @@ The format of a <refspec> parameter is an optional plus +, followed by the sourc
4344
+
4445
The remote ref that matches <src> is fetched, and if <dst> is not empty string, the local ref that matches it is fast-forwarded using <src>. If the optional plus + is used, the local ref is updated even if it does not result in a fast-forward update.
4546
prune:: (`boolean`, default `false`) Before fetching, remove any remote-tracking references that no longer exist on the remote. Tags are not subject to pruning if they are fetched only because of the default tag auto-following or due to a `tagMode` option. However, if tags are fetched due to an explicit refspec, then they are also subject to pruning.
47+
depth: (`Integer`, default `null`) If set, limits fetching to the specified number of commits from the tip each remote branch history
4648
tagMode:: (`String`, default `auto`) Must be one of `'auto'`, `'all'`, `'none'`.
4749
+
4850
`'auto'` - tags that point at objects that are downloaded from the remote repository are fetched and stored locally.

grgit-core/src/main/groovy/org/ajoberstar/grgit/operation/FetchOp.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class FetchOp implements Callable<Void> {
3535
*/
3636
boolean prune = false
3737

38+
/**
39+
* The depth of the clone. Defaults to full history.
40+
*/
41+
Integer depth = null
42+
3843
/**
3944
* How should tags be handled.
4045
*/
@@ -58,6 +63,7 @@ class FetchOp implements Callable<Void> {
5863
cmd.refSpecs = refSpecs.collect { new RefSpec(it) }
5964
cmd.removeDeletedRefs = prune
6065
cmd.tagOpt = tagMode.jgit
66+
if (depth) { cmd.depth = depth }
6167
cmd.call()
6268
return null
6369
}

grgit-core/src/test/groovy/org/ajoberstar/grgit/operation/FetchOpSpec.groovy

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package org.ajoberstar.grgit.operation
33
import org.ajoberstar.grgit.Grgit
44
import org.ajoberstar.grgit.fixtures.GitTestUtil
55
import org.ajoberstar.grgit.fixtures.MultiGitOpSpec
6-
import org.ajoberstar.grgit.operation.FetchOp.TagMode
76
import org.eclipse.jgit.api.errors.GitAPIException
8-
97
import spock.lang.Unroll
108

119
class FetchOpSpec extends MultiGitOpSpec {
@@ -106,4 +104,16 @@ class FetchOpSpec extends MultiGitOpSpec {
106104
'refs/remotes/origin/master',
107105
'refs/remotes/origin/my-branch']
108106
}
107+
108+
def 'fetch with depth does a shallow fetch'() {
109+
given:
110+
def shallowGrgit = init('shallow')
111+
shallowGrgit.remote.add(name: 'origin', url: remoteGrgit.repository.rootDir.toURI())
112+
when:
113+
shallowGrgit.fetch(remote: 'origin', depth: 1)
114+
shallowGrgit.checkout(branch: 'master', createBranch: true, startPoint: 'origin/master')
115+
then:
116+
shallowGrgit.head().id == remoteGrgit.resolve.toCommit('master').id
117+
shallowGrgit.head().parentIds.isEmpty()
118+
}
109119
}

0 commit comments

Comments
 (0)