Skip to content

Commit 4d7acec

Browse files
committed
[CPFeature-001] Replaced the bash script with a swift script YAYYYYY
1 parent d9185cb commit 4d7acec

File tree

1 file changed

+81
-15
lines changed

1 file changed

+81
-15
lines changed

Sources/CommitPrefix/CommitMessageHook.swift

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,90 @@ public struct CommitMessageHook {
4747
}
4848

4949
private var contents: String { """
50-
#!/bin/sh
51-
#
52-
# Commit-msg
53-
#
54-
# \(fileIdentifier) on \(currentDate)
55-
#
50+
#!/usr/bin/swift
51+
//
52+
// Commit-msg
53+
//
54+
// \(fileIdentifier) on \(currentDate)
55+
//
5656
57-
# Get the current directory and store it
58-
currentDirectory=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
57+
import Foundation
5958
60-
# Locate the Commit Prefix file and read/store its contents
61-
prefix=$( echo $( cat $currentDirectory/../commitPrefix.txt ) )
62-
63-
# Read and store the contents of the original commit message
64-
message=$( echo $( cat $1 ) )
59+
enum IOError: Error {
60+
61+
case invalidArgument
62+
case overwriteError
63+
64+
var message: String {
65+
switch self {
66+
case .invalidArgument:
67+
return "Intended to recieve .git/COMMIT_EDITMSG arg"
68+
case .overwriteError:
69+
return "There was an error writting to the commit message"
70+
}
71+
}
72+
73+
}
74+
75+
struct IOHelper {
76+
77+
let commitMsgPath: String
78+
let prefixPath = ".git/CommitPrefix.txt"
79+
80+
init(filePath: [String] = Array(CommandLine.arguments.dropFirst())) throws {
81+
guard let firstArg = filePath.first else {
82+
throw IOError.invalidArgument
83+
}
84+
self.commitMsgPath = firstArg
85+
}
86+
87+
func readContents(of filePath: String) -> String {
88+
let readProcess = Process()
89+
readProcess.launchPath = "/usr/bin/env"
90+
readProcess.arguments = ["cat", filePath]
91+
92+
let pipe = Pipe()
93+
readProcess.standardOutput = pipe
94+
readProcess.launch()
95+
96+
readProcess.waitUntilExit()
97+
98+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
99+
let contents = String(data: data, encoding: .utf8)
100+
101+
return contents ?? ""
102+
}
103+
104+
func overwriteContents(with contents: String) throws {
105+
do {
106+
try contents.write(toFile: commitMsgPath, atomically: true, encoding: .utf8)
107+
} catch {
108+
throw IOError.overwriteError
109+
}
110+
}
111+
112+
}
113+
114+
115+
do {
116+
117+
let helper = try IOHelper()
118+
119+
let commitMessage = helper.readContents(of: helper.commitMsgPath)
120+
.trimmingCharacters(in: .newlines)
121+
122+
let prefixMessage = helper.readContents(of: helper.prefixPath)
123+
.trimmingCharacters(in: .newlines)
124+
125+
let newCommitMessage = [prefixMessage, commitMessage].joined(separator: " ")
126+
try helper.overwriteContents(with: newCommitMessage)
127+
128+
} catch let ioError as IOError {
129+
130+
print(ioError)
131+
132+
}
65133
66-
# Build the prepended message and overwrite the commit message
67-
echo "$( echo $prefix ) $( echo $message )" > $1
68134
"""
69135
}
70136

0 commit comments

Comments
 (0)