Class: WorktreeManager::HookManager

Inherits:
Object
  • Object
show all
Defined in:
lib/worktree_manager/hook_manager.rb

Constant Summary collapse

HOOK_TYPES =
%w[pre_add post_add pre_remove post_remove].freeze
DEFAULT_HOOK_FILES =
[
  '.worktree.yml',
  '.git/.worktree.yml'
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(repository_path = '.', verbose: false) ⇒ HookManager

Returns a new instance of HookManager.



12
13
14
15
16
# File 'lib/worktree_manager/hook_manager.rb', line 12

def initialize(repository_path = '.', verbose: false)
  @repository_path = File.expand_path(repository_path)
  @verbose = verbose
  @hooks = load_hooks
end

Instance Method Details

#execute_hook(hook_type, context = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/worktree_manager/hook_manager.rb', line 18

def execute_hook(hook_type, context = {})
  log_debug("🪝 Starting hook execution: #{hook_type}")

  return true unless HOOK_TYPES.include?(hook_type.to_s)
  return true unless @hooks.key?(hook_type.to_s)

  hook_config = @hooks[hook_type.to_s]
  return true if hook_config.nil? || hook_config.empty?

  log_debug("📋 Hook configuration: #{hook_config.inspect}")
  log_debug("🔧 Context: #{context.inspect}")

  result = case hook_config
           when String
             execute_command(hook_config, context, hook_type)
           when Array
             hook_config.all? { |command| execute_command(command, context, hook_type) }
           when Hash
             execute_hook_hash(hook_config, context, hook_type)
           else
             true
           end

  log_debug("✅ Hook execution completed: #{hook_type} (result: #{result})")
  result
end

#has_hook?(hook_type) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/worktree_manager/hook_manager.rb', line 45

def has_hook?(hook_type)
  HOOK_TYPES.include?(hook_type.to_s) &&
    @hooks.key?(hook_type.to_s) &&
    !@hooks[hook_type.to_s].nil?
end

#list_hooksObject



51
52
53
# File 'lib/worktree_manager/hook_manager.rb', line 51

def list_hooks
  @hooks.select { |type, config| HOOK_TYPES.include?(type) && !config.nil? }
end