zapwhite 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitattributes +6 -0
- data/.gitignore +9 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/CONTRIBUTING.md +26 -0
- data/Gemfile +5 -0
- data/LICENSE +202 -0
- data/README.md +22 -0
- data/Rakefile +18 -0
- data/bin/zapwhite +42 -0
- data/lib/reality/zapwhite.rb +144 -0
- data/test/helper.rb +106 -0
- data/test/reality/test_zapwhite.rb +114 -0
- data/zapwhite.gemspec +28 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 677ac315bf9fc59519513f0bd2ca5342d65ad8e2
|
4
|
+
data.tar.gz: 530f4202a1219f64804591ac586fbc421c97de08
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca3ba0ad1a9c1464ebb8af02db54074d3bfbe07897b9bcea18933cc1eb58c5e6fe44978c97ec940ae0b7d0935ef269a8a0e8d03a302098b05581d7977ab6949e
|
7
|
+
data.tar.gz: 199e6210ff898f3ac64019b57984a24bedd40d06de00dd1d513065ee1f3ace7fb6b60429f23fc6a6b9459a8ff1a29e88231b81b5f80af1594ca50e5933c17891
|
data/.gitattributes
ADDED
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.travis.yml
ADDED
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
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# zapwhite
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/realityforge/zapwhite.png?branch=master)](http://travis-ci.org/realityforge/zapwhite)
|
4
|
+
|
5
|
+
A simple tool to normalize whitespace in git repositories. The tool:
|
6
|
+
|
7
|
+
* removes trailing whitespace from each line
|
8
|
+
* ensures files end with a new line
|
9
|
+
* ensure files are in UTF-8 format with no invalid UTF sequences
|
10
|
+
* ensures dos files use dos line endings and all other files do not.
|
11
|
+
|
12
|
+
Files that are part of the repository are candidates for normalization.
|
13
|
+
(It is sufficient for the file to be staged). Files are matched against
|
14
|
+
patterns supplied either in the command line or in the `.gitattributes`
|
15
|
+
file associated with the repository.
|
16
|
+
|
17
|
+
The tool will ensure files annotated with `text` will be processed and
|
18
|
+
files with the `crlf` flag as true will be treated as dos files. If the
|
19
|
+
file has an `encoding` attribute, the tool will not try to convert to
|
20
|
+
UTF-8. The tool will also scan and remove duplicate new lines if any file
|
21
|
+
has a attribute `-dupnl`. The tool will not enforce end of file new
|
22
|
+
lines if attribute `-eofnl` is set.
|
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
|
data/bin/zapwhite
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
|
6
|
+
require 'optparse'
|
7
|
+
require 'reality/zapwhite'
|
8
|
+
|
9
|
+
@check_only = false
|
10
|
+
@exclude_patterns = []
|
11
|
+
@base_directory = Dir.pwd
|
12
|
+
OptionParser.new do |opts|
|
13
|
+
opts.banner = 'Usage: zapwhite [options] directory'
|
14
|
+
|
15
|
+
opts.on('-c', '--check-only', 'Run check and emit messages but do not change files') do
|
16
|
+
@check_only = true
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.on('-d', '--directory DIR', 'Base directory of git repository') do |base_directory|
|
20
|
+
@base_directory = base_directory
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on('-e', '--exclude-pattern PATTERN', 'Replace default exclude patterns with pattern(s) specified.') do |pattern|
|
24
|
+
@exclude_patterns << pattern
|
25
|
+
end
|
26
|
+
end.parse!
|
27
|
+
|
28
|
+
unless ARGV.empty?
|
29
|
+
puts "Unhandled options: #{ARGV.inspect}"
|
30
|
+
exit -1
|
31
|
+
end
|
32
|
+
|
33
|
+
runner = Reality::Zapwhite.new(File.expand_path(@base_directory))
|
34
|
+
runner.check_only = @check_only
|
35
|
+
unless @exclude_patterns.empty?
|
36
|
+
runner.exclude_patterns.clear
|
37
|
+
@exclude_patterns.each do |exclude_pattern|
|
38
|
+
runner.exclude_patterns << exclude_pattern
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
exit runner.run
|
@@ -0,0 +1,144 @@
|
|
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
|
+
|
15
|
+
require 'reality/gitattributes'
|
16
|
+
|
17
|
+
module Reality
|
18
|
+
class Zapwhite
|
19
|
+
|
20
|
+
def initialize(base_directory)
|
21
|
+
@base_directory = base_directory
|
22
|
+
@attributes = Reality::GitAttributes.new(@base_directory)
|
23
|
+
@exclude_patterns = %w(vendor/.* node_modules/.*)
|
24
|
+
@check_only = false
|
25
|
+
end
|
26
|
+
|
27
|
+
def exclude_patterns
|
28
|
+
@exclude_patterns
|
29
|
+
end
|
30
|
+
|
31
|
+
def check_only?
|
32
|
+
!!@check_only
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_writer :check_only
|
36
|
+
|
37
|
+
# Run normalization process across directory.
|
38
|
+
# Return the number of files that need normalization
|
39
|
+
def run
|
40
|
+
normalize_count = 0
|
41
|
+
files = {}
|
42
|
+
|
43
|
+
collect_files(files)
|
44
|
+
|
45
|
+
files.each_pair do |filename, config|
|
46
|
+
full_filename = "#{@base_directory}/#{filename}"
|
47
|
+
content = File.read(full_filename)
|
48
|
+
|
49
|
+
content = patch_encoding(content) unless config[:encoding]
|
50
|
+
original_content = content.dup
|
51
|
+
content =
|
52
|
+
config[:dos] ?
|
53
|
+
clean_dos_whitespace(filename, content, config[:eofnl]) :
|
54
|
+
clean_whitespace(filename, content, config[:eofnl])
|
55
|
+
if config[:nodupnl]
|
56
|
+
while content.gsub!(/\n\n\n/, "\n\n")
|
57
|
+
# Keep removing duplicate new lines till they have gone
|
58
|
+
end
|
59
|
+
end
|
60
|
+
if content != original_content
|
61
|
+
normalize_count += 1
|
62
|
+
if check_only?
|
63
|
+
puts "Non-normalized whitespace in #{filename}"
|
64
|
+
else
|
65
|
+
puts "Fixing: #{filename}"
|
66
|
+
File.open(full_filename, 'wb') do |out|
|
67
|
+
out.write content
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
normalize_count
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def collect_files(files)
|
79
|
+
exclude_patterns = self.exclude_patterns.collect {|s| /^#{s}$/}
|
80
|
+
|
81
|
+
in_dir(@base_directory) do
|
82
|
+
`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['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
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def clean_whitespace(filename, content, eofnl)
|
100
|
+
begin
|
101
|
+
content.gsub!(/\r\n/, "\n")
|
102
|
+
content.gsub!(/[ \t]+\n/, "\n")
|
103
|
+
content.gsub!(/[ \r\t\n]+\Z/, '')
|
104
|
+
content += "\n" if eofnl
|
105
|
+
rescue
|
106
|
+
puts "Skipping whitespace cleanup: #{filename}"
|
107
|
+
end
|
108
|
+
content
|
109
|
+
end
|
110
|
+
|
111
|
+
def clean_dos_whitespace(filename, content, eofnl)
|
112
|
+
begin
|
113
|
+
content.gsub!(/\r\n/, "\n")
|
114
|
+
content.gsub!(/[ \t]+\n/, "\n")
|
115
|
+
content.gsub!(/[ \r\t\n]+\Z/, '')
|
116
|
+
content += "\n" if eofnl
|
117
|
+
content.gsub!(/\n/, "\r\n")
|
118
|
+
rescue
|
119
|
+
puts "Skipping dos whitespace cleanup: #{filename}"
|
120
|
+
end
|
121
|
+
content
|
122
|
+
end
|
123
|
+
|
124
|
+
def patch_encoding(content)
|
125
|
+
content =
|
126
|
+
content.respond_to?(:encode!) ?
|
127
|
+
content.encode!('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '') :
|
128
|
+
content
|
129
|
+
content.gsub!(/^\xEF\xBB\xBF/, '')
|
130
|
+
content
|
131
|
+
end
|
132
|
+
|
133
|
+
# Evaluate block after changing directory to specified directory
|
134
|
+
def in_dir(dir, &block)
|
135
|
+
original_dir = Dir.pwd
|
136
|
+
begin
|
137
|
+
Dir.chdir(dir)
|
138
|
+
block.call
|
139
|
+
ensure
|
140
|
+
Dir.chdir(original_dir)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,106 @@
|
|
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
|
+
|
15
|
+
require 'securerandom'
|
16
|
+
require 'minitest/autorun'
|
17
|
+
require 'test/unit/assertions'
|
18
|
+
|
19
|
+
require 'fileutils'
|
20
|
+
|
21
|
+
module Reality
|
22
|
+
class TestCase < Minitest::Test
|
23
|
+
BASE_DIR = File.expand_path("#{File.dirname(__FILE__)}/..")
|
24
|
+
ZAPWHITE_BIN = ((defined?(JRUBY_VERSION) || Gem.win_platform?) ? 'ruby ' : '') + File.join(BASE_DIR, 'bin', 'zapwhite')
|
25
|
+
|
26
|
+
include Test::Unit::Assertions
|
27
|
+
|
28
|
+
def setup
|
29
|
+
self.setup_working_dir
|
30
|
+
end
|
31
|
+
|
32
|
+
def teardown
|
33
|
+
self.teardown_working_dir
|
34
|
+
end
|
35
|
+
|
36
|
+
def setup_working_dir
|
37
|
+
@cwd = Dir.pwd
|
38
|
+
|
39
|
+
FileUtils.mkdir_p self.working_dir
|
40
|
+
Dir.chdir(self.working_dir)
|
41
|
+
end
|
42
|
+
|
43
|
+
def teardown_working_dir
|
44
|
+
Dir.chdir(@cwd)
|
45
|
+
if passed?
|
46
|
+
FileUtils.rm_rf self.working_dir if File.exist?(self.working_dir)
|
47
|
+
else
|
48
|
+
$stderr.puts "Test #{self.class.name}.#{name} Failed. Leaving working directory #{self.working_dir}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def working_dir
|
53
|
+
@working_dir ||= "#{workspace_dir}/#{self.class.name.gsub(/[\.\:]/, '_')}_#{name}_#{::SecureRandom.hex}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def workspace_dir
|
57
|
+
@workspace_dir ||= File.expand_path(ENV['TEST_TMP_DIR'] || "#{BASE_DIR}/tmp/workspace")
|
58
|
+
end
|
59
|
+
|
60
|
+
# Evaluate block after changing directory to specified directory
|
61
|
+
def in_dir(dir, &block)
|
62
|
+
original_dir = Dir.pwd
|
63
|
+
begin
|
64
|
+
Dir.chdir(dir)
|
65
|
+
block.call
|
66
|
+
ensure
|
67
|
+
Dir.chdir(original_dir)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def run_command(command, exitcode = 0)
|
72
|
+
output = `#{command}`
|
73
|
+
status = $?.exitstatus
|
74
|
+
raise "Error executing command: #{command}\nExpected exitcode #{exitcode}, Actual exitcode #{status}\nOutput: #{output}" unless status == exitcode
|
75
|
+
output
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_git_repo
|
79
|
+
directory = "#{working_dir}#{::SecureRandom.hex}"
|
80
|
+
|
81
|
+
FileUtils.mkdir_p directory
|
82
|
+
|
83
|
+
in_dir(directory) do
|
84
|
+
run_command('git init')
|
85
|
+
run_command("git config --local user.name \"Your Name\"")
|
86
|
+
run_command("git config --local user.email \"you@example.com\"")
|
87
|
+
yield
|
88
|
+
run_command('git add *')
|
89
|
+
run_command("git commit -m \"initial commit\"")
|
90
|
+
end
|
91
|
+
|
92
|
+
directory
|
93
|
+
end
|
94
|
+
|
95
|
+
# Write .gitattributes relative to current working directory
|
96
|
+
def write_gitattributes_file(content)
|
97
|
+
write_file('.gitattributes', content)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Write file relative to current working directory
|
101
|
+
def write_file(filename, content)
|
102
|
+
FileUtils.mkdir_p File.dirname(filename)
|
103
|
+
IO.binwrite(filename, content)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
|
3
|
+
class Reality::TestZapwhite < Reality::TestCase
|
4
|
+
def test_no_changes_required
|
5
|
+
dir = create_git_repo do
|
6
|
+
write_gitattributes_file(<<TEXT)
|
7
|
+
*.md text
|
8
|
+
TEXT
|
9
|
+
write_file('README.md', "Hello\n")
|
10
|
+
end
|
11
|
+
in_dir(dir) do
|
12
|
+
output = run_command("#{ZAPWHITE_BIN}", 0)
|
13
|
+
assert_equal '', output
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_fixing_trailing_whitespace
|
18
|
+
dir = create_git_repo do
|
19
|
+
write_gitattributes_file(<<TEXT)
|
20
|
+
*.md text
|
21
|
+
TEXT
|
22
|
+
write_file('README.md', "Hello \n")
|
23
|
+
end
|
24
|
+
in_dir(dir) do
|
25
|
+
output = run_command("#{ZAPWHITE_BIN}", 1)
|
26
|
+
assert_equal "Fixing: README.md\n", output
|
27
|
+
assert_equal "Hello\n", IO.binread("#{dir}/README.md")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_fixing_trailing_whitespace_not_crlf_specified
|
32
|
+
dir = create_git_repo do
|
33
|
+
write_gitattributes_file(<<TEXT)
|
34
|
+
*.md text -crlf
|
35
|
+
TEXT
|
36
|
+
write_file('README.md', "Hello \n")
|
37
|
+
end
|
38
|
+
in_dir(dir) do
|
39
|
+
output = run_command("#{ZAPWHITE_BIN}", 1)
|
40
|
+
assert_equal "Fixing: README.md\n", output
|
41
|
+
assert_equal "Hello\n", IO.binread("#{dir}/README.md")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_end_of_file_new_line
|
46
|
+
dir = create_git_repo do
|
47
|
+
write_gitattributes_file(<<TEXT)
|
48
|
+
*.md text
|
49
|
+
TEXT
|
50
|
+
write_file('README.md', 'Hello')
|
51
|
+
end
|
52
|
+
in_dir(dir) do
|
53
|
+
output = run_command("#{ZAPWHITE_BIN}", 1)
|
54
|
+
assert_equal "Fixing: README.md\n", output
|
55
|
+
assert_equal "Hello\n", IO.binread("#{dir}/README.md")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_dos_eol
|
60
|
+
dir = create_git_repo do
|
61
|
+
write_gitattributes_file(<<TEXT)
|
62
|
+
*.bat text crlf
|
63
|
+
TEXT
|
64
|
+
write_file('run.bat', "echo hi\n")
|
65
|
+
end
|
66
|
+
in_dir(dir) do
|
67
|
+
output = run_command("#{ZAPWHITE_BIN}", 1)
|
68
|
+
assert_equal "Fixing: run.bat\n", output
|
69
|
+
assert_equal "echo hi\r\n", IO.binread("#{dir}/run.bat")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_dedup_new_line
|
74
|
+
dir = create_git_repo do
|
75
|
+
write_gitattributes_file(<<TEXT)
|
76
|
+
*.md text -dupnl
|
77
|
+
TEXT
|
78
|
+
write_file('README.md', "Hello\n\n\n\n\nHow are you?\n")
|
79
|
+
end
|
80
|
+
in_dir(dir) do
|
81
|
+
output = run_command("#{ZAPWHITE_BIN}", 1)
|
82
|
+
assert_equal "Fixing: README.md\n", output
|
83
|
+
assert_equal "Hello\n\nHow are you?\n", IO.binread("#{dir}/README.md")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_no_dedup_new_line
|
88
|
+
dir = create_git_repo do
|
89
|
+
write_gitattributes_file(<<TEXT)
|
90
|
+
*.md text dupnl
|
91
|
+
TEXT
|
92
|
+
write_file('README.md', "Hello\n\n\n\nHow are you?\n")
|
93
|
+
end
|
94
|
+
in_dir(dir) do
|
95
|
+
output = run_command("#{ZAPWHITE_BIN}", 0)
|
96
|
+
assert_equal '', output
|
97
|
+
assert_equal "Hello\n\n\n\nHow are you?\n", IO.binread("#{dir}/README.md")
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_no_eofnl
|
102
|
+
dir = create_git_repo do
|
103
|
+
write_gitattributes_file(<<TEXT)
|
104
|
+
*.md text -eofnl
|
105
|
+
TEXT
|
106
|
+
write_file('README.md', 'Hello')
|
107
|
+
end
|
108
|
+
in_dir(dir) do
|
109
|
+
output = run_command("#{ZAPWHITE_BIN}", 0)
|
110
|
+
assert_equal '', output
|
111
|
+
assert_equal 'Hello', IO.binread("#{dir}/README.md")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/zapwhite.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{zapwhite}
|
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/zapwhite}
|
12
|
+
s.summary = %q{A simple tool to normalize whitespace.}
|
13
|
+
s.description = %q{A simple tool to normalize whitespace.}
|
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_dependency 'gitattributes', '= 1.1.0'
|
25
|
+
|
26
|
+
s.add_development_dependency(%q<minitest>, ['= 5.9.1'])
|
27
|
+
s.add_development_dependency(%q<test-unit>, ['= 3.1.5'])
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zapwhite
|
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-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: gitattributes
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.9.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.9.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.5
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.1.5
|
55
|
+
description: A simple tool to normalize whitespace.
|
56
|
+
email: peter@realityforge.org
|
57
|
+
executables:
|
58
|
+
- zapwhite
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitattributes"
|
63
|
+
- ".gitignore"
|
64
|
+
- ".ruby-version"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CONTRIBUTING.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/zapwhite
|
72
|
+
- lib/reality/zapwhite.rb
|
73
|
+
- test/helper.rb
|
74
|
+
- test/reality/test_zapwhite.rb
|
75
|
+
- zapwhite.gemspec
|
76
|
+
homepage: https://github.com/realityforge/zapwhite
|
77
|
+
licenses: []
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options:
|
81
|
+
- "--line-numbers"
|
82
|
+
- "--inline-source"
|
83
|
+
- "--title"
|
84
|
+
- gitattributes
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.5.1
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: A simple tool to normalize whitespace.
|
103
|
+
test_files: []
|