Class: GitNewlineAtEof::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/git-newline-at-eof.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Application



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git-newline-at-eof.rb', line 8

def initialize(argv)
  @in_git_dir = nil
  @options = {}
  @options[:feed_last_line] = false
  @options[:discard_last_newline] = false
  @options[:treat_all] = false
  @options[:check_all] = false
  @options[:help] = false
  @options[:opted] = false

  @opt = OptionParser.new
  [
    [
      '-f', '--feed-last-line',
      'Add newline to not terminated line at end of file.',
      proc { |v|
        @options[:feed_last_line] = true
        @options[:opted] = true
      }
    ],
    [
      '-d',
      '--discard-last-newline',
      'Remove discarded newline at end of file.',
      proc { |v|
        @options[:discard_last_newline] = true
        @options[:opted] = true
      }
    ],
    [
      '-a',
      '--treat-all',
      'This is identical with -f -d.',
      proc { |v|
        @options[:treat_all] = true
        @options[:opted] = true
      }
    ],
    [
      '-c',
      '--check-all',
      'Check and show warning about newline at end of file.',
      proc { |v|
        @options[:check_all] = true
        @options[:opted] = true
      }
    ],
    [
      '-h',
      '--help',
      'Show this message.',
      proc { |v|
        @options[:help] = true
        @options[:opted] = true
      }
    ],
    [
      '-v',
      '--version',
      'Show version.',
      proc {
        @options[:opted] = true
        puts @opt.ver
      }
    ]
  ].each do |short, long, desc, proc_obj|
    @opt.on(short, long, desc, &proc_obj)
  end
  @opt.program_name = 'git newline-at-eof'
  @opt.version = GitNewlineAtEof::VERSION
  @opt.summary_width = 27
  @opt.parse!(argv)
end

Instance Method Details

#check_allObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/git-newline-at-eof.rb', line 126

def check_all
  exist_warning = false
  @files.each do |f|
    if no_newline?(f[:last_newlines_num])
      exist_warning = true
      puts "#{f[:filename]}: no newline at end of file"
    elsif discarded_newline?(f[:last_newlines_num])
      exist_warning = true
      discarded_num = f[:last_newlines_num] - 1
      puts "#{f[:filename]}: discarded #{discarded_num} newline#{discarded_num > 1 ? 's' : ''} at end of file"
    end
  end
  if exist_warning
    :warning
  else
    :clean
  end
end

#discard_last_newline_allObject



163
164
165
166
167
168
169
# File 'lib/git-newline-at-eof.rb', line 163

def discard_last_newline_all
  @files.each do |f|
    if discarded_newline?(f[:last_newlines_num])
      discard_last_newline(f[:filename], f[:last_newlines_num] - 1)
    end
  end
end

#feed_last_line_allObject



155
156
157
158
159
160
161
# File 'lib/git-newline-at-eof.rb', line 155

def feed_last_line_all
  @files.each do |f|
    if no_newline?(f[:last_newlines_num])
      feed_last_line(f[:filename])
    end
  end
end

#in_git_dir?Boolean



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/git-newline-at-eof.rb', line 113

def in_git_dir?
  if !@in_git_dir.nil?
    @in_git_dir
  else
    o, e, exit_status = Open3.capture3('git rev-parse')
    if exit_status == 0
      @in_git_dir = true
    else
      @in_git_dir = false
    end
  end
end

#runObject

return value is used for exit status



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/git-newline-at-eof.rb', line 82

def run # return value is used for exit status
  if !@options[:opted] || @options[:help]
    puts @opt.help
    0
  elsif !in_git_dir?
    puts 'Here is not Git dir.'
    128
  else
    @files = files
    if @options[:check_all]
      case check_all
      when :clean
        0
      when :warning
        1
      end
    elsif @options[:treat_all]
      treat_all
      0
    else
      if @options[:feed_last_line]
        feed_last_line_all
      end
      if @options[:discard_last_newline]
        discard_last_newline_all
      end
      0
    end
  end
end

#treat_allObject



145
146
147
148
149
150
151
152
153
# File 'lib/git-newline-at-eof.rb', line 145

def treat_all
  @files.each do |f|
    if no_newline?(f[:last_newlines_num])
      feed_last_line(f[:filename])
    elsif discarded_newline?(f[:last_newlines_num])
      discard_last_newline(f[:filename], f[:last_newlines_num] - 1)
    end
  end
end