Class: Mercurial::ConfigFile
- Inherits:
-
Object
- Object
- Mercurial::ConfigFile
- Defined in:
- lib/mercurial-ruby/config_file.rb
Overview
Represents .hg/hgrc
configuration from the repository. Useful for adding/removing various settings.
For general information on hgrc:
Defined Under Namespace
Classes: DuplicateSetting
Instance Attribute Summary collapse
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Instance Method Summary collapse
-
#add_setting(header, name, value) ⇒ Object
Adds specified setting to a specified section of the config:.
-
#contents ⇒ Object
Returns contents of the config file as a string.
-
#delete_setting!(header, name) ⇒ Object
Removes specified setting from hgrc:.
-
#exists? ⇒ Boolean
Returns true if the config file actually exists on disk.
-
#find_header(header) ⇒ Object
Returns content of the specified section of hgrc.
-
#find_setting(header, setting) ⇒ Object
Returns content of the specified setting from a section.
-
#initialize(repository) ⇒ ConfigFile
constructor
A new instance of ConfigFile.
-
#path ⇒ Object
Returns an absolute path to the config file:.
-
#setting_exists?(header, name) ⇒ Boolean
Returns true if a specified setting exists in specified group.
Constructor Details
#initialize(repository) ⇒ ConfigFile
Returns a new instance of ConfigFile.
17 18 19 |
# File 'lib/mercurial-ruby/config_file.rb', line 17 def initialize(repository) @repository = repository end |
Instance Attribute Details
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
15 16 17 |
# File 'lib/mercurial-ruby/config_file.rb', line 15 def repository @repository end |
Instance Method Details
#add_setting(header, name, value) ⇒ Object
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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# 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 ⇒ Object
Returns contents of the config file as a string.
41 42 43 |
# File 'lib/mercurial-ruby/config_file.rb', line 41 def contents File.read(path) if exists? end |
#delete_setting!(header, name) ⇒ Object
Removes specified setting from hgrc:
config.delete_setting!('merge-tools', 'kdiff3.executable')
75 76 77 78 79 |
# 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? ⇒ Boolean
Returns true if the config file actually exists on disk. The file is usually missing in new repositories.
34 35 36 |
# File 'lib/mercurial-ruby/config_file.rb', line 34 def exists? File.exists?(path) end |
#find_header(header) ⇒ Object
Returns content of the specified section of hgrc.
93 94 95 96 97 98 99 100 |
# 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 |
#find_setting(header, setting) ⇒ Object
Returns content of the specified setting from a section.
105 106 107 108 |
# File 'lib/mercurial-ruby/config_file.rb', line 105 def find_setting(header, setting) #:nodoc: return nil if contents.nil? contents.scan(setting_regexp(header, setting)).flatten.first end |
#path ⇒ Object
Returns an absolute path to the config file:
config.path # => /home/ilya/repos/fancyrepo/.hg/hgrc
26 27 28 |
# File 'lib/mercurial-ruby/config_file.rb', line 26 def path File.join(repository.path, '.hg', 'hgrc') end |
#setting_exists?(header, name) ⇒ Boolean
Returns true if a specified setting exists in specified group.
config.setting_exists?('hooks', 'changegroup')
86 87 88 |
# File 'lib/mercurial-ruby/config_file.rb', line 86 def setting_exists?(header, name) !!find_setting(header, name) end |