Skip to content

Commit 8f65978

Browse files
authored
Merge pull request #37 from rockstaedt/36-amount-of-characters-after-soft-limit-is-wrong
Resolve "Amount of characters after soft limit is wrong"
2 parents 146f6f1 + d658d42 commit 8f65978

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

cmd/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (h *Handler) validate() int {
3333

3434
message := fmt.Sprintf("Your subject exceeds the soft limit of 50 chars by %d chars.", numOfExceedingChars)
3535
h.notify(message, "yellow")
36-
h.notify(cm.Subject[:softLimit] + color.InYellow(cm.Subject[softLimit:]))
36+
h.notify(cm.Subject[:softLimit].String() + color.InYellow(cm.Subject[softLimit:].String()))
3737

3838
return 0
3939
}

cmd/validate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestValidate(t *testing.T) {
2828
t.Run("returns 0 when soft limit exceeds and logs a warning", func(t *testing.T) {
2929
buffer.Reset()
3030
testFile := t.TempDir() + "/text.txt"
31-
err := os.WriteFile(testFile, []byte("i am two characters more thaaaaaaaaaaaaaaaaaaaaan 50"), 0666)
31+
err := os.WriteFile(testFile, []byte("i am two characters more thäaaaaaaaaaaaaaaaaaaaan 50"), 0666)
3232
assert.Nil(t, err)
3333
handler := NewHandler(model.Config{CommitMsgFile: testFile})
3434
handler.Writer = buffer
@@ -37,7 +37,7 @@ func TestValidate(t *testing.T) {
3737

3838
assert.Equal(t, status, 0)
3939
assert.Contains(t, buffer.String(), color.Yellow+"Your subject exceeds the soft limit of 50 chars by 2 chars.")
40-
assert.Contains(t, buffer.String(), "i am two characters more thaaaaaaaaaaaaaaaaaaaaan "+color.Yellow+"50")
40+
assert.Contains(t, buffer.String(), "i am two characters more thäaaaaaaaaaaaaaaaaaaaan "+color.Yellow+"50")
4141
})
4242

4343
t.Run("returns 1 when commit message too long", func(t *testing.T) {

internal/model/commit_message.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package model
22

33
import (
4-
"regexp"
54
"strings"
65
)
76

7+
type Subject []rune
8+
89
type CommitMessage struct {
9-
Subject string
10+
Subject Subject
1011
Body []string
1112
InvalidBody bool
1213
}
@@ -22,27 +23,24 @@ func CreateCommitMessageFrom(messageLines []string) *CommitMessage {
2223
func (cm *CommitMessage) ValidateSubject() int {
2324
currentSubjectLength := len(cm.Subject)
2425

25-
if strings.HasPrefix(cm.Subject, "Merge ") {
26+
if strings.HasPrefix(cm.Subject.String(), "Merge ") {
2627
return 0
2728
}
2829

29-
if currentSubjectLength > 72 {
30-
return currentSubjectLength - 50
31-
}
32-
3330
if currentSubjectLength > 50 {
34-
re := regexp.MustCompile(`^#\d+ -\s*(.*)$`)
35-
trimmedSubject := re.ReplaceAllString(cm.Subject, `$1`)
36-
37-
return len(trimmedSubject) - 50
31+
return currentSubjectLength - 50
3832
}
3933

4034
return 0
4135
}
4236

37+
func (s Subject) String() string {
38+
return string(s)
39+
}
40+
4341
func (cm *CommitMessage) addSubject(messageLines []string) {
4442
if len(messageLines) >= 1 {
45-
cm.Subject = messageLines[0]
43+
cm.Subject = []rune(messageLines[0])
4644
}
4745
}
4846

internal/model/commit_message_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestNewCommitMessage(t *testing.T) {
1919
t.Run("create new commit message object from File", func(t *testing.T) {
2020
cm := CreateCommitMessageFrom(validCommitMsgLines)
2121

22-
assert.Equal(t, "I am a valid Subject with less than 50 characters", cm.Subject)
22+
assert.Equal(t, "I am a valid Subject with less than 50 characters", cm.Subject.String())
2323
assert.Len(t, cm.Body, 3)
2424
assert.False(t, cm.InvalidBody)
2525
})
@@ -28,7 +28,7 @@ func TestNewCommitMessage(t *testing.T) {
2828
var emptyCommitMsgLines []string
2929
cm := CreateCommitMessageFrom(emptyCommitMsgLines)
3030

31-
assert.Equal(t, "", cm.Subject)
31+
assert.Equal(t, "", cm.Subject.String())
3232
assert.Len(t, cm.Body, 0)
3333
})
3434

@@ -41,7 +41,7 @@ func TestNewCommitMessage(t *testing.T) {
4141
cm := CreateCommitMessageFrom(invalidCommitMsgLines)
4242

4343
t.Run("sets body correct", func(t *testing.T) {
44-
assert.Equal(t, "subject line", cm.Subject)
44+
assert.Equal(t, "subject line", cm.Subject.String())
4545
assert.Len(t, cm.Body, 2)
4646
})
4747

@@ -62,10 +62,11 @@ func TestNewCommitMessage(t *testing.T) {
6262
}{
6363
{"more than................72....................................characters", 23},
6464
{"more than................50................less than 72 characters", 16},
65-
{"#1301 - more than........50..............through ID prefix", 0},
65+
{"#1301 - more than........50..............through ID prefix", 8},
6666
{"short subject line", 0},
6767
{"Merge pull request commits are ignored because they can easily exceed 52 characters", 0},
6868
{"Merge branch commits are also ignored..............................................", 0},
69+
{"I am a commit containing an umlaut ü.................", 3},
6970
}
7071

7172
for _, tc := range testcases {

0 commit comments

Comments
 (0)