git-hooks-helper 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ gitt-hooks-helper-*.gem
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+ gemspec
3
+ gem 'rspec'
4
+ gem 'pry'
5
+ gem 'rails_best_practices'
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ git-hooks-helper (0.0.1)
5
+ rails-erb-check
6
+ rails_best_practices
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ abstract (1.0.0)
12
+ actionpack (3.0.11)
13
+ activemodel (= 3.0.11)
14
+ activesupport (= 3.0.11)
15
+ builder (~> 2.1.2)
16
+ erubis (~> 2.6.6)
17
+ i18n (~> 0.5.0)
18
+ rack (~> 1.2.1)
19
+ rack-mount (~> 0.6.14)
20
+ rack-test (~> 0.5.7)
21
+ tzinfo (~> 0.3.23)
22
+ activemodel (3.0.11)
23
+ activesupport (= 3.0.11)
24
+ builder (~> 2.1.2)
25
+ i18n (~> 0.5.0)
26
+ activesupport (3.0.11)
27
+ builder (2.1.2)
28
+ coderay (0.9.8)
29
+ colored (1.2)
30
+ diff-lcs (1.1.3)
31
+ erubis (2.6.6)
32
+ abstract (>= 1.0.0)
33
+ i18n (0.5.0)
34
+ method_source (0.6.7)
35
+ ruby_parser (>= 2.3.1)
36
+ progressbar (0.9.1)
37
+ pry (0.9.7.4)
38
+ coderay (~> 0.9.8)
39
+ method_source (~> 0.6.7)
40
+ ruby_parser (>= 2.3.1)
41
+ slop (~> 2.1.0)
42
+ rack (1.2.4)
43
+ rack-mount (0.6.14)
44
+ rack (>= 1.0.0)
45
+ rack-test (0.5.7)
46
+ rack (>= 1.0)
47
+ rails-erb-check (0.1.0)
48
+ actionpack (~> 3.0)
49
+ rails_best_practices (1.3.0)
50
+ activesupport
51
+ colored
52
+ erubis
53
+ i18n
54
+ progressbar
55
+ sexp_processor
56
+ rspec (2.8.0)
57
+ rspec-core (~> 2.8.0)
58
+ rspec-expectations (~> 2.8.0)
59
+ rspec-mocks (~> 2.8.0)
60
+ rspec-core (2.8.0)
61
+ rspec-expectations (2.8.0)
62
+ diff-lcs (~> 1.1.2)
63
+ rspec-mocks (2.8.0)
64
+ ruby_parser (2.3.1)
65
+ sexp_processor (~> 3.0)
66
+ sexp_processor (3.0.10)
67
+ slop (2.1.0)
68
+ tzinfo (0.3.31)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ git-hooks-helper!
75
+ pry
76
+ rails_best_practices
77
+ rspec
data/HISTORY ADDED
@@ -0,0 +1 @@
1
+ 2012-01-20 Initial release 0.0.1
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 Martynas Margis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README ADDED
@@ -0,0 +1,50 @@
1
+ Instalation
2
+ -----------
3
+ gem install git-hooks-helper
4
+
5
+ File clasification
6
+ ------------------
7
+
8
+ Most commands use file classes internally or accept type class as a param.
9
+ File type classes and associated file extensions:
10
+ 1. :rb - .rb .rake .task .prawn
11
+ 2. :js - .js
12
+ 3. :erb - .erb
13
+
14
+ Usage
15
+ -----
16
+ Create your hit hook in .git/hooks directory and make it executable.
17
+
18
+ My example pre-commit hook that i use myself.
19
+ To create such a hook type in console:
20
+
21
+ cd your/project/
22
+ touch .git/hooks/pre-commit
23
+ chmod +x .git/hooks/pre-commit
24
+ vim .git/hooks/pre-commit
25
+
26
+ and paste following code:
27
+
28
+
29
+ ```ruby
30
+ #!/usr/bin/env ruby
31
+
32
+ require "git-hooks-helper"
33
+
34
+ GitHooksHelper.results do
35
+ # fail/pass options
36
+ # stop_on_warnings # makes git commit fail if any warnings are found
37
+ # never_stop # hooks never fails commits
38
+ # list_files # shows list of all changed files
39
+
40
+ # schecks
41
+ check_ruby_syntax # errors when ruby syntax has errors
42
+ check_erb # errors when ERB syntax has errors
43
+ check_best_practices # warnings when ruby best practices are violated
44
+ warning_on "WTF?", "binding.pry", "<<<<<<<" # warnings when any of these texts are present in any commited files
45
+ warning_on "console.log", "debugger", {:in => [:js]} # warning when any of these texts are present in JS files
46
+
47
+ # messages
48
+ info "Run rspec tests and have a nice day."
49
+ end
50
+ ```
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "git-hooks-helper"
4
+
5
+ GitHooksHelper.results do
6
+ do_not_stop_on_warnings
7
+
8
+ check_ruby_syntax
9
+ check_erb
10
+ check_best_practices
11
+ warning_on "WTF?", "binding.pry", "<<<<<<<"
12
+ warning_on "console.log", "debugger", {:in => [:js]}
13
+
14
+ info "Run rspec tests and have a nice day."
15
+ end
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "git-hooks-helper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "git-hooks-helper"
7
+ s.version = GitHooksHelper::VERSION
8
+ s.authors = ["Martynas Margis"]
9
+ s.email = []
10
+ s.homepage = ""
11
+ s.summary = %q{Small gem to help write simple git hooks}
12
+ s.description = %q{Gem for creating simple git hooks in ruby.}
13
+
14
+ s.rubyforge_project = "git-hooks-helper"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ # s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "rails-erb-check"
22
+ s.add_runtime_dependency "rails_best_practices"
23
+ end
@@ -0,0 +1,7 @@
1
+ require "git-hooks-helper/hook"
2
+
3
+ module GitHooksHelper
4
+ def self.results(&block)
5
+ GitHooksHelper::Hook.new(&block)
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def red; "\e[1m\e[31m#{self}\e[0m"; end
3
+ def green; "\e[1m\e[32m#{self}\e[0m"; end
4
+ def yellow; "\e[1m\e[33m#{self}\e[0m"; end
5
+ end
@@ -0,0 +1,11 @@
1
+ module GitHooksHelper
2
+ class Git
3
+ class << self
4
+ FILES_TO_WATCH = /(.+\.(e?rb|task|rake|thor|prawn)|[Rr]akefile|[Tt]horfile)/
5
+
6
+ def in_index
7
+ `git diff-index --name-only --cached HEAD`.split("\n").select{ |file| file =~ FILES_TO_WATCH }.map(&:chomp)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,145 @@
1
+ require "git-hooks-helper/result"
2
+ require "git-hooks-helper/git"
3
+ require "git-hooks-helper/extensions/string"
4
+ require "open3"
5
+
6
+ module GitHooksHelper
7
+ class Hook
8
+
9
+ RB_REGEXP = /\.(rb|rake|task|prawn)\z/
10
+ ERB_REGEXP = /\.erb\z/
11
+ JS_REGEXP = /\.js\z/
12
+
13
+ RB_WARNING_REGEXP = /[0-9]+:\s+warning:/
14
+ ERB_INVALID_REGEXP = /invalid\z/
15
+ COLOR_REGEXP = /\e\[(\d+)m/
16
+
17
+ # Set this to true if you want warnings to stop your commit
18
+ def initialize(&block)
19
+ @compiler_ruby = `which ruby`.strip
20
+
21
+ @result = GitHooksHelper::Result.new(false)
22
+ @changed_ruby_files = GitHooksHelper::Git.in_index
23
+
24
+ instance_eval(&block) if block
25
+
26
+ if @result.errors?
27
+ status = 1
28
+ puts "ERRORS:".red
29
+ puts @result.errors.join("\n")
30
+ puts "--------\n".red
31
+ end
32
+
33
+ if @result.warnings?
34
+ if @result.stop_on_warnings
35
+ puts "WARNINGS:".yellow
36
+ else
37
+ puts "Warnings:".yellow
38
+ end
39
+ puts @result.warnings.join("\n")
40
+ puts "--------\n".yellow
41
+ end
42
+
43
+ if @result.perfect_commit?
44
+ puts "Perfect commit!".green
45
+ end
46
+
47
+ if @result.continue?
48
+ # all good
49
+ puts("COMMIT OK:".green)
50
+ exit 0
51
+ else
52
+ puts("COMMIT FAILED".red)
53
+ exit 1
54
+ end
55
+ end
56
+
57
+ def stop_on_warnings
58
+ @result.stop_on_warnings = true
59
+ end
60
+
61
+ def do_not_stop_on_warnings
62
+ @result.stop_on_warnings = false
63
+ end
64
+
65
+ def never_stop
66
+ @result.never_stop = true
67
+ end
68
+
69
+ def list_files(filetypes = [:all])
70
+ each_changed_file(filetypes) do |file|
71
+ puts file
72
+ end
73
+ end
74
+
75
+ def each_changed_file(filetypes = [:all])
76
+ if @result.continue?
77
+ @changed_ruby_files.each do |file|
78
+ unless filetypes.include?(:all)
79
+ next unless (filetypes.include?(:rb) and file =~ RB_REGEXP) or (filetypes.include?(:erb) and file =~ ERB_REGEXP) or (filetypes.include?(:js) and file =~ JS_REGEXP)
80
+ end
81
+ yield file if File.readable?(file)
82
+ end
83
+ end
84
+ end
85
+
86
+ def check_ruby_syntax
87
+ each_changed_file([:rb]) do |file|
88
+ Open3.popen3("#{@compiler_ruby} -wc #{file}") do |stdin, stdout, stderr|
89
+ stderr.read.split("\n").each do |line|
90
+ line =~ RB_WARNING_REGEXP ? @result.warnings << line : @result.errors << line
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ def check_erb
97
+ each_changed_file([:erb]) do |file|
98
+ Open3.popen3("rails-erb-check #{file}") do |stdin, stdout, stderr|
99
+ @result.errors.concat stdout.read.split("\n").map{|line| "#{file} => invalid ERB syntax" if line.gsub(COLOR_REGEXP, '') =~ ERB_INVALID_REGEXP}.compact
100
+ end
101
+ end
102
+ end
103
+
104
+ def check_best_practices
105
+ each_changed_file([:rb, :erb]) do |file|
106
+ puts "bundle exec rails_best_practices --spec --test #{file}"
107
+ Open3.popen3("bundle exec rails_best_practices --spec --test #{file}") do |stdin, stdout, stderr|
108
+ @result.warn(stdout.read.split("\n").map do |line|
109
+ if line =~ /#{file}/
110
+ line.gsub(COLOR_REGEXP, '').strip
111
+ end
112
+ end.compact)
113
+ end
114
+ end
115
+ end
116
+
117
+ # Maybe need options for different file types :rb :erb :js
118
+ def warning_on(*args)
119
+ options = (args[-1].kind_of?(Hash) ? args.pop : {})
120
+ each_changed_file(options[:in] || [:all]) do |file|
121
+ Open3.popen3("fgrep -nH \"#{args.join("\n")}\" #{file}") do |stdin, stdout, stderr|
122
+ err = stdout.read
123
+ err.split("\n").each do |msg|
124
+ args.each do |string|
125
+ @result.warn("#{msg.split(" ").first} contains #{string}") if msg =~ /#{string}/
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ def info(text)
133
+ puts(text.green)
134
+ end
135
+
136
+ def notice(text)
137
+ puts(text.yellow)
138
+ end
139
+
140
+ def warn(text)
141
+ puts(text.red)
142
+ end
143
+
144
+ end
145
+ end
@@ -0,0 +1,42 @@
1
+ module GitHooksHelper
2
+ class Result
3
+
4
+ attr_accessor :errors, :warnings, :stop_on_warnings, :never_stop
5
+
6
+ def initialize(stop_on_warnings)
7
+ @errors = []
8
+ @warnings = []
9
+ @stop_on_warnings = stop_on_warnings
10
+ @never_stop = false
11
+ end
12
+
13
+ def warn(msg)
14
+ if msg.class == Array
15
+ @warnings.concat msg
16
+ else
17
+ @warnings << msg
18
+ end
19
+ end
20
+
21
+ def continue?
22
+ @never_stop || (!(errors? || (warnings? && @stop_on_warnings)))
23
+ end
24
+
25
+ def warnings?
26
+ @warnings.size > 0
27
+ end
28
+
29
+ def errors?
30
+ @errors.size > 0
31
+ end
32
+
33
+ def pass?
34
+ @never_stop || errors?
35
+ end
36
+
37
+ def perfect_commit?
38
+ !(errors? || warnings?)
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module GitHooksHelper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ class Test
2
+ def
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ class Test
2
+ def test
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ class Test
2
+ # extra tab and whitespace in the end
3
+ def test
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ <html>
2
+ <% form do |f| %>
3
+ <% end %>
4
+ </html>
@@ -0,0 +1,5 @@
1
+ class Test
2
+ # extra tab and whitespace in the end
3
+ def test
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ <html>
2
+ <% form do |f| %>
3
+ <% end >
4
+ </html>
@@ -0,0 +1,127 @@
1
+ require 'spec_helper'
2
+
3
+ describe GitHooksHelper do
4
+
5
+ it "should run empty hook" do
6
+ GitHooksHelper::Git.should_receive(:in_index).and_return([])
7
+
8
+ begin
9
+ GitHooksHelper.results do
10
+ end
11
+ rescue SystemExit
12
+ $!.status.should eql 0
13
+ end
14
+ end
15
+
16
+ describe "check_ruby_syntax" do
17
+ before(:each) do
18
+ @files = [
19
+ "spec/git_files/bad_syntax.rb",
20
+ "spec/git_files/good_syntax.rb"
21
+ ]
22
+ end
23
+
24
+ it "should fail on bad rb syntax" do
25
+ GitHooksHelper::Git.should_receive(:in_index).and_return(@files)
26
+ begin
27
+ GitHooksHelper.results do
28
+ check_ruby_syntax
29
+ end
30
+ rescue SystemExit => e
31
+ $!.status.should eql 1
32
+ end
33
+ end
34
+
35
+ it "should pass good rb syntax" do
36
+ GitHooksHelper::Git.should_receive(:in_index).and_return(["spec/git_files/good_syntax.rb"])
37
+ begin
38
+ GitHooksHelper.results do
39
+ check_ruby_syntax
40
+ end
41
+ rescue SystemExit => e
42
+ $!.status.should eql 0
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "never_stop" do
48
+ it "should pass even on errors and warnings" do
49
+ GitHooksHelper::Git.should_receive(:in_index).and_return(["spec/git_files/bad_syntax.rb"])
50
+ begin
51
+ GitHooksHelper.results do
52
+ never_stop
53
+ check_ruby_syntax
54
+ end
55
+ rescue SystemExit => e
56
+ $!.status.should eql 0
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "stop_on_warnings" do # also sort of checks best practices
62
+ it "should stop even on warnings" do
63
+ GitHooksHelper::Git.should_receive(:in_index).and_return(["spec/git_files/no_best_practices.rb"])
64
+ begin
65
+ GitHooksHelper.results do
66
+ stop_on_warnings
67
+ check_best_practices
68
+ end
69
+ rescue SystemExit => e
70
+ $!.status.should eql 1
71
+ end
72
+ end
73
+
74
+ it "should run OK when no errors" do
75
+ GitHooksHelper::Git.should_receive(:in_index).and_return(["spec/git_files/ok_best_practices.rb"])
76
+ begin
77
+ GitHooksHelper.results do
78
+ stop_on_warnings
79
+ check_best_practices
80
+ end
81
+ rescue SystemExit => e
82
+ $!.status.should eql 0
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "check_erb" do
88
+ it "should stop even on warnings on syntax error in ERB" do
89
+ GitHooksHelper::Git.should_receive(:in_index).and_return(["spec/git_files/syntax_error.html.erb"])
90
+ begin
91
+ GitHooksHelper.results do
92
+ stop_on_warnings
93
+ check_erb
94
+ end
95
+ rescue SystemExit => e
96
+ $!.status.should eql 1
97
+ end
98
+ end
99
+
100
+ it "should show no error on correct ERB" do
101
+ GitHooksHelper::Git.should_receive(:in_index).and_return(["spec/git_files/ok.html.erb"])
102
+ begin
103
+ GitHooksHelper.results do
104
+ stop_on_warnings
105
+ check_erb
106
+ end
107
+ rescue SystemExit => e
108
+ $!.status.should eql 0
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "warning_on" do
114
+ it "should show error when file contains specific text" do
115
+ GitHooksHelper::Git.should_receive(:in_index).and_return(["spec/git_files/ok.html.erb"])
116
+ begin
117
+ GitHooksHelper.results do
118
+ stop_on_warnings
119
+ check_erb
120
+ end
121
+ rescue SystemExit => e
122
+ $!.status.should eql 0
123
+ end
124
+ end
125
+ end
126
+
127
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'git-hooks-helper'
5
+
6
+ RSpec.configure do |config|
7
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-hooks-helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martynas Margis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails-erb-check
16
+ requirement: &19072720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *19072720
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails_best_practices
27
+ requirement: &19071260 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *19071260
36
+ description: Gem for creating simple git hooks in ruby.
37
+ email: []
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - .gitignore
43
+ - Gemfile
44
+ - Gemfile.lock
45
+ - HISTORY
46
+ - LICENSE
47
+ - README
48
+ - README.md
49
+ - Rakefile
50
+ - git-hooks-helper.gemspec
51
+ - lib/git-hooks-helper.rb
52
+ - lib/git-hooks-helper/extensions/string.rb
53
+ - lib/git-hooks-helper/git.rb
54
+ - lib/git-hooks-helper/hook.rb
55
+ - lib/git-hooks-helper/result.rb
56
+ - lib/git-hooks-helper/version.rb
57
+ - spec/git_files/bad_syntax.rb
58
+ - spec/git_files/good_syntax.rb
59
+ - spec/git_files/no_best_practices.rb
60
+ - spec/git_files/ok.html.erb
61
+ - spec/git_files/ok_best_practices.rb
62
+ - spec/git_files/syntax_error.html.erb
63
+ - spec/git_hooks_helper_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project: git-hooks-helper
85
+ rubygems_version: 1.8.10
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Small gem to help write simple git hooks
89
+ test_files: []