Skip to content

Commit

Permalink
fix: print error output from git
Browse files Browse the repository at this point in the history
  • Loading branch information
tingzhen.lin committed Sep 26, 2022
1 parent 3983532 commit 7794765
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ func RootCmd(command *cobra.Command, args []string) {
log.Printf("run git commit failed, err=%v\n", err)
log.Printf("commit message is: \n\n%s\n\n", string(message))
}
fmt.Print(result)
fmt.Print(string(result))
}
}
86 changes: 34 additions & 52 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,66 +30,49 @@ func IsCurrentDirectoryGitRepo() (bool, error) {
// run git remote command
cmd := exec.Command("git", "remote")

if stderr, err := cmd.StderrPipe(); err != nil {
var err error
var stderr io.ReadCloser
if stderr, err = cmd.StderrPipe(); err != nil {
return false, err
} else {
defer stderr.Close()

if err := cmd.Start(); err != nil {
return false, err
}
}

if result, err := ioutil.ReadAll(stderr); err != nil {
return false, err
} else {
if err := cmd.Start(); err != nil {
return false, err
}

if err := cmd.Wait(); err != nil {
return false, fmt.Errorf("%s", string(result))
}
var result []byte
if result, err = ioutil.ReadAll(stderr); err != nil {
return false, err
}

return true, nil
}
if err := cmd.Wait(); err != nil {
return false, fmt.Errorf("%s", string(result))
}

return true, nil
}

func CommitMessage(message []byte, all bool) (string, error) {
func CommitMessage(message []byte, all bool) ([]byte, error) {
// save the commit message to temp file
if file, err := ioutil.TempFile("", "COMMIT_MESSAGE_"); err != nil {
return "", err
} else {
defer os.Remove(file.Name())

if _, err := file.Write(message); err != nil {
return "", err
}

// run git commit command
cmd := exec.Command("git", "commit", "-F")
cmd.Args = append(cmd.Args, file.Name())
if all {
cmd.Args = append(cmd.Args, "-a")
}

if stdout, err := cmd.StdoutPipe(); err != nil {
return "", err
} else {
defer stdout.Close()

if err := cmd.Start(); err != nil {
return "", err
}

if result, err := ioutil.ReadAll(stdout); err != nil {
return string(result), err
} else {
if err := cmd.Wait(); err != nil {
return string(result), err
} else {
return string(result), nil
}
}
}
var err error
var file *os.File
if file, err = ioutil.TempFile("", "COMMIT_MESSAGE_"); err != nil {
return nil, err
}
defer os.Remove(file.Name())

if _, err := file.Write(message); err != nil {
return nil, err
}

// run git commit command
cmd := exec.Command("git", "commit", "-F")
cmd.Args = append(cmd.Args, file.Name())
if all {
cmd.Args = append(cmd.Args, "-a")
}

return cmd.CombinedOutput()
}

func copyFile(dstName, srcName string) (written int64, err error) {
Expand All @@ -112,7 +95,6 @@ func execPath() (string, error) {
if err != nil {
return "", err
}
defer stdout.Close()

if err := cmd.Start(); err != nil {
return "", err
Expand Down

0 comments on commit 7794765

Please sign in to comment.