zapwhite 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c25ab12f3242c55f77939e13b490e0e43a06acd5
4
- data.tar.gz: 017e4bcaf501f92565ffb7c0c1adcd1ec98ae593
3
+ metadata.gz: 8658aeee48480bf54a4b34c2f47f604bd1794ffb
4
+ data.tar.gz: b877979084895a497561d98e67304b968f7963c7
5
5
  SHA512:
6
- metadata.gz: c287d0db17c22631d3c9de92e93683af4dc8a06aed43cfe4866f7069ad76cdc8ea210e0071d32dbef7f62c7e6c4cba615960e7c2cca6875539e48fe80bf4e343
7
- data.tar.gz: 0d447308e8b4aebb7c242720c3ae1dc9c65ca62cf378441ae9eb1c2bed8a21c19678d33a57a4924db80c004cee4ccf546eb5ecebc42bff7225e8c3f12f7c51e2
6
+ metadata.gz: 2ccfe5ec550b04c9b16730359744615d1df941df36f3be9715937c29bf53c98126b577a69f70d1e2fc0f07eb7e3cf1982decce1ca1036cb4ed6556575db68f6a
7
+ data.tar.gz: 8dfd58c741c1a57322dc22bd9f049fc0fd7e0d46db4a5df94f90d03621630f848f02c2ef806d6ab52bb7f175089592f4980e6a3d0a31391e59c7bb69534af7a3
data/.gitattributes CHANGED
@@ -1,6 +1,12 @@
1
+ # DO NOT EDIT: File is auto-generated
1
2
  * -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
3
+ *.gemspec text
4
+ *.md text
5
+ *.rb text
6
+ *.yml text
7
+ .gitattributes text
8
+ .gitignore text
9
+ .ruby-version text
10
+ Gemfile text
11
+ LICENSE text
12
+ Rakefile text
data/bin/zapwhite CHANGED
@@ -7,7 +7,9 @@ require 'optparse'
7
7
  require 'reality/zapwhite'
8
8
 
9
9
  @check_only = false
10
+ @generate_gitattributes = true
10
11
  @exclude_patterns = []
12
+ @rules = []
11
13
  @base_directory = Dir.pwd
12
14
  OptionParser.new do |opts|
13
15
  opts.banner = 'Usage: zapwhite [options] directory'
@@ -16,6 +18,10 @@ OptionParser.new do |opts|
16
18
  @check_only = true
17
19
  end
18
20
 
21
+ opts.on('-g', '--[no-]generate-gitattributes', 'Generate the .gitattributes file based on files present on filesystem') do |generate_gitattributes|
22
+ @generate_gitattributes = generate_gitattributes
23
+ end
24
+
19
25
  opts.on('-d', '--directory DIR', 'Base directory of git repository') do |base_directory|
20
26
  @base_directory = base_directory
21
27
  end
@@ -23,6 +29,10 @@ OptionParser.new do |opts|
23
29
  opts.on('-e', '--exclude-pattern PATTERN', 'Replace default exclude patterns with pattern(s) specified.') do |pattern|
24
30
  @exclude_patterns << pattern
25
31
  end
32
+
33
+ opts.on('-r', '--rule RULE', 'Additional rule(s) (a.k.a. lines) to add when generating .gitattributes file.') do |rule|
34
+ @rules << rule
35
+ end
26
36
  end.parse!
27
37
 
28
38
  unless ARGV.empty?
@@ -32,6 +42,8 @@ end
32
42
 
33
43
  runner = Reality::Zapwhite.new(File.expand_path(@base_directory))
34
44
  runner.check_only = @check_only
45
+ runner.generate_gitattributes = @generate_gitattributes
46
+ runner.additional_gitattribute_rules = @rules
35
47
  unless @exclude_patterns.empty?
36
48
  runner.exclude_patterns.clear
37
49
  @exclude_patterns.each do |exclude_pattern|
@@ -22,12 +22,21 @@ module Reality
22
22
  @attributes = Reality::Git::Attributes.parse(@base_directory)
23
23
  @exclude_patterns = %w(vendor/.* node_modules/.*)
24
24
  @check_only = false
25
+ @additional_gitattribute_rules = []
25
26
  end
26
27
 
27
28
  def exclude_patterns
28
29
  @exclude_patterns
29
30
  end
30
31
 
32
+ attr_accessor :additional_gitattribute_rules
33
+
34
+ attr_writer :generate_gitattributes
35
+
36
+ def generate_gitattributes?
37
+ @generate_gitattributes
38
+ end
39
+
31
40
  def check_only?
32
41
  !!@check_only
33
42
  end
@@ -38,9 +47,27 @@ module Reality
38
47
  # Return the number of files that need normalization
39
48
  def run
40
49
  normalize_count = 0
50
+
51
+ if generate_gitattributes?
52
+ output_options = {:prefix => '# DO NOT EDIT: File is auto-generated', :normalize => true}
53
+ new_gitattributes = generate_gitattributes!
54
+ new_content = new_gitattributes.as_file_contents(output_options)
55
+ old_content = File.exist?(@attributes.attributes_file) ? IO.read(@attributes.attributes_file) : nil
56
+ if new_content != old_content
57
+ @attributes = new_gitattributes
58
+ if check_only?
59
+ puts 'Non-normalized .gitattributes file'
60
+ else
61
+ puts 'Fixing: .gitattributes'
62
+ @attributes.write(output_options)
63
+ end
64
+ normalize_count += 1
65
+ end
66
+ end
67
+
41
68
  files = {}
42
69
 
43
- collect_files(files)
70
+ collect_file_attributes(files)
44
71
 
45
72
  files.each_pair do |filename, config|
46
73
  full_filename = "#{@base_directory}/#{filename}"
@@ -75,23 +102,46 @@ module Reality
75
102
 
76
103
  private
77
104
 
78
- def collect_files(files)
105
+ def generate_gitattributes!
106
+ attributes = Reality::Git::Attributes.new(@base_directory)
107
+ template = create_template_gitattributes
108
+ each_git_filename do |f|
109
+ full_filename = "#{@base_directory}/#{f}"
110
+ template.rules_for_path(full_filename).each do |rule|
111
+ attributes.rule(rule.pattern, rule.attributes.merge(:priority => rule.priority))
112
+ template.remove_rule(rule)
113
+ end
114
+ end
115
+ self.additional_gitattribute_rules.each do |line|
116
+ rule = Reality::Git::AttributeRule.parse_line(line)
117
+ attributes.rule(rule.pattern, rule.attributes.merge(:priority => 2))
118
+ end
119
+ attributes
120
+ end
121
+
122
+ def collect_file_attributes(files)
123
+ each_git_filename do |f|
124
+ full_filename = "#{@base_directory}/#{f}"
125
+ if File.exist?(full_filename)
126
+ attr = @attributes.attributes(f)
127
+ if attr['text']
128
+ files[f] = {
129
+ :dos => (attr['eol'] == 'crlf'),
130
+ :encoding => attr['encoding'],
131
+ :nodupnl => attr['dupnl'].nil? ? false : !attr['dupnl'],
132
+ :eofnl => attr['eofnl'].nil? ? true : !!attr['eofnl']
133
+ }
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ def each_git_filename
79
140
  exclude_patterns = self.exclude_patterns.collect {|s| /^#{s}$/}
80
141
 
81
142
  in_dir(@base_directory) do
82
143
  `git ls-files`.split("\n").each do |f|
83
- full_filename = "#{@base_directory}/#{f}"
84
- if !exclude_patterns.any? {|p| p =~ f} && File.exist?(full_filename)
85
- attr = @attributes.attributes(f)
86
- if attr['text']
87
- files[f] = {
88
- :dos => (attr['eol'] == 'crlf'),
89
- :encoding => attr['encoding'],
90
- :nodupnl => attr['dupnl'].nil? ? false : !attr['dupnl'],
91
- :eofnl => attr['eofnl'].nil? ? true : !!attr['eofnl']
92
- }
93
- end
94
- end
144
+ yield f unless exclude_patterns.any? {|p| p =~ f}
95
145
  end
96
146
  end
97
147
  end
@@ -140,5 +190,92 @@ module Reality
140
190
  Dir.chdir(original_dir)
141
191
  end
142
192
  end
193
+
194
+ def create_template_gitattributes
195
+ attributes = Reality::Git::Attributes.new(@base_directory)
196
+ attributes.rule('*', :text => false)
197
+
198
+ attributes.dos_text_rule('*.rdl', :eofnl => false)
199
+ attributes.unix_text_rule('*.sh')
200
+ attributes.text_rule('*.md')
201
+ attributes.binary_rule('*.jpg')
202
+
203
+ attributes.text_rule('.gitignore')
204
+ attributes.text_rule('.gitattributes')
205
+
206
+ # Ruby defaults
207
+ attributes.text_rule('Gemfile')
208
+ attributes.text_rule('*.gemspec')
209
+ attributes.text_rule('.ruby-version')
210
+ attributes.text_rule('*.rb')
211
+ attributes.text_rule('*.yaml')
212
+ attributes.text_rule('*.yml')
213
+
214
+ # Documentation defaults
215
+ attributes.text_rule('*.txt')
216
+ attributes.text_rule('*.md')
217
+ attributes.text_rule('*.textile')
218
+ attributes.text_rule('*.rdoc')
219
+ attributes.text_rule('*.html')
220
+ attributes.text_rule('*.xhtml')
221
+ attributes.text_rule('*.css', :encoding => 'UTF8')
222
+ attributes.text_rule('*.js')
223
+ attributes.binary_rule('*.jpg')
224
+ attributes.binary_rule('*.jpeg')
225
+ attributes.binary_rule('*.png')
226
+ attributes.binary_rule('*.bmp')
227
+ attributes.binary_rule('*.ico')
228
+
229
+ attributes.binary_rule('*.pdf')
230
+ attributes.binary_rule('*.doc')
231
+
232
+ # Common file formats
233
+ attributes.text_rule('*.json')
234
+ attributes.text_rule('*.xml')
235
+ attributes.text_rule('*.xsd')
236
+ attributes.text_rule('*.xsl')
237
+ attributes.text_rule('*.wsdl')
238
+
239
+ # Build system defaults
240
+ attributes.text_rule('buildfile')
241
+ attributes.text_rule('Buildfile')
242
+ attributes.text_rule('Rakefile')
243
+ attributes.text_rule('rakefile')
244
+ attributes.text_rule('*.rake')
245
+
246
+ attributes.text_rule('*.graphql')
247
+ attributes.text_rule('*.graphqls')
248
+ attributes.text_rule('*.ts')
249
+ attributes.text_rule('*.tsx')
250
+ attributes.text_rule('*.ts')
251
+ attributes.text_rule('*.tsx')
252
+ attributes.text_rule('Jenkinsfile')
253
+ attributes.text_rule('*.groovy')
254
+ attributes.dos_text_rule('*.rdl', :eofnl => false)
255
+ attributes.text_rule('*.erb')
256
+ attributes.text_rule('*.sass')
257
+ attributes.text_rule('*.scss')
258
+ attributes.text_rule('*.less')
259
+ attributes.text_rule('*.sql')
260
+ attributes.text_rule('*.java')
261
+ attributes.text_rule('*.jsp')
262
+ attributes.text_rule('*.properties')
263
+ attributes.rule('*.jar', :binary => true)
264
+ attributes.text_rule('Dockerfile')
265
+ attributes.text_rule('LICENSE')
266
+ attributes.text_rule('CHANGELOG')
267
+
268
+ # Shell scripts
269
+ attributes.dos_text_rule('*.cmd')
270
+ attributes.dos_text_rule('*.bat')
271
+ attributes.text_rule('*.sh')
272
+
273
+ # Native development files
274
+ attributes.text_rule('*.c')
275
+ attributes.binary_rule('*.dll')
276
+ attributes.binary_rule('*.so')
277
+
278
+ attributes
279
+ end
143
280
  end
144
281
  end
data/test/helper.rb CHANGED
@@ -82,7 +82,7 @@ module Reality
82
82
 
83
83
  in_dir(directory) do
84
84
  run_command('git init')
85
- run_command("git config --local commit.gpgsign false")
85
+ run_command('git config --local commit.gpgsign false')
86
86
  run_command("git config --local user.name \"Your Name\"")
87
87
  run_command("git config --local user.email \"you@example.com\"")
88
88
  yield
@@ -9,7 +9,7 @@ TEXT
9
9
  write_file('README.md', "Hello\n")
10
10
  end
11
11
  in_dir(dir) do
12
- output = run_command("#{ZAPWHITE_BIN}", 0)
12
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 0)
13
13
  assert_equal '', output
14
14
  end
15
15
  end
@@ -22,7 +22,7 @@ TEXT
22
22
  write_file('README.md', "Hello \n")
23
23
  end
24
24
  in_dir(dir) do
25
- output = run_command("#{ZAPWHITE_BIN}", 1)
25
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 1)
26
26
  assert_equal "Fixing: README.md\n", output
27
27
  assert_equal "Hello\n", IO.binread("#{dir}/README.md")
28
28
  end
@@ -36,7 +36,7 @@ TEXT
36
36
  write_file('Read Me.txt', "Hello \n")
37
37
  end
38
38
  in_dir(dir) do
39
- output = run_command("#{ZAPWHITE_BIN}", 1)
39
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 1)
40
40
  assert_equal "Fixing: Read Me.txt\n", output
41
41
  assert_equal "Hello\n", IO.binread("#{dir}/Read Me.txt")
42
42
  end
@@ -50,7 +50,7 @@ TEXT
50
50
  write_file('README.md', "Hello \n")
51
51
  end
52
52
  in_dir(dir) do
53
- output = run_command("#{ZAPWHITE_BIN}", 1)
53
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 1)
54
54
  assert_equal "Fixing: README.md\n", output
55
55
  assert_equal "Hello\n", IO.binread("#{dir}/README.md")
56
56
  end
@@ -64,7 +64,7 @@ TEXT
64
64
  write_file('README.md', 'Hello')
65
65
  end
66
66
  in_dir(dir) do
67
- output = run_command("#{ZAPWHITE_BIN}", 1)
67
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 1)
68
68
  assert_equal "Fixing: README.md\n", output
69
69
  assert_equal "Hello\n", IO.binread("#{dir}/README.md")
70
70
  end
@@ -78,7 +78,7 @@ TEXT
78
78
  write_file('run.bat', "echo hi\n")
79
79
  end
80
80
  in_dir(dir) do
81
- output = run_command("#{ZAPWHITE_BIN}", 1)
81
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 1)
82
82
  assert_equal "Fixing: run.bat\n", output
83
83
  assert_equal "echo hi\r\n", IO.binread("#{dir}/run.bat")
84
84
  end
@@ -92,7 +92,7 @@ TEXT
92
92
  write_file('README.md', "Hello\n\n\n\n\nHow are you?\n")
93
93
  end
94
94
  in_dir(dir) do
95
- output = run_command("#{ZAPWHITE_BIN}", 1)
95
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 1)
96
96
  assert_equal "Fixing: README.md\n", output
97
97
  assert_equal "Hello\n\nHow are you?\n", IO.binread("#{dir}/README.md")
98
98
  end
@@ -106,7 +106,7 @@ TEXT
106
106
  write_file('README.md', "Hello\n\n\n\nHow are you?\n")
107
107
  end
108
108
  in_dir(dir) do
109
- output = run_command("#{ZAPWHITE_BIN}", 0)
109
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 0)
110
110
  assert_equal '', output
111
111
  assert_equal "Hello\n\n\n\nHow are you?\n", IO.binread("#{dir}/README.md")
112
112
  end
@@ -120,9 +120,64 @@ TEXT
120
120
  write_file('README.md', 'Hello')
121
121
  end
122
122
  in_dir(dir) do
123
- output = run_command("#{ZAPWHITE_BIN}", 0)
123
+ output = run_command("#{ZAPWHITE_BIN} --no-generate-gitattributes", 0)
124
124
  assert_equal '', output
125
125
  assert_equal 'Hello', IO.binread("#{dir}/README.md")
126
126
  end
127
127
  end
128
+
129
+ def test_generate_gitattributes
130
+ dir = create_git_repo do
131
+ write_file('README.md', "Hello\n")
132
+ end
133
+ in_dir(dir) do
134
+ output = run_command("#{ZAPWHITE_BIN} --generate-gitattributes", 1)
135
+ assert_equal "Fixing: .gitattributes\n", output
136
+ assert_equal "Hello\n", IO.binread("#{dir}/README.md")
137
+ assert_equal "# DO NOT EDIT: File is auto-generated\n* -text\n*.md text\n", IO.binread("#{dir}/.gitattributes")
138
+ end
139
+ end
140
+
141
+ def test_generate_gitattributes_updates_whitespace_subsequently
142
+ dir = create_git_repo do
143
+ write_file('README.md', "Hello \n")
144
+ end
145
+ in_dir(dir) do
146
+ output = run_command("#{ZAPWHITE_BIN} --generate-gitattributes", 2)
147
+ assert_equal "Fixing: .gitattributes\nFixing: README.md\n", output
148
+ assert_equal "Hello\n", IO.binread("#{dir}/README.md")
149
+ assert_equal "# DO NOT EDIT: File is auto-generated\n* -text\n*.md text\n", IO.binread("#{dir}/.gitattributes")
150
+ end
151
+ end
152
+
153
+ def test_generate_gitattributes_matches
154
+ dir = create_git_repo do
155
+ write_gitattributes_file(<<TEXT)
156
+ # DO NOT EDIT: File is auto-generated
157
+ * -text
158
+ *.md text
159
+ TEXT
160
+ write_file('README.md', "Hello\n")
161
+ end
162
+ in_dir(dir) do
163
+ output = run_command("#{ZAPWHITE_BIN} --generate-gitattributes", 0)
164
+ assert_equal '', output
165
+ assert_equal "Hello\n", IO.binread("#{dir}/README.md")
166
+ assert_equal "# DO NOT EDIT: File is auto-generated\n* -text\n*.md text\n", IO.binread("#{dir}/.gitattributes")
167
+ end
168
+ end
169
+
170
+ def test_generate_gitattributes_with_additional_rules
171
+ dir = create_git_repo do
172
+ write_file('README.md', "Hello\n")
173
+ end
174
+ in_dir(dir) do
175
+ output = run_command("#{ZAPWHITE_BIN} --generate-gitattributes --rule '*.bin -diff' --rule '*.rxt text'", 1)
176
+ assert_equal "Fixing: .gitattributes\n", output
177
+ assert_equal "Hello\n", IO.binread("#{dir}/README.md")
178
+ assert_equal "# DO NOT EDIT: File is auto-generated\n* -text\n*.md text\n*.bin -diff\n*.rxt text\n", IO.binread("#{dir}/.gitattributes")
179
+ end
180
+ end
181
+
182
+
128
183
  end
data/zapwhite.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{zapwhite}
5
- s.version = '2.4.0'
5
+ s.version = '2.5.0'
6
6
  s.platform = Gem::Platform::RUBY
7
7
 
8
8
  s.authors = ['Peter Donald']
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.has_rdoc = false
22
22
  s.rdoc_options = %w(--line-numbers --inline-source --title zapwhite)
23
23
 
24
- s.add_dependency 'gitattributes', '= 2.4.0'
24
+ s.add_dependency 'gitattributes', '= 2.9.0'
25
25
 
26
26
  s.add_development_dependency(%q<minitest>, ['= 5.9.1'])
27
27
  s.add_development_dependency(%q<test-unit>, ['= 3.1.5'])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zapwhite
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Donald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-07 00:00:00.000000000 Z
11
+ date: 2017-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gitattributes
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 2.4.0
19
+ version: 2.9.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 2.4.0
26
+ version: 2.9.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement