add RunPipe()
This commit is contained in:
parent
ab666ddbc3
commit
540eadaa96
|
@ -81,7 +81,11 @@ func (repo *Repo) checkoutBranch(bName string) bool {
|
||||||
func (repo *Repo) createUserBranch(branch string) error {
|
func (repo *Repo) createUserBranch(branch string) error {
|
||||||
if branch == "" {
|
if branch == "" {
|
||||||
// get the username here?
|
// get the username here?
|
||||||
return fmt.Errorf("gitpb createuserBranch() logic err. git branch name can not be blank")
|
usr, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
branch = usr.Username
|
||||||
}
|
}
|
||||||
if repo.IsDirty() {
|
if repo.IsDirty() {
|
||||||
// never change repos on dirty branches
|
// never change repos on dirty branches
|
||||||
|
|
69
shell.go
69
shell.go
|
@ -1,9 +1,11 @@
|
||||||
package gitpb
|
package gitpb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -185,3 +187,70 @@ func (repo *Repo) CountDiffObjects(branch1, branch2 string) int {
|
||||||
// log.Info("countDiffObjects()", cmd, len(r.Stdout), strings.Join(r.Stdout, " "))
|
// log.Info("countDiffObjects()", cmd, len(r.Stdout), strings.Join(r.Stdout, " "))
|
||||||
return len(r.Stdout)
|
return len(r.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repo *Repo) RunPipe(cmd1 []string, cmd2 []string) cmd.Status {
|
||||||
|
var s cmd.Status
|
||||||
|
var arg0 string
|
||||||
|
var args []string
|
||||||
|
if len(cmd1) == 0 {
|
||||||
|
s.Error = errors.New("Error: Command slice is empty.")
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
if len(cmd1) == 1 {
|
||||||
|
// Pass the first element as the command, and the rest as variadic arguments
|
||||||
|
arg0 = cmd1[0]
|
||||||
|
} else {
|
||||||
|
arg0 = cmd1[0]
|
||||||
|
args = cmd1[1:]
|
||||||
|
}
|
||||||
|
cmdShow := exec.Command(arg0, args...)
|
||||||
|
cmdShow.Dir = repo.GetFullPath()
|
||||||
|
|
||||||
|
if len(cmd2) == 0 {
|
||||||
|
s.Error = errors.New("Error: Command slice is empty.")
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
if len(cmd2) == 1 {
|
||||||
|
// Pass the first element as the command, and the rest as variadic arguments
|
||||||
|
arg0 = cmd2[0]
|
||||||
|
} else {
|
||||||
|
arg0 = cmd2[0]
|
||||||
|
args = cmd2[1:]
|
||||||
|
}
|
||||||
|
// 2. Create the command to calculate the patch-id from stdin.
|
||||||
|
cmdPipeID := exec.Command(arg0, args...)
|
||||||
|
cmdPipeID.Dir = repo.GetFullPath()
|
||||||
|
|
||||||
|
// 3. Connect the output of "git show" to the input of "git patch-id".
|
||||||
|
// This is the Go equivalent of the shell pipe `|`.
|
||||||
|
pipe, err := cmdShow.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
s.Error = fmt.Errorf("failed to create pipe: %w", err)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
cmdPipeID.Stdin = pipe
|
||||||
|
|
||||||
|
// 4. We need a buffer to capture the final output from git patch-id.
|
||||||
|
var output bytes.Buffer
|
||||||
|
cmdPipeID.Stdout = &output
|
||||||
|
|
||||||
|
// 5. Start the reading command (patch-id) first.
|
||||||
|
if err := cmdPipeID.Start(); err != nil {
|
||||||
|
s.Error = fmt.Errorf("failed to start git-patch-id: %w", err)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Run the writing command (show). This will block until it's done.
|
||||||
|
if err := cmdShow.Run(); err != nil {
|
||||||
|
s.Error = fmt.Errorf("failed to run git-show: %w", err)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Wait for the reading command to finish.
|
||||||
|
if err := cmdPipeID.Wait(); err != nil {
|
||||||
|
s.Error = fmt.Errorf("failed to wait for git-patch-id: %w", err)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue