Skip to content

Commit 58508f5

Browse files
author
Eugene Kalyada
committed
fix: (validation) Fix validation of commit message
Fixed messages Fixed requrement length Fixed footer length and requirements
1 parent 0bdbe04 commit 58508f5

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

gitcom/Commands/ValidateCommand.swift

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ Add message as parameter.
2323
public weak var delegate: ValidationResultDelegate?
2424

2525
private func checkType(header: Header, subject: String) -> Result<String, String> {
26+
2627
//get type
27-
guard let type = subject.split(separator: ":").first else {
28-
return Result.failure("Could not find type in subject commit.")
28+
guard subject.contains(":"), let type = subject.split(separator: ":").first else {
29+
return Result.failure("Could not find type of commit in message `\(subject)`.")
2930
}
3031
//check type
3132
guard let _ = header.type.items.first(where: { row in
3233
return row.key == type
3334
}) else {
34-
return Result.failure("Type \(type) is unavaliable.")
35+
return Result.failure("Type `\(type)` is unavaliable type of commit.")
3536
}
3637
return Result.success(String(type))
3738
}
@@ -41,7 +42,7 @@ Add message as parameter.
4142
let scope = subject.capturedGroups(withRegex: pattern).first
4243
//scope disabled but exists
4344
if scope != nil && !header.scope.enabled {
44-
return Result.failure("Scope is not avaliable for using.")
45+
return Result.failure("Sope is not avaliable, but found: `\(scope!)`")
4546
}
4647
//scope enabled
4748
guard header.scope.enabled else {
@@ -50,7 +51,7 @@ Add message as parameter.
5051

5152
//scope empty but not allowed to skip
5253
if scope == nil && !header.scope.skip {
53-
return Result.failure("Scope does not exist.")
54+
return Result.failure("Could not find scope of commit in message `\(subject)`)")
5455
}
5556
//allowed to skip and empty
5657
if scope == nil && header.scope.skip {
@@ -70,10 +71,10 @@ Add message as parameter.
7071
return Result.failure("Scope `\(sScope)` is not avaliable for this type `\(type)`.")
7172
}
7273
if !isAvaliableScope && header.customScope.enabled {
73-
if let min = header.customScope.length.min, min.value > sScope.count {
74+
if let min = header.customScope.length?.min, min.value > sScope.count {
7475
return Result.failure("Scope `\(sScope)` has small length.")
7576
}
76-
if let max = header.customScope.length.max, max.value < sScope.count {
77+
if let max = header.customScope.length?.max, max.value < sScope.count {
7778
return Result.failure("Scope `\(sScope)` has big length.")
7879
}
7980
}
@@ -84,17 +85,14 @@ Add message as parameter.
8485
return Result.success(sScope)
8586
}
8687

87-
private func checkHeaderLength(header: Header, subject: String, scope: String) -> Bool {
88-
guard let subjectString = subject.split(separator: ")")[safe: 1] else {
89-
print(error: "Commit subject must not be empty.")
90-
return false
91-
}
88+
private func checkHeaderLength(header: Header, subject: String, type: String, scope: String) -> Bool {
89+
let subjectString = subject.stringByReplacingFirstOccurrenceOfString(target: "\(type): (\(scope))", withString: "")
9290
if let min = header.length.min, min.value > subjectString.count {
93-
print(error: min.message)
91+
print(error: min.insufficient(prefix: "Subject"))
9492
return false
9593
}
9694
if let max = header.length.max, max.value < subjectString.count {
97-
print(error: max.message)
95+
print(error: max.excess(prefix: "Subject"))
9896
return false
9997
}
10098
return true
@@ -105,7 +103,7 @@ Add message as parameter.
105103
case let .success(type):
106104
switch checkScope(header: header, subject: subject, type: type) {
107105
case .success(let scope):
108-
return checkHeaderLength(header: header, subject: subject, scope: scope)
106+
return checkHeaderLength(header: header, subject: subject, type: type, scope: scope)
109107
case let .failure(message):
110108
print(error: message)
111109
}
@@ -122,11 +120,11 @@ Add message as parameter.
122120
return false
123121
}
124122
if let min = bodySettings.length.min, min.value > bodyString.count {
125-
print(error: min.message)
123+
print(error: min.insufficient(prefix: "Body"))
126124
return false
127125
}
128126
if let max = bodySettings.length.max, max.value < bodyString.count {
129-
print(error: max.message)
127+
print(error: max.excess(prefix: "Body"))
130128
return false
131129

132130
}
@@ -135,14 +133,22 @@ Add message as parameter.
135133

136134
private func validateFooter(footerSettings: Footer, footer: String?) -> Bool {
137135
guard var footerString = footer, !footerString.isEmpty else {
138-
if footerSettings.enabled {
136+
if let min = footerSettings.length.min, min.value > 0 {
139137
print(error: "Footer is enabled but empty.")
138+
return false
139+
}
140+
else {
141+
return true
140142
}
141-
return footerSettings.enabled ? false : true
143+
}
144+
145+
if !footerSettings.enabled {
146+
print(error: "Footer is not allowed, but has `\(footerString)`.")
147+
return false
142148
}
143149

144150
if !footerString.hasPrefix(footerSettings.prefix) {
145-
print(error: "Footer has wrong prefix.")
151+
print(error: "Footer has no has prefix `\(footerSettings.prefix)`.")
146152
return false
147153
}
148154

@@ -158,11 +164,11 @@ Add message as parameter.
158164
}
159165

160166
if let min = footerSettings.length.min, min.value > footerString.count {
161-
print(error: min.message)
167+
print(error: min.insufficient(prefix: "Footer"))
162168
return false
163169
}
164170
else if let max = footerSettings.length.max, max.value < footerString.count {
165-
print(error: max.message)
171+
print(error: max.excess(prefix: "Footer"))
166172
return false
167173
}
168174

@@ -212,20 +218,12 @@ Add message as parameter.
212218
}
213219

214220
func perform(arguments: [String]) {
215-
// let isSilent = arguments.contains("--silent")
216-
var parts = arguments.first?.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "|")
217-
if let commitParts = parts, commitParts.count > 1 {
218-
processCommitParts(parts: commitParts)
219-
}
220-
else {
221-
parts = arguments.first?.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n")
222-
if let commitParts = parts, commitParts.count > 1 {
223-
processCommitParts(parts: commitParts)
224-
}
225-
else {
226-
print(error(errorMessage: "Commit message does not exist"))
227-
exit(1)
228-
}
221+
guard var message = arguments.first else {
222+
print(error: "Commit message does not exist")
223+
exit(1)
229224
}
225+
message = message.replacingOccurrences(of: "|", with: "\n")
226+
let parts = message.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n")
227+
processCommitParts(parts: parts)
230228
}
231229
}

gitcom/Helpers.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ extension String {
3333

3434
return results
3535
}
36+
37+
func stringByReplacingFirstOccurrenceOfString(target: String, withString replaceString: String) -> String {
38+
if let range = self.range(of: target) {
39+
return self.replacingCharacters(in: range, with: replaceString)
40+
}
41+
return self
42+
}
3643
}
3744

3845
public extension Array {

0 commit comments

Comments
 (0)