Class: Reality::Git::Attributes
- Inherits:
-
Object
- Object
- Reality::Git::Attributes
- Defined in:
- lib/reality/git/attributes.rb
Overview
Representation of a gitattributes file.
Instance Attribute Summary collapse
-
#attributes_file ⇒ Object
readonly
Returns the value of attribute attributes_file.
Class Method Summary collapse
-
.parse(repository_path, attributes_file = nil, relative_path = nil) ⇒ Object
path - The path to the Git repository.
Instance Method Summary collapse
- #as_file_contents(options = {}) ⇒ Object
-
#attributes(path) ⇒ Object
Returns the attributes for the specified path as a hash.
- #binary_rule(pattern, attributes = {}) ⇒ Object
-
#dos_text_rule(pattern, attributes = {}) ⇒ Object
Adds a rule for pattern that sets the text attribute and eol=crlf.
-
#initialize(repository_path, attributes_file = nil, relative_path = nil, rules = []) ⇒ Attributes
constructor
A new instance of Attributes.
- #remove_rule(rule) ⇒ Object
- #rule(pattern, attributes) ⇒ Object
-
#rules ⇒ Object
Returns a list of attribute rules to apply.
- #rules_by_pattern(pattern) ⇒ Object
- #rules_by_pattern?(pattern) ⇒ Boolean
-
#rules_for_path(path) ⇒ Object
Returns the rules for the specified path.
-
#text_rule(pattern, attributes = {}) ⇒ Object
Adds a rule for pattern that sets the text attribute.
-
#unix_text_rule(pattern, attributes = {}) ⇒ Object
Adds a rule for pattern that sets the text attribute and eol=lf.
- #write(options = {}) ⇒ Object
- #write_to(filename, options = {}) ⇒ Object
Constructor Details
#initialize(repository_path, attributes_file = nil, relative_path = nil, rules = []) ⇒ Attributes
Returns a new instance of Attributes.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/reality/git/attributes.rb', line 45 def initialize(repository_path, attributes_file = nil, relative_path = nil, rules = []) @path = File.(repository_path) @attributes_file = attributes_file || "#{@path}/.gitattributes" @relative_path = relative_path || File.dirname(@attributes_file) @rules = rules @rule_map = {} rules.each do |rule| cache_rule(rule) end end |
Instance Attribute Details
#attributes_file ⇒ Object (readonly)
Returns the value of attribute attributes_file.
56 57 58 |
# File 'lib/reality/git/attributes.rb', line 56 def attributes_file @attributes_file end |
Class Method Details
.parse(repository_path, attributes_file = nil, relative_path = nil) ⇒ Object
path - The path to the Git repository. attributes_file - The path to the “.gitattributes” file. Defaults to “<path>/.gitattributes”. relative_path - The path to which attributes apply. Defaults to direcotyr containing attributes file.
24 25 26 27 28 29 |
# File 'lib/reality/git/attributes.rb', line 24 def parse(repository_path, attributes_file = nil, relative_path = nil) path = File.(repository_path) attributes_file ||= "#{path}/.gitattributes" rules = File.exist?(attributes_file) ? parse_file(attributes_file) : [] Attributes.new(repository_path, attributes_file, relative_path, rules) end |
Instance Method Details
#as_file_contents(options = {}) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/reality/git/attributes.rb', line 84 def as_file_contents( = {}) prefix = [:prefix].nil? ? '' : "#{[:prefix]}\n" rules = self.rules rules = rules.dup.sort.uniq if [:normalize] content = rules.collect {|r| r.to_s}.join("\n") content += "\n" unless content.empty? prefix + content end |
#attributes(path) ⇒ Object
Returns the attributes for the specified path as a hash.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/reality/git/attributes.rb', line 59 def attributes(path) full_path = File.join(@path, path) self.rules.reverse.each do |rule| full_pattern = rule.pattern[0] == '/' ? "#{@relative_path}#{rule.pattern}" : "#{@relative_path}/**/#{rule.pattern}" return rule.attributes if File.fnmatch?(full_pattern, full_path, File::FNM_PATHNAME | File::FNM_DOTMATCH) end {} end |
#binary_rule(pattern, attributes = {}) ⇒ Object
132 133 134 |
# File 'lib/reality/git/attributes.rb', line 132 def binary_rule(pattern, attributes = {}) rule(pattern, { :binary => true }.merge(attributes)) end |
#dos_text_rule(pattern, attributes = {}) ⇒ Object
Adds a rule for pattern that sets the text attribute and eol=crlf. This means that the file will be stored in the repository with line endings converted to LF and the local checkout will have line endings converted to CRLF
128 129 130 |
# File 'lib/reality/git/attributes.rb', line 128 def dos_text_rule(pattern, attributes = {}) text_rule(pattern, { :eol => 'crlf' }.merge(attributes)) end |
#remove_rule(rule) ⇒ Object
108 109 110 |
# File 'lib/reality/git/attributes.rb', line 108 def remove_rule(rule) uncache_rule(rule) if @rules.delete(rule) end |
#rule(pattern, attributes) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/reality/git/attributes.rb', line 101 def rule(pattern, attributes) rule = AttributeRule.new(pattern, attributes) @rules << rule cache_rule(rule) rule end |
#rules ⇒ Object
Returns a list of attribute rules to apply.
137 138 139 |
# File 'lib/reality/git/attributes.rb', line 137 def rules @rules.dup end |
#rules_by_pattern(pattern) ⇒ Object
141 142 143 |
# File 'lib/reality/git/attributes.rb', line 141 def rules_by_pattern(pattern) @rule_map[pattern].nil? ? [] : @rule_map[pattern].dup end |
#rules_by_pattern?(pattern) ⇒ Boolean
145 146 147 |
# File 'lib/reality/git/attributes.rb', line 145 def rules_by_pattern?(pattern) !rules_by_pattern(pattern).empty? end |
#rules_for_path(path) ⇒ Object
Returns the rules for the specified path.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/reality/git/attributes.rb', line 71 def rules_for_path(path) full_path = File.join(@path, path) rules = [] self.rules.each do |rule| full_pattern = rule.pattern[0] == '/' ? "#{@relative_path}#{rule.pattern}" : "#{@relative_path}/**/#{rule.pattern}" rules << rule if File.fnmatch?(full_pattern, full_path, File::FNM_PATHNAME | File::FNM_DOTMATCH) end rules end |
#text_rule(pattern, attributes = {}) ⇒ Object
Adds a rule for pattern that sets the text attribute. This means that the file will be stored in the repository with line endings converted to LF
114 115 116 |
# File 'lib/reality/git/attributes.rb', line 114 def text_rule(pattern, attributes = {}) rule(pattern, { :text => true }.merge(attributes)) end |
#unix_text_rule(pattern, attributes = {}) ⇒ Object
Adds a rule for pattern that sets the text attribute and eol=lf. This means that the file will be stored in the repository with line endings converted to LF and the local checkout will have line endings converted to LF
121 122 123 |
# File 'lib/reality/git/attributes.rb', line 121 def unix_text_rule(pattern, attributes = {}) text_rule(pattern, { :eol => 'lf' }.merge(attributes)) end |
#write(options = {}) ⇒ Object
97 98 99 |
# File 'lib/reality/git/attributes.rb', line 97 def write( = {}) write_to(@attributes_file, ) end |
#write_to(filename, options = {}) ⇒ Object
93 94 95 |
# File 'lib/reality/git/attributes.rb', line 93 def write_to(filename, = {}) IO.write(filename, as_file_contents()) end |