Skip to content

Commit

Permalink
feat: init git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
CinematicCow committed Mar 14, 2024
1 parent 7dc2e5d commit 0fb30fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/commands/new/parachain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
engines::parachain_engine::{instantiate_template_dir, Config},
style::{style, Theme},
engines::parachain_engine::{instantiate_template_dir, Config}, helpers::git_init, style::{style, Theme}
};
use clap::{Args, Parser};
use std::{fs, path::Path};
Expand Down Expand Up @@ -77,6 +76,7 @@ impl NewParachainCommand {
initial_endowment: self.initial_endowment.clone().expect("default values"),
},
)?;
git_init(destination_path, "initialized parachain")?;
spinner.stop("Generation complete");
outro(format!("cd into \"{}\" and enjoy hacking! 🚀", &self.name))?;
Ok(())
Expand All @@ -85,6 +85,7 @@ impl NewParachainCommand {

#[cfg(test)]
mod tests {

use super::*;
use std::fs;

Expand All @@ -98,8 +99,15 @@ mod tests {
initial_endowment: Some("1u64 << 60".to_string()),
};
let result = command.execute();

// Check if the Git repository was initialized
let destination_path = Path::new(&command.name);
let git_dir = destination_path.join(".git");
assert!(git_dir.exists(), "Git repository not initialized correctly");

assert!(result.is_ok());


// Clean up
if let Err(err) = fs::remove_dir_all("test_parachain") {
eprintln!("Failed to delete directory: {}", err);
Expand Down
20 changes: 19 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use cliclack::{log, outro_cancel};
use git2::Repository;
use git2::{IndexAddOption, Repository, ResetType};
use std::{
env::current_dir,
fs::{self, OpenOptions},
Expand Down Expand Up @@ -50,6 +50,24 @@ pub(crate) fn clone_and_degit(url: &str, target: &Path) -> Result<()> {
Ok(())
}

/// Init a new git repo on creation of a parachain
pub(crate) fn git_init(target: &Path, message: &str) -> Result<(), git2::Error> {
let repo = Repository::init(target)?;
let signature = repo.signature()?;

let mut index = repo.index()?;
index.add_all(["*"].iter(), IndexAddOption::DEFAULT, None)?;
let tree_id = index.write_tree()?;

let tree = repo.find_tree(tree_id)?;
let commit_id = repo.commit(Some("HEAD"), &signature, &signature, message, &tree, &[])?;

let commit_object = repo.find_object(commit_id, Some(git2::ObjectType::Commit))?;
repo.reset(&commit_object, ResetType::Hard, None)?;

Ok(())
}

/// Resolve pallet path
/// For a template it should be `<template>/pallets/`
/// For no path, it should just place it in the current working directory
Expand Down

0 comments on commit 0fb30fc

Please sign in to comment.