Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: init git repo #65

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
weezy20 marked this conversation as resolved.
Show resolved Hide resolved

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))?;
weezy20 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading