codegrade 0.0.1 → 0.0.2

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: e387a4ec843d880c7c20f2f251851de9797ad772
4
- data.tar.gz: 6b22863716a4f1ea94f8c68020db34d32ddbb400
3
+ metadata.gz: 5db65c0710cb981a6a70470a77af6483a0003067
4
+ data.tar.gz: fa8eb37af5f94816e754f3d114c993b72fc9152b
5
5
  SHA512:
6
- metadata.gz: 2c8d198ee979cf3d2a8b8851ea8269c3c6f8d41be20154ae39af2f17d2c8abb8dc53881d23d693acab4caabb589a76015708a2709440c7cb0f704a9ab0fb1a76
7
- data.tar.gz: 123e9b9abfc205e094889c5bc6af56c810aa0c54fd9aaff6f81236a9851c7dc172333b6935cb9b07a78ab54313434fe44f001c4af21d66ba47740994f424e2a8
6
+ metadata.gz: ed5a8224328dc6347e910839b9b93ad444dfa5add89fdb0d8ee3c5eecc324e627415ca8e95c4277b6c426045373bf1081f8f1fe9238fdba483367bc6c7bd202b
7
+ data.tar.gz: b4722a0199d9084dcaca7695113061cd116edc40dda13b2e13a3eccb5059c40349bd9ef8fa733a58ee96508e008595cc9b116d4854001bd3102da370d9796dfc
data/bin/codegrade CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: utf-8
3
2
 
4
3
  $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
5
4
 
6
5
  require 'codegrade'
7
6
 
8
- offenses = Codegrade::Grader::Grader.new(Codegrade::Commit.new).grade
7
+ commit = Codegrade::Commit.new('.', ARGV[0])
8
+ offenses = Codegrade::Grader::Grader.new(commit).grade
9
9
  Codegrade::Formatter.new(offenses).print
@@ -32,9 +32,12 @@ module Codegrade
32
32
  def parse_git_tree(tree, path)
33
33
  files = []
34
34
 
35
- tree.walk_blobs do |root, entry|
36
- path = File.expand_path(root, working_directory)
37
- files.push(File.join(path, entry[:name]))
35
+ return [] if commit.parents.size > 1
36
+
37
+ diff = commit.parents[0].diff(commit)
38
+
39
+ diff.deltas.each do |delta|
40
+ files.push(delta.new_file[:path])
38
41
  end
39
42
 
40
43
  files
@@ -13,7 +13,7 @@ module Codegrade
13
13
  puts
14
14
 
15
15
  offenses.each do |offense|
16
- file = offense.file.gsub(working_directory, '')[1..-1]
16
+ file = offense.file.gsub(working_directory, '') unless offense.file.nil?
17
17
  puts "- #{file}:#{offense.line_number}:#{offense.column_number}"
18
18
  end
19
19
 
@@ -20,199 +20,213 @@ module Codegrade
20
20
 
21
21
  def parse_commit_message
22
22
  lines = @message.split("\n")
23
-
24
- paragraph, paragraph_start, paragraph_line = [], 1, 0
25
- inside_punctation = false
23
+ state = {
24
+ :paragraph => [],
25
+ :paragraph_start => 1,
26
+ :inside_punctation => false }
26
27
 
27
28
  lines.each_with_index do |line, index|
28
- paragraph << line
29
- paragraph_line += 1
30
-
31
- line_number = index + 1
32
- blank_line = blank?(line)
33
- title_line = line_number == 1
34
- end_of_paragraph = (index == lines.length - 1 ||
35
- blank?(lines[index + 1])) && !title_line &&
36
- paragraph.any? && !blank?(paragraph.last)
37
-
38
- if line.start_with?('* ')
39
- check_punctation_no_separating_line(inside_punctation, line_number)
40
-
41
- inside_punctation = true
42
- end
43
-
44
- if title_line
45
- check_title_leading_lowercase(line, line_number)
46
- check_title_too_long(line, line_number)
47
- check_title_trailing_dot(line, line_number)
48
- end
49
-
50
- if end_of_paragraph
51
- if inside_punctation
52
- check_punctation_leading_lowercase(paragraph, paragraph_start)
53
- check_punctation_trailing_dot(paragraph, paragraph_start)
54
- else
55
- check_paragraph_leading_lowercase(paragraph, paragraph_start)
56
- check_paragraph_no_trailing_dot(paragraph, paragraph_start)
57
- end
58
- end
59
-
60
- if blank_line
61
- check_redundant_empty_line(paragraph, line_number)
62
-
63
- paragraph, paragraph_start, paragraph_line = [], line_number + 1, 0
64
- inside_punctation = false
65
- else
66
- check_title_multiple_lines(line_number)
67
-
68
- check_line_trailing_whitespace(line, line_number)
69
- check_line_too_long(line, line_number) unless title_line
70
- end
71
-
72
- if inside_punctation
73
- if paragraph_line > 1
74
- check_punctation_leading_whitespace(line, line_number)
75
- end
76
- else
77
- if ! blank_line
78
- check_line_leading_whitespace(line, line_number)
79
- end
29
+ state[:paragraph] << line
30
+
31
+ state.merge!(
32
+ :line => line,
33
+ :index => index,
34
+ :line_number => index + 1,
35
+ :blank_line => blank?(line),
36
+ :title_line => index == 0,
37
+ :start_of_punctation => line.start_with?('* '),
38
+ :was_in_punctation => state[:inside_punctation])
39
+
40
+ state[:end_of_paragraph] = !state[:title_line] &&
41
+ state[:paragraph].any? &&
42
+ (index == lines.length - 1 || blank?(lines[index + 1])) &&
43
+ !blank?(state[:paragraph].last)
44
+
45
+ state[:inside_punctation] = state[:inside_punctation] ||
46
+ state[:start_of_punctation]
47
+
48
+ check_title_leading_lowercase(state)
49
+ check_title_too_long(state)
50
+ check_title_trailing_dot(state)
51
+ check_title_multiple_lines(state)
52
+
53
+ check_line_trailing_whitespace(state)
54
+ check_line_too_long(state)
55
+ check_line_leading_whitespace(state)
56
+ check_redundant_empty_line(state)
57
+
58
+ check_punctation_leading_whitespace(state)
59
+ check_punctation_leading_lowercase(state)
60
+ check_punctation_trailing_dot(state)
61
+ check_punctation_no_separating_line(state)
62
+
63
+ check_paragraph_leading_lowercase(state)
64
+ check_paragraph_no_trailing_dot(state)
65
+
66
+ if state[:blank_line]
67
+ state.merge!(
68
+ :paragraph => [],
69
+ :paragraph_start => state[:line_number] + 1,
70
+ :inside_punctation => false)
80
71
  end
81
72
  end
82
73
  end
83
74
 
84
- def check_title_leading_lowercase(line, line_number)
85
- if line[0].downcase == line[0]
75
+ def check_title_leading_lowercase(state)
76
+ if state[:title_line] && state[:line][0].downcase == state[:line][0]
86
77
  add_offense(
87
78
  :category => 'title_leading_lowercase',
88
- :line_number => line_number,
79
+ :line_number => state[:line_number],
89
80
  :column_number => 1)
90
81
  end
91
82
  end
92
83
 
93
- def check_title_too_long(line, line_number)
94
- if line.length > 50
84
+ def check_title_too_long(state)
85
+ if state[:title_line] && state[:line].length > 50
95
86
  add_offense(
96
87
  :category => 'title_too_long',
97
- :line_number => line_number,
88
+ :line_number => state[:line_number],
98
89
  :column_number => 51)
99
90
  end
100
91
  end
101
92
 
102
- def check_title_trailing_dot(line, line_number)
103
- if (m = line.match(/\.\s*$/))
93
+ def check_title_trailing_dot(state)
94
+ if state[:title_line] && (m = state[:line].match(/\.\s*$/))
104
95
  add_offense(
105
96
  :category => 'title_trailing_dot',
106
- :line_number => line_number,
97
+ :line_number => state[:line_number],
107
98
  :column_number => m.begin(0))
108
99
  end
109
100
  end
110
101
 
111
- def check_title_multiple_lines(line_number)
112
- if line_number == 2
102
+ def check_title_multiple_lines(state)
103
+ return if state[:blank_line]
104
+
105
+ if state[:line_number] == 2
113
106
  add_offense(
114
107
  :category => 'title_multiple_lines',
115
- :line_number => line_number)
108
+ :line_number => state[:line_number])
116
109
  end
117
110
  end
118
111
 
119
- def check_redundant_empty_line(paragraph, line_number)
120
- if paragraph.select { |line| !blank?(line) }.empty?
112
+ def check_redundant_empty_line(state)
113
+ return unless state[:blank_line]
114
+
115
+ if state[:paragraph].select { |line| !blank?(line) }.empty?
121
116
  add_offense(
122
117
  :category => 'redundant_empty_line',
123
- :line_number => line_number)
118
+ :line_number => state[:line_number])
124
119
  end
125
120
  end
126
121
 
127
- def check_line_trailing_whitespace(line, line_number)
128
- if (m = line.match(/\s+$/))
122
+ def check_line_trailing_whitespace(state)
123
+ return if state[:blank_line]
124
+
125
+ if (m = state[:line].match(/\s+$/))
129
126
  add_offense(
130
127
  :category => 'line_trailing_whitespace',
131
- :line_number => line_number,
128
+ :line_number => state[:line_number],
132
129
  :column_number => m.begin(0) + 1)
133
130
  end
134
131
  end
135
132
 
136
- def check_line_leading_whitespace(line, line_number)
137
- if (m = line.match(/^\s+/))
133
+ def check_line_leading_whitespace(state)
134
+ if !state[:inside_punctation] &&
135
+ !state[:blank_line] &&
136
+ (m = state[:line].match(/^\s+/))
138
137
  add_offense(
139
138
  :category => 'line_leading_whitespace',
140
- :line_number => line_number,
139
+ :line_number => state[:line_number],
141
140
  :column_number => m.end(0))
142
141
  end
143
142
  end
144
143
 
145
- def check_line_too_long(line, line_number)
146
- if line.length > 70
144
+ def check_line_too_long(state)
145
+ return if state[:blank_line] || state[:title_line]
146
+
147
+ if state[:line].length > 70
147
148
  add_offense(
148
149
  :category => 'line_too_long',
149
- :line_number => line_number,
150
+ :line_number => state[:line_number],
150
151
  :column_number => 71)
151
152
  end
152
153
  end
153
154
 
154
- def check_paragraph_leading_lowercase(paragraph, paragraph_start)
155
- line = strip(paragraph.first)
155
+ def check_paragraph_leading_lowercase(state)
156
+ return unless state[:end_of_paragraph] && !state[:inside_punctation]
157
+
158
+ line = strip(state[:paragraph].first)
156
159
 
157
160
  if line[0].downcase == line[0] &&
158
161
  !link?(line.split[0])
159
162
  add_offense(
160
163
  :category => 'paragraph_leading_lowercase',
161
- :line_number => paragraph_start,
164
+ :line_number => state[:paragraph_start],
162
165
  :column_number => 1)
163
166
  end
164
167
  end
165
168
 
166
- def check_paragraph_no_trailing_dot(paragraph, paragraph_start)
167
- line = strip(paragraph.last)
169
+ def check_paragraph_no_trailing_dot(state)
170
+ return unless state[:end_of_paragraph] && !state[:inside_punctation]
171
+
172
+ line = strip(state[:paragraph].last)
168
173
 
169
174
  if line[-1] != '.' &&
170
175
  !link?(line.split[-1])
171
176
  add_offense(
172
177
  :category => 'paragraph_no_trailing_dot',
173
- :line_number => paragraph_start + paragraph.length - 1,
174
- :column_number => paragraph.last.length)
178
+ :line_number => state[:paragraph_start] +
179
+ state[:paragraph].length - 1,
180
+ :column_number => state[:paragraph].last.length)
175
181
  end
176
182
  end
177
183
 
178
- def check_punctation_no_separating_line(inside_punctation, line_number)
179
- if inside_punctation
184
+ def check_punctation_no_separating_line(state)
185
+ if state[:start_of_punctation] && state[:was_in_punctation]
180
186
  add_offense(
181
187
  :category => 'punctation_no_separating_line',
182
- :line_number => line_number)
188
+ :line_number => state[:line_number])
183
189
  end
184
190
  end
185
191
 
186
- def check_punctation_leading_whitespace(line, line_number)
187
- if (m = line.match(/^\s*/)) && m.end(0) != 2
192
+ def check_punctation_leading_whitespace(state)
193
+ if state[:inside_punctation] &&
194
+ !state[:blank_line] &&
195
+ state[:paragraph].length > 1 &&
196
+ (m = state[:line].match(/^\s*/)) && m.end(0) != 2
188
197
  add_offense(
189
198
  :category => 'punctation_leading_whitespace',
190
- :line_number => line_number,
199
+ :line_number => state[:line_number],
191
200
  :column_number => m.end(0))
192
201
  end
193
202
  end
194
203
 
195
- def check_punctation_leading_lowercase(paragraph, paragraph_start)
196
- line = strip(paragraph.first)
204
+ def check_punctation_leading_lowercase(state)
205
+ return unless state[:end_of_paragraph] && state[:inside_punctation]
206
+
207
+ line = strip(state[:paragraph].first)
197
208
 
198
209
  if line[2].downcase == line[2] &&
199
210
  !link?(line.split[0])
200
211
  add_offense(
201
212
  :category => 'punctation_leading_lowercase',
202
- :line_number => paragraph_start,
213
+ :line_number => state[:paragraph_start],
203
214
  :column_number => 3)
204
215
  end
205
216
  end
206
217
 
207
- def check_punctation_trailing_dot(paragraph, paragraph_start)
208
- line = strip(paragraph.last)
218
+ def check_punctation_trailing_dot(state)
219
+ return unless state[:end_of_paragraph] && state[:inside_punctation]
220
+
221
+ line = strip(state[:paragraph].last)
209
222
 
210
223
  if line[-1] == '.' &&
211
224
  !link?(line.split[-1])
212
225
  add_offense(
213
226
  :category => 'punctation_trailing_dot',
214
- :line_number => paragraph_start + paragraph.length - 1,
215
- :column_number => paragraph.last.length)
227
+ :line_number => state[:paragraph_start] +
228
+ state[:paragraph].length - 1,
229
+ :column_number => state[:paragraph].last.length)
216
230
  end
217
231
  end
218
232
 
@@ -1,3 +1,3 @@
1
1
  module Codegrade
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codegrade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michał Młoźniak