diff --git a/checkout.go b/checkout.go index edc508a..18253b7 100644 --- a/checkout.go +++ b/checkout.go @@ -81,7 +81,11 @@ func (repo *Repo) checkoutBranch(bName string) bool { func (repo *Repo) createUserBranch(branch string) error { if branch == "" { // 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() { // never change repos on dirty branches 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 +}