feat: add git-user and git-email flags to recipe new

This commit is contained in:
2024-03-07 17:46:59 +01:00
committed by decentral1se
parent 9ec99c7712
commit b2485cc122
4 changed files with 37 additions and 14 deletions

View File

@ -1,35 +1,41 @@
package git
import (
"fmt"
"github.com/go-git/go-git/v5"
gitPkg "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/sirupsen/logrus"
)
// Init inits a new repo and commits all the stuff if you want
func Init(repoPath string, commit bool) error {
if _, err := gitPkg.PlainInit(repoPath, false); err != nil {
logrus.Fatal(err)
func Init(repoPath string, commit bool, gitName, gitEmail string) error {
if _, err := git.PlainInit(repoPath, false); err != nil {
return fmt.Errorf("git init: %s", err)
}
logrus.Debugf("initialised new git repo in %s", repoPath)
if commit {
commitRepo, err := git.PlainOpen(repoPath)
if err != nil {
logrus.Fatal(err)
return fmt.Errorf("git open: %s", err)
}
commitWorktree, err := commitRepo.Worktree()
if err != nil {
logrus.Fatal(err)
return fmt.Errorf("git worktree: %s", err)
}
if err := commitWorktree.AddWithOptions(&git.AddOptions{All: true}); err != nil {
return err
return fmt.Errorf("git add: %s", err)
}
if _, err = commitWorktree.Commit("init", &git.CommitOptions{}); err != nil {
return err
var author *object.Signature
if gitName != "" && gitEmail != "" {
author = &object.Signature{Name: gitName, Email: gitEmail}
}
if _, err = commitWorktree.Commit("init", &git.CommitOptions{Author: author}); err != nil {
return fmt.Errorf("git commit: %s", err)
}
logrus.Debugf("init committed all files for new git repo in %s", repoPath)
}