Class: SuperHooks::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/super_hooks/hook.rb

Overview

Interface with the list of hooks available

Constant Summary collapse

LIST =

An array of existing git hooks

%w( applypatch-msg commit-msg post-applypatch post-checkout post-commit post-merge post-receive
pre-applypatch pre-auto-gc pre-commit prepare-commit-msg pre-rebase pre-receive update pre-push )

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Hook

The initializer

*path: the path to the executable



72
73
74
# File 'lib/super_hooks/hook.rb', line 72

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

The path to the executable



66
67
68
# File 'lib/super_hooks/hook.rb', line 66

def path
  @path
end

Class Method Details

.where(name: nil, kind: LIST, level: [:user, :project, :global]) ⇒ Object Also known as: all

Find a list of hooks by applying the levels to them

*name: the name of the path (can be partial) *kind: the type of hook. eg: pre-commit *level: the folder to search for hooks: [:user, :project, :global]

Example

# where(name: "rake", kind: ["pre-rebase", "pre-commit"], level: [:user])
#
# => [#<SuperHooks::Hook:0x007ffa32030758 @path="/home/franky/.git/git_hooks/pre-rebase/rake"]

Returns an array of Hooks



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/super_hooks/hook.rb', line 23

def where(name: nil, kind: LIST, level: [:user, :project, :global])
  hooks = [*level].map { |l| send("#{l}_hooks") }

  hooks.flatten!

  hooks.select! { |f| a_hook? f }

  hooks.select! { |hook| hook =~ /#{name}/ } unless name.nil?

  hooks.select! { |hook| [*kind].any? { |foo| hook =~ /#{foo}/ } }

  hooks.map { |hook| new(hook) }
end

Instance Method Details

#descriptionObject

Get a short description of the hook

It gets the description of the file by running the file name with the –about flag

Example

 # description
 #   => "A signoff commit msg",

Returns a string


101
102
103
# File 'lib/super_hooks/hook.rb', line 101

def description
  `#{path} --about`.chomp
end

#execute!(arguments = '') ⇒ Object Also known as: run

Acatully execute the hook

*arguments: the arguments passed from git

Example

 execute("GIT_HEAD_MSG")
 # => true

Returns a boolean indicating if this was a successfull run


86
87
88
# File 'lib/super_hooks/hook.rb', line 86

def execute!(arguments = '')
  system("#{path} #{arguments}", out: $stdout, err: $stderr)
end