Skip to content

Commit

Permalink
Use 'repo_init' and 'clone' to initialize unexisting submodule reposi…
Browse files Browse the repository at this point in the history
…tory

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.
  • Loading branch information
Skia committed Jan 18, 2023
1 parent 81da106 commit c28aafa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 47 deletions.
14 changes: 6 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
48 changes: 10 additions & 38 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,47 +479,19 @@ fn checkout_repo(
let submodule = match submodule.open() {
Ok(submodule) => submodule,
Err(_) => {
// git init the submodule in .git/modules/<submodule_path> 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")?
}
};

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c28aafa

Please sign in to comment.