lib/mercurial-ruby/config_file.rb in mercurial-ruby-0.5.0 vs lib/mercurial-ruby/config_file.rb in mercurial-ruby-0.6.0

- old
+ new

@@ -1,16 +1,18 @@ module Mercurial - + # # Represents +.hg/hgrc+ configuration file stored in the repository. # Useful for adding/removing various settings. # # You can read more about hgrc here: # # http://www.selenic.com/mercurial/hgrc.5.html # class ConfigFile + + class DuplicateSetting < Error; end attr_reader :repository def initialize(repository) @repository = repository @@ -49,10 +51,12 @@ # # [merge-tools] # kdiff3.executable = ~/bin/kdiff3 # 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? @@ -71,10 +75,19 @@ def delete_setting!(header, name) write do contents.gsub(find_setting(header, name), '') end end + + # + # Returns true if specified setting exists in specified group. + # + # config.setting_exists?('hooks', 'changegroup') + # + def setting_exists?(header, name) + !!find_setting(header, name) + end # # Returns content of the specified section of hgrc. # def find_header(header) @@ -88,10 +101,11 @@ # # Returns content of the specified setting from a section. # def find_setting(header, setting) #:nodoc: + return nil if contents.nil? contents.scan(setting_regexp(header, setting)).flatten.first end private @@ -114,6 +128,6 @@ /\[#{ Regexp.escape(header) }\]\s*[^\[\]]*(^#{ Regexp.escape(setting) }.+\n*)/i end end -end \ No newline at end of file +end