Class: Reality::Git::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/reality/git/attributes.rb

Overview

Representation of a gitattributes file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_path, attributes_file = nil, relative_path = nil, rules = []) ⇒ Attributes

Returns a new instance of Attributes.



32
33
34
35
36
37
# File 'lib/reality/git/attributes.rb', line 32

def initialize(repository_path, attributes_file = nil, relative_path = nil, rules = [])
  @path = File.expand_path(repository_path)
  @attributes_file = attributes_file || "#{@path}/.gitattributes"
  @relative_path = relative_path || File.dirname(@attributes_file)
  @rules = rules
end

Instance Attribute Details

#attributes_fileObject (readonly)

Returns the value of attribute attributes_file.



39
40
41
# File 'lib/reality/git/attributes.rb', line 39

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.expand_path(repository_path)
  attributes_file ||= "#{path}/.gitattributes"
  rules = File.exist?(attributes_file) ? Reality::Git::AttributesParser.parse_file(attributes_file) : {}
  Attributes.new(repository_path, attributes_file, relative_path, rules)
end

Instance Method Details

#attributes(path) ⇒ Object

Returns the attributes for the specified path as a hash.



42
43
44
45
46
47
48
49
50
# File 'lib/reality/git/attributes.rb', line 42

def attributes(path)
  full_path = File.join(@path, path)

  @rules.reverse.each do |rule|
    return rule.attributes if File.fnmatch?(File.join(@relative_path, rule.pattern), full_path)
  end

  {}
end

#binary_rule(pattern, attributes = {}) ⇒ Object



72
73
74
# File 'lib/reality/git/attributes.rb', line 72

def binary_rule(pattern, attributes = {})
  rule(pattern, { :binary => true }.merge(attributes))
end

#dos_text_rule(pattern, attributes = {}) ⇒ Object



68
69
70
# File 'lib/reality/git/attributes.rb', line 68

def dos_text_rule(pattern, attributes = {})
  text_rule(pattern, { :crlf => true }.merge(attributes))
end

#rule(pattern, attributes) ⇒ Object



60
61
62
# File 'lib/reality/git/attributes.rb', line 60

def rule(pattern, attributes)
  @rules << AttributeRule.new(pattern, attributes)
end

#rulesObject

Returns a list of attribute rules to apply.



77
78
79
# File 'lib/reality/git/attributes.rb', line 77

def rules
  @rules.dup
end

#text_rule(pattern, attributes = {}) ⇒ Object



64
65
66
# File 'lib/reality/git/attributes.rb', line 64

def text_rule(pattern, attributes = {})
  rule(pattern, { :text => true, :crlf => false, :binary => false }.merge(attributes))
end

#write_to(filename, options = {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/reality/git/attributes.rb', line 52

def write_to(filename, options = {})
  prefix = options[:prefix].nil? ? '' : "#{options[:prefix]}\n"
  rules = options[:normalize] ? @rules.dup.sort.uniq : @rules
  content = rules.collect {|r| r.to_s}.join("\n")
  content += "\n" unless content.empty?
  IO.write(filename, prefix + content)
end