From cd7468b3110623d7f3e6f70e1377ec3e1c100932 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:15:49 -0800 Subject: [PATCH 1/9] [CPBugFix-003][UpdateHook] Made the Shabang more robust by grabbing Swift from the users env --- Sources/CommitPrefix/Hook/CommitMessageHookContents.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift b/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift index 428a112..8e25f23 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 // From 5cbaa92fb7ed22e992f7ae4b331e07bd1fb42a87 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:17:52 -0800 Subject: [PATCH 2/9] [CPBugFix-003][UpdateHook] Updated the environment PATH variable so that commitPrefix can be called from the hook when GUI based applications activate it. --- Sources/CommitPrefix/Hook/CommitMessageHookContents.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift b/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift index 8e25f23..92cb6c8 100644 --- a/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift +++ b/Sources/CommitPrefix/Hook/CommitMessageHookContents.swift @@ -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() From 5e00ceca9b40d28c2480eff86ed5d8141a7859a2 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:21:08 -0800 Subject: [PATCH 3/9] [CPBugFix-003][UpdateHook] Removed the current branch from the Shell helper due to complications of calling this command line utility from a GUI application and reopening a process where the context is lost. Using the HEAD file instead. --- Sources/CommitPrefix/Utilities/Shell.swift | 23 ---------------------- 1 file changed, 23 deletions(-) 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 - } - } From f9887d0a18fe26cfa01437bbd9422da28a995082 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:21:29 -0800 Subject: [PATCH 4/9] [CPBugFix-003][UpdateHook] Updated the patch number for this fix --- Sources/CommitPrefix/Constants.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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" } From 4e43508c154c692ee1aea02051fd7ebd2cb08633 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:22:35 -0800 Subject: [PATCH 5/9] [CPBugFix-003][UpdateHook] Added an error and message for reading the HEAD file --- Sources/CommitPrefix/Error+Debug/CPError.swift | 3 +++ 1 file changed, 3 insertions(+) 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" } } From 261bc7332daf86b66f040ecd3c8c1bce6e1c0250 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:24:10 -0800 Subject: [PATCH 6/9] [CPBugFix-003][UpdateHook] Updated the initializer for the CPInteractor to be able to grab the git HEAD file. --- Sources/CommitPrefix/CPInteractor.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/CommitPrefix/CPInteractor.swift b/Sources/CommitPrefix/CPInteractor.swift index 35990ee..9137445 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 From 45c2c7544be6d78545b93f9b914865c5c09c5581 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:25:20 -0800 Subject: [PATCH 7/9] [CPBugFix-003][UpdateHook] Read the HEAD file for branch info and applied the regex to the extracted value. --- Sources/CommitPrefix/CPInteractor.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/CommitPrefix/CPInteractor.swift b/Sources/CommitPrefix/CPInteractor.swift index 9137445..0a2476a 100644 --- a/Sources/CommitPrefix/CPInteractor.swift +++ b/Sources/CommitPrefix/CPInteractor.swift @@ -73,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 { From a03d09e2b3fddcc6434453ca4a445db4bdd697e1 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:25:57 -0800 Subject: [PATCH 8/9] [CPBugFix-003][UpdateHook] Updated the main catch section with unexpected errors catch --- Sources/CommitPrefix/main.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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) + } - From 5d4f126e6f1c421914dc56fa5f2c06f5bda48f60 Mon Sep 17 00:00:00 2001 From: Stephen Martinez Date: Wed, 18 Dec 2019 19:31:38 -0800 Subject: [PATCH 9/9] [CPBugFix-003][UpdateDocs] Updated the readme with the ability to work with GUI Clients --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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