Skip to content

Commit

Permalink
Add shallow cloneing capability
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Aug 8, 2023
1 parent 7f21181 commit 67676d2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ impl<'cb> RepoBuilder<'cb> {
self
}

/// Specify fetch depth, a value less than zero is interpreted as pull everything (effectively the same as no declaring a limit depth)
pub fn depth(&mut self, depth: i32) -> &mut RepoBuilder<'cb> {
if !self.fetch_opts
.as_mut()
.is_some_and(|fetch_opts| {
fetch_opts.depth(depth); true
}) {
let mut fo = FetchOptions::new();
fo.depth(depth);
self.fetch_options(fo);
}

self
// if let Some(ref fetch_opts) = &self.fetch_opts {
// fetch_opts.depth(depth);
// } else {
// let mut fo = FetchOptions::new();
// fo.depth(depth);
// self.fetch_options(fo);
// }
// self
}

/// Specify the name of the branch to check out after the clone.
///
/// If not specified, the remote's default branch will be used.
Expand Down
10 changes: 9 additions & 1 deletion src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct RemoteHead<'remote> {
/// Options which can be specified to various fetch operations.
pub struct FetchOptions<'cb> {
callbacks: Option<RemoteCallbacks<'cb>>,
depth: i32,
proxy: Option<ProxyOptions<'cb>>,
prune: FetchPrune,
update_fetchhead: bool,
Expand Down Expand Up @@ -509,6 +510,7 @@ impl<'cb> FetchOptions<'cb> {
follow_redirects: RemoteRedirect::Initial,
custom_headers: Vec::new(),
custom_headers_ptrs: Vec::new(),
depth: 0 // Not limited depth
}
}

Expand Down Expand Up @@ -538,6 +540,12 @@ impl<'cb> FetchOptions<'cb> {
self
}

/// Set fetch depth, a value less than zero is interpreted as pull everything (effectively the same as no declaring a limit depth)
pub fn depth(&mut self, depth: i32) -> &mut Self {
self.depth = depth;
self
}

/// Set how to behave regarding tags on the remote, such as auto-downloading
/// tags for objects we're downloading or downloading all of them.
///
Expand Down Expand Up @@ -590,7 +598,7 @@ impl<'cb> Binding for FetchOptions<'cb> {
prune: crate::call::convert(&self.prune),
update_fetchhead: crate::call::convert(&self.update_fetchhead),
download_tags: crate::call::convert(&self.download_tags),
depth: 0, // GIT_FETCH_DEPTH_FULL.
depth: self.depth,
follow_redirects: self.follow_redirects.raw(),
custom_headers: git_strarray {
count: self.custom_headers_ptrs.len(),
Expand Down
7 changes: 7 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ impl Repository {
Ok(repo)
}

/// Clone a remote repository with a depth of one (shallow cloning)
///
/// Similar to `git clone --depth 1`
pub fn shallow_clone<P: AsRef<Path>>(url: &str, into: P) -> Result<Repository, Error> {
RepoBuilder::new().depth(1).clone(url, into.as_ref())
}

/// Attempt to wrap an object database as a repository.
pub fn from_odb(odb: Odb<'_>) -> Result<Repository, Error> {
crate::init();
Expand Down

0 comments on commit 67676d2

Please sign in to comment.