git-newline-at-eof 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +149 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/git-newline-at-eof +7 -0
- data/git-newline-at-eof.gemspec +25 -0
- data/lib/git-newline-at-eof.rb +163 -0
- data/lib/git-newline-at-eof/version.rb +3 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9174c33ef7a822bbbe6b7f0b791bed36d7b3ad19
|
4
|
+
data.tar.gz: f3b93079fee26591df3bc50f29a8176dd6206289
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 373d11b4fdabfcdda3299720c92211f35d4c06e77119bbe434c488e771a24d35694df78e5a287d2ddb68b8f8b478fe314d8869759a0fcaaba77f42e46c44a81d
|
7
|
+
data.tar.gz: e79543fb3b7e4aa15abefda059db662d90043f4b57911d4399d873509acadb1996d677a5b9f85d112460b7939e18479825d259071edf0c77aee6f611235b30f2
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Code Ass
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# git-newline-at-eof
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Install it yourself as:
|
6
|
+
|
7
|
+
$ gem install git-newline-at-eof
|
8
|
+
|
9
|
+
## Description
|
10
|
+
|
11
|
+
[`POSIX.1-2008`](http://pubs.opengroup.org/onlinepubs/9699919799/) says about [definition of `Line`](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206):
|
12
|
+
|
13
|
+
> ### 3.206 Line
|
14
|
+
> A sequence of zero or more non- \<newline\> characters plus a terminating \<newline\> character.
|
15
|
+
|
16
|
+
And also says about [definition of `Text File`](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_403):
|
17
|
+
|
18
|
+
> ### 3.403 Text File
|
19
|
+
> A file that contains characters organized into zero or more lines. The lines do not contain NUL characters and none can exceed {LINE_MAX} bytes in length, including the \<newline\> character. Although POSIX.1-2008 does not distinguish between text files and binary files (see the ISO C standard), many utilities only produce predictable or meaningful output when operating on text files. The standard utilities that have such restrictions always specify "text files" in their STDIN or INPUT FILES sections.
|
20
|
+
|
21
|
+
This Git subcommand checks and fixes newlines at end of file in your Git repository.
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### `--feed-last-line`
|
26
|
+
|
27
|
+
Add newline to line what is not terminated by newline at end of file.
|
28
|
+
|
29
|
+
```bash
|
30
|
+
$ mkdir /tmp/test
|
31
|
+
$ cd /tmp/test
|
32
|
+
$ printf "" > file0
|
33
|
+
$ printf "aaa" > file1
|
34
|
+
$ printf "bbb\n" > file2
|
35
|
+
$ git init
|
36
|
+
Initialized empty Git repository in /tmp/test/.git/
|
37
|
+
$ git add .
|
38
|
+
$ git commit -m "Initial commit"
|
39
|
+
[master (root-commit) 7c4d543] Initial commit
|
40
|
+
3 files changed, 2 insertions(+)
|
41
|
+
create mode 100644 file0
|
42
|
+
create mode 100644 file1
|
43
|
+
create mode 100644 file2
|
44
|
+
$ git newline-at-eof --feed-last-line
|
45
|
+
$ git diff
|
46
|
+
diff --git a/file1 b/file1
|
47
|
+
index 7c4a013..72943a1 100644
|
48
|
+
--- a/file1
|
49
|
+
+++ b/file1
|
50
|
+
@@ -1 +1 @@
|
51
|
+
-aaa
|
52
|
+
|
53
|
+
+aaa
|
54
|
+
$ git add file1
|
55
|
+
$ git commit -m "Fix last line terminator"
|
56
|
+
[master aba3275] Fix last line terminator
|
57
|
+
1 file changed, 1 insertion(+), 1 deletion(-)
|
58
|
+
```
|
59
|
+
|
60
|
+
### `--discard-last-newline`
|
61
|
+
|
62
|
+
Remove discarded newline at end of file.
|
63
|
+
|
64
|
+
```bash
|
65
|
+
$ mkdir /tmp/test
|
66
|
+
$ cd /tmp/test
|
67
|
+
$ printf "" > file0
|
68
|
+
$ printf "aaa\n" > file1
|
69
|
+
$ printf "bbb\n\n\n" > file2
|
70
|
+
$ git init
|
71
|
+
Initialized empty Git repository in /tmp/test/.git/
|
72
|
+
$ git add .
|
73
|
+
$ git commit -m "Initial commit"
|
74
|
+
[master (root-commit) 1ef005b] Initial commit
|
75
|
+
3 files changed, 4 insertions(+)
|
76
|
+
create mode 100644 file0
|
77
|
+
create mode 100644 file1
|
78
|
+
create mode 100644 file2
|
79
|
+
$ git newline-at-eof --discard-last-newline
|
80
|
+
$ git diff
|
81
|
+
diff --git a/file2 b/file2
|
82
|
+
index 6da7e67..f761ec1 100644
|
83
|
+
--- a/file2
|
84
|
+
+++ b/file2
|
85
|
+
@@ -1,3 +1 @@
|
86
|
+
bbb
|
87
|
+
-
|
88
|
+
-
|
89
|
+
$ git add file1
|
90
|
+
$ git commit -m "Discard newlines at eof"
|
91
|
+
[master 68de945] Discard newlines at eof
|
92
|
+
1 file changed, 2 deletions(-)
|
93
|
+
```
|
94
|
+
|
95
|
+
### `--treat-all`
|
96
|
+
|
97
|
+
This is identical with `--feed-last-line --discard-last-newline`.
|
98
|
+
|
99
|
+
### `--check-all`
|
100
|
+
|
101
|
+
Check and show warning about newline at end of file.
|
102
|
+
|
103
|
+
```bash
|
104
|
+
$ mkdir /tmp/test
|
105
|
+
$ cd /tmp/test
|
106
|
+
$ printf "" > file0
|
107
|
+
$ printf "aaa" > file1
|
108
|
+
$ printf "bbb\n\n" > file2
|
109
|
+
$ printf "ccc\n\n\n" > file3
|
110
|
+
$ git init
|
111
|
+
Initialized empty Git repository in /tmp/test/.git/
|
112
|
+
$ git add .
|
113
|
+
$ git commit -m "Initial commit"
|
114
|
+
[master (root-commit) 28cf1f0] Initial commit
|
115
|
+
4 files changed, 6 insertions(+)
|
116
|
+
create mode 100644 file0
|
117
|
+
create mode 100644 file1
|
118
|
+
create mode 100644 file2
|
119
|
+
create mode 100644 file3
|
120
|
+
$ git newline-at-eof --check-all
|
121
|
+
file1: no newline at end of file
|
122
|
+
file2: discarded 1 newline at end of file
|
123
|
+
file3: discarded 2 newlines at end of file
|
124
|
+
```
|
125
|
+
|
126
|
+
## Supported Versions
|
127
|
+
|
128
|
+
- Ruby 2.3
|
129
|
+
- Ruby 2.4
|
130
|
+
- JRuby 9.1.x.x
|
131
|
+
|
132
|
+
## Development
|
133
|
+
|
134
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
135
|
+
|
136
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
137
|
+
|
138
|
+
## Contributing
|
139
|
+
|
140
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aycabta/git-newline-at-eof.
|
141
|
+
|
142
|
+
|
143
|
+
## License
|
144
|
+
|
145
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
146
|
+
|
147
|
+
## Badges
|
148
|
+
|
149
|
+
[![Build Status](https://travis-ci.org/aycabta/git-newline-at-eof.svg)](https://travis-ci.org/aycabta/git-newline-at-eof)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "git/newline/at/eof"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'git-newline-at-eof/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'git-newline-at-eof'
|
8
|
+
spec.version = GitNewlineAtEof::VERSION
|
9
|
+
spec.authors = ['Code Ass']
|
10
|
+
spec.email = ['aycabta@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Check and fix newline at end of file in Git respository.}
|
13
|
+
spec.description = %q{Check and fix newline at end of file in Git respository.}
|
14
|
+
spec.homepage = 'https://github.com/aycabta/git-newline-at-eof'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'test-unit'
|
25
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'git-newline-at-eof/version'
|
2
|
+
require 'optparse'
|
3
|
+
|
4
|
+
module GitNewlineAtEof
|
5
|
+
class Application
|
6
|
+
def initialize(argv)
|
7
|
+
@files = files
|
8
|
+
@is_feed_last_line = false
|
9
|
+
@is_discard_last_newline = false
|
10
|
+
@is_treat_all = false
|
11
|
+
@is_check_all = false
|
12
|
+
|
13
|
+
opt = OptionParser.new
|
14
|
+
opt.on('-f', '--feed-last-line') { |v| @is_feed_last_line = true }
|
15
|
+
opt.on('-d', '--discard-last-newline') { |v| @is_discard_last_newline = true }
|
16
|
+
opt.on('-a', '--treat-all') { |v| @is_treat_all = true }
|
17
|
+
opt.on('-c', '--check-all') { |v| @is_check_all = true }
|
18
|
+
opt.parse!(argv)
|
19
|
+
end
|
20
|
+
|
21
|
+
def run
|
22
|
+
if @is_check_all
|
23
|
+
check_all
|
24
|
+
elsif @is_treat_all
|
25
|
+
treat_all
|
26
|
+
else
|
27
|
+
if @is_feed_last_line
|
28
|
+
feed_last_line_all
|
29
|
+
end
|
30
|
+
if @is_discard_last_newline
|
31
|
+
discard_last_newline_all
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def check_all
|
37
|
+
@files.each do |f|
|
38
|
+
if no_newline?(f[:last_newlines_num])
|
39
|
+
puts "#{f[:filename]}: no newline at end of file"
|
40
|
+
elsif discarded_newline?(f[:last_newlines_num])
|
41
|
+
discarded_num = f[:last_newlines_num] - 1
|
42
|
+
puts "#{f[:filename]}: discarded #{discarded_num} newline#{discarded_num > 1 ? 's' : ''} at end of file"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def treat_all
|
48
|
+
@files.each do |f|
|
49
|
+
if no_newline?(f[:last_newlines_num])
|
50
|
+
feed_last_line(f[:filename])
|
51
|
+
elsif discarded_newline?(f[:last_newlines_num])
|
52
|
+
discard_last_newline(f[:filename], f[:last_newlines_num] - 1)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def feed_last_line_all
|
58
|
+
@files.each do |f|
|
59
|
+
if no_newline?(f[:last_newlines_num])
|
60
|
+
feed_last_line(f[:filename])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def discard_last_newline_all
|
66
|
+
@files.each do |f|
|
67
|
+
if discarded_newline?(f[:last_newlines_num])
|
68
|
+
discard_last_newline(f[:filename], f[:last_newlines_num] - 1)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def feed_last_line(filename)
|
74
|
+
filepath = current_dir(filename)
|
75
|
+
File.open(filepath, 'at') do |f|
|
76
|
+
f.write("\n")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
private :feed_last_line
|
80
|
+
|
81
|
+
def discard_last_newline(filename, discard_num)
|
82
|
+
filepath = current_dir(filename)
|
83
|
+
lines = nil
|
84
|
+
File.open(filepath, 'rt') do |f|
|
85
|
+
lines = f.readlines
|
86
|
+
end
|
87
|
+
File.open(filepath, 'wt') do |f|
|
88
|
+
lines[0, lines.size - discard_num].each do |l|
|
89
|
+
f.write(l)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
private :discard_last_newline
|
94
|
+
|
95
|
+
def no_newline?(last_newlines_num)
|
96
|
+
if last_newlines_num.nil?
|
97
|
+
false
|
98
|
+
elsif last_newlines_num == 0
|
99
|
+
true
|
100
|
+
else
|
101
|
+
false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
private :no_newline?
|
105
|
+
|
106
|
+
def discarded_newline?(last_newlines_num)
|
107
|
+
if last_newlines_num.nil?
|
108
|
+
false
|
109
|
+
elsif last_newlines_num > 1
|
110
|
+
true
|
111
|
+
else
|
112
|
+
false
|
113
|
+
end
|
114
|
+
end
|
115
|
+
private :discarded_newline?
|
116
|
+
|
117
|
+
def files
|
118
|
+
`git ls-files`.split("\n").map { |filename|
|
119
|
+
filepath = current_dir(filename)
|
120
|
+
num = 0
|
121
|
+
begin
|
122
|
+
num = File.open(filepath, 'rt') { |f| count_last_newlines(f) }
|
123
|
+
rescue
|
124
|
+
num = nil
|
125
|
+
end
|
126
|
+
{
|
127
|
+
filename: filename,
|
128
|
+
last_newlines_num: num
|
129
|
+
}
|
130
|
+
}
|
131
|
+
end
|
132
|
+
private :files
|
133
|
+
|
134
|
+
def count_last_newlines(f)
|
135
|
+
if f.size == 0
|
136
|
+
nil
|
137
|
+
else
|
138
|
+
count = 0
|
139
|
+
f.size.step(1, -1) do |offset|
|
140
|
+
offset -= 1
|
141
|
+
f.seek(offset, IO::SEEK_SET)
|
142
|
+
if f.getc == "\n"
|
143
|
+
count += 1
|
144
|
+
else
|
145
|
+
break
|
146
|
+
end
|
147
|
+
end
|
148
|
+
count
|
149
|
+
end
|
150
|
+
end
|
151
|
+
private :count_last_newlines
|
152
|
+
|
153
|
+
def repository_toplevel_dir
|
154
|
+
`git rev-parse --show-toplevel`.chomp
|
155
|
+
end
|
156
|
+
private :repository_toplevel_dir
|
157
|
+
|
158
|
+
def current_dir(filename)
|
159
|
+
File.join(repository_toplevel_dir, `git rev-parse --show-prefix`.chomp, filename)
|
160
|
+
end
|
161
|
+
private :current_dir
|
162
|
+
end
|
163
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: git-newline-at-eof
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Code Ass
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Check and fix newline at end of file in Git respository.
|
56
|
+
email:
|
57
|
+
- aycabta@gmail.com
|
58
|
+
executables:
|
59
|
+
- git-newline-at-eof
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- exe/git-newline-at-eof
|
72
|
+
- git-newline-at-eof.gemspec
|
73
|
+
- lib/git-newline-at-eof.rb
|
74
|
+
- lib/git-newline-at-eof/version.rb
|
75
|
+
homepage: https://github.com/aycabta/git-newline-at-eof
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.6.8
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Check and fix newline at end of file in Git respository.
|
99
|
+
test_files: []
|