refactor: centralise git commit machinery
This commit is contained in:
parent
66666e30b7
commit
5113db1612
|
@ -15,7 +15,6 @@ import (
|
||||||
gitPkg "coopcloud.tech/abra/pkg/git"
|
gitPkg "coopcloud.tech/abra/pkg/git"
|
||||||
"coopcloud.tech/abra/pkg/limit"
|
"coopcloud.tech/abra/pkg/limit"
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/go-git/go-git/v5"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
@ -224,43 +223,19 @@ A new catalogue copy can be published to the recipes repository by passing the
|
||||||
logrus.Infof("generated new recipe catalogue in %s", config.APPS_JSON)
|
logrus.Infof("generated new recipe catalogue in %s", config.APPS_JSON)
|
||||||
|
|
||||||
if internal.Commit {
|
if internal.Commit {
|
||||||
repoPath := path.Join(config.ABRA_DIR, "catalogue")
|
if internal.CommitMessage == "" && !internal.NoInput {
|
||||||
commitRepo, err := git.PlainOpen(repoPath)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
commitWorktree, err := commitRepo.Worktree()
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if internal.CommitMessage == "" {
|
|
||||||
prompt := &survey.Input{
|
prompt := &survey.Input{
|
||||||
Message: "commit message",
|
Message: "commit message",
|
||||||
Default: "chore: publish new catalogue changes",
|
Default: fmt.Sprintf("chore: publish catalogue changes"),
|
||||||
}
|
}
|
||||||
if err := survey.AskOne(prompt, &internal.CommitMessage); err != nil {
|
if err := survey.AskOne(prompt, &internal.CommitMessage); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = commitWorktree.AddGlob("**.json")
|
if err := gitPkg.Commit("**.json", internal.CommitMessage, false); err != nil {
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
logrus.Debug("staged **.json for commit")
|
|
||||||
|
|
||||||
_, err = commitWorktree.Commit(internal.CommitMessage, &git.CommitOptions{})
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
logrus.Info("changes commited")
|
|
||||||
|
|
||||||
if err := commitRepo.Push(&git.PushOptions{}); err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
logrus.Info("changes pushed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
|
gitPkg "coopcloud.tech/abra/pkg/git"
|
||||||
"coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
recipePkg "coopcloud.tech/abra/pkg/recipe"
|
||||||
"coopcloud.tech/tagcmp"
|
"coopcloud.tech/tagcmp"
|
||||||
|
@ -140,15 +141,6 @@ You may invoke this command in "wizard" mode and be prompted for input:
|
||||||
}
|
}
|
||||||
|
|
||||||
if internal.Commit || internal.CommitMessage != "" {
|
if internal.Commit || internal.CommitMessage != "" {
|
||||||
commitRepo, err := git.PlainOpen(directory)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
commitWorktree, err := commitRepo.Worktree()
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if internal.CommitMessage == "" && !internal.NoInput {
|
if internal.CommitMessage == "" && !internal.NoInput {
|
||||||
prompt := &survey.Input{
|
prompt := &survey.Input{
|
||||||
Message: "commit message",
|
Message: "commit message",
|
||||||
|
@ -159,21 +151,14 @@ You may invoke this command in "wizard" mode and be prompted for input:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = commitWorktree.AddGlob("compose.**yml")
|
if internal.CommitMessage == "" {
|
||||||
if err != nil {
|
logrus.Fatal("no commit message specified?")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := gitPkg.Commit("compose.**yml", internal.CommitMessage, internal.Dry); err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
logrus.Debug("staged compose.**yml for commit")
|
|
||||||
|
|
||||||
if !internal.Dry {
|
|
||||||
_, err = commitWorktree.Commit(internal.CommitMessage, &git.CommitOptions{})
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
logrus.Info("changes commited")
|
|
||||||
} else {
|
|
||||||
logrus.Info("dry run only: NOT committing changes")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
repo, err := git.PlainOpen(directory)
|
repo, err := git.PlainOpen(directory)
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"coopcloud.tech/abra/pkg/config"
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Commit runs a git commit
|
||||||
|
func Commit(glob, commitMessage string, dryRun bool) error {
|
||||||
|
if commitMessage == "" {
|
||||||
|
return fmt.Errorf("no commit message specified?")
|
||||||
|
}
|
||||||
|
|
||||||
|
repoPath := path.Join(config.ABRA_DIR, "catalogue")
|
||||||
|
commitRepo, err := git.PlainOpen(repoPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
commitWorktree, err := commitRepo.Worktree()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
patterns, err := GetExcludesFiles()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(patterns) > 0 {
|
||||||
|
commitWorktree.Excludes = append(patterns, commitWorktree.Excludes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dryRun {
|
||||||
|
err = commitWorktree.AddGlob(glob)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logrus.Debugf("staged %s for commit", glob)
|
||||||
|
} else {
|
||||||
|
logrus.Debugf("dry run: did not stage %s for commit", glob)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dryRun {
|
||||||
|
_, err = commitWorktree.Commit(commitMessage, &git.CommitOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logrus.Info("changes commited")
|
||||||
|
} else {
|
||||||
|
logrus.Info("dry run: no changes commited")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dryRun {
|
||||||
|
if err := commitRepo.Push(&git.PushOptions{}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logrus.Info("changes pushed")
|
||||||
|
} else {
|
||||||
|
logrus.Info("dry run: no changes pushed")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue