Skip to content

Commit 6417b38

Browse files
refactor: update parseHeader and parseLineAsFooter
1 parent 297012c commit 6417b38

2 files changed

Lines changed: 47 additions & 40 deletions

File tree

parser.go

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,47 @@ func Parse(message string) (*Commit, error) {
4545

4646
inFooters := false
4747
for i, msgLine := range messageLines {
48-
switch i {
49-
case 0:
50-
err := parseHeader(msgLine, commit)
48+
// First Line
49+
if i == 0 {
50+
head, isBreak, err := parseHeader(msgLine)
5151
if err != nil {
52-
return commit, err
52+
return nil, err
5353
}
54-
case 1:
54+
commit.Header = head
55+
commit.BreakingChange = isBreak
56+
continue
57+
}
58+
59+
// Second Line
60+
if i == 1 {
5561
if msgLine != "" {
56-
return commit, errNoBlankLine
62+
return nil, errNoBlankLine
5763
}
58-
default:
59-
key, value := parseLineAsFooter(msgLine)
64+
continue
65+
}
6066

61-
if key != "" && value != "" {
62-
inFooters = true
67+
// Remaining Line
68+
key, value, isFooter := parseLineAsFooter(msgLine)
69+
if isFooter {
70+
inFooters = true
6371

64-
// Check if we have previously found a footer. If we have, set the current footer,
65-
// otherwise just record it.
66-
if currKeyValue != "" {
67-
foot.Notes = append(foot.Notes, newFooterNote(currKeyValue, currFooterValue))
68-
foot.FullFooter += messageLines[i-1] + "\n" // add previous line to FullFooter
69-
}
70-
currKeyValue = key
71-
currFooterValue = value
72+
// Check if we have previously found a footer. If we have, set the current footer,
73+
// otherwise just record it.
74+
if currKeyValue != "" {
75+
foot.Notes = append(foot.Notes, newFooterNote(currKeyValue, currFooterValue))
76+
foot.FullFooter += messageLines[i-1] + "\n" // add previous line to FullFooter
77+
}
78+
79+
currKeyValue = key
80+
currFooterValue = value
81+
} else {
82+
if inFooters {
83+
currFooterValue = fmt.Sprintf("%s\n%s", currFooterValue, msgLine)
7284
} else {
73-
if inFooters {
74-
currFooterValue = fmt.Sprintf("%s\n%s", currFooterValue, msgLine)
85+
if commit.Body == "" {
86+
commit.Body = msgLine
7587
} else {
76-
if commit.Body == "" {
77-
commit.Body = msgLine
78-
} else {
79-
commit.Body += fmt.Sprintf("\n%s", msgLine)
80-
}
88+
commit.Body += fmt.Sprintf("\n%s", msgLine)
8189
}
8290
}
8391
}
@@ -96,7 +104,7 @@ func Parse(message string) (*Commit, error) {
96104
commit.Body = strings.TrimSpace(commit.Body)
97105
commit.Footer = foot
98106

99-
// Check if a footer contained a breaking change
107+
// Check if a footer contains a breaking change
100108
for _, footer := range commit.Footer.Notes {
101109
if footer.Token == "BREAKING CHANGE" || footer.Token == "BREAKING-CHANGE" {
102110
commit.BreakingChange = true
@@ -108,30 +116,32 @@ func Parse(message string) (*Commit, error) {
108116
}
109117

110118
// parseLineAsFooter attempts to parse the given line as a footer, returning both the key and the value of the header.
111-
// If the line cannot be parsed then both return values will be empty.
112-
func parseLineAsFooter(line string) (key, value string) {
119+
// If the line cannot be parsed then isFooter is false
120+
func parseLineAsFooter(line string) (key, value string, isFooter bool) {
113121
matches := footerRegexp.FindStringSubmatch(line)
114122
if len(matches) != 4 {
115-
return "", ""
123+
return "", "", false
116124
}
117125

118126
if matches[1] == "" {
119-
return matches[2], matches[3]
127+
return matches[2], matches[3], true
120128
}
121-
return matches[1], matches[3]
129+
return matches[1], matches[3], true
122130
}
123131

124132
// parseHeader attempts to parse the commit description line and set the appropriate values in the the given commit
125-
func parseHeader(header string, commit *Commit) error {
133+
func parseHeader(header string) (Header, bool, error) {
126134
matches := headerRegexp.FindStringSubmatch(header)
127135
if matches == nil {
128-
return errHeader
136+
return Header{}, false, errHeader
129137
}
130138

131139
head := Header{
132140
FullHeader: header,
133141
}
134142

143+
isBreakingChange := false
144+
135145
names := headerRegexp.SubexpNames()
136146
for i, match := range matches {
137147
switch names[i] {
@@ -143,12 +153,11 @@ func parseHeader(header string, commit *Commit) error {
143153
case "description":
144154
head.Description = match
145155
case "breaking":
146-
commit.BreakingChange = (match == "!")
156+
isBreakingChange = (match == "!")
147157
}
148158
}
149159

150-
commit.Header = head
151-
return nil
160+
return head, isBreakingChange, nil
152161
}
153162

154163
// IsHeaderErr checks if given error is header parse error

parser_header_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ func TestParseHeaderValid(t *testing.T) {
2424
testName := "case#" + strconv.Itoa(index+1)
2525
t.Run(testName, func(innerT *testing.T) {
2626
headerLine := strings.Split(validCase, "\n")[0]
27-
commit := &Commit{}
28-
err := parseHeader(headerLine, commit)
27+
_, _, err := parseHeader(headerLine)
2928
if err != nil {
3029
innerT.Error("parseHeader failed for", headerLine, err)
3130
return
@@ -52,8 +51,7 @@ func TestParseHeaderInvalid(t *testing.T) {
5251
testName := "case#" + strconv.Itoa(index+1)
5352
t.Run(testName, func(innerT *testing.T) {
5453
headerLine := strings.Split(validCase, "\n")[0]
55-
commit := &Commit{}
56-
err := parseHeader(headerLine, commit)
54+
_, _, err := parseHeader(headerLine)
5755
if err == nil {
5856
innerT.Error("parseHeader passed without error for", headerLine)
5957
}

0 commit comments

Comments
 (0)