Add git2 crate for Git integration of init subcommand

This commit is contained in:
Moritz Hölting 2024-04-06 22:03:00 +02:00
parent fbb76bb3d2
commit 0e5f8b9f4b
3 changed files with 32 additions and 0 deletions

View File

@ -19,4 +19,5 @@ thiserror = "1.0.58"
toml = "0.8.12"
shulkerscript-lang = {path = "../shulkerscript-lang", features = ["shulkerbox"], default-features = false}
shulkerbox = {path = "../shulkerbox", default-features = false}
git2 = { version = "0.18.3", default-features = false }

View File

@ -18,6 +18,8 @@ pub enum Error {
InvalidPackPathError(PathBuf),
#[error("An error occured while compiling the project.")]
ShulkerScriptError(#[from] shulkerscript_lang::base::Error),
#[error("An error occured during a git action.")]
GitError(#[from] git2::Error),
}
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -3,6 +3,9 @@ use std::{
path::{Path, PathBuf},
};
use clap::ValueEnum;
use git2::Repository;
use crate::{
config::ProjectConfig,
error::{Error, Result},
@ -27,6 +30,15 @@ pub struct InitArgs {
/// Force initialization even if the directory is not empty.
#[clap(short, long)]
force: bool,
#[clap(long, default_value = "git")]
vcs: VersionControlSystem,
}
#[derive(Debug, Clone, Copy, Default, ValueEnum)]
enum VersionControlSystem {
#[default]
Git,
None,
}
pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
@ -72,6 +84,8 @@ pub fn init(verbose: bool, args: &InitArgs) -> Result<()> {
verbose,
)?;
initalize_vcs(path, args.vcs, verbose)?;
print_success("Project initialized successfully.");
Ok(())
@ -165,6 +179,21 @@ fn create_main_file(path: &Path, namespace: &str, verbose: bool) -> std::io::Res
Ok(())
}
fn initalize_vcs(path: &Path, vcs: VersionControlSystem, verbose: bool) -> Result<()> {
match vcs {
VersionControlSystem::None => Ok(()),
VersionControlSystem::Git => {
if verbose {
print_info("Initializing a new Git repository...");
}
Repository::init(path)?;
print_info("Initialized a new Git repository.");
Ok(())
}
}
}
fn name_to_namespace(name: &str) -> String {
const VALID_CHARS: &str = "0123456789abcdefghijklmnopqrstuvwxyz_-.";