Skip to content

Commit 7a9f02c

Browse files
authored
Merge pull request #7 from enuance/CPBugFix-003-GitGuiClientsNotCallingHooks
CPBugFix-003 Git GUI Clients not calling hooks
2 parents 769c11b + 5d4f126 commit 7a9f02c

File tree

7 files changed

+30
-31
lines changed

7 files changed

+30
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ There's also a branch parse mode that allows commitPrefix to parse the current b
1212

1313
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.
1414

15+
CommitPrefix has been verified to work both from the command line as well as with GUI based Git Clients like Tower.
16+
1517
The actions that can be done are:
1618

1719
* Store an arbitrary number of commit prefixes

Sources/CommitPrefix/CPInteractor.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@ struct CPInteractor {
3131

3232
private let commitPrefixFile: File
3333
private let commitPrefixModel: CPModel
34+
private let gitHEADFile: File
3435

3536
init (gitDirectory: Folder) throws {
36-
let (commitPrefixFile, commitPrefixModel) = try Self.build(using: gitDirectory)
37+
let (commitPrefixFile, commitPrefixModel, gitHEADFile) = try Self.build(using: gitDirectory)
3738
self.commitPrefixFile = commitPrefixFile
3839
self.commitPrefixModel = commitPrefixModel
40+
self.gitHEADFile = gitHEADFile
3941
}
4042

41-
private static func build(using gitDirectory: Folder) throws -> (File, CPModel) {
43+
private static func build(using gitDirectory: Folder) throws -> (File, CPModel, File) {
4244
do {
4345
let initialModelData = try JSONEncoder().encode(CPModel.empty())
4446
let cpFile = try gitDirectory.createFileIfNeeded(
4547
withName: FileName.commitPrefix,
46-
contents: initialModelData)
48+
contents: initialModelData
49+
)
4750
let cpFileData = try cpFile.read()
4851
let cpModel = try JSONDecoder().decode(CPModel.self, from: cpFileData)
49-
return (cpFile, cpModel)
52+
let headFile = try gitDirectory.file(named: "HEAD")
53+
return (cpFile, cpModel, headFile)
5054
} catch {
5155
cpDebugPrint(error)
5256
throw CPError.fileReadWriteError
@@ -69,7 +73,10 @@ struct CPInteractor {
6973
throw CPTermination.branchValidatorNotPresent
7074
}
7175

72-
let branch = Shell.currentBranch() ?? ""
76+
guard let branch = try? gitHEADFile.readAsString(encodedAs: .utf8) else {
77+
throw CPTermination.unableToReadHEAD
78+
}
79+
7380
let matches = branch.occurances(ofRegex: regexValue)
7481

7582
guard matches.count > 0 else {

Sources/CommitPrefix/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Foundation
2828

2929
struct CPInfo {
3030

31-
static let version = "1.3.1"
31+
static let version = "1.3.2"
3232

3333
}
3434

Sources/CommitPrefix/Error+Debug/CPError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ enum CPTermination: Error {
7272
case expectedYesOrNo
7373
case branchValidatorNotPresent
7474
case invalidBranchPrefix(validator: String)
75+
case unableToReadHEAD
7576

7677
var message: String {
7778
switch self {
@@ -86,6 +87,8 @@ enum CPTermination: Error {
8687
Your branch does not begin with \(validator) and is invalid.
8788
Either change your branch name or use commitPrefix in non-branch mode.
8889
"""
90+
case .unableToReadHEAD:
91+
return "Unable to read the git HEAD for branch information"
8992
}
9093

9194
}

Sources/CommitPrefix/Hook/CommitMessageHookContents.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct CommitMessageHookContents {
3737
}
3838

3939
func renderScript() -> String { """
40-
#!/usr/bin/swift
40+
#!/usr/bin/env swift
4141
//
4242
// Commit-msg
4343
//
@@ -98,6 +98,12 @@ struct CommitMessageHookContents {
9898
func getPrefixes() -> String {
9999
let readProcess = Process()
100100
readProcess.launchPath = "/usr/bin/env"
101+
102+
var readProcessEnv = ProcessInfo.processInfo.environment
103+
let paths = readProcessEnv["PATH"]
104+
paths.map { readProcessEnv["PATH"] = "/usr/local/bin:\\($0)" }
105+
106+
readProcess.environment = readProcessEnv
101107
readProcess.arguments = ["commitPrefix", "-o"]
102108
103109
let pipe = Pipe()

Sources/CommitPrefix/Utilities/Shell.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ struct Shell {
3131
static func makeExecutable(_ fileName: String) {
3232
let executableProcess = Process()
3333
executableProcess.launchPath = "/usr/bin/env"
34-
cpDebugPrint(executableProcess.launchPath ?? "nil")
3534
executableProcess.arguments = ["chmod", "755", fileName]
3635

3736
let pipe = Pipe()
@@ -41,26 +40,4 @@ struct Shell {
4140
executableProcess.waitUntilExit()
4241
}
4342

44-
static func currentBranch() -> String? {
45-
let gitProcess = Process()
46-
gitProcess.launchPath = "/usr/bin/env"
47-
gitProcess.arguments = ["git", "rev-parse", "--abbrev-ref", "HEAD"]
48-
49-
let pipe = Pipe()
50-
gitProcess.standardOutput = pipe
51-
gitProcess.launch()
52-
53-
gitProcess.waitUntilExit()
54-
55-
let data = pipe.fileHandleForReading.readDataToEndOfFile()
56-
let branchName = String(data: data, encoding: .utf8)
57-
let trimmedBranchName = branchName?.trimmingCharacters(in: .whitespacesAndNewlines)
58-
59-
if trimmedBranchName == nil {
60-
cpDebugPrint("Unable to get branch")
61-
}
62-
63-
return trimmedBranchName
64-
}
65-
6643
}

Sources/CommitPrefix/main.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,9 @@ do {
7777
print(terminationError.message)
7878
exit(0)
7979

80+
} catch {
81+
82+
print("Unexpected Error: ", error)
83+
exit(0)
84+
8085
}
81-

0 commit comments

Comments
 (0)