Skip to content

Commit 5e7424e

Browse files
fix: check for stdin pipe input
1 parent f38e266 commit 5e7424e

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

cmd/util.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func setGitConf(hookDir string, isGlobal bool) error {
6464
}
6565

6666
func getCommitMsg(fileInput string) (string, error) {
67-
commitMsg := readStdIn()
67+
commitMsg, err := readStdInPipe()
68+
if err != nil {
69+
return "", err
70+
}
71+
6872
if commitMsg != "" {
6973
return commitMsg, nil
7074
}
@@ -81,12 +85,23 @@ func getCommitMsg(fileInput string) (string, error) {
8185
return string(inBytes), nil
8286
}
8387

84-
func readStdIn() string {
88+
func readStdInPipe() (string, error) {
89+
stat, err := os.Stdin.Stat()
90+
if err != nil {
91+
return "", err
92+
}
93+
94+
// user input from terminal
95+
if (stat.Mode() & os.ModeCharDevice) != 0 {
96+
// not handling this case
97+
return "", nil
98+
}
99+
100+
// user input from stdin pipe
85101
readBytes, err := io.ReadAll(os.Stdin)
86102
if err != nil {
87-
// TODO: handle error?
88-
return ""
103+
return "", err
89104
}
90105
s := string(readBytes)
91-
return strings.TrimSpace(s)
106+
return strings.TrimSpace(s), nil
92107
}

0 commit comments

Comments
 (0)