48 lines
902 B
Go
48 lines
902 B
Go
package git
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Commit runs a git commit
|
|
func Commit(repoPath, commitMessage string, dryRun bool) error {
|
|
if commitMessage == "" {
|
|
return fmt.Errorf("no commit message specified?")
|
|
}
|
|
|
|
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 {
|
|
// NOTE(d1): `All: true` does not include untracked files
|
|
_, err = commitWorktree.Commit(commitMessage, &git.CommitOptions{All: true})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
logrus.Debug("git changes commited")
|
|
} else {
|
|
logrus.Debug("dry run: no changes commited")
|
|
}
|
|
|
|
return nil
|
|
}
|