69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
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
|
|
}
|