Class: Gitty::HookCommand::List

Inherits:
Runner show all
Includes:
FileUtils, Gitty::Helpers
Defined in:
lib/gitty/commands/list.rb

Constant Summary collapse

KINDS =
[:local, :shared, :uninstalled]

Instance Attribute Summary

Attributes inherited from Runner

#args, #stderr, #stdout

Instance Method Summary collapse

Methods included from Gitty::Helpers

#cmd, #existing_directory!, #file_with_existing_directory!, #search_for_file_in_paths, #with_env_var

Methods inherited from Runner

#handle_show_help, #initialize, #options, run

Constructor Details

This class inherits a constructor from Gitty::Runner

Instance Method Details

#all_hooksObject



14
15
16
# File 'lib/gitty/commands/list.rb', line 14

def all_hooks
  @all_hooks = Gitty::Hook.find_all
end

#listify(hooks) ⇒ Object



30
31
32
# File 'lib/gitty/commands/list.rb', line 30

def listify(hooks)
  hooks.map { |h| "- #{h}" }.join("\n")
end

#option_parserObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitty/commands/list.rb', line 41

def option_parser
  super.tap do |opts|
    opts.banner = "Usage: git hook list"
    opts.on("-l", "--local", "Show local hooks") do
      options[:local] = true
    end
    opts.on("-s", "--shared", "Show shared hooks") do
      options[:shared] = true
    end
    opts.on("-u", "--uninstalled", "Show uninstalled hooks") do
      options[:uninstalled] = true
    end
  end
end

#runObject



7
8
9
10
11
12
# File 'lib/gitty/commands/list.rb', line 7

def run
  stdout.puts "Listing hooks"
  which_to_show = KINDS.select { |k| options[k] }
  which_to_show = KINDS if which_to_show.empty? # default to everything
  which_to_show.each { |w| show_hooks(w) }
end

#show_hooks(which) ⇒ Object



18
19
20
21
22
23
# File 'lib/gitty/commands/list.rb', line 18

def show_hooks(which)
  case which
  when :local, :shared then show_local_or_shared_hooks(which)
  when :uninstalled then show_uninstalled_hooks
  end
end

#show_local_or_shared_hooks(which) ⇒ Object



25
26
27
28
# File 'lib/gitty/commands/list.rb', line 25

def show_local_or_shared_hooks(which)
  hook_names = all_hooks.select { |h| h.install_kind == which.to_sym }.map(&:name)
  puts "#{which}:\n#{listify(hook_names)}\n\n"
end

#show_uninstalled_hooksObject



34
35
36
37
38
39
# File 'lib/gitty/commands/list.rb', line 34

def show_uninstalled_hooks
  available_hook_names = all_hooks.select { |h| ! h.installed? }.map(&:name)
  installed_hook_names = all_hooks.select { |h| h.installed? }.map(&:name)
  uninstalled_hooks = (available_hook_names - installed_hook_names).sort
  puts "uninstalled:\n#{listify(uninstalled_hooks)}\n\n"
end