Class: GitHooks::Repository::Config
- 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
- #[](option) ⇒ Object
- #get(option, options = {}) ⇒ Object
-
#initialize(repository) ⇒ Config
constructor
A new instance of Config.
- #inspect ⇒ Object
- #list(options = {}) ⇒ Object
- #remove_section(options = {}) ⇒ Object
-
#set(option, value, options = {}) ⇒ Object
rubocop:disable CyclomaticComplexity, MethodLength, PerceivedComplexity, AbcSize.
-
#unset(option, *args) ⇒ Object
rubocop:disable AbcSize.
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, = {}) option = normalize_option(option) begin repo = [:repo_path] || @repository.path return unless (value = list()['githooks'][repo.to_s][option]) OPTIONS[option][:type] == :path ? Pathname.new(value) : value rescue NoMethodError nil end end |
#inspect ⇒ Object
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( = {}) config(chdir: .delete(:repo_path) || .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( = {}) repo = .delete(:repo_path) || @repository.path global = (opt = .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, = {}) # rubocop:disable CyclomaticComplexity, MethodLength, PerceivedComplexity, AbcSize option = normalize_option(option) repo = .delete(:repo_path) || @repository.path var_type = "--#{OPTIONS[option][:type]}" add_type = OPTIONS[option][:multiple] ? '--add' : '--replace-all' overwrite = !!.delete(:overwrite) global = (opt = .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 = args. global = (opt = .delete(:global)).nil? ? false : opt global = global ? '--global' : '--local' option = "githooks.#{repo}.#{normalize_option(option)}" value_regex = args.first if .delete(:all) || value_regex.nil? git(global, '--unset-all', option, ) else git(global, '--unset', option, value_regex, ) end @config = nil # reset config result.status.success? end |