Skip to content

Commit 7aa5d85

Browse files
author
Sven
authored
feat: add git_submodule_clone (#611)
Closes #609
1 parent a9c8abb commit 7aa5d85

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

libgit2-sys/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,11 @@ extern "C" {
23242324
) -> c_int;
23252325
pub fn git_submodule_add_to_index(submodule: *mut git_submodule, write_index: c_int) -> c_int;
23262326
pub fn git_submodule_branch(submodule: *mut git_submodule) -> *const c_char;
2327+
pub fn git_submodule_clone(
2328+
repo: *mut *mut git_repository,
2329+
submodule: *mut git_submodule,
2330+
opts: *const git_submodule_update_options,
2331+
) -> c_int;
23272332
pub fn git_submodule_foreach(
23282333
repo: *mut git_repository,
23292334
callback: git_submodule_cb,

src/submodule.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ impl<'repo> Submodule<'repo> {
3333
unsafe { crate::opt_bytes(self, raw::git_submodule_branch(self.raw)) }
3434
}
3535

36+
/// Perform the clone step for a newly created submodule.
37+
///
38+
/// This performs the necessary `git_clone` to setup a newly-created submodule.
39+
pub fn clone(
40+
&mut self,
41+
opts: Option<&mut SubmoduleUpdateOptions<'_>>,
42+
) -> Result<Repository, Error> {
43+
unsafe {
44+
let raw_opts = opts.map(|o| o.raw());
45+
let mut raw_repo = ptr::null_mut();
46+
try_call!(raw::git_submodule_clone(
47+
&mut raw_repo,
48+
self.raw,
49+
raw_opts.as_ref()
50+
));
51+
Ok(Binding::from_raw(raw_repo))
52+
}
53+
}
54+
3655
/// Get the submodule's url.
3756
///
3857
/// Returns `None` if the url is not valid utf-8 or if the URL isn't present
@@ -360,4 +379,26 @@ mod tests {
360379
t!(submodule.update(init, opts));
361380
}
362381
}
382+
383+
#[test]
384+
fn clone_submodule() {
385+
// -----------------------------------
386+
// Same as `add_a_submodule()`
387+
let (_td, repo1) = crate::test::repo_init();
388+
let (_td, repo2) = crate::test::repo_init();
389+
let (_td, parent) = crate::test::repo_init();
390+
391+
let url1 = Url::from_file_path(&repo1.workdir().unwrap()).unwrap();
392+
let url3 = Url::from_file_path(&repo2.workdir().unwrap()).unwrap();
393+
let mut s1 = parent
394+
.submodule(&url1.to_string(), Path::new("bar"), true)
395+
.unwrap();
396+
let mut s2 = parent
397+
.submodule(&url3.to_string(), Path::new("bar2"), true)
398+
.unwrap();
399+
// -----------------------------------
400+
401+
t!(s1.clone(Some(&mut SubmoduleUpdateOptions::default())));
402+
t!(s2.clone(None));
403+
}
363404
}

0 commit comments

Comments
 (0)