diff --git a/git/clone.go b/git/clone.go index 7ded806..a98034f 100644 --- a/git/clone.go +++ b/git/clone.go @@ -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 } diff --git a/git/commit.go b/git/commit.go index 4ce2ad7..3c5783a 100644 --- a/git/commit.go +++ b/git/commit.go @@ -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( - gitBin, - "add", - "--all", - ) + 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( - gitBin, - "diff-index", - "--quiet", - "HEAD", - "--ignore-submodules", - ) - cmd.Dir = r.WorkDir - - return &types.Cmd{ - Cmd: 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", @@ -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 } diff --git a/git/config.go b/git/config.go index b4ebc6d..90a6400 100644 --- a/git/config.go +++ b/git/config.go @@ -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 } diff --git a/git/init.go b/git/init.go index ea7ec4b..81f461d 100644 --- a/git/init.go +++ b/git/init.go @@ -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 } diff --git a/git/remote.go b/git/remote.go index d308bfa..59c4d68 100644 --- a/git/remote.go +++ b/git/remote.go @@ -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 } diff --git a/git/status.go b/git/status.go index e5fb570..e558eb8 100644 --- a/git/status.go +++ b/git/status.go @@ -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( - gitBin, - "status", - "--porcelain", - ) + 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. diff --git a/plugin/impl.go b/plugin/impl.go index df2c72c..fdffc1e 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -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()) - } + 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 diff --git a/plugin/rsync.go b/plugin/rsync.go index 52a5e1c..b6170a0 100644 --- a/plugin/rsync.go +++ b/plugin/rsync.go @@ -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 }