From 0e5f8b9f4b526b72cb70246e0217578ba10d5272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:03:00 +0200 Subject: [PATCH] Add git2 crate for Git integration of init subcommand --- Cargo.toml | 1 + src/error.rs | 2 ++ src/subcommands/init.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index baf7f47..69cab35 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/error.rs b/src/error.rs index 4869264..96d6289 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 = std::result::Result; diff --git a/src/subcommands/init.rs b/src/subcommands/init.rs index 638db42..bc83fdc 100644 --- a/src/subcommands/init.rs +++ b/src/subcommands/init.rs @@ -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_-.";