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 3 commits
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
14 changes: 14 additions & 0 deletions src/commands/new/parachain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
engines::parachain_engine::{instantiate_template_dir, Config},
helpers::git_init,
style::{style, Theme},
};
use clap::{Args, Parser};
Expand Down Expand Up @@ -77,6 +78,11 @@ impl NewParachainCommand {
initial_endowment: self.initial_endowment.clone().expect("default values"),
},
)?;
if let Err(err) = git_init(destination_path, "initialized parachain") {
if err.class() == git2::ErrorClass::Config && err.code() == git2::ErrorCode::NotFound {
outro_cancel("git signature could not be found. Please configure your git config with your name and email")?;
}
}
spinner.stop("Generation complete");
outro(format!("cd into \"{}\" and enjoy hacking! 🚀", &self.name))?;
Ok(())
Expand All @@ -85,6 +91,9 @@ impl NewParachainCommand {

#[cfg(test)]
mod tests {

use git2::Repository;

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

Expand All @@ -100,6 +109,11 @@ mod tests {
let result = command.execute();
assert!(result.is_ok());

// check for git_init
let repo = Repository::open(Path::new(&command.name))?;
let reflog = repo.reflog("HEAD")?;
assert_eq!(reflog.len(), 1);

// 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