Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions Sources/CommitPrefix/CPInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/CommitPrefix/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation

struct CPInfo {

static let version = "1.3.1"
static let version = "1.3.2"

}

Expand Down
3 changes: 3 additions & 0 deletions Sources/CommitPrefix/Error+Debug/CPError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ enum CPTermination: Error {
case expectedYesOrNo
case branchValidatorNotPresent
case invalidBranchPrefix(validator: String)
case unableToReadHEAD

var message: String {
switch self {
Expand All @@ -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"
}

}
Expand Down
8 changes: 7 additions & 1 deletion Sources/CommitPrefix/Hook/CommitMessageHookContents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct CommitMessageHookContents {
}

func renderScript() -> String { """
#!/usr/bin/swift
#!/usr/bin/env swift
//
// Commit-msg
//
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 0 additions & 23 deletions Sources/CommitPrefix/Utilities/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

}
6 changes: 5 additions & 1 deletion Sources/CommitPrefix/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@ do {
print(terminationError.message)
exit(0)

} catch {

print("Unexpected Error: ", error)
exit(0)

}