Skip to content

Commit

Permalink
(minor) Add fetch depth option
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoberstar committed Apr 22, 2023
1 parent 61c477a commit d3af195
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/modules/ROOT/pages/grgit-fetch.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ grgit.fetch()

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

[source, groovy]
Expand All @@ -22,6 +22,7 @@ grgit.fetch {
remote = '<name or uri>'
refspecs = [<refspec>, ...]
prune = <boolean>
depth = <int>
tagMode = <mode>
}
----
Expand All @@ -43,6 +44,7 @@ The format of a <refspec> parameter is an optional plus +, followed by the sourc
+
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.
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.
depth: (`Integer`, default `null`) If set, limits fetching to the specified number of commits from the tip each remote branch history
tagMode:: (`String`, default `auto`) Must be one of `'auto'`, `'all'`, `'none'`.
+
`'auto'` - tags that point at objects that are downloaded from the remote repository are fetched and stored locally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class FetchOp implements Callable<Void> {
*/
boolean prune = false

/**
* The depth of the clone. Defaults to full history.
*/
Integer depth = null

/**
* How should tags be handled.
*/
Expand All @@ -58,6 +63,7 @@ class FetchOp implements Callable<Void> {
cmd.refSpecs = refSpecs.collect { new RefSpec(it) }
cmd.removeDeletedRefs = prune
cmd.tagOpt = tagMode.jgit
if (depth) { cmd.depth = depth }
cmd.call()
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package org.ajoberstar.grgit.operation
import org.ajoberstar.grgit.Grgit
import org.ajoberstar.grgit.fixtures.GitTestUtil
import org.ajoberstar.grgit.fixtures.MultiGitOpSpec
import org.ajoberstar.grgit.operation.FetchOp.TagMode
import org.eclipse.jgit.api.errors.GitAPIException

import spock.lang.Unroll

class FetchOpSpec extends MultiGitOpSpec {
Expand Down Expand Up @@ -106,4 +104,16 @@ class FetchOpSpec extends MultiGitOpSpec {
'refs/remotes/origin/master',
'refs/remotes/origin/my-branch']
}

def 'fetch with depth does a shallow fetch'() {
given:
def shallowGrgit = init('shallow')
shallowGrgit.remote.add(name: 'origin', url: remoteGrgit.repository.rootDir.toURI())
when:
shallowGrgit.fetch(remote: 'origin', depth: 1)
shallowGrgit.checkout(branch: 'master', createBranch: true, startPoint: 'origin/master')
then:
shallowGrgit.head().id == remoteGrgit.resolve.toCommit('master').id
shallowGrgit.head().parentIds.isEmpty()
}
}

0 comments on commit d3af195

Please sign in to comment.