Skip to content

Commit c9250b4

Browse files
committed
Add shallow cloneing capability
1 parent 4570c39 commit c9250b4

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/build.rs

+23
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,29 @@ impl<'cb> RepoBuilder<'cb> {
171171
self
172172
}
173173

174+
/// Specify fetch depth, a value less than zero is interpreted as pull everything (effectively the same as no declaring a limit depth)
175+
pub fn depth(&mut self, depth: i32) -> &mut RepoBuilder<'cb> {
176+
if !self.fetch_opts
177+
.as_mut()
178+
.is_some_and(|fetch_opts| {
179+
fetch_opts.depth(depth); true
180+
}) {
181+
let mut fo = FetchOptions::new();
182+
fo.depth(depth);
183+
self.fetch_options(fo);
184+
}
185+
186+
self
187+
// if let Some(ref fetch_opts) = &self.fetch_opts {
188+
// fetch_opts.depth(depth);
189+
// } else {
190+
// let mut fo = FetchOptions::new();
191+
// fo.depth(depth);
192+
// self.fetch_options(fo);
193+
// }
194+
// self
195+
}
196+
174197
/// Specify the name of the branch to check out after the clone.
175198
///
176199
/// If not specified, the remote's default branch will be used.

src/remote.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct RemoteHead<'remote> {
4141
/// Options which can be specified to various fetch operations.
4242
pub struct FetchOptions<'cb> {
4343
callbacks: Option<RemoteCallbacks<'cb>>,
44+
depth: i32,
4445
proxy: Option<ProxyOptions<'cb>>,
4546
prune: FetchPrune,
4647
update_fetchhead: bool,
@@ -509,6 +510,7 @@ impl<'cb> FetchOptions<'cb> {
509510
follow_redirects: RemoteRedirect::Initial,
510511
custom_headers: Vec::new(),
511512
custom_headers_ptrs: Vec::new(),
513+
depth: 0 // Not limited depth
512514
}
513515
}
514516

@@ -538,6 +540,12 @@ impl<'cb> FetchOptions<'cb> {
538540
self
539541
}
540542

543+
/// Set fetch depth, a value less than zero is interpreted as pull everything (effectively the same as no declaring a limit depth)
544+
pub fn depth(&mut self, depth: i32) -> &mut Self {
545+
self.depth = depth;
546+
self
547+
}
548+
541549
/// Set how to behave regarding tags on the remote, such as auto-downloading
542550
/// tags for objects we're downloading or downloading all of them.
543551
///
@@ -590,7 +598,7 @@ impl<'cb> Binding for FetchOptions<'cb> {
590598
prune: crate::call::convert(&self.prune),
591599
update_fetchhead: crate::call::convert(&self.update_fetchhead),
592600
download_tags: crate::call::convert(&self.download_tags),
593-
depth: 0, // GIT_FETCH_DEPTH_FULL.
601+
depth: self.depth,
594602
follow_redirects: self.follow_redirects.raw(),
595603
custom_headers: git_strarray {
596604
count: self.custom_headers_ptrs.len(),

src/repo.rs

+7
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ impl Repository {
341341
Ok(repo)
342342
}
343343

344+
/// Clone a remote repository with a depth of one (shallow cloning)
345+
///
346+
/// Similar to `git clone --depth 1`
347+
pub fn shallow_clone<P: AsRef<Path>>(url: &str, into: P) -> Result<Repository, Error> {
348+
RepoBuilder::new().depth(1).clone(url, into.as_ref())
349+
}
350+
344351
/// Attempt to wrap an object database as a repository.
345352
pub fn from_odb(odb: Odb<'_>) -> Result<Repository, Error> {
346353
crate::init();

0 commit comments

Comments
 (0)