Skip to content

Commit

Permalink
Merge pull request #54 from minuscorp/install-desired-hooks
Browse files Browse the repository at this point in the history
Install only desired hooks
  • Loading branch information
orta authored Mar 3, 2022
2 parents a315206 + d8267e0 commit dedffea
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
77 changes: 49 additions & 28 deletions Sources/Komondor/Commands/install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,56 @@ import ShellOut

/// The available hooks for git
///
let hookList = [
"applypatch-msg",
"pre-applypatch",
"post-applypatch",
"pre-commit",
"prepare-commit-msg",
"commit-msg",
"post-commit",
"pre-rebase",
"post-checkout",
"post-merge",
"pre-push",
"pre-receive",
"update",
"post-receive",
"post-update",
"push-to-checkout",
"pre-auto-gc",
"post-rewrite",
"sendemail-validate"
public enum Hooks: String, CaseIterable {
case applyPatchMsg = "applypatch-msg"
case preApplyPatch = "pre-applypatch"
case postApplyPatch = "post-applypatch"
case preCommit = "pre-commit"
case prepareCommitMsg = "prepare-commit-msg"
case commitMsg = "commit-msg"
case postCommit = "post-commit"
case preRebase = "pre-rebase"
case postCheckout = "post-checkout"
case postMerge = "post-merge"
case prePush = "pre-push"
case preReceive = "pre-receive"
case update
case postReceive = "post-receive"
case postUpdate = "post-update"
case pushToCheckout = "push-to-checkout"
case preAutoGc = "pre-auto-gc"
case postRewrite = "post-rewrite"
case sendEmailValidate = "sendemail-validate"
}

public let hookList: [Hooks] = [
.applyPatchMsg,
.preApplyPatch,
.postApplyPatch,
.preCommit,
.prepareCommitMsg,
.preRebase,
.postCheckout,
.postMerge,
.prePush,
.preReceive,
.update,
.postReceive,
.postUpdate,
.pushToCheckout,
.preAutoGc,
.postRewrite,
.sendEmailValidate
]

let skippableHooks = [
"commit-msg",
"pre-commit",
"pre-rebase",
"pre-push"
let skippableHooks: [Hooks] = [
.commitMsg,
.preCommit,
.preRebase,
.prePush
]

public func install(logger _: Logger) throws {
public func install(hooks: [Hooks] = hookList, logger _: Logger) throws {
// Add a skip env var
let env = ProcessInfo.processInfo.environment
if env["SKIP_KOMONDOR"] != nil {
Expand Down Expand Up @@ -80,7 +100,8 @@ public func install(logger _: Logger) throws {
}

// Copy in the komondor templates
try hookList.forEach { hookName in
let hooksToInstall = hooks.isEmpty ? hookList : hooks
try hooksToInstall.map(\.rawValue).forEach { hookName in
let hookPath = hooksRoot.appendingPathComponent(hookName)

// Separate header from script so we can
Expand Down Expand Up @@ -114,5 +135,5 @@ public func install(logger _: Logger) throws {
}
}
}
print("[Komondor] git-hooks installed")
print("[Komondor] git-hooks installed" + (hooks.isEmpty ? "" : ": \(hooks.map(\.rawValue))"))
}
2 changes: 1 addition & 1 deletion Sources/Komondor/Commands/runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public func runner(logger _: Logger, args: [String]) throws {
print(error.message)
print(error.output)

let noVerifyMessage = skippableHooks.contains(hook) ? "add --no-verify to skip" : "cannot be skipped due to Git specs"
let noVerifyMessage = skippableHooks.map(\.rawValue).contains(hook) ? "add --no-verify to skip" : "cannot be skipped due to Git specs"
print("[Komondor] > \(hook) hook failed (\(noVerifyMessage))")
exit(error.terminationStatus)
} catch {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Komondor/Commands/uninstall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public func uninstall(logger: Logger) throws {
exit(0)
}

try hookList.forEach { hookName in
try hookList.map(\.rawValue).forEach { hookName in
var hookPath = URL(fileURLWithPath: hooksRoot.absoluteString)
hookPath.appendPathComponent(hookName)

Expand Down
8 changes: 5 additions & 3 deletions Sources/Komondor/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ guard cliLength > 1 else {
print("""
Welcome to Komondor, it has 3 commands:
- `swift run komondor install` sets up your git repo to use Komondor
- `swift run komondor run` used by the git-hooks to run your hooks
- `swift run komondor install [pre-commit post-checkout pre-rebase ...]` sets up your git repo to use Komondor
- `swift run komondor run [hook-name]` used by the git-hooks to run your hooks
- `swift run komondor uninstall` removes git-hooks created by Komondor
Docs are available at: https://github.com/shibapm/Komondor
Expand All @@ -27,7 +27,9 @@ let task = CommandLine.arguments[1]

switch task {
case "install":
try install(logger: logger)
let hooks = Array(CommandLine.arguments.dropFirst(2))
.compactMap { Hooks(rawValue: $0) }
try install(hooks: hooks, logger: logger)
case "run":
let runnerArgs = Array(CommandLine.arguments.dropFirst().dropFirst())
try runner(logger: logger, args: runnerArgs)
Expand Down

0 comments on commit dedffea

Please sign in to comment.