git_hook 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.
@@ -0,0 +1,3 @@
1
+ with_bundler!
2
+
3
+ pre_commit 'GitHook::Hooks::RubySyntaxCheck', require: 'git_hook/hooks/ruby_syntax_check'
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in git-hook.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rspec'
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sho Kusano
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # git-hook
2
+
3
+ git hooks management command line tool.
4
+
5
+ ## Installation
6
+
7
+ in global:
8
+
9
+ $ gem install git-hook
10
+
11
+ in project local:
12
+
13
+ $ echo "gem 'git-hook'" >> Gemfile
14
+
15
+ $ bundle install
16
+
17
+ use
18
+
19
+ $ git hook --version
20
+
21
+ ## Usage
22
+
23
+ write .githooks file:
24
+
25
+ pre_commit 'GitHook::Hooks::RubySyntaxCheck', :require => 'git_hook/hooks/ruby_syntax_check'
26
+
27
+ and run it:
28
+
29
+ $ git hook apply
30
+
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'git_hook'
4
+ require 'git_hook/cli'
5
+
6
+ GitHook.with_pretty_exception {
7
+ GitHook::CLI.start
8
+ }
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/git_hook/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Sho Kusano"]
6
+ gem.email = ["rosylilly@aduca.org"]
7
+ gem.description = %q{git hooks management command line tool}
8
+ gem.summary = %q{git hooks management command line tool}
9
+ gem.homepage = "https://github.com/rosylilly/git_hook"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "git_hook"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = GitHook::VERSION
17
+
18
+ gem.add_dependency "thor", "0.15.4"
19
+ gem.add_dependency "hashr", "0.0.21"
20
+ gem.add_dependency "git", "1.2.5"
21
+ end
@@ -0,0 +1,49 @@
1
+ require 'rbconfig'
2
+ require 'pathname'
3
+ require 'git_hook/version'
4
+ require 'git_hook/io'
5
+
6
+ module GitHook
7
+ WINDOWS = RbConfig::CONFIG["host_os"] =~ %r!(msdos|mswin|djgpp|mingw)!
8
+ NULL = WINDOWS ? 'NUL' : '/dev/null'
9
+
10
+ TIMINGS = [:'applypatch-msg', :'pre-applypatch', :'post-applypatch', :'pre-commit', :'prepare-commit-msg', :'commit-msg', :'post-commit', :'pre-rebase', :'post-checkout', :'post-merge', :'pre-receive', :'update', :'post-receive', :'post-update', :'pre-auto-gc', :'post-rewrite']
11
+
12
+ class << self
13
+ def with_pretty_exception(&block)
14
+ begin
15
+ block.call
16
+ rescue Exception => exception
17
+ GitHook.io.error(exception.class)
18
+ GitHook.io.info(">> #{exception.message}")
19
+ exception.backtrace.each do | backtrace |
20
+ GitHook.io.warn("+ #{backtrace}")
21
+ end
22
+ end
23
+ end
24
+
25
+ def io
26
+ @io ||= GitHook::IO.new
27
+ end
28
+
29
+ def repo_dir
30
+ @repo_dir ||= Pathname.new(Dir.pwd).join(`git rev-parse --show-cdup 2> #{NULL}`.strip).expand_path
31
+ end
32
+
33
+ def git_dir
34
+ @git_dir ||= begin
35
+ dir = `git rev-parse --git-dir 2> #{NULL}`.strip
36
+ raise NotAGitRepository unless $? == 0
37
+ Pathname.new(dir).expand_path
38
+ end
39
+ end
40
+
41
+ def hooks_dir
42
+ git_dir.join('hooks')
43
+ end
44
+ end
45
+ end
46
+
47
+ require 'git_hook/dsl'
48
+ require 'git_hook/hook'
49
+ require 'git_hook/hooks'
@@ -0,0 +1,79 @@
1
+ require 'thor'
2
+ require 'git_hook'
3
+ require 'git_hook/runner'
4
+
5
+ module GitHook
6
+ class CLI < Thor
7
+ include Thor::Actions
8
+
9
+ class_option 'no-color', type: :boolean, desc: 'Disable color output'
10
+ class_option 'verbose', type: :boolean, desc: 'Verbose output'
11
+
12
+ def initialize(*args)
13
+ super(*args)
14
+ GitHook.io.tty = (options['no-color'] ? Thor::Shell::BasicShell.new : shell)
15
+ GitHook.io.verbose!(true) if options['verbose']
16
+ Dir.chdir(GitHook.repo_dir)
17
+ end
18
+
19
+ desc "version", "display git-hook's version"
20
+ def version
21
+ GitHook.io.info("git-hook version #{GitHook::VERSION}")
22
+ end
23
+ map %w(-v --version) => :version
24
+
25
+ def self.source_root
26
+ Pathname.new(File.dirname(__FILE__)).join('template').to_s
27
+ end
28
+
29
+ desc "install", "install git hooks"
30
+ def install
31
+ opts = {
32
+ with_bundler: File.exist?('Gemfile')
33
+ }
34
+ unless File.exist?('.githooks')
35
+ template('githooks.tt', '.githooks', opts)
36
+ end
37
+
38
+ invoke(:apply)
39
+ end
40
+
41
+ desc "apply", "apply git hook execute file in .git/hooks/*"
42
+ def apply
43
+ opts = {
44
+ with_bundler: File.exist?('Gemfile')
45
+ }
46
+ config.hooks.keys.each do | timing |
47
+ opts[:timing] = timing
48
+ template('hook.tt', ".git/hooks/#{timing}", opts)
49
+ chmod(".git/hooks/#{timing}", 0755)
50
+ end
51
+ end
52
+
53
+ desc "list", "display hooks those name starts with STRING"
54
+ def list
55
+ config.hooks.each_pair do | timing, hooks |
56
+ say(timing, :blue)
57
+ hooks.each do | hook |
58
+ say(" #{hook[:class]}", :green, true)
59
+ hook[:options].each_pair do | key, value |
60
+ say(" " * 4 + key.to_s, :yellow, false)
61
+ say(" : #{value}", nil, true)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ desc "test TIMING HOOK", "test HOOK on TIMING"
68
+ def test(timing, hook)
69
+ timing = timing.to_s.to_sym
70
+ result = GitHook::Runner.invoke(timing, hook)
71
+ say_status("success", "#{result}", :yellow)
72
+ end
73
+
74
+ private
75
+ def config
76
+ @config ||= GitHook::DSL.eval('.githooks')
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,57 @@
1
+ require 'git_hook'
2
+
3
+ module GitHook
4
+ class DSL
5
+ def self.eval(hooks_file)
6
+ env = new
7
+ env.load(hooks_file)
8
+ env
9
+ end
10
+
11
+ def initialize
12
+ @hooks = {}
13
+ @with_bundler = false
14
+ end
15
+ attr_reader :hooks, :with_bundler
16
+
17
+ def with_bundler!
18
+ @with_bundler = true
19
+ end
20
+
21
+ # @param [String, Symbol, Class] name
22
+ # @param [Symbol] timing GitHook::Timings
23
+ # @param [Hash] options
24
+ # @option options [String] gem require path
25
+ def hook(name, timing, options = {})
26
+ timing = timing.to_s.to_sym
27
+ options = options || {}
28
+ @hooks[timing] = [] if @hooks[timing].nil?
29
+
30
+ obj = GitHook::Hook.resolve(name)
31
+ obj[:require] = options[:require] if options[:require]
32
+ Kernel.instance_eval do
33
+ begin
34
+ require obj[:require]
35
+ rescue LoadError
36
+ end
37
+ end
38
+ obj[:class] = obj[:class].to_s.split('::').inject(Kernel){|namespace, klass|
39
+ namespace.const_get(klass)
40
+ }
41
+ obj[:options] = options
42
+
43
+ @hooks[timing].push(obj)
44
+ end
45
+
46
+ GitHook::TIMINGS.each do | timing |
47
+ define_method(timing.to_s.gsub('-', '_')) do | name, *options |
48
+ hook(name, timing, options.first)
49
+ end
50
+ end
51
+
52
+ # @param [String] path
53
+ def load(path)
54
+ instance_eval(open(path){|f| f.read })
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,65 @@
1
+ require 'hashr'
2
+ require 'git'
3
+ require 'git_hook'
4
+
5
+ module GitHook
6
+ class Hook
7
+ def self.gem_name(name)
8
+ name.sub(/^[A-Z]/) { | match |
9
+ match.downcase
10
+ }.gsub(/::([A-Z])/) { | match |
11
+ "-#{$1.to_s.downcase}"
12
+ }.gsub(/[A-Z]/) { | match |
13
+ "_#{match.downcase}"
14
+ }
15
+ end
16
+
17
+ def self.class_name(name)
18
+ name.sub(/^[a-z]/) { | match |
19
+ match.upcase
20
+ }.gsub(/\-([a-z])/) { | match |
21
+ "::#{$1.to_s.upcase}"
22
+ }.gsub(/_([a-z])/) { | match |
23
+ $1.to_s.upcase
24
+ }
25
+ end
26
+
27
+ def self.resolve(name)
28
+ name = name.to_s
29
+ if name =~ /^[a-z]/
30
+ { class: class_name(name),
31
+ require: name }
32
+ else
33
+ { class: name,
34
+ require: gem_name(name) }
35
+ end
36
+ end
37
+
38
+ def initialize(repo, timing, options)
39
+ @git = repo
40
+ @timing = timing
41
+ @options = Hashr.new(options).freeze
42
+ end
43
+
44
+ def git
45
+ @git
46
+ end
47
+
48
+ def timing
49
+ @timing
50
+ end
51
+
52
+ def options
53
+ @options
54
+ end
55
+
56
+ def invoke
57
+ error("This Hook is abstruct class. please override your Hook")
58
+ true
59
+ end
60
+
61
+ def info(msg); GitHook.io.info(msg); end
62
+ def warn(msg); GitHook.io.warn(msg); end
63
+ def error(msg); GitHook.io.error(msg); end
64
+ end
65
+ end
@@ -0,0 +1,6 @@
1
+ require 'git_hook/hook'
2
+
3
+ module GitHook
4
+ module Hooks
5
+ end
6
+ end
@@ -0,0 +1,41 @@
1
+ require 'git_hook/hooks'
2
+ require 'ripper'
3
+
4
+ class GitHook::Hooks::RubySyntaxCheck < GitHook::Hook
5
+ def invoke
6
+ result = true
7
+ extensions = options[:extensions] || ['rb']
8
+ git.status.changed.merge(git.status.added).each_pair do | file, obj |
9
+ if file =~ /\.(#{extensions.join('|')})$/
10
+ parser = Parser.new(open(file){|f| f.read}, file)
11
+ parser.parse()
12
+ parser.errors.each do | err |
13
+ error(err)
14
+ end
15
+ parser.warns.each do | warning |
16
+ warn(warning)
17
+ end
18
+ result = result && parser.errors.empty?
19
+ end
20
+ end
21
+
22
+ return result
23
+ end
24
+
25
+ class Parser < Ripper
26
+ def initialize(*args)
27
+ super(*args)
28
+ @errors = []
29
+ @warns = []
30
+ end
31
+ attr_reader :errors, :warns
32
+
33
+ def compile_error(msg)
34
+ @errors.push(msg)
35
+ end
36
+
37
+ def warn(msg, *args)
38
+ @warns.push(msg)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,33 @@
1
+ require 'git_hook'
2
+
3
+ module GitHook
4
+ class IO
5
+ attr_accessor :tty
6
+
7
+ def initialize
8
+ @tty = STDOUT
9
+ @verbose = false
10
+ end
11
+
12
+ def verbose!(bool)
13
+ @verbose = !!bool
14
+ end
15
+
16
+ def info(msg)
17
+ say(msg, nil)
18
+ end
19
+
20
+ def warn(msg)
21
+ @verbose && say(msg, :yellow)
22
+ end
23
+
24
+ def error(msg)
25
+ say(msg, :red)
26
+ end
27
+
28
+ private
29
+ def say(msg, color)
30
+ @tty.respond_to?(:say) ? @tty.say(msg, color) : @tty.puts(msg)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ require 'git'
2
+ require 'thor'
3
+ require 'hashr'
4
+ require 'git_hook'
5
+
6
+ module GitHook
7
+ class Runner
8
+ def self.run(timing)
9
+ ctx = self.context
10
+ result = ctx.config.hooks[timing.to_sym].inject(true) do | result, hook |
11
+ result && hook[:class].new(ctx.repository, timing.to_sym, hook[:options] || {}).invoke
12
+ end
13
+
14
+ unless result
15
+ exit(1)
16
+ end
17
+ end
18
+
19
+ def self.invoke(timing, hook_name)
20
+ ctx = self.context
21
+ ctx.config.hooks[timing.to_sym].each do | hook |
22
+ if hook[:class].name == hook_name
23
+ return hook[:class].new(ctx.repository, timing.to_sym, hook[:options] || {}).invoke
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.context
29
+ dsl = GitHook::DSL.eval('.githooks')
30
+ repository = Git.open('.')
31
+ GitHook.io.tty = `git config --get color.ui 2> #{GitHook::NULL}`.strip == 'true' ? Thor::Shell::Color.new : Thor::Shell::Basic.new
32
+ return Hashr.new({
33
+ config: dsl,
34
+ repository: repository,
35
+ io: GitHook.io
36
+ })
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ <%- if config[:with_bundler] -%>
2
+ with_bundler!
3
+ <%- end -%>
4
+
5
+ # example
6
+ # pre_commit 'GitHook::Hook'
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ dir = File.dirname(__FILE__)
4
+ if dir =~ /\.git\/hooks$/
5
+ dir = File.expand_path(File.join(dir, '..', '..'))
6
+ else
7
+ dir = File.expand_path(File.join(dir, '..'))
8
+ end
9
+
10
+ Dir.chdir(dir) do | dir |
11
+ <%- if config[:with_bundler] -%>
12
+ require 'bundler/setup'
13
+ <%- end -%>
14
+ require 'git_hook/runner'
15
+ GitHook::Runner.run("<%= config[:timing] %>")
16
+ end
@@ -0,0 +1,3 @@
1
+ module GitHook
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'git_hook/hook'
3
+
4
+ describe GitHook::Hook do
5
+ it ".gem_name" do
6
+ GitHook::Hook.gem_name('Bundler::SetUp').should == 'bundler-set_up'
7
+ end
8
+
9
+ it ".class_name" do
10
+ GitHook::Hook.class_name('bundler-set_up').should == 'Bundler::SetUp'
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require 'git_hook'
3
+
4
+ describe GitHook do
5
+ its(:repo_dir) {
6
+ subject.to_s.should == Pathname.new(Dir.pwd).expand_path.to_s
7
+ }
8
+
9
+ its(:git_dir) {
10
+ subject.to_s.should == Pathname.new('.git').expand_path.to_s
11
+ }
12
+
13
+ its(:hooks_dir) {
14
+ subject.to_s.should == Pathname.new('.git/hooks').expand_path.to_s
15
+ }
16
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sho Kusano
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.15.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.15.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: hashr
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.21
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.21
46
+ - !ruby/object:Gem::Dependency
47
+ name: git
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.5
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.5
62
+ description: git hooks management command line tool
63
+ email:
64
+ - rosylilly@aduca.org
65
+ executables:
66
+ - git-hook
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .githooks
71
+ - .gitignore
72
+ - .rspec
73
+ - Gemfile
74
+ - LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - bin/git-hook
78
+ - git_hook.gemspec
79
+ - lib/git_hook.rb
80
+ - lib/git_hook/cli.rb
81
+ - lib/git_hook/dsl.rb
82
+ - lib/git_hook/hook.rb
83
+ - lib/git_hook/hooks.rb
84
+ - lib/git_hook/hooks/ruby_syntax_check.rb
85
+ - lib/git_hook/io.rb
86
+ - lib/git_hook/runner.rb
87
+ - lib/git_hook/template/githooks.tt
88
+ - lib/git_hook/template/hook.tt
89
+ - lib/git_hook/version.rb
90
+ - spec/lib/git_hook/hook_spec.rb
91
+ - spec/lib/git_hook_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/rosylilly/git_hook
94
+ licenses: []
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.23
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: git hooks management command line tool
117
+ test_files:
118
+ - spec/lib/git_hook/hook_spec.rb
119
+ - spec/lib/git_hook_spec.rb
120
+ - spec/spec_helper.rb