Skip to content

Commit 35f6084

Browse files
committed
Remove convenience functions + first try at a test
1 parent a4a5b8b commit 35f6084

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

src/build.rs

-22
Original file line numberDiff line numberDiff line change
@@ -171,28 +171,6 @@ 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.as_mut().is_some_and(|fetch_opts| {
177-
fetch_opts.depth(depth);
178-
true
179-
}) {
180-
let mut fo = FetchOptions::new();
181-
fo.depth(depth);
182-
self.fetch_options(fo);
183-
}
184-
185-
self
186-
// if let Some(ref fetch_opts) = &self.fetch_opts {
187-
// fetch_opts.depth(depth);
188-
// } else {
189-
// let mut fo = FetchOptions::new();
190-
// fo.depth(depth);
191-
// self.fetch_options(fo);
192-
// }
193-
// self
194-
}
195-
196174
/// Specify the name of the branch to check out after the clone.
197175
///
198176
/// If not specified, the remote's default branch will be used.

src/remote.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,12 @@ impl<'cb> FetchOptions<'cb> {
540540
self
541541
}
542542

543-
/// Set fetch depth, a value less than zero is interpreted as pull everything (effectively the same as no declaring a limit depth)
543+
/// Set fetch depth, a value less or equal to 0 is interpreted as pull everything (effectively the same as not declaring a limit depth)
544544
pub fn depth(&mut self, depth: i32) -> &mut Self {
545+
if depth.is_negative() {
546+
self.depth = 0;
547+
return self;
548+
}
545549
self.depth = depth;
546550
self
547551
}
@@ -752,6 +756,7 @@ impl RemoteRedirect {
752756

753757
#[cfg(test)]
754758
mod tests {
759+
use crate::build::RepoBuilder;
755760
use crate::{AutotagOption, PushOptions};
756761
use crate::{Direction, FetchOptions, Remote, RemoteCallbacks, Repository};
757762
use std::cell::Cell;
@@ -1115,4 +1120,43 @@ mod tests {
11151120
assert_eq!(flag, 7);
11161121
assert_eq!(remote_repo.branches(None).unwrap().count(), 3);
11171122
}
1123+
1124+
#[test]
1125+
fn shallow_clone() {
1126+
let (td, repo) = crate::test::repo_init();
1127+
1128+
let commit = repo.head().unwrap().target().unwrap();
1129+
let first_commit = repo.find_commit(commit).unwrap();
1130+
1131+
let sig = repo.signature().unwrap();
1132+
let mut index = repo.index().unwrap();
1133+
let tree = index.write_tree().unwrap();
1134+
let tree = repo.find_tree(tree).unwrap();
1135+
1136+
let second_commit = repo.commit(
1137+
Some("HEAD"),
1138+
&sig,
1139+
&sig,
1140+
"second commit",
1141+
&tree,
1142+
&[&first_commit]
1143+
).unwrap();
1144+
1145+
let td2 = TempDir::new().unwrap();
1146+
let mut fo = FetchOptions::new();
1147+
fo.depth(1);
1148+
1149+
let new_repo = RepoBuilder::new()
1150+
.fetch_options(fo)
1151+
.clone(&td.into_path().as_os_str().to_str().unwrap(), td2.path())
1152+
.unwrap();
1153+
1154+
dbg!(new_repo.is_shallow());
1155+
1156+
let _ = dbg!(new_repo.find_commit(first_commit.id())); // ????
1157+
let _ = dbg!(new_repo.find_commit(second_commit));
1158+
1159+
// Check that there's only one commit
1160+
panic!();
1161+
}
11181162
}

src/repo.rs

-7
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,6 @@ 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-
351344
/// Attempt to wrap an object database as a repository.
352345
pub fn from_odb(odb: Odb<'_>) -> Result<Repository, Error> {
353346
crate::init();

0 commit comments

Comments
 (0)