From c28aafac50643f602c0e2620ac7d030bb7e50761 Mon Sep 17 00:00:00 2001 From: Skia Date: Wed, 18 Jan 2023 11:33:39 +0100 Subject: [PATCH] Use 'repo_init' and 'clone' to initialize unexisting submodule repository Following https://github.com/rust-lang/git2-rs/pull/914, we can now use `repo_init`, which gives us a real repository that we can clone to populate it. --- Cargo.lock | 14 ++++++-------- Cargo.toml | 4 +++- src/main.rs | 48 ++++++++++-------------------------------------- 3 files changed, 19 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d0060e..4a506dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,9 +127,8 @@ dependencies = [ [[package]] name = "git2" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0155506aab710a86160ddb504a480d2964d7ab5b9e62419be69e0032bc5931c" +version = "0.16.0" +source = "git+https://github.com/Hyask/git2-rs?branch=master#f587915c99faaeb93d2e5e6e417ad6d6b6792466" dependencies = [ "bitflags", "libc", @@ -205,9 +204,8 @@ checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] name = "libgit2-sys" -version = "0.13.4+1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0fa6563431ede25f5cc7f6d803c6afbc1c5d3ad3d4925d12c882bf2b526f5d1" +version = "0.14.1+1.5.0" +source = "git+https://github.com/Hyask/git2-rs?branch=master#f587915c99faaeb93d2e5e6e417ad6d6b6792466" dependencies = [ "cc", "libc", @@ -294,9 +292,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-src" -version = "111.22.0+1.1.1q" +version = "111.24.0+1.1.1s" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f31f0d509d1c1ae9cada2f9539ff8f37933831fd5098879e482aa687d659853" +checksum = "3498f259dab01178c6228c6b00dcef0ed2a2d5e20d648c017861227773ea4abd" dependencies = [ "cc", ] diff --git a/Cargo.toml b/Cargo.toml index 5f47d9f..f2db409 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,9 @@ edition = "2021" [dependencies] anyhow = "1.0.58" clap = { version = "3.2.6", features = ["derive"] } -git2 = { version = "0.14.4", features = ["vendored-libgit2", "vendored-openssl"] } +# git2 = { version = "0.14.4", features = ["vendored-libgit2", "vendored-openssl"] } +# TODO reset this to something like the line above when https://github.com/rust-lang/git2-rs/pull/914 will be merged +git2 = { git = "https://github.com/Hyask/git2-rs", branch = "master", features = ["vendored-libgit2", "vendored-openssl"] } owo-colors = "3.4.0" serde = { version = "1.0.137", features = ["derive"] } tokio = { version = "1.19.2", features = ["rt-multi-thread", "process", "macros", "sync", "time"] } diff --git a/src/main.rs b/src/main.rs index 0c14b6d..0c9858e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -479,47 +479,19 @@ fn checkout_repo( let submodule = match submodule.open() { Ok(submodule) => submodule, Err(_) => { - // git init the submodule in .git/modules/ of the parent repository - let mut init_opts = git2::RepositoryInitOptions::new(); - init_opts.no_reinit(false).no_dotgit_dir(true); - let workdir = repository - .workdir() - .context("Could not get parent workdir")?; - init_opts.workdir_path(&workdir.join(&submodule_path)); - let gitdir = repository - .path() - .join(Path::new("modules")) - .join(&submodule_path); - std::fs::create_dir_all(&gitdir).context("Could not create submodule path")?; // RepositoryInitOptions.mkpath() doesn't seem to work, let's help it - git2::Repository::init_opts(gitdir, &init_opts).context("Could not init submodule")?; - - // update the submodule + // Init submodule + submodule + .repo_init(true) + .context("Could not initialize submodule repository")?; + + // Then clone it let mut options = git2::SubmoduleUpdateOptions::new(); options.fetch(ssh_agent_fetch_options()); options.allow_fetch(false); - let fetch_result = retry_if_net(|| submodule.update(true, Some(&mut options))); - - let fetch_result = if let Err(error) = &fetch_result { - // ignore if the update failed due to missing commit: we'll try fetching it again later - if matches!(error.class(), git2::ErrorClass::Odb) - && matches!(error.code(), git2::ErrorCode::NotFound) - { - Ok(()) - } else { - fetch_result - } - } else { - fetch_result - }; - - fetch_result.context("Could not update submodule")?; - // Set the origin remote - retry_if_locked(|| submodule.sync()) - .context("Could not sync submodule") - .unwrap(); - - submodule.open().context("Could not open submodule")? + submodule + .clone(Some(&mut options)) + .context("Could not clone submodule")? } }; @@ -738,7 +710,7 @@ fn ssh_agent_fetch_options() -> git2::FetchOptions<'static> { }); cbs.certificate_check(|_cert, _s| { tracing::warn!(%_s, "Ignoring certificate"); - true + Ok(git2::CertificateCheckStatus::CertificateOk) }); let mut fetch_opts = git2::FetchOptions::new(); fetch_opts.remote_callbacks(cbs);