Skip to content

Commit c28aafa

Browse files
author
Skia
committed
Use 'repo_init' and 'clone' to initialize unexisting submodule repository
Following rust-lang/git2-rs#914, we can now use `repo_init`, which gives us a real repository that we can clone to populate it.
1 parent 81da106 commit c28aafa

File tree

3 files changed

+19
-47
lines changed

3 files changed

+19
-47
lines changed

Cargo.lock

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ edition = "2021"
88
[dependencies]
99
anyhow = "1.0.58"
1010
clap = { version = "3.2.6", features = ["derive"] }
11-
git2 = { version = "0.14.4", features = ["vendored-libgit2", "vendored-openssl"] }
11+
# git2 = { version = "0.14.4", features = ["vendored-libgit2", "vendored-openssl"] }
12+
# TODO reset this to something like the line above when https://github.com/rust-lang/git2-rs/pull/914 will be merged
13+
git2 = { git = "https://github.com/Hyask/git2-rs", branch = "master", features = ["vendored-libgit2", "vendored-openssl"] }
1214
owo-colors = "3.4.0"
1315
serde = { version = "1.0.137", features = ["derive"] }
1416
tokio = { version = "1.19.2", features = ["rt-multi-thread", "process", "macros", "sync", "time"] }

src/main.rs

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -479,47 +479,19 @@ fn checkout_repo(
479479
let submodule = match submodule.open() {
480480
Ok(submodule) => submodule,
481481
Err(_) => {
482-
// git init the submodule in .git/modules/<submodule_path> of the parent repository
483-
let mut init_opts = git2::RepositoryInitOptions::new();
484-
init_opts.no_reinit(false).no_dotgit_dir(true);
485-
let workdir = repository
486-
.workdir()
487-
.context("Could not get parent workdir")?;
488-
init_opts.workdir_path(&workdir.join(&submodule_path));
489-
let gitdir = repository
490-
.path()
491-
.join(Path::new("modules"))
492-
.join(&submodule_path);
493-
std::fs::create_dir_all(&gitdir).context("Could not create submodule path")?; // RepositoryInitOptions.mkpath() doesn't seem to work, let's help it
494-
git2::Repository::init_opts(gitdir, &init_opts).context("Could not init submodule")?;
495-
496-
// update the submodule
482+
// Init submodule
483+
submodule
484+
.repo_init(true)
485+
.context("Could not initialize submodule repository")?;
486+
487+
// Then clone it
497488
let mut options = git2::SubmoduleUpdateOptions::new();
498489
options.fetch(ssh_agent_fetch_options());
499490
options.allow_fetch(false);
500-
let fetch_result = retry_if_net(|| submodule.update(true, Some(&mut options)));
501-
502-
let fetch_result = if let Err(error) = &fetch_result {
503-
// ignore if the update failed due to missing commit: we'll try fetching it again later
504-
if matches!(error.class(), git2::ErrorClass::Odb)
505-
&& matches!(error.code(), git2::ErrorCode::NotFound)
506-
{
507-
Ok(())
508-
} else {
509-
fetch_result
510-
}
511-
} else {
512-
fetch_result
513-
};
514-
515-
fetch_result.context("Could not update submodule")?;
516491

517-
// Set the origin remote
518-
retry_if_locked(|| submodule.sync())
519-
.context("Could not sync submodule")
520-
.unwrap();
521-
522-
submodule.open().context("Could not open submodule")?
492+
submodule
493+
.clone(Some(&mut options))
494+
.context("Could not clone submodule")?
523495
}
524496
};
525497

@@ -738,7 +710,7 @@ fn ssh_agent_fetch_options() -> git2::FetchOptions<'static> {
738710
});
739711
cbs.certificate_check(|_cert, _s| {
740712
tracing::warn!(%_s, "Ignoring certificate");
741-
true
713+
Ok(git2::CertificateCheckStatus::CertificateOk)
742714
});
743715
let mut fetch_opts = git2::FetchOptions::new();
744716
fetch_opts.remote_callbacks(cbs);

0 commit comments

Comments
 (0)