summaryrefslogtreecommitdiff
path: root/shell.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-22 16:58:20 -0500
committerJeff Carr <[email protected]>2025-09-22 16:58:20 -0500
commit540eadaa967c4d981b2ef896a67587a2674ccd48 (patch)
tree0ade39fca8fb4b8ccd974c7a80d48e112e073d1e /shell.go
parentab666ddbc382707f598613405ef4fe4755d34fce (diff)
add RunPipe()v0.0.131
Diffstat (limited to 'shell.go')
-rw-r--r--shell.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/shell.go b/shell.go
index 27252d4..d9c7bcd 100644
--- a/shell.go
+++ b/shell.go
@@ -1,9 +1,11 @@
package gitpb
import (
+ "bytes"
"errors"
"fmt"
"os"
+ "os/exec"
"path/filepath"
"strings"
@@ -185,3 +187,70 @@ func (repo *Repo) CountDiffObjects(branch1, branch2 string) int {
// log.Info("countDiffObjects()", cmd, len(r.Stdout), strings.Join(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
+}