diff --git a/README.md b/README.md index 19c63fe..5d3272e 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ There's also a branch parse mode that allows commitPrefix to parse the current b Prefixes can be re-assigned or deleted at any time. Additionally, this is a git repository specific tool, meaning that stored prefixes are specific to the repository you're in. +CommitPrefix has been verified to work both from the command line as well as with GUI based Git Clients like Tower. + The actions that can be done are: * Store an arbitrary number of commit prefixes diff --git a/Sources/CommitPrefix/CPInteractor.swift b/Sources/CommitPrefix/CPInteractor.swift index 35990ee..0a2476a 100644 --- a/Sources/CommitPrefix/CPInteractor.swift +++ b/Sources/CommitPrefix/CPInteractor.swift @@ -31,22 +31,26 @@ struct CPInteractor { private let commitPrefixFile: File private let commitPrefixModel: CPModel + private let gitHEADFile: File init (gitDirectory: Folder) throws { - let (commitPrefixFile, commitPrefixModel) = try Self.build(using: gitDirectory) + let (commitPrefixFile, commitPrefixModel, gitHEADFile) = try Self.build(using: gitDirectory) self.commitPrefixFile = commitPrefixFile self.commitPrefixModel = commitPrefixModel + self.gitHEADFile = gitHEADFile } - private static func build(using gitDirectory: Folder) throws -> (File, CPModel) { + private static func build(using gitDirectory: Folder) throws -> (File, CPModel, File) { do { let initialModelData = try JSONEncoder().encode(CPModel.empty()) let cpFile = try gitDirectory.createFileIfNeeded( withName: FileName.commitPrefix, - contents: initialModelData) + contents: initialModelData + ) let cpFileData = try cpFile.read() let cpModel = try JSONDecoder().decode(CPModel.self, from: cpFileData) - return (cpFile, cpModel) + let headFile = try gitDirectory.file(named: "HEAD") + return (cpFile, cpModel, headFile) } catch { cpDebugPrint(error) throw CPError.fileReadWriteError @@ -69,7 +73,10 @@ struct CPInteractor { throw CPTermination.branchValidatorNotPresent } - let branch = Shell.currentBranch() ?? "" + guard let branch = try? gitHEADFile.readAsString(encodedAs: .utf8) else { + throw CPTermination.unableToReadHEAD + } + let matches = branch.occurances(ofRegex: regexValue) guard matches.count > 0 else { diff --git a/Sources/CommitPrefix/Constants.swift b/Sources/CommitPrefix/Constants.swift index bc76ae9..09748ca 100644 --- a/Sources/CommitPrefix/Constants.swift +++ b/Sources/CommitPrefix/Constants.swift @@ -28,7 +28,7 @@ import Foundation struct CPInfo { - static let version = "1.3.1" + static let version = "1.3.2" } diff --git a/Sources/CommitPrefix/Error+Debug/CPError.swift b/Sources/CommitPrefix/Error+Debug/CPError.swift index c92514f..f919169 100644 --- a/Sources/CommitPrefix/Error+Debug/CPError.swift +++ b/Sources/CommitPrefix/Error+Debug/CPError.swift @@ -72,6 +72,7 @@ enum CPTermination: Error { case expectedYesOrNo case branchValidatorNotPresent case invalidBranchPrefix(validator: String) + case unableToReadHEAD var message: String { switch self { @@ -86,6 +87,8 @@ enum CPTermination: Error { Your branch does not begin with \(validator) and is invalid. Either change your branch name or use commitPrefix in non-branch mode. """ + case .unableToReadHEAD: + return "Unable to read the git HEAD for branch information" } } diff --git a/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift b/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift index 428a112..92cb6c8 100644 --- a/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift +++ b/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift @@ -37,7 +37,7 @@ struct CommitMessageHookContents { } func renderScript() -> String { """ - #!/usr/bin/swift + #!/usr/bin/env swift // // Commit-msg // @@ -98,6 +98,12 @@ struct CommitMessageHookContents { func getPrefixes() -> String { let readProcess = Process() readProcess.launchPath = "/usr/bin/env" + + var readProcessEnv = ProcessInfo.processInfo.environment + let paths = readProcessEnv["PATH"] + paths.map { readProcessEnv["PATH"] = "/usr/local/bin:\\($0)" } + + readProcess.environment = readProcessEnv readProcess.arguments = ["commitPrefix", "-o"] let pipe = Pipe() diff --git a/Sources/CommitPrefix/Utilities/Shell.swift b/Sources/CommitPrefix/Utilities/Shell.swift index 94b8497..8cace1d 100644 --- a/Sources/CommitPrefix/Utilities/Shell.swift +++ b/Sources/CommitPrefix/Utilities/Shell.swift @@ -31,7 +31,6 @@ struct Shell { static func makeExecutable(_ fileName: String) { let executableProcess = Process() executableProcess.launchPath = "/usr/bin/env" - cpDebugPrint(executableProcess.launchPath ?? "nil") executableProcess.arguments = ["chmod", "755", fileName] let pipe = Pipe() @@ -41,26 +40,4 @@ struct Shell { executableProcess.waitUntilExit() } - static func currentBranch() -> String? { - let gitProcess = Process() - gitProcess.launchPath = "/usr/bin/env" - gitProcess.arguments = ["git", "rev-parse", "--abbrev-ref", "HEAD"] - - let pipe = Pipe() - gitProcess.standardOutput = pipe - gitProcess.launch() - - gitProcess.waitUntilExit() - - let data = pipe.fileHandleForReading.readDataToEndOfFile() - let branchName = String(data: data, encoding: .utf8) - let trimmedBranchName = branchName?.trimmingCharacters(in: .whitespacesAndNewlines) - - if trimmedBranchName == nil { - cpDebugPrint("Unable to get branch") - } - - return trimmedBranchName - } - } diff --git a/Sources/CommitPrefix/main.swift b/Sources/CommitPrefix/main.swift index 186cfec..4bb1576 100644 --- a/Sources/CommitPrefix/main.swift +++ b/Sources/CommitPrefix/main.swift @@ -77,5 +77,9 @@ do { print(terminationError.message) exit(0) +} catch { + + print("Unexpected Error: ", error) + exit(0) + } -