gitattributes 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00c283708237ba1a58d0f9d1a36eee187bc12020
4
+ data.tar.gz: 2a4778a8dd6c4143abe7076c68f7cfa2f6dedaa0
5
+ SHA512:
6
+ metadata.gz: a232e58683f1ffb0c99e8e8431d37b1f1fb507f722a2c3da65a935fae27e2d47a97c4e811f00114db018f9fcaa4180dfaec914fc51eea3c6dd7eeb5240a66e11
7
+ data.tar.gz: 8b8e43b2b234988ad277c5cde56b782eb9a8d7999f16d228619d65d676ee7054b6609c09a14ee259c9ec957441bcf881cac6a903089cbe074b5c8eaf3b1f4cfe
data/.gitattributes ADDED
@@ -0,0 +1,6 @@
1
+ * -text
2
+ *.rb text -crlf -binary
3
+ *.rdoc text -crlf -binary
4
+ *.textile text -crlf -binary
5
+ *.txt text -crlf -binary
6
+ *.erb text -crlf -binary
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *.iml
2
+ /*.ipr
3
+ /*.iws
4
+ /.rakeTasks
5
+ /Gemfile.lock
6
+ /patch.diff
7
+ /pkg
8
+ /rdoc
9
+ /tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.3.1
5
+ git:
6
+ depth: 10
7
+ before_install:
8
+ - gem update bundler
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,26 @@
1
+ # How to Contribute
2
+
3
+ Pull requests are greatly appreciated and are what makes opensource great. Here's a quick guide:
4
+
5
+ * Fork it
6
+ * Create your feature branch (`git checkout -b my-new-feature`)
7
+ * Commit your changes (`git commit -am 'Add some feature'`)
8
+ * Push to the branch (`git push origin my-new-feature`)
9
+ * Create new Pull Request
10
+
11
+ Pester us if we don't get your Pull Requests merged in a timely fashion. :)
12
+
13
+ ## How to speed the merging of pull requests
14
+
15
+ * Describe your changes in the CHANGELOG.
16
+ * Give yourself some credit in the appropriate place (usually the CHANGELOG).
17
+ * Make commits of logical units.
18
+ * Ensure your commit messages help others understand what you are doing and why.
19
+ * Check for unnecessary whitespace with `git diff --check` before committing.
20
+ * Maintain the same code style.
21
+ * Maintain the same level of test coverage or improve it.
22
+
23
+ ## Additional Resources
24
+
25
+ * [General GitHub documentation](http://help.github.com/)
26
+ * [GitHub pull request documentation](http://help.github.com/send-pull-requests/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake', '= 0.9.2.2'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 Peter Donald
2
+ Copyright (c) 2013 Dmitriy Zaporozhets
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # gitattributes
2
+
3
+ [![Build Status](https://secure.travis-ci.org/realityforge/gitattributes.png?branch=master)](http://travis-ci.org/realityforge/gitattributes)
4
+
5
+ Classes to parse `.gitattributes` files.
6
+
7
+ A simple example of it's usage:
8
+
9
+ ```ruby
10
+ attributes = Reality::Git::Attributes.new(some_repo.path)
11
+ attributes.attributes('README.md') # => { "eol" => "lf }
12
+ ```
13
+
14
+ ## Credit
15
+
16
+ These classes were extracted from the [gitlab_git](https://gitlab.com/ben.boeckel/gitlab_git) by
17
+ with the gitattribtues code originally authored by Douwe Maan. All credit goes to the original
18
+ authors.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rubygems/package_task'
6
+ require 'rake/testtask'
7
+
8
+ desc 'Default Task'
9
+ task :default => [:test, :build]
10
+
11
+ desc 'Test Task'
12
+ Rake::TestTask.new do |t|
13
+ files = FileList['test/helper.rb', 'test/**/test_*.rb']
14
+ t.loader = :rake
15
+ t.test_files = files
16
+ t.libs << '.'
17
+ t.warning = true
18
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{gitattributes}
5
+ s.version = '1.0.0'
6
+ s.platform = Gem::Platform::RUBY
7
+
8
+ s.authors = ['Peter Donald']
9
+ s.email = %q{peter@realityforge.org}
10
+
11
+ s.homepage = %q{https://github.com/realityforge/gitattributes}
12
+ s.summary = %q{Class to parse gitattributes files.}
13
+ s.description = %q{Class to parse gitattributes files.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18
+ s.default_executable = []
19
+ s.require_paths = %w(lib)
20
+
21
+ s.has_rdoc = false
22
+ s.rdoc_options = %w(--line-numbers --inline-source --title gitattributes)
23
+
24
+ s.add_development_dependency(%q<minitest>, ['= 5.9.1'])
25
+ s.add_development_dependency(%q<test-unit>, ['= 3.1.5'])
26
+ end
@@ -0,0 +1,124 @@
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
data/test/helper.rb ADDED
@@ -0,0 +1,54 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'securerandom'
4
+ require 'minitest/autorun'
5
+ require 'test/unit/assertions'
6
+ require 'reality/attributes'
7
+
8
+ class Reality::TestCase < Minitest::Test
9
+ include Test::Unit::Assertions
10
+
11
+ def setup
12
+ self.setup_working_dir
13
+ end
14
+
15
+ def teardown
16
+ self.teardown_working_dir
17
+ end
18
+
19
+ def setup_working_dir
20
+ @cwd = Dir.pwd
21
+
22
+ FileUtils.mkdir_p self.working_dir
23
+ Dir.chdir(self.working_dir)
24
+ end
25
+
26
+ def teardown_working_dir
27
+ Dir.chdir(@cwd)
28
+ if passed?
29
+ FileUtils.rm_rf self.working_dir if File.exist?(self.working_dir)
30
+ else
31
+ $stderr.puts "Test #{self.class.name}.#{name} Failed. Leaving working directory #{self.working_dir}"
32
+ end
33
+ end
34
+
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
+ def working_dir
44
+ @working_dir ||= "#{workspace_dir}/#{self.class.name.gsub(/[\.\:]/, '_')}_#{name}_#{::SecureRandom.hex}"
45
+ end
46
+
47
+ def workspace_dir
48
+ @workspace_dir ||= File.expand_path(ENV['TEST_TMP_DIR'] || "#{File.dirname(__FILE__)}/../tmp/workspace")
49
+ end
50
+
51
+ def random_string
52
+ ::SecureRandom.hex
53
+ end
54
+ end
@@ -0,0 +1,88 @@
1
+ require File.expand_path('../../helper', __FILE__)
2
+
3
+ class Reality::TestAttributes < Reality::TestCase
4
+ def test_basic_operation_using_default_attribtues
5
+ content = <<TEXT
6
+ * -text
7
+ TEXT
8
+ dir = random_local_dir
9
+ write_standard_file(dir, content)
10
+
11
+ attributes = Reality::Git::Attributes.new(dir)
12
+ assert_equal({ "#{dir}/*" => { 'text' => false } }, attributes.patterns)
13
+
14
+ assert_equal({ 'text' => false }, attributes.attributes('README.md'))
15
+ end
16
+
17
+ def test_gitattributes_in_non_standard_location
18
+ content = <<TEXT
19
+ * -text
20
+ TEXT
21
+ dir = random_local_dir
22
+ attributes_file = "#{dir}/non-standard-gitattributes"
23
+ write_file(attributes_file, content)
24
+
25
+ attributes = Reality::Git::Attributes.new(dir, attributes_file)
26
+ assert_equal({ "#{dir}/*" => { 'text' => false } }, attributes.patterns)
27
+
28
+ assert_equal({ 'text' => false }, attributes.attributes('README.md'))
29
+ assert_equal({ 'text' => false }, attributes.attributes('docs/README.md'))
30
+ end
31
+
32
+ def test_multi_value_attribute
33
+ content = <<TEXT
34
+ *.textile text -crlf -binary
35
+ TEXT
36
+ dir = random_local_dir
37
+ write_standard_file(dir, content)
38
+
39
+ attributes = Reality::Git::Attributes.new(dir)
40
+ assert_equal({ "#{dir}/*.textile" => { 'text' => true, 'crlf' => false, 'binary' => false } },
41
+ attributes.patterns)
42
+
43
+ assert_equal({}, attributes.attributes('README.md'))
44
+ assert_equal({ 'text' => true, 'crlf' => false, 'binary' => false }, attributes.attributes('README.textile'))
45
+ assert_equal({ 'text' => true, 'crlf' => false, 'binary' => false }, attributes.attributes('doc/X.textile'))
46
+ end
47
+
48
+ def test_multi_patterns
49
+ content = <<TEXT
50
+ * -text
51
+ *.textile text -crlf -binary
52
+ TEXT
53
+ dir = random_local_dir
54
+ write_standard_file(dir, content)
55
+
56
+ attributes = Reality::Git::Attributes.new(dir)
57
+ assert_equal({
58
+ "#{dir}/*" => { 'text' => false },
59
+ "#{dir}/*.textile" => { 'text' => true, 'crlf' => false, 'binary' => false }
60
+ },
61
+ attributes.patterns)
62
+
63
+ assert_equal({ 'text' => false }, attributes.attributes('README.md'))
64
+ assert_equal({ 'text' => true, 'crlf' => false, 'binary' => false }, attributes.attributes('doc/X.textile'))
65
+ end
66
+
67
+ def test_ignore_comments
68
+ content = <<TEXT
69
+ # DO NOT EDIT: File is auto-generated
70
+ * -text
71
+ # DO NOT EDIT: File is auto-generated
72
+ TEXT
73
+ dir = random_local_dir
74
+ write_standard_file(dir, content)
75
+
76
+ attributes = Reality::Git::Attributes.new(dir)
77
+ assert_equal({ "#{dir}/*" => { 'text' => false } }, attributes.patterns)
78
+ end
79
+
80
+ def write_standard_file(dir, content)
81
+ write_file("#{dir}/.gitattributes", content)
82
+ end
83
+
84
+ def write_file(attributes_file, content)
85
+ FileUtils.mkdir_p File.dirname(attributes_file)
86
+ IO.write(attributes_file, content)
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitattributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Donald
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 5.9.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 5.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: test-unit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.1.5
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.5
41
+ description: Class to parse gitattributes files.
42
+ email: peter@realityforge.org
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitattributes"
48
+ - ".gitignore"
49
+ - ".ruby-version"
50
+ - ".travis.yml"
51
+ - CONTRIBUTING.md
52
+ - Gemfile
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - gitattributes.gemspec
57
+ - lib/reality/attributes.rb
58
+ - test/helper.rb
59
+ - test/reality/test_attributes.rb
60
+ homepage: https://github.com/realityforge/gitattributes
61
+ licenses: []
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options:
65
+ - "--line-numbers"
66
+ - "--inline-source"
67
+ - "--title"
68
+ - gitattributes
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.5.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Class to parse gitattributes files.
87
+ test_files: []