Skip to content

feat: add git_submodule_clone #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,11 @@ extern "C" {
) -> c_int;
pub fn git_submodule_add_to_index(submodule: *mut git_submodule, write_index: c_int) -> c_int;
pub fn git_submodule_branch(submodule: *mut git_submodule) -> *const c_char;
pub fn git_submodule_clone(
repo: *mut *mut git_repository,
submodule: *mut git_submodule,
opts: *const git_submodule_update_options,
) -> c_int;
pub fn git_submodule_foreach(
repo: *mut git_repository,
callback: git_submodule_cb,
Expand Down
41 changes: 41 additions & 0 deletions src/submodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ impl<'repo> Submodule<'repo> {
unsafe { crate::opt_bytes(self, raw::git_submodule_branch(self.raw)) }
}

/// Perform the clone step for a newly created submodule.
///
/// This performs the necessary `git_clone` to setup a newly-created submodule.
pub fn clone(
&mut self,
opts: Option<&mut SubmoduleUpdateOptions<'_>>,
) -> Result<Repository, Error> {
unsafe {
let raw_opts = opts.map(|o| o.raw());
let mut raw_repo = ptr::null_mut();
try_call!(raw::git_submodule_clone(
&mut raw_repo,
self.raw,
raw_opts.as_ref()
));
Ok(Binding::from_raw(raw_repo))
}
}

/// Get the submodule's url.
///
/// Returns `None` if the url is not valid utf-8 or if the URL isn't present
Expand Down Expand Up @@ -360,4 +379,26 @@ mod tests {
t!(submodule.update(init, opts));
}
}

#[test]
fn clone_submodule() {
// -----------------------------------
// Same as `add_a_submodule()`
let (_td, repo1) = crate::test::repo_init();
let (_td, repo2) = crate::test::repo_init();
let (_td, parent) = crate::test::repo_init();

let url1 = Url::from_file_path(&repo1.workdir().unwrap()).unwrap();
let url3 = Url::from_file_path(&repo2.workdir().unwrap()).unwrap();
let mut s1 = parent
.submodule(&url1.to_string(), Path::new("bar"), true)
.unwrap();
let mut s2 = parent
.submodule(&url3.to_string(), Path::new("bar2"), true)
.unwrap();
// -----------------------------------

t!(s1.clone(Some(&mut SubmoduleUpdateOptions::default())));
t!(s2.clone(None));
}
}