diff --git a/Cargo.lock b/Cargo.lock index c630b2f8..b78ffa67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -237,6 +237,7 @@ version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" dependencies = [ + "jobserver", "libc", ] @@ -717,6 +718,19 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" +[[package]] +name = "git2" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf97ba92db08df386e10c8ede66a2a0369bd277090afd8710e19e38de9ec0cd" +dependencies = [ + "bitflags 2.4.1", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "h2" version = "0.3.21" @@ -978,6 +992,15 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +[[package]] +name = "jobserver" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.64" @@ -999,12 +1022,36 @@ version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" +[[package]] +name = "libgit2-sys" +version = "0.16.1+1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2a2bb3680b094add03bb3732ec520ece34da31a8cd2d633d1389d0f0fb60d0c" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + [[package]] name = "libm" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "libz-sys" +version = "1.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linked-hash-map" version = "0.5.6" @@ -2672,6 +2719,7 @@ version = "0.8.3" dependencies = [ "anyhow", "dirs", + "git2", "heck 0.4.1", "itertools", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index 061b25fc..bd1ed275 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ futures = "0.3" futures-util = "0.3" flate2 = "1" fxhash = "0.2" +git2 = { version = "0.18", default-features = false } h2 = "0.3" heck = "0.4" hex = "0.4" diff --git a/volo-build/Cargo.toml b/volo-build/Cargo.toml index f245c93e..025d071f 100644 --- a/volo-build/Cargo.toml +++ b/volo-build/Cargo.toml @@ -23,6 +23,7 @@ pilota-build.workspace = true anyhow.workspace = true dirs.workspace = true +git2.workspace = true heck.workspace = true itertools.workspace = true lazy_static.workspace = true diff --git a/volo-build/src/util.rs b/volo-build/src/util.rs index 5f3b367f..10115e60 100644 --- a/volo-build/src/util.rs +++ b/volo-build/src/util.rs @@ -272,3 +272,28 @@ where Ok(r) } + +pub fn git_repo_init(path: &Path) -> anyhow::Result<()> { + // Check if we are in a git repo and the path to the new package is not an ignored path in that + // repo. + // + // Reference: https://github.com/rust-lang/cargo/blob/0.74.0/src/cargo/util/vcs.rs + fn in_git_repo(path: &Path) -> bool { + if let Ok(repo) = git2::Repository::discover(path) { + // Don't check if the working directory itself is ignored. + if repo.workdir().map_or(false, |workdir| workdir == path) { + true + } else { + !repo.is_path_ignored(path).unwrap_or(false) + } + } else { + false + } + } + + if !in_git_repo(path) { + git2::Repository::init(path)?; + } + + Ok(()) +} diff --git a/volo-cli/src/init.rs b/volo-cli/src/init.rs index a7c08d62..65363c06 100644 --- a/volo-cli/src/init.rs +++ b/volo-cli/src/init.rs @@ -4,7 +4,7 @@ use clap::{value_parser, Parser}; use volo_build::{ config_builder::InitBuilder, model::{Entry, GitSource, Idl, Source, DEFAULT_FILENAME}, - util::{get_repo_latest_commit_id, DEFAULT_CONFIG_FILE}, + util::{get_repo_latest_commit_id, git_repo_init, DEFAULT_CONFIG_FILE}, }; use crate::command::CliCommand; @@ -275,6 +275,9 @@ impl CliCommand for Init { let _ = Command::new("cargo").arg("fmt").arg("--all").output()?; + let cwd = std::env::current_dir()?; + git_repo_init(&cwd)?; + Ok(()) } }