v3.1.0
Sparse checkout
Sparse checkout allows to create only necessary files in the directory of the repository.
To make a sparse checkout, two steps should be performed:
- A clone operation with
--sparseflag enabled; - Specifying the directories that are needed to be checked out (after the clone);
Git.framework makes it easy to perform such operation:
let options = GitCloneOptions()
options.branches = .single(named: "main")
options.checkout = .noCheckout
options.sparse = .sparse
try repo.clone(atPath: clonePath, options: options)
guard let reference = try repo.listReferences().references.first else { return }
try repo.sparseCheckoutAdd(files: ["config"])
try repo.checkout(reference: reference)In the shown example, only the directory config will be checked out in the working copy.
Partial clone
Partial clone feature allows to drastically reduce the size of the downloading repository by filtering out some data during the clone operation. This can be helpful for instance during CI builds when only a part of repository is needed. Another option is to quickly clone the repo, modify a single file and then commit it to the remote.
During the clone operation the new filter option can be specified:
let options = GitCloneOptions()
options.filter = .omitAllBlobsOr the custom filter:
let options = GitCloneOptions()
options.filter = .custom("combine:tree:3+blob:none")With the combine of shallow clone (that was already implemented in the previous versions) + sparse checkout + partial clone it is possible to achieve great size optimisation during the clone operation:
let options = GitCloneOptions()
// Limit the history of the clone to a single commit
options.depth = .head
// Limit the list of branches to a single branch
options.branches = .single(named: "main")
// Do not perform a checkout
options.checkout = .noCheckout
// Indicate that the sparse checkout will be made
options.sparse = .sparse
// Omit BLOB data during the clone
options.filter = .omitAllBlobsTo rewrite this to the git command line:
git clone --depth=1 --no-checkout --sparse --filter=blob:none --single-branch --branch main