gitattributes 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00c283708237ba1a58d0f9d1a36eee187bc12020
4
- data.tar.gz: 2a4778a8dd6c4143abe7076c68f7cfa2f6dedaa0
3
+ metadata.gz: 7ce63e4149e74cf32bbff64049cf633fac63ba63
4
+ data.tar.gz: 2cb92c1110a46176e0e4e973fd8a0c2c02087186
5
5
  SHA512:
6
- metadata.gz: a232e58683f1ffb0c99e8e8431d37b1f1fb507f722a2c3da65a935fae27e2d47a97c4e811f00114db018f9fcaa4180dfaec914fc51eea3c6dd7eeb5240a66e11
7
- data.tar.gz: 8b8e43b2b234988ad277c5cde56b782eb9a8d7999f16d228619d65d676ee7054b6609c09a14ee259c9ec957441bcf881cac6a903089cbe074b5c8eaf3b1f4cfe
6
+ metadata.gz: e70fd5e6e915580d2aa0358556ba0e7dd338a2cf451557cc163df5cad9637098be78316c6cd93c0cb1e58e59831e51eafad699be992f34da702fb996f4f6cb37
7
+ data.tar.gz: 446c351faf8abda62986acb0ea4e2e7b742605baeeab662e04052f2e5509a97ee670683cabc805ea4e0d6144388f2712fd9a8584216e6214469140a9a86c71be
data/README.md CHANGED
@@ -7,7 +7,7 @@ Classes to parse `.gitattributes` files.
7
7
  A simple example of it's usage:
8
8
 
9
9
  ```ruby
10
- attributes = Reality::Git::Attributes.new(some_repo.path)
10
+ attributes = Reality::GitAttributes.new('/home/user/myrepo')
11
11
  attributes.attributes('README.md') # => { "eol" => "lf }
12
12
  ```
13
13
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{gitattributes}
5
- s.version = '1.0.0'
5
+ s.version = '1.1.0'
6
6
  s.platform = Gem::Platform::RUBY
7
7
 
8
8
  s.authors = ['Peter Donald']
@@ -0,0 +1,123 @@
1
+ module Reality
2
+ # Class for parsing Git attribute files and extracting the attributes for
3
+ # file patterns.
4
+ #
5
+ # Basic usage:
6
+ #
7
+ # attributes = Reality::Git::Attributes.new(some_repo.path)
8
+ #
9
+ # attributes.attributes('README.md') # => { "eol" => "lf }
10
+ class GitAttributes
11
+ # path - The path to the Git repository.
12
+ # attributes_file - The path to the ".gitattribtues" file. Defaults to "<path>/.gitattribtues".
13
+ def initialize(repository_path, attributes_file = nil)
14
+ @path = File.expand_path(repository_path)
15
+ @attributes_file = attributes_file || "#{@path}/.gitattributes"
16
+ @patterns = nil
17
+ end
18
+
19
+ # Returns all the Git attributes for the given path.
20
+ #
21
+ # path - A path to a file for which to get the attributes.
22
+ #
23
+ # Returns a Hash.
24
+ def attributes(path)
25
+ full_path = File.join(@path, path)
26
+
27
+ patterns.each do |pattern, attrs|
28
+ return attrs if File.fnmatch?(pattern, full_path)
29
+ end
30
+
31
+ {}
32
+ end
33
+
34
+ # Returns a Hash containing the file patterns and their attributes.
35
+ def patterns
36
+ @patterns ||= parse_file
37
+ end
38
+
39
+ private
40
+
41
+ # Parses an attribute string.
42
+ #
43
+ # These strings can be in the following formats:
44
+ #
45
+ # text # => { "text" => true }
46
+ # -text # => { "text" => false }
47
+ # key=value # => { "key" => "value" }
48
+ #
49
+ # string - The string to parse.
50
+ #
51
+ # Returns a Hash containing the attributes and their values.
52
+ def parse_attributes(string)
53
+ values = {}
54
+ dash = '-'
55
+ equal = '='
56
+ binary = 'binary'
57
+
58
+ string.split(/\s+/).each do |chunk|
59
+ # Data such as "foo = bar" should be treated as "foo" and "bar" being
60
+ # separate boolean attributes.
61
+ next if chunk == equal
62
+
63
+ key = chunk
64
+
65
+ # Input: "-foo"
66
+ if chunk.start_with?(dash)
67
+ key = chunk.byteslice(1, chunk.length - 1)
68
+ value = false
69
+
70
+ # Input: "foo=bar"
71
+ elsif chunk.include?(equal)
72
+ key, value = chunk.split(equal, 2)
73
+
74
+ # Input: "foo"
75
+ else
76
+ value = true
77
+ end
78
+
79
+ values[key] = value
80
+
81
+ # When the "binary" option is set the "diff" option should be set to
82
+ # the inverse. If "diff" is later set it should overwrite the
83
+ # automatically set value.
84
+ values['diff'] = false if key == binary && value
85
+ end
86
+
87
+ values
88
+ end
89
+
90
+ # Iterates over every line in the attributes file.
91
+ def each_line
92
+ full_path = @attributes_file
93
+
94
+ return unless File.exist?(full_path)
95
+
96
+ File.open(full_path, 'r') do |handle|
97
+ handle.each_line do |line|
98
+ yield line.strip
99
+ end
100
+ end
101
+ end
102
+
103
+ # Parses the Git attributes file.
104
+ def parse_file
105
+ pairs = []
106
+ comment = '#'
107
+
108
+ each_line do |line|
109
+ next if line.start_with?(comment) || line.empty?
110
+
111
+ pattern, attrs = line.split(/\s+/, 2)
112
+
113
+ parsed = attrs ? parse_attributes(attrs) : {}
114
+
115
+ pairs << [File.join(@path, pattern), parsed]
116
+ end
117
+
118
+ # Newer entries take precedence over older entries.
119
+ pairs.reverse.to_h
120
+ end
121
+ end
122
+ end
123
+
data/test/helper.rb CHANGED
@@ -1,9 +1,23 @@
1
+ #
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ #
14
+
1
15
  $:.unshift File.expand_path('../../lib', __FILE__)
2
16
 
3
17
  require 'securerandom'
4
18
  require 'minitest/autorun'
5
19
  require 'test/unit/assertions'
6
- require 'reality/attributes'
20
+ require 'reality/gitattributes'
7
21
 
8
22
  class Reality::TestCase < Minitest::Test
9
23
  include Test::Unit::Assertions
@@ -32,14 +46,6 @@ class Reality::TestCase < Minitest::Test
32
46
  end
33
47
  end
34
48
 
35
- def random_local_dir
36
- local_dir(random_string)
37
- end
38
-
39
- def local_dir(directory)
40
- "#{working_dir}/#{directory}"
41
- end
42
-
43
49
  def working_dir
44
50
  @working_dir ||= "#{workspace_dir}/#{self.class.name.gsub(/[\.\:]/, '_')}_#{name}_#{::SecureRandom.hex}"
45
51
  end
@@ -47,8 +53,4 @@ class Reality::TestCase < Minitest::Test
47
53
  def workspace_dir
48
54
  @workspace_dir ||= File.expand_path(ENV['TEST_TMP_DIR'] || "#{File.dirname(__FILE__)}/../tmp/workspace")
49
55
  end
50
-
51
- def random_string
52
- ::SecureRandom.hex
53
- end
54
56
  end
@@ -1,14 +1,28 @@
1
+ #
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ #
14
+
1
15
  require File.expand_path('../../helper', __FILE__)
2
16
 
3
- class Reality::TestAttributes < Reality::TestCase
17
+ class Reality::TestGitAttributes < Reality::TestCase
4
18
  def test_basic_operation_using_default_attribtues
5
19
  content = <<TEXT
6
20
  * -text
7
21
  TEXT
8
- dir = random_local_dir
22
+ dir = working_dir
9
23
  write_standard_file(dir, content)
10
24
 
11
- attributes = Reality::Git::Attributes.new(dir)
25
+ attributes = Reality::GitAttributes.new(dir)
12
26
  assert_equal({ "#{dir}/*" => { 'text' => false } }, attributes.patterns)
13
27
 
14
28
  assert_equal({ 'text' => false }, attributes.attributes('README.md'))
@@ -18,11 +32,11 @@ TEXT
18
32
  content = <<TEXT
19
33
  * -text
20
34
  TEXT
21
- dir = random_local_dir
35
+ dir = working_dir
22
36
  attributes_file = "#{dir}/non-standard-gitattributes"
23
37
  write_file(attributes_file, content)
24
38
 
25
- attributes = Reality::Git::Attributes.new(dir, attributes_file)
39
+ attributes = Reality::GitAttributes.new(dir, attributes_file)
26
40
  assert_equal({ "#{dir}/*" => { 'text' => false } }, attributes.patterns)
27
41
 
28
42
  assert_equal({ 'text' => false }, attributes.attributes('README.md'))
@@ -33,10 +47,10 @@ TEXT
33
47
  content = <<TEXT
34
48
  *.textile text -crlf -binary
35
49
  TEXT
36
- dir = random_local_dir
50
+ dir = working_dir
37
51
  write_standard_file(dir, content)
38
52
 
39
- attributes = Reality::Git::Attributes.new(dir)
53
+ attributes = Reality::GitAttributes.new(dir)
40
54
  assert_equal({ "#{dir}/*.textile" => { 'text' => true, 'crlf' => false, 'binary' => false } },
41
55
  attributes.patterns)
42
56
 
@@ -50,10 +64,10 @@ TEXT
50
64
  * -text
51
65
  *.textile text -crlf -binary
52
66
  TEXT
53
- dir = random_local_dir
67
+ dir = working_dir
54
68
  write_standard_file(dir, content)
55
69
 
56
- attributes = Reality::Git::Attributes.new(dir)
70
+ attributes = Reality::GitAttributes.new(dir)
57
71
  assert_equal({
58
72
  "#{dir}/*" => { 'text' => false },
59
73
  "#{dir}/*.textile" => { 'text' => true, 'crlf' => false, 'binary' => false }
@@ -70,10 +84,10 @@ TEXT
70
84
  * -text
71
85
  # DO NOT EDIT: File is auto-generated
72
86
  TEXT
73
- dir = random_local_dir
87
+ dir = working_dir
74
88
  write_standard_file(dir, content)
75
89
 
76
- attributes = Reality::Git::Attributes.new(dir)
90
+ attributes = Reality::GitAttributes.new(dir)
77
91
  assert_equal({ "#{dir}/*" => { 'text' => false } }, attributes.patterns)
78
92
  end
79
93
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitattributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Donald
@@ -54,9 +54,9 @@ files:
54
54
  - README.md
55
55
  - Rakefile
56
56
  - gitattributes.gemspec
57
- - lib/reality/attributes.rb
57
+ - lib/reality/gitattributes.rb
58
58
  - test/helper.rb
59
- - test/reality/test_attributes.rb
59
+ - test/reality/test_gitattributes.rb
60
60
  homepage: https://github.com/realityforge/gitattributes
61
61
  licenses: []
62
62
  metadata: {}
@@ -1,124 +0,0 @@
1
- module Reality
2
- module Git
3
- # Class for parsing Git attribute files and extracting the attributes for
4
- # file patterns.
5
- #
6
- # Basic usage:
7
- #
8
- # attributes = Reality::Git::Attributes.new(some_repo.path)
9
- #
10
- # attributes.attributes('README.md') # => { "eol" => "lf }
11
- class Attributes
12
- # path - The path to the Git repository.
13
- # attributes_file - The path to the ".gitattribtues" file. Defaults to "<path>/.gitattribtues".
14
- def initialize(repository_path, attributes_file = nil)
15
- @path = File.expand_path(repository_path)
16
- @attributes_file = attributes_file || "#{@path}/.gitattributes"
17
- @patterns = nil
18
- end
19
-
20
- # Returns all the Git attributes for the given path.
21
- #
22
- # path - A path to a file for which to get the attributes.
23
- #
24
- # Returns a Hash.
25
- def attributes(path)
26
- full_path = File.join(@path, path)
27
-
28
- patterns.each do |pattern, attrs|
29
- return attrs if File.fnmatch?(pattern, full_path)
30
- end
31
-
32
- {}
33
- end
34
-
35
- # Returns a Hash containing the file patterns and their attributes.
36
- def patterns
37
- @patterns ||= parse_file
38
- end
39
-
40
- private
41
-
42
- # Parses an attribute string.
43
- #
44
- # These strings can be in the following formats:
45
- #
46
- # text # => { "text" => true }
47
- # -text # => { "text" => false }
48
- # key=value # => { "key" => "value" }
49
- #
50
- # string - The string to parse.
51
- #
52
- # Returns a Hash containing the attributes and their values.
53
- def parse_attributes(string)
54
- values = {}
55
- dash = '-'
56
- equal = '='
57
- binary = 'binary'
58
-
59
- string.split(/\s+/).each do |chunk|
60
- # Data such as "foo = bar" should be treated as "foo" and "bar" being
61
- # separate boolean attributes.
62
- next if chunk == equal
63
-
64
- key = chunk
65
-
66
- # Input: "-foo"
67
- if chunk.start_with?(dash)
68
- key = chunk.byteslice(1, chunk.length - 1)
69
- value = false
70
-
71
- # Input: "foo=bar"
72
- elsif chunk.include?(equal)
73
- key, value = chunk.split(equal, 2)
74
-
75
- # Input: "foo"
76
- else
77
- value = true
78
- end
79
-
80
- values[key] = value
81
-
82
- # When the "binary" option is set the "diff" option should be set to
83
- # the inverse. If "diff" is later set it should overwrite the
84
- # automatically set value.
85
- values['diff'] = false if key == binary && value
86
- end
87
-
88
- values
89
- end
90
-
91
- # Iterates over every line in the attributes file.
92
- def each_line
93
- full_path = @attributes_file
94
-
95
- return unless File.exist?(full_path)
96
-
97
- File.open(full_path, 'r') do |handle|
98
- handle.each_line do |line|
99
- yield line.strip
100
- end
101
- end
102
- end
103
-
104
- # Parses the Git attributes file.
105
- def parse_file
106
- pairs = []
107
- comment = '#'
108
-
109
- each_line do |line|
110
- next if line.start_with?(comment) || line.empty?
111
-
112
- pattern, attrs = line.split(/\s+/, 2)
113
-
114
- parsed = attrs ? parse_attributes(attrs) : {}
115
-
116
- pairs << [File.join(@path, pattern), parsed]
117
- end
118
-
119
- # Newer entries take precedence over older entries.
120
- pairs.reverse.to_h
121
- end
122
- end
123
- end
124
- end