0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-06-02 18:29:41 +02:00
wp-git-action/git/commit.go

71 lines
1.1 KiB
Go

package git
import (
"io"
"github.com/thegeeklab/wp-plugin-go/v2/types"
"golang.org/x/sys/execabs"
)
// Add updates the index to match the working tree.
func (r *Repository) Add() *types.Cmd {
cmd := &types.Cmd{
Cmd: execabs.Command(
gitBin,
"add",
"--all",
),
}
cmd.Dir = r.WorkDir
return cmd
}
// TestCleanTree returns non-zero if diff between index and local repository.
func (r *Repository) IsCleanTree() *types.Cmd {
cmd := &types.Cmd{
Cmd: execabs.Command(
gitBin,
"diff-index",
"--quiet",
"HEAD",
"--ignore-submodules",
),
}
cmd.Dir = r.WorkDir
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard
cmd.SetTrace(false)
return cmd
}
// Commit creates a new commit with the specified commit message.
func (r *Repository) Commit() *types.Cmd {
if err := r.IsCleanTree().Run(); err == nil && !r.EmptyCommit {
return nil
}
args := []string{
"commit",
"-m",
r.CommitMsg,
}
if r.EmptyCommit {
args = append(args, "--allow-empty")
}
if r.NoVerify {
args = append(args, "--no-verify")
}
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return cmd
}