git-hook 0.1.8 → 0.1.9
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 +31 -2
- data/Rakefile +3 -3
- data/bin/githook +2 -2
- data/githook.gemspec +13 -13
- data/lib/githook.rb +6 -6
- data/lib/githook/config.rb +3 -3
- data/lib/githook/context.rb +2 -2
- data/lib/githook/tasks.rb +4 -4
- data/lib/githook/tasks/1-pre-commit.rake +6 -6
- data/lib/githook/tasks/2-prepare-commit-msg.rake +8 -8
- data/lib/githook/tasks/3-commit-msg.rake +12 -13
- data/lib/githook/tasks/install.rake +9 -9
- data/lib/githook/tasks/run.rake +3 -3
- data/lib/githook/tasks/setup.rake +42 -42
- data/lib/githook/templates/config.rb +7 -6
- data/lib/githook/util.rb +33 -13
- 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: 59be1b7b50d12a2c488354d0ae590fdf4d1f880b
|
4
|
+
data.tar.gz: 0f0788df757f05f8e2d1b8d73cbe82d60ee68198
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d685f15442defe685c1c5cddbe6378ae26de64236b2c772d526c997eb0d9da9978e65fc0750e9962ecb74147cb611af7ea47f310cada8476d9a6dd3af8a1162
|
7
|
+
data.tar.gz: 8963581e1246700d95933bc48c1e925d609408cc8e7d1e22023d102ba2d74fa1e9cdcd6b4c9e083fa1e6f2bb504ba61096642655f7feefbba4e1ccb9cf9e7ce3
|
data/README.md
CHANGED
@@ -45,7 +45,7 @@ Help:
|
|
45
45
|
install -- Init githook, create .githook folder, prepare template files
|
46
46
|
setup -- Setup hooks, copy hooks from .githook/hooks to .git/hooks
|
47
47
|
backup -- Backup old hooks in .git/hooks
|
48
|
-
clean
|
48
|
+
clean -- Clear backup hooks in .git/hooks
|
49
49
|
disable -- Disable hooks: [HOOKS=pre_commit,commit_msg] githook disable
|
50
50
|
enable -- Enable hooks: [HOOKS=pre_commit,commit_msg] githook enable
|
51
51
|
list -- List all hooks
|
@@ -57,7 +57,11 @@ Getting started:
|
|
57
57
|
$ githook install
|
58
58
|
$ githook setup
|
59
59
|
|
60
|
-
|
60
|
+
Modify `.githook/config.rb` according to your requirement:
|
61
|
+
|
62
|
+
$ vim .githook/config.rb
|
63
|
+
|
64
|
+
## Demo
|
61
65
|
|
62
66
|
1. install, setup hooks
|
63
67
|
|
@@ -241,6 +245,31 @@ Demo:
|
|
241
245
|
|
242
246
|
1. [How to Build a Gem](note/how-to-build-a-gem.md)
|
243
247
|
|
248
|
+
## Releases
|
249
|
+
|
250
|
+
### 0.1.9
|
251
|
+
|
252
|
+
1. Add a `changed_files` util method to get the added or modified files, to make rubocop only check changed files instead of all files.
|
253
|
+
|
254
|
+
### 0.1.8
|
255
|
+
|
256
|
+
1. Rename `githook clearup` command to `githook clean`
|
257
|
+
|
258
|
+
1. Unify `githook pre_commit`, `githook prepare_commit_msg`, `githook commit_msg` commands to `githook run` but with different ENV value
|
259
|
+
|
260
|
+
githook pre_commit --> HOOK=pre_commit githook run
|
261
|
+
githook prepare_commit_msg --> HOOK=prepare_commit_msg githook run
|
262
|
+
githook commit_msg --> HOOK=commit_msg githook run
|
263
|
+
|
264
|
+
1. Support gitlab type branch name, for example "100-support-gitlab-branch", it will generate "FEATURE #100 - Support gitlab branch" commit message by `prepare_commit_msg` hook, uncomment `"prepare_commit_msg:prepare_for_gitlab_branch"` line and comment `"prepare_commit_msg:prepare_for_ekohe_branch"` line in `.githook/config.rb` to make it work
|
265
|
+
|
266
|
+
// .githook/config
|
267
|
+
set :prepare_commit_msg, fetch(:prepare_commit_msg, []).push(
|
268
|
+
# comment following lines if you want to skip it
|
269
|
+
# "prepare_commit_msg:prepare_for_ekohe_branch"
|
270
|
+
"prepare_commit_msg:prepare_for_gitlab_branch"
|
271
|
+
)
|
272
|
+
|
244
273
|
## License
|
245
274
|
|
246
275
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
data/bin/githook
CHANGED
data/githook.gemspec
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('
|
1
|
+
|
2
|
+
lib = File.expand_path('lib', __dir__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'githook/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'git-hook'
|
8
8
|
spec.version = Githook::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['baurine']
|
10
|
+
spec.email = ['2008.hbl@gmail.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
12
|
+
spec.summary = 'Setup git hooks easily'
|
13
|
+
spec.description = 'Help to setup git hooks easily, base on Rake, inspired from Capistrano.'
|
14
|
+
spec.homepage = 'https://github.com/baurine/githook'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
18
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
@@ -28,9 +28,9 @@ Gem::Specification.new do |spec|
|
|
28
28
|
end
|
29
29
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
30
30
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
31
|
-
spec.require_paths = [
|
31
|
+
spec.require_paths = ['lib']
|
32
32
|
|
33
|
-
spec.add_development_dependency
|
34
|
-
spec.add_development_dependency
|
35
|
-
spec.add_development_dependency
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
36
|
end
|
data/lib/githook.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
1
|
+
require 'rake'
|
2
|
+
require 'githook/version'
|
3
|
+
require 'githook/context'
|
4
|
+
require 'githook/util'
|
5
|
+
require 'githook/tasks'
|
6
|
+
require 'githook/config'
|
7
7
|
|
8
8
|
module Githook
|
9
9
|
# Your code goes here...
|
data/lib/githook/config.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# we must be very careful to load outside ruby code
|
2
2
|
# because they are out of our control
|
3
3
|
# only load outside "*.rake" when there are ".git" and ".githook" folder, and target task isn't "install"
|
4
|
-
if Dir.exist?(
|
4
|
+
if Dir.exist?('.git') && Dir.exist?('.githook') && ARGV[0] != 'install'
|
5
5
|
begin
|
6
|
-
load
|
7
|
-
rescue => e
|
6
|
+
load '.githook/config.rb'
|
7
|
+
rescue StandardError => e
|
8
8
|
puts "Error: #{e.message} in .githook/config.rb"
|
9
9
|
exit 1
|
10
10
|
end
|
data/lib/githook/context.rb
CHANGED
data/lib/githook/tasks.rb
CHANGED
@@ -4,18 +4,18 @@
|
|
4
4
|
Rake::TaskManager.record_task_metadata = true
|
5
5
|
|
6
6
|
# load rake files from lib
|
7
|
-
rake_files_pattern = File.dirname(__FILE__) +
|
7
|
+
rake_files_pattern = File.dirname(__FILE__) + '/tasks/*.rake'
|
8
8
|
# => gems/git-hook-0.1.1/lib/githook/tasks/*.rake
|
9
9
|
Dir.glob(rake_files_pattern).each { |r| load r }
|
10
10
|
|
11
11
|
# we must be very careful to load outside ruby code
|
12
12
|
# because they are out of our control
|
13
13
|
# only load outside "*.rake" when there are ".git" and ".githook" folder, and target task isn't "install"
|
14
|
-
if Dir.exist?(
|
15
|
-
Dir.glob(
|
14
|
+
if Dir.exist?('.git') && Dir.exist?('.githook') && ARGV[0] != 'install'
|
15
|
+
Dir.glob('.githook/tasks/**/*.rake').each do |rake|
|
16
16
|
begin
|
17
17
|
load rake
|
18
|
-
rescue => e
|
18
|
+
rescue StandardError => e
|
19
19
|
puts "Error: #{e.message} in #{rake}"
|
20
20
|
exit 1
|
21
21
|
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
namespace :pre_commit do
|
2
|
-
desc
|
2
|
+
desc 'Check ruby code style by rubocop'
|
3
3
|
task :rubocop do |t|
|
4
4
|
Githook::Util.log_task(t.name)
|
5
|
-
exit 1 unless system("bundle exec rubocop")
|
5
|
+
exit 1 unless system("bundle exec rubocop #{Githook::Util.changed_ruby_files}")
|
6
6
|
end
|
7
7
|
|
8
|
-
desc
|
8
|
+
desc 'Test ruby code by rspec'
|
9
9
|
task :rspec do |t|
|
10
10
|
Githook::Util.log_task(t.name)
|
11
|
-
exit 1 unless system(
|
11
|
+
exit 1 unless system('bundle exec rspec')
|
12
12
|
end
|
13
13
|
|
14
|
-
desc
|
14
|
+
desc 'Check java code style by checkstyle'
|
15
15
|
task :checkstyle do |t|
|
16
16
|
Githook::Util.log_task(t.name)
|
17
|
-
exit 1 unless system(
|
17
|
+
exit 1 unless system('./gradlew checkstyle')
|
18
18
|
end
|
19
19
|
end
|
@@ -13,9 +13,9 @@ namespace :prepare_commit_msg do
|
|
13
13
|
issue_num = match_group[2]
|
14
14
|
issue_content = match_group[3]
|
15
15
|
|
16
|
-
issue_type =
|
16
|
+
issue_type = 'BUG' if issue_type == 'HOTFIX'
|
17
17
|
issue_num = " \##{issue_num}" unless issue_num.empty?
|
18
|
-
issue_content = issue_content.tr(
|
18
|
+
issue_content = issue_content.tr('_', ' ').strip.capitalize
|
19
19
|
|
20
20
|
"#{issue_type}#{issue_num} - #{issue_content}"
|
21
21
|
else
|
@@ -34,9 +34,9 @@ namespace :prepare_commit_msg do
|
|
34
34
|
issue_num = match_group[1]
|
35
35
|
issue_content = match_group[2]
|
36
36
|
|
37
|
-
issue_type =
|
37
|
+
issue_type = 'FEATURE'
|
38
38
|
issue_num = "\##{issue_num}"
|
39
|
-
issue_content = issue_content.tr(
|
39
|
+
issue_content = issue_content.tr('-', ' ').strip.capitalize
|
40
40
|
|
41
41
|
"#{issue_type} #{issue_num} - #{issue_content}"
|
42
42
|
else
|
@@ -45,7 +45,7 @@ namespace :prepare_commit_msg do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
desc
|
48
|
+
desc 'Prepare commit msg for ekohe type branch'
|
49
49
|
task :prepare_for_ekohe_branch do |t|
|
50
50
|
Githook::Util.log_task(t.name)
|
51
51
|
|
@@ -54,13 +54,13 @@ namespace :prepare_commit_msg do
|
|
54
54
|
if Githook::Util.commit_msg_empty?(commit_msg)
|
55
55
|
branch_name = Githook::Util.branch_name
|
56
56
|
pre_msg = gen_pre_msg_for_ekohe_branch(branch_name)
|
57
|
-
puts
|
57
|
+
puts 'pre-msg:'
|
58
58
|
puts pre_msg
|
59
59
|
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
-
desc
|
63
|
+
desc 'Prepare commit msg for gitlab type branch'
|
64
64
|
task :prepare_for_gitlab_branch do |t|
|
65
65
|
Githook::Util.log_task(t.name)
|
66
66
|
|
@@ -69,7 +69,7 @@ namespace :prepare_commit_msg do
|
|
69
69
|
if Githook::Util.commit_msg_empty?(commit_msg)
|
70
70
|
branch_name = Githook::Util.branch_name
|
71
71
|
pre_msg = gen_pre_msg_for_gitlab_branch(branch_name)
|
72
|
-
puts
|
72
|
+
puts 'pre-msg:'
|
73
73
|
puts pre_msg
|
74
74
|
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
75
75
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
namespace :commit_msg do
|
2
2
|
def check_msg_for_ekohe_format?(commit_msg_arr)
|
3
3
|
def_ekohe_msg_summary_reg = /^(FEATURE|BUG|MISC|REFACTOR)(\s#\d+)* - ([A-Z].*)[^.]$/
|
4
|
-
def_ekohe_msg_format =
|
4
|
+
def_ekohe_msg_format = 'FEAUTER|BUG|MISC|REFACTOR #issue_num - Summary'
|
5
5
|
def_ekohe_body_reg = /^- ([a-z].*)[^.]$/
|
6
|
-
def_ekohe_body_format =
|
6
|
+
def_ekohe_body_format = '- detail'
|
7
7
|
|
8
|
-
summary = commit_msg_arr[0] ||
|
9
|
-
second_line = commit_msg_arr[1] ||
|
8
|
+
summary = commit_msg_arr[0] || ''
|
9
|
+
second_line = commit_msg_arr[1] || ''
|
10
10
|
body = commit_msg_arr[2..-1] || []
|
11
11
|
|
12
|
-
valid = summary.start_with?(
|
12
|
+
valid = summary.start_with?('Merge branch') || def_ekohe_msg_summary_reg.match(summary)
|
13
13
|
unless valid
|
14
14
|
puts "Commit message summary \"#{summary}\" format isn't correct."
|
15
15
|
puts "Expected format: \"#{def_ekohe_msg_format}\""
|
@@ -18,27 +18,26 @@ namespace :commit_msg do
|
|
18
18
|
|
19
19
|
valid = second_line.strip.empty?
|
20
20
|
unless valid
|
21
|
-
puts
|
21
|
+
puts 'Commit message the first line after summary should be blank.'
|
22
22
|
return false
|
23
23
|
end
|
24
24
|
|
25
25
|
body.each do |line|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
26
|
+
next if def_ekohe_body_reg.match(line)
|
27
|
+
puts "Commit message body line \"#{line}\" format isn't correct."
|
28
|
+
puts "Expected format: \"#{def_ekohe_body_format}\""
|
29
|
+
return false
|
31
30
|
end
|
32
31
|
true
|
33
32
|
end
|
34
33
|
|
35
|
-
desc
|
34
|
+
desc 'Check commit msg style for ekohe format'
|
36
35
|
task :check_msg_for_ekohe_format do |t|
|
37
36
|
Githook::Util.log_task(t.name)
|
38
37
|
|
39
38
|
commit_msg_file = Githook::Util.commit_msg_file
|
40
39
|
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
41
|
-
puts
|
40
|
+
puts 'commit-msg:'
|
42
41
|
puts commit_msg.join("\n")
|
43
42
|
|
44
43
|
# can't use return in block
|
@@ -1,23 +1,23 @@
|
|
1
|
-
desc
|
1
|
+
desc 'Init githook, create .githook folder, prepare template files'
|
2
2
|
task :install do
|
3
3
|
# step 1, check whether Dir.pwd is in git repo root folder
|
4
|
-
git_path =
|
5
|
-
unless Dir.
|
4
|
+
git_path = '.git'
|
5
|
+
unless Dir.exist?(git_path)
|
6
6
|
puts "It isn't in a git repo root folder."
|
7
7
|
exit 1
|
8
8
|
end
|
9
9
|
|
10
10
|
# step 2, check whether ".githook" folder already exists
|
11
|
-
githook_path =
|
12
|
-
if Dir.
|
13
|
-
print
|
11
|
+
githook_path = '.githook'
|
12
|
+
if Dir.exist?(githook_path)
|
13
|
+
print '.githook already exists, do you want to override it? [y/(n)]: '
|
14
14
|
choice = STDIN.gets
|
15
|
-
exit 0 if [
|
15
|
+
exit 0 if %W[n N \n].include?(choice[0])
|
16
16
|
FileUtils.rm_r(githook_path)
|
17
17
|
end
|
18
18
|
|
19
19
|
# setup 3, copy templates to .githook
|
20
|
-
templates_path = File.expand_path(
|
20
|
+
templates_path = File.expand_path('../templates', __dir__)
|
21
21
|
FileUtils.cp_r(templates_path, githook_path)
|
22
|
-
puts
|
22
|
+
puts 'Create .githook folder.'
|
23
23
|
end
|
data/lib/githook/tasks/run.rake
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
desc
|
1
|
+
desc 'Check whether .githook/hooks folder exists'
|
2
2
|
task :check_githook_folder do
|
3
|
-
hooks_path =
|
4
|
-
unless Dir.
|
3
|
+
hooks_path = '.githook/hooks'
|
4
|
+
unless Dir.exist?(hooks_path)
|
5
5
|
puts "There isn't a .githook/hooks folder."
|
6
6
|
exit 1
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
desc
|
10
|
+
desc 'Check whether .git/hooks folder exists'
|
11
11
|
task :check_git_folder do
|
12
|
-
git_path =
|
13
|
-
unless Dir.
|
12
|
+
git_path = '.git/hooks'
|
13
|
+
unless Dir.exist?(git_path)
|
14
14
|
puts "There isn't a .git/hooks folder."
|
15
15
|
exit 1
|
16
16
|
end
|
@@ -18,27 +18,27 @@ end
|
|
18
18
|
|
19
19
|
#################################################################
|
20
20
|
|
21
|
-
desc
|
22
|
-
task :
|
21
|
+
desc 'Setup hooks, copy hooks from .githook/hooks to .git/hooks'
|
22
|
+
task setup: %i[check_githook_folder check_git_folder] do
|
23
23
|
# setup 1, check whether has ".githook/hooks" and ".git" folder
|
24
24
|
# => [:check_githook_folder, :check_git_folder]
|
25
25
|
|
26
26
|
# setup 2, backup hooks
|
27
|
-
puts
|
27
|
+
puts 'Backup old hooks:'
|
28
28
|
Rake::Task[:backup].invoke
|
29
29
|
|
30
30
|
# setup 3, copy hooks to .git/hooks
|
31
|
-
FileUtils.cp_r(
|
31
|
+
FileUtils.cp_r('.githook/hooks', '.git')
|
32
32
|
end
|
33
33
|
|
34
|
-
desc
|
35
|
-
task :
|
34
|
+
desc 'Backup old hooks in .git/hooks'
|
35
|
+
task backup: :check_git_folder do
|
36
36
|
has_backup = false
|
37
|
-
Dir.glob(
|
38
|
-
file_name = path.split(
|
39
|
-
next if file_name.include?(
|
37
|
+
Dir.glob('.git/hooks/*').each do |path|
|
38
|
+
file_name = path.split('/').last
|
39
|
+
next if file_name.include?('.')
|
40
40
|
|
41
|
-
appendix = ".#{Time.now.strftime(
|
41
|
+
appendix = ".#{Time.now.strftime('%Y%m%d%H%m%S')}.bak"
|
42
42
|
puts "Backup old #{file_name} to #{file_name}#{appendix}"
|
43
43
|
FileUtils.cp(path, "#{path}#{appendix}")
|
44
44
|
has_backup = true
|
@@ -47,10 +47,10 @@ task :backup => :check_git_folder do
|
|
47
47
|
puts "You can run 'githook clean' to delete these backup." if has_backup
|
48
48
|
end
|
49
49
|
|
50
|
-
desc
|
51
|
-
task :
|
52
|
-
backup = Dir.glob(
|
53
|
-
Githook::Util.interactive_delete_files(backup,
|
50
|
+
desc 'Clear backup hooks in .git/hooks'
|
51
|
+
task clean: :check_git_folder do
|
52
|
+
backup = Dir.glob('.git/hooks/*.bak')
|
53
|
+
Githook::Util.interactive_delete_files(backup, 'backup hooks')
|
54
54
|
end
|
55
55
|
|
56
56
|
# all hooks
|
@@ -73,14 +73,14 @@ end
|
|
73
73
|
# post_update
|
74
74
|
# )
|
75
75
|
|
76
|
-
desc
|
77
|
-
task :
|
78
|
-
target_hooks = (ENV[
|
76
|
+
desc 'Disable hooks: [HOOKS=pre_commit,commit_msg] githook disable'
|
77
|
+
task disable: :check_git_folder do
|
78
|
+
target_hooks = (ENV['HOOKS'] || '').split(',')
|
79
79
|
target_hooks = Githook::Util.all_hooks if target_hooks.empty?
|
80
80
|
|
81
81
|
target_hooks.each do |hook|
|
82
|
-
hook_path = File.join(
|
83
|
-
disable_path = hook_path +
|
82
|
+
hook_path = File.join('.git/hooks', hook.tr('_', '-'))
|
83
|
+
disable_path = hook_path + '.disable'
|
84
84
|
if File.file?(hook_path)
|
85
85
|
FileUtils.mv(hook_path, disable_path)
|
86
86
|
puts "Disable #{hook} hook."
|
@@ -92,14 +92,14 @@ task :disable => :check_git_folder do
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
-
desc
|
96
|
-
task :
|
97
|
-
target_hooks = (ENV[
|
95
|
+
desc 'Enable hooks: [HOOKS=pre_commit,commit_msg] githook enable'
|
96
|
+
task enable: :check_git_folder do
|
97
|
+
target_hooks = (ENV['HOOKS'] || '').split(',')
|
98
98
|
target_hooks = Githook::Util.all_hooks if target_hooks.empty?
|
99
99
|
|
100
100
|
target_hooks.each do |hook|
|
101
|
-
hook_path = File.join(
|
102
|
-
disable_path = hook_path +
|
101
|
+
hook_path = File.join('.git/hooks', hook.tr('_', '-'))
|
102
|
+
disable_path = hook_path + '.disable'
|
103
103
|
if File.file?(hook_path)
|
104
104
|
puts "#{hook} hook is arleady enabled, skip."
|
105
105
|
elsif File.file?(disable_path)
|
@@ -111,32 +111,32 @@ task :enable => :check_git_folder do
|
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
desc
|
115
|
-
task :
|
114
|
+
desc 'List all hooks'
|
115
|
+
task list: :check_git_folder do
|
116
116
|
enabled_hooks = []
|
117
117
|
disabled_hooks = []
|
118
118
|
all_hooks = Githook::Util.all_hooks
|
119
119
|
all_hooks.each do |hook|
|
120
|
-
hook_path = File.join(
|
121
|
-
disable_path = hook_path +
|
120
|
+
hook_path = File.join('.git/hooks', hook.tr('_', '-'))
|
121
|
+
disable_path = hook_path + '.disable'
|
122
122
|
if File.file?(hook_path)
|
123
123
|
enabled_hooks << hook
|
124
124
|
elsif File.file?(disable_path)
|
125
125
|
disabled_hooks << hook
|
126
126
|
end
|
127
127
|
end
|
128
|
-
puts
|
128
|
+
puts 'Enabled hooks:'
|
129
129
|
enabled_hooks.each { |h| puts " * #{h}" }
|
130
|
-
puts
|
130
|
+
puts 'Disabled hooks:'
|
131
131
|
disabled_hooks.each { |h| puts " * #{h}" }
|
132
132
|
end
|
133
133
|
|
134
|
-
desc
|
134
|
+
desc 'Version'
|
135
135
|
task :version do
|
136
136
|
puts Githook::VERSION
|
137
137
|
end
|
138
138
|
|
139
|
-
TASKS_NAME = %w
|
139
|
+
TASKS_NAME = %w[
|
140
140
|
install
|
141
141
|
setup
|
142
142
|
backup
|
@@ -146,12 +146,12 @@ TASKS_NAME = %w(
|
|
146
146
|
list
|
147
147
|
version
|
148
148
|
help
|
149
|
-
|
150
|
-
desc
|
149
|
+
].freeze
|
150
|
+
desc 'Help'
|
151
151
|
task :help do
|
152
|
-
puts
|
152
|
+
puts 'Usage: githook task_name'
|
153
153
|
puts
|
154
|
-
puts
|
154
|
+
puts 'task_name:'
|
155
155
|
left_len = TASKS_NAME.map(&:length).max + 2
|
156
156
|
TASKS_NAME.each do |task_name|
|
157
157
|
task = Rake::Task[task_name]
|
@@ -1,19 +1,20 @@
|
|
1
1
|
set :pre_commit, fetch(:pre_commit, []).push(
|
2
2
|
# uncomment following lines if it is a ruby project
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# 'pre_commit:rubocop',
|
4
|
+
# 'pre_commit:rspec',
|
5
5
|
|
6
6
|
# uncomment following lines if it is a java project built by gradle
|
7
|
-
#
|
7
|
+
# 'pre_commit:checkstyle'
|
8
8
|
|
9
|
-
#
|
9
|
+
# 'pre_commit:check_branch_name'
|
10
10
|
)
|
11
|
+
|
11
12
|
set :prepare_commit_msg, fetch(:prepare_commit_msg, []).push(
|
12
13
|
# comment following lines if you want to skip it
|
13
|
-
|
14
|
+
'prepare_commit_msg:prepare_for_ekohe_branch'
|
14
15
|
# "prepare_commit_msg:prepare_for_gitlab_branch"
|
15
16
|
)
|
16
17
|
set :commit_msg, fetch(:commit_msg, []).push(
|
17
18
|
# comment following lines if you want to skip it
|
18
|
-
|
19
|
+
'commit_msg:check_msg_for_ekohe_format'
|
19
20
|
)
|
data/lib/githook/util.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Githook
|
2
2
|
class Util
|
3
3
|
def self.log_task(task_name)
|
4
|
-
puts "[#{Time.now.strftime('%H:%m:%S')}] #{task_name.
|
4
|
+
puts "[#{Time.now.strftime('%H:%m:%S')}] #{task_name.tr('_', ' ')}"
|
5
5
|
end
|
6
6
|
|
7
7
|
def self.run_tasks(hook_stage)
|
@@ -18,15 +18,15 @@ module Githook
|
|
18
18
|
#######################################################
|
19
19
|
|
20
20
|
def self.interactive_delete_files(path_arr, type)
|
21
|
-
if path_arr.
|
21
|
+
if path_arr.empty?
|
22
22
|
puts "There are no #{type}."
|
23
23
|
else
|
24
24
|
puts "There are following #{type}:"
|
25
25
|
puts path_arr
|
26
|
-
print
|
26
|
+
print 'Are you sure want to delete all of them [y/(n)]: '
|
27
27
|
# https://stackoverflow.com/a/40643667/2998877
|
28
28
|
choice = STDIN.gets
|
29
|
-
return if [
|
29
|
+
return if %W[n N \n].include?(choice[0])
|
30
30
|
|
31
31
|
path_arr.each do |path|
|
32
32
|
FileUtils.rm(path)
|
@@ -46,18 +46,18 @@ module Githook
|
|
46
46
|
|
47
47
|
# include enabled_hooks and disabled_hooks
|
48
48
|
def self.all_hooks
|
49
|
-
Dir.glob(
|
50
|
-
.map { |path| path.split(
|
51
|
-
.select { |name| !name.include?(
|
52
|
-
.map { |name| name.gsub(
|
49
|
+
Dir.glob('.git/hooks/*')
|
50
|
+
.map { |path| path.split('/').last }
|
51
|
+
.select { |name| !name.include?('.') || name.include?('.disable') }
|
52
|
+
.map { |name| name.gsub('.disable', '') }
|
53
53
|
.uniq
|
54
|
-
.map { |name| name.
|
54
|
+
.map { |name| name.tr('-', '_') }
|
55
55
|
end
|
56
56
|
|
57
57
|
#######################################################
|
58
58
|
|
59
59
|
def self.commit_msg_file
|
60
|
-
|
60
|
+
'.git/COMMIT_EDITMSG'
|
61
61
|
end
|
62
62
|
|
63
63
|
def self.branch_name
|
@@ -67,9 +67,9 @@ module Githook
|
|
67
67
|
def self.get_commit_msg(commit_msg_file)
|
68
68
|
commit_msg = []
|
69
69
|
# trim begining empty lines
|
70
|
-
File.open(commit_msg_file,
|
70
|
+
File.open(commit_msg_file, 'r') do |f|
|
71
71
|
f.readlines.each do |line|
|
72
|
-
next if line[0] ==
|
72
|
+
next if line[0] == '#'
|
73
73
|
next if commit_msg.empty? && line.strip.empty?
|
74
74
|
commit_msg << line
|
75
75
|
end
|
@@ -96,12 +96,32 @@ module Githook
|
|
96
96
|
|
97
97
|
# write the pre msg at the begining of commit_msg_file
|
98
98
|
def self.prefill_msg(commit_msg_file, pre_msg)
|
99
|
-
File.open(commit_msg_file,
|
99
|
+
File.open(commit_msg_file, 'r+') do |f|
|
100
100
|
ori_content = f.read
|
101
101
|
f.seek(0, IO::SEEK_SET)
|
102
102
|
f.puts pre_msg
|
103
103
|
f.puts ori_content
|
104
104
|
end
|
105
105
|
end
|
106
|
+
|
107
|
+
# get changed files, include added or modified
|
108
|
+
def self.changed_files
|
109
|
+
added_or_modified_reg = /A|AM|^M/
|
110
|
+
`git status --porcelain`.split(/\n/)
|
111
|
+
.select do |file_name_with_status|
|
112
|
+
file_name_with_status =~ added_or_modified_reg
|
113
|
+
end
|
114
|
+
.map do |file_name_with_status|
|
115
|
+
file_name_with_status.split(' ')[1]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.changed_ruby_files
|
120
|
+
changed_files
|
121
|
+
.select do |file_name|
|
122
|
+
File.extname(file_name) == '.rb'
|
123
|
+
end
|
124
|
+
.join(' ')
|
125
|
+
end
|
106
126
|
end
|
107
127
|
end
|
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.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- baurine
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|