Class: GitHooks::Repository::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/githooks/repository/config.rb

Constant Summary collapse

OPTIONS =
{
  'hooks-path'       => { type: :path, multiple: false },
  'script'           => { type: :path, multiple: false },
  'pre-run-execute'  => { type: :path, multiple: true },
  'post-run-execute' => { type: :path, multiple: true },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ Config

Returns a new instance of Config.



40
41
42
43
# File 'lib/githooks/repository/config.rb', line 40

def initialize(repository)
  @repository = repository
  @config = nil
end

Instance Method Details

#[](option) ⇒ Object



45
46
47
# File 'lib/githooks/repository/config.rb', line 45

def [](option)
  send(option.to_s.tr('-', '_'))
end

#get(option, options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/githooks/repository/config.rb', line 111

def get(option, options = {})
  option = normalize_option(option)

  begin
    repo = options[:repo_path] || @repository.path
    return unless (value = list(options)['githooks'][repo.to_s][option])
    OPTIONS[option][:type] == :path ? Pathname.new(value) : value
  rescue NoMethodError
    nil
  end
end

#inspectObject



127
128
129
130
# File 'lib/githooks/repository/config.rb', line 127

def inspect
  opts = OPTIONS.keys.collect { |k| ":'#{k}'=>#{get(k).inspect}" }.join(' ')
  format '<%s:0x%0x014 %s>', self.class.name, (__id__ * 2), opts
end

#list(options = {}) ⇒ Object



123
124
125
# File 'lib/githooks/repository/config.rb', line 123

def list(options = {})
  config(chdir: options.delete(:repo_path) || options.delete(:chdir))
end

#remove_section(options = {}) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/githooks/repository/config.rb', line 84

def remove_section(options = {})
  repo    = options.delete(:repo_path) || @repository.path
  global  = (opt = options.delete(:global)).nil? ? false : opt
  global  = global ? '--global' : '--local'
  @config = nil # reset config
  git(global, '--remove-section', "githooks.#{repo}", chdir: repo)
end

#set(option, value, options = {}) ⇒ Object

rubocop:disable CyclomaticComplexity, MethodLength, PerceivedComplexity, AbcSize



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/githooks/repository/config.rb', line 49

def set(option, value, options = {}) # rubocop:disable CyclomaticComplexity, MethodLength, PerceivedComplexity, AbcSize
  option    = normalize_option(option)
  repo      = options.delete(:repo_path) || @repository.path
  var_type  = "--#{OPTIONS[option][:type]}"
  add_type  = OPTIONS[option][:multiple] ? '--add' : '--replace-all'
  overwrite = !!options.delete(:overwrite)

  global    = (opt = options.delete(:global)).nil? ? false : opt
  global    = global ? '--global' : '--local'

  if OPTIONS[option][:type] == :path
    new_path = Pathname.new(value)
    unless new_path.exist?
      puts "Unable to set option option #{option} for [#{repo}]:"
      puts "  Path does not exist: #{new_path}"
      fail ArgumentError
    end
  else
    fail ArgumentError unless Pathname.new(value).executable?
  end

  value = Pathname.new(value).realpath.to_s

  if overwrite && !self[option].nil? && !self[option].empty?
    puts "Overwrite requested for option '#{option}'" if GitHooks.verbose
    unset(option, chdir: repo, global: global)
  end

  option = "githooks.#{repo}.#{option}"
  git(global, var_type, add_type, option, value, chdir: repo).tap do |result|
    puts "Added option #{option} with value #{value}" if result.status.success?
    @config = nil # reset config
  end
end

#unset(option, *args) ⇒ Object

rubocop:disable AbcSize



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/githooks/repository/config.rb', line 92

def unset(option, *args) # rubocop:disable AbcSize
  options = args.extract_options!
  global  = (opt = options.delete(:global)).nil? ? false : opt
  global  = global ? '--global' : '--local'
  option  = "githooks.#{repo}.#{normalize_option(option)}"

  value_regex = args.first

  if options.delete(:all) || value_regex.nil?
    git(global, '--unset-all', option, options)
  else
    git(global, '--unset', option, value_regex, options)
  end

  @config = nil # reset config

  result.status.success?
end