From 1f6e4fa4a3efab5f2ad5aa751055c06e76df1292 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 15 Nov 2021 18:54:44 +0100 Subject: [PATCH] fix: ensure to init/commit the new recipe repo Part of https://git.coopcloud.tech/coop-cloud/organising/issues/247. --- cli/recipe/new.go | 5 +++++ pkg/git/init.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 pkg/git/init.go diff --git a/cli/recipe/new.go b/cli/recipe/new.go index 33a9bf87..74aa2eb0 100644 --- a/cli/recipe/new.go +++ b/cli/recipe/new.go @@ -82,6 +82,11 @@ The new example repository is cloned to ~/.abra/apps/. } } + newGitRepo := path.Join(config.APPS_DIR, recipeName) + if err := git.Init(newGitRepo, true); err != nil { + logrus.Fatal(err) + } + logrus.Infof( "new recipe '%s' created in %s, happy hacking!\n", recipeName, path.Join(config.APPS_DIR, recipeName), diff --git a/pkg/git/init.go b/pkg/git/init.go new file mode 100644 index 00000000..6d7e1afa --- /dev/null +++ b/pkg/git/init.go @@ -0,0 +1,38 @@ +package git + +import ( + "github.com/go-git/go-git/v5" + gitPkg "github.com/go-git/go-git/v5" + "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) + } + logrus.Debugf("initialised new git repo in %s", repoPath) + + if commit { + commitRepo, err := git.PlainOpen(repoPath) + if err != nil { + logrus.Fatal(err) + } + + commitWorktree, err := commitRepo.Worktree() + if err != nil { + logrus.Fatal(err) + } + + if err := commitWorktree.AddGlob("**"); err != nil { + return err + } + + if _, err = commitWorktree.Commit("init", &git.CommitOptions{}); err != nil { + return err + } + logrus.Debugf("init committed all files for new git repo in %s", repoPath) + } + + return nil +}