git-hook 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/bin/githook +4 -4
- data/lib/githook/config.rb +10 -2
- data/lib/githook/tasks.rb +19 -7
- data/lib/githook/tasks/commit-msg.rake +9 -4
- data/lib/githook/tasks/install.rake +2 -1
- data/lib/githook/tasks/prepare-commit-msg.rake +9 -5
- data/lib/githook/tasks/setup.rake +116 -22
- data/lib/githook/util.rb +5 -1
- data/lib/githook/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83556fdb46bba0169e6b6b49af7bb3db20149749
|
4
|
+
data.tar.gz: 444a35267d74f379355d450d378e968874250a89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f86dbbdd1c145db185b07dcfa4e6a9af0bb63a2350c32e7f910551c02edd85a813542c453ae1c1deb0f63c6947fd190a0588a63d589f0ff0c806c24616eac9f7
|
7
|
+
data.tar.gz: b4726565873251918b6d45ba8140279aa70dd7c9e272247d0c6e9ddb140c7c7399c95b944aca5e2af94c609d4fe9683ae270810742270d92f0111cf068abfea1
|
data/README.md
CHANGED
@@ -2,6 +2,20 @@
|
|
2
2
|
|
3
3
|
A ruby gem that help to setup git hooks easily, base on Rake, inspired from Capistrano.
|
4
4
|
|
5
|
+
## TODO
|
6
|
+
|
7
|
+
- [x] install task
|
8
|
+
- [x] setup hooks task
|
9
|
+
- [x] clear hooks task
|
10
|
+
- [x] disable/enable hooks task
|
11
|
+
- [x] list hooks task
|
12
|
+
- [x] version/help task
|
13
|
+
- [x] pre-commit hook tasks
|
14
|
+
- [x] prepare-commit-msg hook tasks
|
15
|
+
- [x] commit-msg hook tasks
|
16
|
+
- [x] implement as a gem
|
17
|
+
- [ ] more document
|
18
|
+
|
5
19
|
## Installation
|
6
20
|
|
7
21
|
Add this line to your application's Gemfile:
|
@@ -20,6 +34,7 @@ Or install it yourself as:
|
|
20
34
|
|
21
35
|
## Usage
|
22
36
|
|
37
|
+
$ githook help
|
23
38
|
$ githook install
|
24
39
|
$ githook setup
|
25
40
|
|
data/bin/githook
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
require "githook"
|
4
4
|
|
5
|
-
task_name = ARGV[0]
|
6
|
-
|
5
|
+
task_name = ARGV[0] || 'help'
|
6
|
+
if Rake::Task.task_defined?(task_name)
|
7
7
|
Rake::Task[task_name].invoke
|
8
|
-
|
9
|
-
puts "Error: #{
|
8
|
+
else
|
9
|
+
puts "Error: #{task_name} task doesn't exist"
|
10
10
|
end
|
data/lib/githook/config.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# we must be very careful to load outside ruby code
|
2
|
+
# because they are out of our control
|
3
|
+
# only load outside "*.rake" when there are ".git" and ".githook" folder, and target task isn't "install"
|
4
|
+
if Dir.exist?('.git') && Dir.exist?('.githook') && ARGV[0] != "install"
|
5
|
+
begin
|
6
|
+
load ".githook/config.rb"
|
7
|
+
rescue => e
|
8
|
+
puts "Error: #{e.message} in .githook/config.rb"
|
9
|
+
exit 1
|
10
|
+
end
|
3
11
|
end
|
data/lib/githook/tasks.rb
CHANGED
@@ -1,11 +1,23 @@
|
|
1
|
+
# https://stackoverflow.com/questions/8781263/access-rake-task-description-from-within-task
|
2
|
+
# enable get task description
|
3
|
+
# you aslo can see it in Rake source code: lib/rake/application.rb#select_tasks_to_show
|
4
|
+
Rake::TaskManager.record_task_metadata = true
|
5
|
+
|
1
6
|
# load rake files from lib
|
2
|
-
rake_files_pattern = File.dirname(__FILE__) + "/tasks
|
3
|
-
# => gems/git-hook-0.1.1/lib/githook/tasks
|
7
|
+
rake_files_pattern = File.dirname(__FILE__) + "/tasks/*.rake"
|
8
|
+
# => gems/git-hook-0.1.1/lib/githook/tasks/*.rake
|
4
9
|
Dir.glob(rake_files_pattern).each { |r| load r }
|
5
10
|
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
if ARGV[0] != "install"
|
10
|
-
Dir.glob(".githook/tasks/**/*.rake").each
|
11
|
+
# we must be very careful to load outside ruby code
|
12
|
+
# because they are out of our control
|
13
|
+
# only load outside "*.rake" when there are ".git" and ".githook" folder, and target task isn't "install"
|
14
|
+
if Dir.exist?('.git') && Dir.exist?('.githook') && ARGV[0] != "install"
|
15
|
+
Dir.glob(".githook/tasks/**/*.rake").each do |rake|
|
16
|
+
begin
|
17
|
+
load rake
|
18
|
+
rescue => e
|
19
|
+
puts "Error: #{e.message} in #{rake}"
|
20
|
+
exit 1
|
21
|
+
end
|
22
|
+
end
|
11
23
|
end
|
@@ -6,11 +6,16 @@ namespace :commit_msg do
|
|
6
6
|
commit_msg_file = '.git/COMMIT_EDITMSG'
|
7
7
|
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
8
8
|
puts "commit-msg: #{commit_msg}"
|
9
|
-
|
9
|
+
# can't use return in block
|
10
|
+
# can't "exit 0" in advance, else will abort later tasks
|
11
|
+
# but we can "exit 1" in advance
|
12
|
+
# exit 0 if Githook::Util.expected_msg_format?(commit_msg)
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
+
unless Githook::Util.expected_msg_format?(commit_msg)
|
15
|
+
puts "ERROR! commit failed, commit msg doesn't match the required format"
|
16
|
+
puts "expected msg format: FEAUTER|BUG|MISC|REFACTOR #issue_num - Content"
|
17
|
+
exit 1
|
18
|
+
end
|
14
19
|
end
|
15
20
|
end
|
16
21
|
|
@@ -5,12 +5,16 @@ namespace :prepare_commit_msg do
|
|
5
5
|
|
6
6
|
commit_msg_file = '.git/COMMIT_EDITMSG'
|
7
7
|
# can't use return in block
|
8
|
-
exit 0
|
8
|
+
# can't "exit 0" in advance, else will abort later tasks
|
9
|
+
# but we can "exit 1" in advance
|
10
|
+
# exit 0 unless Githook::Util.commit_msg_empty?(commit_msg_file)
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
if Githook::Util.commit_msg_empty?(commit_msg_file)
|
13
|
+
branch_name = `git symbolic-ref --short HEAD`
|
14
|
+
pre_msg = Githook::Util.gen_pre_msg(branch_name)
|
15
|
+
puts "pre-msg: #{pre_msg}"
|
16
|
+
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
17
|
+
end
|
14
18
|
end
|
15
19
|
end
|
16
20
|
|
@@ -1,31 +1,28 @@
|
|
1
|
-
desc '
|
2
|
-
task :setup do
|
3
|
-
|
4
|
-
|
5
|
-
# setup 1, check whether has '.git-hooks/hooks' and '.git' folder
|
1
|
+
desc 'Setup hooks'
|
2
|
+
task :setup do
|
3
|
+
# setup 1, check whether has '.githook/hooks' and '.git' folder
|
6
4
|
hooks_path = '.githook/hooks'
|
7
5
|
unless Dir.exists?(hooks_path)
|
8
|
-
puts "There isn't .
|
6
|
+
puts "There isn't a .githook/hooks folder."
|
9
7
|
exit 1
|
10
8
|
end
|
11
9
|
|
12
10
|
git_path = '.git'
|
13
11
|
unless Dir.exists?(git_path)
|
14
|
-
puts "There isn't .git folder."
|
12
|
+
puts "There isn't a .git folder."
|
15
13
|
exit 1
|
16
14
|
end
|
17
15
|
|
18
16
|
# setup 2, backup hooks
|
19
|
-
|
17
|
+
puts "Backup old hooks:"
|
18
|
+
Rake::Task[:backup].invoke
|
20
19
|
|
21
20
|
# setup 3, copy hooks to .git/hooks
|
22
21
|
FileUtils.cp_r(hooks_path, git_path)
|
23
22
|
end
|
24
23
|
|
25
|
-
desc '
|
26
|
-
task :
|
27
|
-
Githook::Util.log(t.name)
|
28
|
-
|
24
|
+
desc 'Backup old hooks in .git/hooks'
|
25
|
+
task :backup do
|
29
26
|
has_backup = false
|
30
27
|
Dir.glob('.git/hooks/*').each do |path|
|
31
28
|
file_name = path.split('/').last
|
@@ -40,19 +37,116 @@ task :backup_hooks do |t|
|
|
40
37
|
puts "you can run 'rake clear_backup' to delete these backup" if has_backup
|
41
38
|
end
|
42
39
|
|
43
|
-
desc '
|
44
|
-
task :clear_backup do
|
45
|
-
Githook::Util.log(t.name)
|
46
|
-
|
40
|
+
desc 'Clear backup hooks in .git/hooks'
|
41
|
+
task :clear_backup do
|
47
42
|
backup = Dir.glob('.git/hooks/*.bak')
|
48
43
|
Githook::Util.interactive_delete_files(backup, 'backup hooks')
|
49
44
|
end
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
# later I think we don't need to clear hooks, use disable/enable replace them
|
47
|
+
# desc 'clear all hooks (include backup) in .git/hooks'
|
48
|
+
# task :clear => :clear_backup do |t|
|
49
|
+
# Githook::Util.log(t.name)
|
50
|
+
|
51
|
+
# hooks = Dir.glob('.git/hooks/*')
|
52
|
+
# .reject { |path| path.split('/').last.include?('.') }
|
53
|
+
# Githook::Util.interactive_delete_files(hooks, 'hooks')
|
54
|
+
# end
|
55
|
+
|
56
|
+
ALL_HOOKS = %w(
|
57
|
+
applypatch_msg
|
58
|
+
pre_applypatch
|
59
|
+
post_applypatch
|
60
|
+
pre_commit
|
61
|
+
prepare_commit_msg
|
62
|
+
commit_msg
|
63
|
+
post_commit
|
64
|
+
pre_rebase
|
65
|
+
post_checkout
|
66
|
+
post_merge
|
67
|
+
pre_receive
|
68
|
+
post_receive
|
69
|
+
update
|
70
|
+
post_update
|
71
|
+
pre_auto_gc
|
72
|
+
post_rewrite
|
73
|
+
)
|
54
74
|
|
55
|
-
|
56
|
-
|
57
|
-
|
75
|
+
desc 'Disable hooks: HOOKS=pre_commit,commit_msg githook disable'
|
76
|
+
task :disable do
|
77
|
+
target_hooks = (ENV['HOOKS'] || '').split(',') || ALL_HOOKS
|
78
|
+
target_hooks.each do |hook|
|
79
|
+
hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
|
80
|
+
if File.file?(hook_path)
|
81
|
+
disable_path = hook_path + '.disable'
|
82
|
+
FileUtils.mv(hook_path, disable_path)
|
83
|
+
puts "Disable #{hook} hook."
|
84
|
+
else
|
85
|
+
puts "#{hook} hook doesn't exist, skip."
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
desc 'Enable hooks: HOOKS=pre_commit,commit_msg githook enable'
|
91
|
+
task :enable do
|
92
|
+
target_hooks = (ENV['HOOKS'] || '').split(',') || ALL_HOOKS
|
93
|
+
target_hooks.each do |hook|
|
94
|
+
hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
|
95
|
+
disable_path = hook_path + '.disable'
|
96
|
+
if File.file?(hook_path)
|
97
|
+
puts "#{hook} hook is arleady enabled, skip."
|
98
|
+
elsif File.file?(disable_path)
|
99
|
+
FileUtils.mv(disable_path, hook_path)
|
100
|
+
puts "Enable #{hook} hook."
|
101
|
+
else
|
102
|
+
puts "#{hook} hook doesn't exist, skip."
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
desc 'List all hooks'
|
108
|
+
task :list do
|
109
|
+
enabled_hooks = []
|
110
|
+
disabled_hooks = []
|
111
|
+
ALL_HOOKS.each do |hook|
|
112
|
+
hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
|
113
|
+
disable_path = hook_path + '.disable'
|
114
|
+
if File.file?(hook_path)
|
115
|
+
enabled_hooks << hook
|
116
|
+
elsif File.file?(disable_path)
|
117
|
+
disabled_hooks << hook
|
118
|
+
end
|
119
|
+
end
|
120
|
+
puts "Enabled hooks:"
|
121
|
+
enabled_hooks.each { |h| puts " * #{h}" }
|
122
|
+
puts "Disabled hooks:"
|
123
|
+
disabled_hooks.each { |h| puts " * #{h}" }
|
124
|
+
end
|
125
|
+
|
126
|
+
desc 'Version'
|
127
|
+
task :version do
|
128
|
+
puts Githook::VERSION
|
129
|
+
end
|
130
|
+
|
131
|
+
TASKS_NAME = %w(
|
132
|
+
install
|
133
|
+
setup
|
134
|
+
backup
|
135
|
+
clear_backup
|
136
|
+
disable
|
137
|
+
enable
|
138
|
+
list
|
139
|
+
version
|
140
|
+
help
|
141
|
+
)
|
142
|
+
desc 'Help'
|
143
|
+
task :help do
|
144
|
+
puts
|
145
|
+
puts "Usage: githook task_name"
|
146
|
+
puts
|
147
|
+
puts "task_name:"
|
148
|
+
TASKS_NAME.each do |task_name|
|
149
|
+
task = Rake::Task[task_name]
|
150
|
+
puts " #{task_name.ljust(13)} -- #{task.comment}"
|
151
|
+
end
|
58
152
|
end
|
data/lib/githook/util.rb
CHANGED
@@ -7,7 +7,11 @@ module Githook
|
|
7
7
|
def self.run_tasks(hook_stage)
|
8
8
|
tasks = fetch(hook_stage, [])
|
9
9
|
tasks.each do |task|
|
10
|
-
Rake::Task
|
10
|
+
if Rake::Task.task_defined?(task)
|
11
|
+
Rake::Task[task].invoke
|
12
|
+
else
|
13
|
+
puts "#{task} task doesn't exist."
|
14
|
+
end
|
11
15
|
end
|
12
16
|
end
|
13
17
|
|
data/lib/githook/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: git-hook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- baurine
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|