class Mercurial::ConfigFile
Represents .hg/hgrc
configuration from the repository. Useful for adding/removing various settings.
For general information on hgrc:
Attributes
repository[R]
Public Class Methods
new(repository)
click to toggle source
# File lib/mercurial-ruby/config_file.rb, line 17 def initialize(repository) @repository = repository end
Public Instance Methods
add_setting(header, name, value)
click to toggle source
Adds specified setting to a specified section of the config:
config.add_setting('merge-tools', 'kdiff3.executable', '~/bin/kdiff3')
will write the following content to hgrc:
[merge-tools] kdiff3.executable = ~/bin/kdiff3
# File lib/mercurial-ruby/config_file.rb, line 55 def add_setting(header, name, value) raise DuplicateSetting if setting_exists?(header, name) new_setting = %Q{[#{ header }]\n#{ name } = #{ value }\n} write do if contents.nil? new_setting elsif contents.scan(header_regexp(header)).empty? contents << "\n\n#{ new_setting }" else contents.gsub(header_regexp(header), new_setting) end end end
contents()
click to toggle source
Returns contents of the config file as a string.
# File lib/mercurial-ruby/config_file.rb, line 41 def contents File.read(path) if exists? end
delete_setting!(header, name)
click to toggle source
Removes specified setting from hgrc:
config.delete_setting!('merge-tools', 'kdiff3.executable')
# File lib/mercurial-ruby/config_file.rb, line 75 def delete_setting!(header, name) write do contents.gsub(find_setting(header, name), '') end end
exists?()
click to toggle source
Returns true if the config file actually exists on disk. The file is usually missing in new repositories.
# File lib/mercurial-ruby/config_file.rb, line 34 def exists? File.exists?(path) end
find_header(header)
click to toggle source
Returns content of the specified section of hgrc.
# File lib/mercurial-ruby/config_file.rb, line 93 def find_header(header) {}.tap do |returning| contents.scan(header_with_content_regexp(header)).flatten.first.split("\n").each do |setting| name, value = *setting.split('=').map(&:strip) returning[name] = value end end end
path()
click to toggle source
Returns an absolute path to the config file:
config.path # => /home/ilya/repos/fancyrepo/.hg/hgrc
# File lib/mercurial-ruby/config_file.rb, line 26 def path File.join(repository.path, '.hg', 'hgrc') end
setting_exists?(header, name)
click to toggle source
Returns true if a specified setting exists in specified group.
config.setting_exists?('hooks', 'changegroup')
# File lib/mercurial-ruby/config_file.rb, line 86 def setting_exists?(header, name) !!find_setting(header, name) end
Private Instance Methods
header_regexp(header)
click to toggle source
# File lib/mercurial-ruby/config_file.rb, line 119 def header_regexp(header) /(\[#{ Regexp.escape(header) }\]\s*)/ end
header_with_content_regexp(header)
click to toggle source
# File lib/mercurial-ruby/config_file.rb, line 123 def header_with_content_regexp(header) /\[#{ Regexp.escape(header) }\]\s*([^\[\]]*)/i end
setting_regexp(header, setting)
click to toggle source
# File lib/mercurial-ruby/config_file.rb, line 127 def setting_regexp(header, setting) /\[#{ Regexp.escape(header) }\]\s*[^\[\]]*(^#{ Regexp.escape(setting) }.+\n*)/i end
write(&block)
click to toggle source
# File lib/mercurial-ruby/config_file.rb, line 112 def write(&block) new_content = block.call File.open(path, 'w') do |f| f << new_content end end