Skip to content

Commit bee7127

Browse files
committed
add integration to usr/bin dir
add commit-msg hook setup
1 parent 5eb2cd4 commit bee7127

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed
Binary file not shown.

gitcom/Commands/IntegrateCommand.swift

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,68 @@ class IntegrateCommand: CommandProtocol {
1313

1414
var description: String = """
1515
Add hook to current repository for checking commit messages
16-
Add command to your system for creating beautiful commit (`git cmt`)
16+
Add command to your system for creating beautiful commit (`gitcom`)
1717
"""
18+
19+
var commitMessageHook: String = """
20+
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
21+
message=`cat $1`
22+
messageWithNewLines=${message//|/"\\n"}
23+
gitcom validate "$message"
24+
"""
25+
26+
private func copyExecutableToBinFolder() {
27+
let executableURLinBin = URL(fileURLWithPath: "/usr/local/bin" + Constants.executableNamePath)
28+
let currentDirPath = FileManager.default.currentDirectoryPath
29+
let executableURL = URL(fileURLWithPath: currentDirPath + Constants.executableNamePath)
30+
do {
31+
if FileManager.default.fileExists(atPath: executableURLinBin.path) {
32+
try FileManager.default.removeItem(at: executableURLinBin)
33+
}
34+
try FileManager.default.copyItem(at: executableURL, to: executableURLinBin)
35+
}
36+
catch let error {
37+
print(error: error.localizedDescription)
38+
exit(1)
39+
}
40+
}
41+
42+
private func changeCommitMessageFile() {
43+
let commitMessageSampleFilePath = FileManager.default.currentDirectoryPath + "/.git/hooks/" + Constants.gitCommitMsgHookSampleFileName
44+
let commitMessageHookFilePath = FileManager.default.currentDirectoryPath + "/.git/hooks/" + Constants.gitCommitMsgHookFileName
45+
do {
46+
if FileManager.default.fileExists(atPath: commitMessageHookFilePath) {
47+
print(error: "\(Constants.gitCommitMsgHookFileName) already exists.")
48+
exit(1)
49+
}
50+
try FileManager.default.moveItem(atPath: commitMessageSampleFilePath, toPath: commitMessageHookFilePath)
51+
appendValidationHook(sampleFilePath: commitMessageSampleFilePath, hookFilePath: commitMessageHookFilePath)
52+
}
53+
catch let error {
54+
print(error: error.localizedDescription)
55+
exit(1)
56+
}
57+
}
58+
59+
private func appendValidationHook(sampleFilePath: String, hookFilePath: String) {
60+
guard let data = commitMessageHook.data(using: .utf8, allowLossyConversion: false) else { return }
61+
62+
if FileManager.default.fileExists(atPath: hookFilePath) {
63+
if let fileHandle = FileHandle(forWritingAtPath: hookFilePath) {
64+
fileHandle.seekToEndOfFile()
65+
fileHandle.write(data)
66+
fileHandle.closeFile()
67+
print("\("Integration completed successfully.".color(.green))\("".color(.default))")
68+
}
69+
else {
70+
print(error: "Can't open file \(hookFilePath)")
71+
}
72+
}
73+
}
74+
1875
func perform(arguments: [String]) {
19-
print(error: "NOT READY")
76+
copyExecutableToBinFolder()
77+
changeCommitMessageFile()
2078
}
2179

2280
}

gitcom/Commands/ValidateCommand.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,13 @@ Add message as parameter.
216216
var parts = arguments.first?.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "|")
217217
if let commitParts = parts, commitParts.count > 1 {
218218
processCommitParts(parts: commitParts)
219+
print("\("Commit message is valid".color(.green))\("".color(.default))")
219220
}
220221
else {
221222
parts = arguments.first?.trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n")
222223
if let commitParts = parts, commitParts.count > 1 {
223224
processCommitParts(parts: commitParts)
225+
print("\("Commit message is valid".color(.green))\("".color(.default))")
224226
}
225227
else {
226228
print(error(errorMessage: "Commit message does not exist"))

gitcom/Constants.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ class Constants {
1515
static let customScopeKey = "custom"
1616
static let customScopeIndex = "0"
1717
static let messageArgument = "-m"
18+
static let gitCommitMsgHookFileName = "commit-msg"
19+
static let gitCommitMsgHookSampleFileName = "commit-msg.sample"
20+
static let executableNamePath = "/gitcom"
1821
}

gitcom/main.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ run.commands.append(CommitCommand())
4747
run.commands.append(IntegrateCommand())
4848
run.commands.append(ValidateCommand())
4949

50-
5150
var arguments = CommandLine.arguments
5251
arguments.removeFirst()
5352

0 commit comments

Comments
 (0)