Skip to content

Commit

Permalink
feat(volo-cli): init for generated codes as a git repo (#254)
Browse files Browse the repository at this point in the history
Signed-off-by: Yu Li <[email protected]>
  • Loading branch information
yukiiiteru authored Nov 2, 2023
1 parent 547d58e commit 57a4b0d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
48 changes: 48 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions volo-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions volo-build/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
5 changes: 4 additions & 1 deletion volo-cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 57a4b0d

Please sign in to comment.