pre-commit 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
data/lib/pre-commit/checks.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'support/all'
|
2
2
|
require 'pre-commit/utils'
|
3
3
|
require 'pre-commit/checks/merge_conflict'
|
4
|
+
require 'pre-commit/checks/tabs'
|
5
|
+
require 'pre-commit/checks/console_log'
|
6
|
+
require 'pre-commit/checks/debugger_check'
|
4
7
|
|
5
8
|
class PreCommit
|
6
9
|
|
@@ -8,47 +11,6 @@ class PreCommit
|
|
8
11
|
WhiteSpaceChecker.check
|
9
12
|
}
|
10
13
|
|
11
|
-
ConsoleLog = lambda {
|
12
|
-
|
13
|
-
if File.exists?('public/javascripts') && (args = Utils.staged_files('public/javascripts')).size > 0
|
14
|
-
if system("git grep -n -q \"console\.log\" #{args}")
|
15
|
-
puts "\n[ERROR] console.log found:"
|
16
|
-
!system("git grep -n \"console\.log\" #{args}")
|
17
|
-
else
|
18
|
-
true
|
19
|
-
end
|
20
|
-
else
|
21
|
-
true
|
22
|
-
end
|
23
|
-
}
|
24
|
-
|
25
|
-
Debugger = lambda {
|
26
|
-
dirs = ['app/', 'lib/', 'script/', 'vendor/', 'test/'].reject {|d| !File.exists?(d)}
|
27
|
-
if dirs.size > 0 && (args = Utils.staged_files(dirs)).size > 0
|
28
|
-
if system("git grep -n -q debugger #{args}")
|
29
|
-
puts "\n[ERROR] debugger statement(s) found:"
|
30
|
-
!system("git grep -n debugger #{args}")
|
31
|
-
else
|
32
|
-
true
|
33
|
-
end
|
34
|
-
else
|
35
|
-
true
|
36
|
-
end
|
37
|
-
}
|
38
|
-
|
39
|
-
Tabs = lambda {
|
40
|
-
if (files = Utils.staged_files('*')).size > 0
|
41
|
-
if system("grep -PnH -q '^\t' #{files}")
|
42
|
-
puts "\n[ERROR] tab before initial space:"
|
43
|
-
!system("grep -PnH '^\t' #{files}")
|
44
|
-
else
|
45
|
-
true
|
46
|
-
end
|
47
|
-
else
|
48
|
-
true
|
49
|
-
end
|
50
|
-
}
|
51
|
-
|
52
14
|
ClosureSyntaxCheck = lambda {
|
53
15
|
compiler = "test/javascript/lib/compiler.jar"
|
54
16
|
|
@@ -88,12 +50,17 @@ class PreCommit
|
|
88
50
|
:console_log => ConsoleLog,
|
89
51
|
:js_lint_all => JSLintAll,
|
90
52
|
:js_lint_new => JSLintNew,
|
91
|
-
:debugger =>
|
53
|
+
:debugger => DebuggerCheck,
|
92
54
|
:tabs => Tabs,
|
93
55
|
:closure_syntax_check => ClosureSyntaxCheck,
|
94
56
|
:merge_conflict => MergeConflict
|
95
57
|
}
|
96
58
|
|
59
|
+
# Can not delete this method with out a deprecation strategy.
|
60
|
+
# It is refered to in the generated pre-commit hook in versions 0.0-0.1.1
|
61
|
+
#
|
62
|
+
# NOTE: The deprecation strategy *may* be just delete it since, we're still
|
63
|
+
# pre 1.0.
|
97
64
|
def self.checks_to_run
|
98
65
|
checks_to_run = `git config pre-commit.checks`.chomp.split(/,\s*/).map(&:to_sym)
|
99
66
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class ConsoleLog
|
2
|
+
|
3
|
+
attr_accessor :staged_files, :error_message
|
4
|
+
|
5
|
+
def self.call(quiet=false)
|
6
|
+
check = new
|
7
|
+
check.staged_files = Utils.staged_files('public/javascripts')
|
8
|
+
|
9
|
+
result = check.run
|
10
|
+
if !quiet && !result
|
11
|
+
puts check.error_message
|
12
|
+
end
|
13
|
+
result
|
14
|
+
end
|
15
|
+
|
16
|
+
def run
|
17
|
+
return true if staged_files.empty?
|
18
|
+
if detected_bad_code?
|
19
|
+
@error_message = "pre-commit: console.log found:\n"
|
20
|
+
@error_message += instances_of_console_log_violations
|
21
|
+
false
|
22
|
+
else
|
23
|
+
true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def detected_bad_code?
|
28
|
+
system("grep -q 'console.log' #{staged_files}")
|
29
|
+
end
|
30
|
+
|
31
|
+
def instances_of_console_log_violations
|
32
|
+
`grep -nH 'console.log' #{staged_files}`
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class DebuggerCheck
|
2
|
+
attr_accessor :staged_files, :error_message, :grep_command
|
3
|
+
|
4
|
+
def self.call(quiet=false)
|
5
|
+
dirs = ['app/', 'lib/', 'script/', 'vendor/', 'test/'].reject {|d| !File.exists?(d)}
|
6
|
+
check = new
|
7
|
+
check.staged_files = Utils.staged_files(*dirs)
|
8
|
+
|
9
|
+
result = check.run
|
10
|
+
if !quiet && !result
|
11
|
+
$stderr.puts check.error_message
|
12
|
+
end
|
13
|
+
result
|
14
|
+
end
|
15
|
+
|
16
|
+
def run
|
17
|
+
return true if staged_files.empty?
|
18
|
+
|
19
|
+
if detected_bad_code?
|
20
|
+
@error_message = "pre-commit: debugger statement found:\n"
|
21
|
+
@error_message += instances_of_debugger_violations
|
22
|
+
false
|
23
|
+
else
|
24
|
+
true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def detected_bad_code?
|
29
|
+
cmd = grep_command || "git grep"
|
30
|
+
system("#{cmd} -nH -q debugger #{staged_files}")
|
31
|
+
end
|
32
|
+
|
33
|
+
def instances_of_debugger_violations
|
34
|
+
cmd = grep_command || "git grep"
|
35
|
+
`#{cmd} -nH debugger #{staged_files}`
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Tabs
|
2
|
+
|
3
|
+
attr_accessor :staged_files, :error_message
|
4
|
+
|
5
|
+
# Maintaining the functionality of `call` for backwards compatibility
|
6
|
+
# Currently, the call method is expected to:
|
7
|
+
# * run a check
|
8
|
+
# * print any corresponding error messages if the check fails
|
9
|
+
def self.call
|
10
|
+
check = new
|
11
|
+
check.staged_files = Utils.staged_files('*')
|
12
|
+
check.run
|
13
|
+
end
|
14
|
+
|
15
|
+
def run
|
16
|
+
return unless staged_files.size > 0
|
17
|
+
|
18
|
+
if detected_bad_code?
|
19
|
+
@error_message = "pre-commit: detected tab before initial space:\n"
|
20
|
+
@error_message += violations
|
21
|
+
|
22
|
+
$stderr.puts @error_message
|
23
|
+
$stderr.puts
|
24
|
+
|
25
|
+
@passed = false
|
26
|
+
else
|
27
|
+
@passed = true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def detected_bad_code?
|
32
|
+
system("grep -PnIH -q '^\t' #{staged_files}")
|
33
|
+
end
|
34
|
+
|
35
|
+
def violations
|
36
|
+
`grep -PnIH '^\t' #{staged_files}`
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Runner
|
2
|
+
|
3
|
+
def run
|
4
|
+
checks_to_run = PreCommit.checks_to_run
|
5
|
+
|
6
|
+
all_passed = checks_to_run.inject(true) do |current_status, check|
|
7
|
+
passed = check.call
|
8
|
+
|
9
|
+
if !passed && check.respond_to?(:error_message)
|
10
|
+
puts check.error_message
|
11
|
+
end
|
12
|
+
|
13
|
+
check && current_status
|
14
|
+
end
|
15
|
+
|
16
|
+
exit(all_passed ? 0 : 1)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/lib/pre-commit/utils.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
class Utils
|
2
2
|
|
3
3
|
def self.staged_files(*dirs)
|
4
|
+
dirs = reject_missing(dirs)
|
5
|
+
|
4
6
|
@staged_files ||= {}
|
5
7
|
@staged_files[dirs.join(' ')] ||= `git diff --cached --name-only --diff-filter=ACM #{dirs.join(' ')} | xargs`.chomp
|
6
8
|
end
|
@@ -9,5 +11,9 @@ class Utils
|
|
9
11
|
@new_files ||= {}
|
10
12
|
@new_files[dirs.join(' ')] ||= `git status --short #{dirs.join(' ')} | grep ^A | xargs`.chomp.split("A ").join(" ")
|
11
13
|
end
|
12
|
-
|
14
|
+
|
15
|
+
def self.reject_missing(dirs)
|
16
|
+
dirs.reject { |dir| !File.exist?(dir) }
|
17
|
+
end
|
18
|
+
|
13
19
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pre-commit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Shajith Chacko, Josh Lubaway
|
@@ -27,8 +28,12 @@ extensions: []
|
|
27
28
|
extra_rdoc_files:
|
28
29
|
- README.md
|
29
30
|
files:
|
31
|
+
- lib/pre-commit/checks/console_log.rb
|
32
|
+
- lib/pre-commit/checks/debugger_check.rb
|
30
33
|
- lib/pre-commit/checks/merge_conflict.rb
|
34
|
+
- lib/pre-commit/checks/tabs.rb
|
31
35
|
- lib/pre-commit/checks.rb
|
36
|
+
- lib/pre-commit/runner.rb
|
32
37
|
- lib/pre-commit/utils.rb
|
33
38
|
- lib/pre-commit.rb
|
34
39
|
- lib/support/all.rb
|
@@ -55,6 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
60
|
requirements:
|
56
61
|
- - ">="
|
57
62
|
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
58
64
|
segments:
|
59
65
|
- 0
|
60
66
|
version: "0"
|
@@ -63,6 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
69
|
requirements:
|
64
70
|
- - ">="
|
65
71
|
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
66
73
|
segments:
|
67
74
|
- 0
|
68
75
|
version: "0"
|