Skip to content

Commit b41ea1d

Browse files
authored
Merge pull request #40 from rockstaedt/5-confirm-abort-by-user
Resolve "Confirm abort by user"
2 parents 8f65978 + 91a2fd4 commit b41ea1d

7 files changed

Lines changed: 46 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ further [reference](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks).
2121
curl -o install.sh -L https://raw.githubusercontent.com/rockstaedt/commit-message-check/main/install.sh && chmod +x ./install.sh && ./install.sh
2222
```
2323

24-
## Usage
24+
## Usage (Deprecated)
2525

2626
Every time a git commit is made, the corresponding hook is fired. For a commit
2727
message that is too long (>50 characters), the commit process is

cmd/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type Handler struct {
1111
Config model.Config
1212
Writer io.Writer
13+
Reader io.Reader
1314
}
1415

1516
func NewHandler(config model.Config) *Handler {

cmd/validate.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,19 @@ func (h *Handler) validate() int {
2727
}
2828

2929
if numOfExceedingChars > (hardLimit - softLimit) {
30-
h.notify("Abort commit. Subject line too long. Please fix.", "red")
31-
return 1
30+
h.notify("Subject line too long. Do you want to abort? (y/n)", "red")
31+
32+
var decision string
33+
if _, err := fmt.Fscanln(h.Reader, &decision); err != nil {
34+
h.notify("Could not read user input.", "red")
35+
return 1
36+
}
37+
38+
if decision == "y" {
39+
return 1
40+
}
41+
42+
return 0
3243
}
3344

3445
message := fmt.Sprintf("Your subject exceeds the soft limit of 50 chars by %d chars.", numOfExceedingChars)

cmd/validate_test.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ func TestValidate(t *testing.T) {
4040
assert.Contains(t, buffer.String(), "i am two characters more thäaaaaaaaaaaaaaaaaaaaan "+color.Yellow+"50")
4141
})
4242

43-
t.Run("returns 1 when commit message too long", func(t *testing.T) {
44-
buffer.Reset()
43+
t.Run("asks user for abort when commit message too long", func(t *testing.T) {
4544
testFile := t.TempDir() + "/text.txt"
4645
content := "waaaaaaaaaaaaaaaaaaaaaaaaaay tooooooooooooooooooo" +
4746
"looooooooooooooooooooooong"
@@ -50,10 +49,34 @@ func TestValidate(t *testing.T) {
5049
handler := NewHandler(model.Config{CommitMsgFile: testFile})
5150
handler.Writer = buffer
5251

53-
status := handler.Run("validate")
52+
t.Run("user confirms abort returns 1", func(t *testing.T) {
53+
buffer.Reset()
54+
handler.Reader = bytes.NewReader([]byte("y"))
5455

55-
assert.Contains(t, buffer.String(), color.Red+"Abort commit")
56-
assert.Equal(t, 1, status)
56+
status := handler.Run("validate")
57+
58+
assert.Contains(t, buffer.String(), color.Red+"Subject line too long. Do you want to abort? (y/n)")
59+
assert.Equal(t, 1, status)
60+
})
61+
62+
t.Run("user declines abort returns 0", func(t *testing.T) {
63+
buffer.Reset()
64+
handler.Reader = bytes.NewReader([]byte("n"))
65+
66+
status := handler.Run("validate")
67+
68+
assert.Equal(t, 0, status)
69+
})
70+
71+
t.Run("error at reading user input returns 1", func(t *testing.T) {
72+
buffer.Reset()
73+
handler.Reader = bytes.NewReader([]byte(""))
74+
75+
status := handler.Run("validate")
76+
77+
assert.Equal(t, 1, status)
78+
assert.Contains(t, buffer.String(), color.Red+"Could not read user input.")
79+
})
5780
})
5881

5982
t.Run("returns 1 when error at reading file", func(t *testing.T) {

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func main() {
6161

6262
handler := cmd.NewHandler(config)
6363
handler.Writer = os.Stdout
64+
handler.Reader = os.Stdin
6465

6566
os.Exit(handler.Run(os.Args[1]))
6667
}

util/hook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func DeleteHook(path, _ string) error {
5252

5353
func writeContent(writer io.Writer, exePath string) {
5454
_, err := fmt.Fprint(writer, "#!/bin/sh\n\n")
55+
_, err = fmt.Fprint(writer, "exec < /dev/tty\n\n")
5556
_, err = fmt.Fprintf(writer, `"%s/commit-message-check" validate $1`, exePath)
5657
_, err = fmt.Fprint(writer, "\n")
5758

util/hook_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func TestWriteContent(t *testing.T) {
136136
writeContent(buffer, "usr/tmp")
137137

138138
assert.Contains(t, buffer.String(), "#!/bin/sh\n\n")
139+
assert.Contains(t, buffer.String(), "exec < /dev/tty\n\n")
139140
})
140141

141142
t.Run("executes commit-message-check with root path and quotes path to handle spaces", func(t *testing.T) {

0 commit comments

Comments
 (0)