Skip to content

Commit

Permalink
feat: init git repo (#65)
Browse files Browse the repository at this point in the history
* feat: init git repo

* test: fix git_init test

* chore: improve error handling

* git config for CI

---------

Co-authored-by: Abhishek Shah <[email protected]>
  • Loading branch information
CinematicCow and weezy20 authored Mar 21, 2024
1 parent 668cfb0 commit 4327abd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

env:
CARGO_TERM_COLOR: always
GITHUB_ACTOR: pop-cli

jobs:
build:
Expand All @@ -27,6 +28,11 @@ jobs:
with:
cache-on-failure: true
cache-all-crates: true

- name: Setup git config
run: |
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- uses: actions/checkout@v3
- name: Check Feature Contracts Excl.
Expand Down
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))?;
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 4327abd

Please sign in to comment.