forked from toolshed/abra
.gitea
cli
cmd
pkg
app
autocomplete
client
compose
config
container
context
dns
formatter
git
branch.go
clone.go
commit.go
common.go
init.go
push.go
read.go
remote.go
integration
limit
lint
recipe
secret
server
service
ssh
upstream
web
scripts
tests
.drone.yml
.e2e.env.sample
.envrc.sample
.gitignore
.goreleaser.yml
Makefile
README.md
go.mod
go.sum
renovate.json
57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package git
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Commit runs a git commit
|
|
func Commit(repoPath, glob, 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 {
|
|
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.Debug("git changes commited")
|
|
} else {
|
|
logrus.Debug("dry run: no changes commited")
|
|
}
|
|
|
|
return nil
|
|
}
|