0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-11-24 12:40:39 +00:00

fix: fix command exec order (#115)

This commit is contained in:
Robert Kaussow 2024-05-06 22:43:56 +02:00 committed by GitHub
parent dbc0ea945b
commit 5930a2ad75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 94 additions and 85 deletions

View File

@ -15,12 +15,12 @@ func (r *Repository) FetchSource() *types.Cmd {
fmt.Sprintf("+%s:", r.Branch),
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// CheckoutHead handles branch checkout.
@ -31,10 +31,10 @@ func (r *Repository) CheckoutHead() *types.Cmd {
r.Branch,
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}

View File

@ -1,42 +1,52 @@
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 := execabs.Command(
cmd := &types.Cmd{
Cmd: execabs.Command(
gitBin,
"add",
"--all",
)
),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// TestCleanTree returns non-zero if diff between index and local repository.
func (r *Repository) IsCleanTree() *types.Cmd {
cmd := execabs.Command(
cmd := &types.Cmd{
Cmd: execabs.Command(
gitBin,
"diff-index",
"--quiet",
"HEAD",
"--ignore-submodules",
)
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
),
}
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",
@ -51,10 +61,10 @@ func (r *Repository) Commit() *types.Cmd {
args = append(args, "--no-verify")
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}

View File

@ -17,12 +17,12 @@ func (r *Repository) ConfigAutocorrect() *types.Cmd {
r.Autocorrect,
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// ConfigUserEmail sets the global git author email.
@ -34,12 +34,12 @@ func (r *Repository) ConfigUserEmail() *types.Cmd {
r.Author.Email,
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// ConfigUserName configures the user.name git config setting for the given repository.
@ -51,12 +51,12 @@ func (r *Repository) ConfigUserName() *types.Cmd {
r.Author.Name,
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// ConfigSSLVerify configures the http.sslVerify git config setting for the given repository.
@ -68,12 +68,12 @@ func (r *Repository) ConfigSSLVerify(skipVerify bool) *types.Cmd {
strconv.FormatBool(!skipVerify),
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// ConfigSSHCommand sets custom SSH key.
@ -85,7 +85,10 @@ func (r *Repository) ConfigSSHCommand(sshKey string) *types.Cmd {
"ssh -i " + sshKey,
}
return &types.Cmd{
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.SetTrace(false)
return cmd
}

View File

@ -13,10 +13,10 @@ func (r *Repository) Init() *types.Cmd {
r.Branch,
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}

View File

@ -15,12 +15,12 @@ func (r *Repository) RemoteRemove() *types.Cmd {
r.RemoteName,
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// RemoteAdd adds an additional remote to a git repo.
@ -32,12 +32,12 @@ func (r *Repository) RemoteAdd() *types.Cmd {
r.RemoteURL,
}
cmd := execabs.Command(gitBin, args...)
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// RemotePush pushs the changes from the local head to a remote branch.
@ -48,18 +48,18 @@ func (r *Repository) RemotePush() *types.Cmd {
fmt.Sprintf("HEAD:%s", r.Branch),
}
cmd := execabs.Command(gitBin, args...)
cmd.Dir = r.WorkDir
if r.ForcePush {
cmd.Args = append(cmd.Args, "--force")
args = append(args, "--force")
}
if r.PushFollowTags {
cmd.Args = append(cmd.Args, "--follow-tags")
args = append(args, "--follow-tags")
}
return &types.Cmd{
Cmd: cmd,
cmd := &types.Cmd{
Cmd: execabs.Command(gitBin, args...),
}
cmd.Dir = r.WorkDir
return cmd
}

View File

@ -8,16 +8,16 @@ import (
// Status returns a command that runs `git status --porcelain` for the given repository.
func (r *Repository) Status() *types.Cmd {
cmd := execabs.Command(
cmd := &types.Cmd{
Cmd: execabs.Command(
gitBin,
"status",
"--porcelain",
)
),
}
cmd.Dir = r.WorkDir
return &types.Cmd{
Cmd: cmd,
}
return cmd
}
// IsDirty checks if the given repository has any uncommitted changes.

View File

@ -137,6 +137,7 @@ func (p *Plugin) Execute() error {
if err := os.MkdirAll(p.Settings.Repo.WorkDir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create working directory: %w", err)
}
defer os.RemoveAll(p.Settings.Repo.WorkDir)
p.Settings.Repo.IsEmpty, err = file.IsDirEmpty(p.Settings.Repo.WorkDir)
if err != nil {
@ -215,10 +216,7 @@ func (p *Plugin) handleCommit() []*types.Cmd {
var cmds []*types.Cmd
cmds = append(cmds, p.Settings.Repo.Add())
if err := p.Settings.Repo.IsCleanTree().Run(); err != nil || p.Settings.Repo.EmptyCommit {
cmds = append(cmds, p.Settings.Repo.Commit())
}
return cmds
}
@ -232,8 +230,6 @@ func (p *Plugin) handlePush() []*types.Cmd {
func (p *Plugin) handlePages() ([]*types.Cmd, error) {
var cmds []*types.Cmd
defer os.RemoveAll(p.Settings.Repo.WorkDir)
ccmd, err := p.handleClone()
if err != nil {
return cmds, err

View File

@ -34,10 +34,10 @@ func SyncDirectories(exclude []string, del bool, src, dest string) *types.Cmd {
dest,
)
cmd := execabs.Command("rsync", args...)
cmd := &types.Cmd{
Cmd: execabs.Command("rsync", args...),
}
cmd.Dir = src
return &types.Cmd{
Cmd: cmd,
}
return cmd
}