git-hook 0.1.7 → 0.1.8
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 +7 -1
- data/githook.gemspec +1 -1
- data/lib/githook/tasks/{pre-commit.rake → 1-pre-commit.rake} +0 -6
- data/lib/githook/tasks/2-prepare-commit-msg.rake +77 -0
- data/lib/githook/tasks/3-commit-msg.rake +49 -0
- data/lib/githook/tasks/run.rake +8 -0
- data/lib/githook/tasks/setup.rake +3 -13
- data/lib/githook/templates/config.rb +3 -2
- data/lib/githook/templates/hooks/commit-msg +1 -1
- data/lib/githook/templates/hooks/pre-commit +1 -1
- data/lib/githook/templates/hooks/prepare-commit-msg +1 -1
- data/lib/githook/util.rb +0 -55
- data/lib/githook/version.rb +1 -1
- metadata +6 -5
- data/lib/githook/tasks/commit-msg.rake +0 -22
- data/lib/githook/tasks/prepare-commit-msg.rake +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebab726ddd8101a28247bc96a20628711c56f22f
|
4
|
+
data.tar.gz: c29c99fa2fcb931101290d18f40c3a75c025f713
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef86bf172e83076e1af209611564b93b4e30345c65d95cf39c003df27c52f632ffa9ab6df3205a9d097f4bdb57d43dc36e4eb69e3494065dd654ff0b5e5114df
|
7
|
+
data.tar.gz: 448237492eaa4dde373d7a2ffa93f4c38af944053f4cb728e1dbcfb773d53eda613cb44fe565a48ace7f1a2b73fedb0b1d676f37cdff7b0626834a1cd383ff59
|
data/README.md
CHANGED
@@ -32,6 +32,8 @@ Or install it yourself as:
|
|
32
32
|
|
33
33
|
$ gem install git-hook
|
34
34
|
|
35
|
+
**Notice!** The gem name isn't **githook**, it is another gem written by some guy.
|
36
|
+
|
35
37
|
## Usage
|
36
38
|
|
37
39
|
Help:
|
@@ -43,7 +45,7 @@ Help:
|
|
43
45
|
install -- Init githook, create .githook folder, prepare template files
|
44
46
|
setup -- Setup hooks, copy hooks from .githook/hooks to .git/hooks
|
45
47
|
backup -- Backup old hooks in .git/hooks
|
46
|
-
|
48
|
+
clean -- Clear backup hooks in .git/hooks
|
47
49
|
disable -- Disable hooks: [HOOKS=pre_commit,commit_msg] githook disable
|
48
50
|
enable -- Enable hooks: [HOOKS=pre_commit,commit_msg] githook enable
|
49
51
|
list -- List all hooks
|
@@ -235,6 +237,10 @@ Demo:
|
|
235
237
|
1. [Git Hooks Sample](https://github.com/baurine/git-hooks-sample)
|
236
238
|
1. [Step-by-Step Guide to Building Your First Ruby Gem](https://quickleft.com/blog/engineering-lunch-series-step-by-step-guide-to-building-your-first-ruby-gem/)
|
237
239
|
|
240
|
+
## Note
|
241
|
+
|
242
|
+
1. [How to Build a Gem](note/how-to-build-a-gem.md)
|
243
|
+
|
238
244
|
## License
|
239
245
|
|
240
246
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/githook.gemspec
CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
# end
|
25
25
|
|
26
26
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
-
f.match(%r{^(test|spec|features|art)/})
|
27
|
+
f.match(%r{^(test|spec|features|art|note)/})
|
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)/})
|
@@ -0,0 +1,77 @@
|
|
1
|
+
namespace :prepare_commit_msg do
|
2
|
+
# generate pre msg according branch name
|
3
|
+
# why do I pass branch_name as a parameter, not implement it inside the gen_pre_msg_for_ekohe_branch,
|
4
|
+
# because this is easy to test, it a kind of inject dependency thinking.
|
5
|
+
def gen_pre_msg_for_ekohe_branch(branch_name)
|
6
|
+
# default ekohe valid branch name: feature/24_add_enable_task
|
7
|
+
# will generate commit message: FEATURE #24 - Add enable task
|
8
|
+
default_ekohe_branch_name_reg = /^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/
|
9
|
+
|
10
|
+
match_group = default_ekohe_branch_name_reg.match(branch_name)
|
11
|
+
if match_group
|
12
|
+
issue_type = match_group[1].upcase
|
13
|
+
issue_num = match_group[2]
|
14
|
+
issue_content = match_group[3]
|
15
|
+
|
16
|
+
issue_type = "BUG" if issue_type == "HOTFIX"
|
17
|
+
issue_num = " \##{issue_num}" unless issue_num.empty?
|
18
|
+
issue_content = issue_content.tr("_", " ").strip.capitalize
|
19
|
+
|
20
|
+
"#{issue_type}#{issue_num} - #{issue_content}"
|
21
|
+
else
|
22
|
+
msg = branch_name.tr('-_/', ' ').strip.capitalize
|
23
|
+
"MISC - #{msg}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def gen_pre_msg_for_gitlab_branch(branch_name)
|
28
|
+
# default gitlab valid branch name: 24-add-enable-task
|
29
|
+
# will generate commit message: FEATURE #24 - Add enable task
|
30
|
+
default_gitlab_branch_name_reg = /^(\d+)-(\S*)/
|
31
|
+
|
32
|
+
match_group = default_gitlab_branch_name_reg.match(branch_name)
|
33
|
+
if match_group
|
34
|
+
issue_num = match_group[1]
|
35
|
+
issue_content = match_group[2]
|
36
|
+
|
37
|
+
issue_type = "FEATURE"
|
38
|
+
issue_num = "\##{issue_num}"
|
39
|
+
issue_content = issue_content.tr("-", " ").strip.capitalize
|
40
|
+
|
41
|
+
"#{issue_type} #{issue_num} - #{issue_content}"
|
42
|
+
else
|
43
|
+
msg = branch_name.tr('-_/', ' ').strip.capitalize
|
44
|
+
"MISC - #{msg}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Prepare commit msg for ekohe type branch"
|
49
|
+
task :prepare_for_ekohe_branch do |t|
|
50
|
+
Githook::Util.log_task(t.name)
|
51
|
+
|
52
|
+
commit_msg_file = Githook::Util.commit_msg_file
|
53
|
+
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
54
|
+
if Githook::Util.commit_msg_empty?(commit_msg)
|
55
|
+
branch_name = Githook::Util.branch_name
|
56
|
+
pre_msg = gen_pre_msg_for_ekohe_branch(branch_name)
|
57
|
+
puts "pre-msg:"
|
58
|
+
puts pre_msg
|
59
|
+
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
desc "Prepare commit msg for gitlab type branch"
|
64
|
+
task :prepare_for_gitlab_branch do |t|
|
65
|
+
Githook::Util.log_task(t.name)
|
66
|
+
|
67
|
+
commit_msg_file = Githook::Util.commit_msg_file
|
68
|
+
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
69
|
+
if Githook::Util.commit_msg_empty?(commit_msg)
|
70
|
+
branch_name = Githook::Util.branch_name
|
71
|
+
pre_msg = gen_pre_msg_for_gitlab_branch(branch_name)
|
72
|
+
puts "pre-msg:"
|
73
|
+
puts pre_msg
|
74
|
+
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
namespace :commit_msg do
|
2
|
+
def check_msg_for_ekohe_format?(commit_msg_arr)
|
3
|
+
def_ekohe_msg_summary_reg = /^(FEATURE|BUG|MISC|REFACTOR)(\s#\d+)* - ([A-Z].*)[^.]$/
|
4
|
+
def_ekohe_msg_format = "FEAUTER|BUG|MISC|REFACTOR #issue_num - Summary"
|
5
|
+
def_ekohe_body_reg = /^- ([a-z].*)[^.]$/
|
6
|
+
def_ekohe_body_format = "- detail"
|
7
|
+
|
8
|
+
summary = commit_msg_arr[0] || ""
|
9
|
+
second_line = commit_msg_arr[1] || ""
|
10
|
+
body = commit_msg_arr[2..-1] || []
|
11
|
+
|
12
|
+
valid = summary.start_with?("Merge branch") || def_ekohe_msg_summary_reg.match(summary)
|
13
|
+
unless valid
|
14
|
+
puts "Commit message summary \"#{summary}\" format isn't correct."
|
15
|
+
puts "Expected format: \"#{def_ekohe_msg_format}\""
|
16
|
+
return false
|
17
|
+
end
|
18
|
+
|
19
|
+
valid = second_line.strip.empty?
|
20
|
+
unless valid
|
21
|
+
puts "Commit message the first line after summary should be blank."
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
|
25
|
+
body.each do |line|
|
26
|
+
unless 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
|
30
|
+
end
|
31
|
+
end
|
32
|
+
true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Check commit msg style for ekohe format"
|
36
|
+
task :check_msg_for_ekohe_format do |t|
|
37
|
+
Githook::Util.log_task(t.name)
|
38
|
+
|
39
|
+
commit_msg_file = Githook::Util.commit_msg_file
|
40
|
+
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
41
|
+
puts "commit-msg:"
|
42
|
+
puts commit_msg.join("\n")
|
43
|
+
|
44
|
+
# can't use return in block
|
45
|
+
# can't "exit 0" in advance, else will abort later tasks
|
46
|
+
# but we can "exit 1" in advance
|
47
|
+
exit 1 unless check_msg_for_ekohe_format?(commit_msg)
|
48
|
+
end
|
49
|
+
end
|
@@ -44,25 +44,15 @@ task :backup => :check_git_folder do
|
|
44
44
|
has_backup = true
|
45
45
|
end
|
46
46
|
|
47
|
-
puts "You can run 'githook
|
47
|
+
puts "You can run 'githook clean' to delete these backup." if has_backup
|
48
48
|
end
|
49
49
|
|
50
50
|
desc "Clear backup hooks in .git/hooks"
|
51
|
-
task :
|
51
|
+
task :clean => :check_git_folder do
|
52
52
|
backup = Dir.glob(".git/hooks/*.bak")
|
53
53
|
Githook::Util.interactive_delete_files(backup, "backup hooks")
|
54
54
|
end
|
55
55
|
|
56
|
-
# later I think we don't need to clear hooks, use disable/enable replace them
|
57
|
-
# desc "Clear all hooks (include backup) in .git/hooks"
|
58
|
-
# task :clear => :clearup do |t|
|
59
|
-
# Githook::Util.log_task(t.name)
|
60
|
-
|
61
|
-
# hooks = Dir.glob(".git/hooks/*")
|
62
|
-
# .reject { |path| path.split("/").last.include?(".") }
|
63
|
-
# Githook::Util.interactive_delete_files(hooks, "hooks")
|
64
|
-
# end
|
65
|
-
|
66
56
|
# all hooks
|
67
57
|
# https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
|
68
58
|
# ALL_HOOKS = %w(
|
@@ -150,7 +140,7 @@ TASKS_NAME = %w(
|
|
150
140
|
install
|
151
141
|
setup
|
152
142
|
backup
|
153
|
-
|
143
|
+
clean
|
154
144
|
disable
|
155
145
|
enable
|
156
146
|
list
|
@@ -10,9 +10,10 @@ set :pre_commit, fetch(:pre_commit, []).push(
|
|
10
10
|
)
|
11
11
|
set :prepare_commit_msg, fetch(:prepare_commit_msg, []).push(
|
12
12
|
# comment following lines if you want to skip it
|
13
|
-
"prepare_commit_msg:
|
13
|
+
"prepare_commit_msg:prepare_for_ekohe_branch"
|
14
|
+
# "prepare_commit_msg:prepare_for_gitlab_branch"
|
14
15
|
)
|
15
16
|
set :commit_msg, fetch(:commit_msg, []).push(
|
16
17
|
# comment following lines if you want to skip it
|
17
|
-
"commit_msg:
|
18
|
+
"commit_msg:check_msg_for_ekohe_format"
|
18
19
|
)
|
data/lib/githook/util.rb
CHANGED
@@ -94,29 +94,6 @@ module Githook
|
|
94
94
|
true
|
95
95
|
end
|
96
96
|
|
97
|
-
# default valid branch name: feature/24_add_enable_task
|
98
|
-
# will generate commit message: FEATURE #24 - Add enable task
|
99
|
-
DEF_BRANCH_NAME_REG = /^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/
|
100
|
-
# generate pre msg according branch name
|
101
|
-
# why do I pass branch_name as a parameter, not implement it inside the gen_pre_msg,
|
102
|
-
# because this is easy to test, it a kind of inject dependency thinking.
|
103
|
-
def self.gen_pre_msg(branch_name)
|
104
|
-
match_group = DEF_BRANCH_NAME_REG.match(branch_name)
|
105
|
-
if match_group
|
106
|
-
issue_type = match_group[1].upcase
|
107
|
-
issue_num = match_group[2]
|
108
|
-
issue_content = match_group[3]
|
109
|
-
|
110
|
-
issue_type = "BUG" if issue_type == "HOTFIX"
|
111
|
-
issue_num = " \##{issue_num}" unless issue_num.empty?
|
112
|
-
issue_content = issue_content.tr("_", " ").strip.capitalize
|
113
|
-
|
114
|
-
"#{issue_type}#{issue_num} - #{issue_content}"
|
115
|
-
else
|
116
|
-
"MISC - "
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
97
|
# write the pre msg at the begining of commit_msg_file
|
121
98
|
def self.prefill_msg(commit_msg_file, pre_msg)
|
122
99
|
File.open(commit_msg_file, "r+") do |f|
|
@@ -126,37 +103,5 @@ module Githook
|
|
126
103
|
f.puts ori_content
|
127
104
|
end
|
128
105
|
end
|
129
|
-
|
130
|
-
DEF_MSG_SUMMARY_REG = /^(FEATURE|BUG|MISC|REFACTOR)(\s#\d+)* - ([A-Z].*)[^.]$/
|
131
|
-
DEF_MSG_SUMMARY_FORMAT = "FEAUTER|BUG|MISC|REFACTOR #issue_num - Summary"
|
132
|
-
DEF_MSG_BODY_REG = /^- ([a-z].*)[^.]$/
|
133
|
-
DEF_MSG_BODY_FORMAT = "- detail"
|
134
|
-
def self.check_msg_format?(commit_msg_arr)
|
135
|
-
summary = commit_msg_arr[0] || ""
|
136
|
-
second_line = commit_msg_arr[1] || ""
|
137
|
-
body = commit_msg_arr[2..-1] || []
|
138
|
-
|
139
|
-
valid = summary.start_with?("Merge branch") || DEF_MSG_SUMMARY_REG.match(summary)
|
140
|
-
unless valid
|
141
|
-
puts "Commit message summary \"#{summary}\" format isn't correct."
|
142
|
-
puts "Expected format: \"#{DEF_MSG_SUMMARY_FORMAT}\""
|
143
|
-
return false
|
144
|
-
end
|
145
|
-
|
146
|
-
valid = second_line.strip.empty?
|
147
|
-
unless valid
|
148
|
-
puts "Commit message the first line after summary should be blank."
|
149
|
-
return false
|
150
|
-
end
|
151
|
-
|
152
|
-
body.each do |line|
|
153
|
-
unless DEF_MSG_BODY_REG.match(line)
|
154
|
-
puts "Commit message body line \"#{line}\" format isn't correct."
|
155
|
-
puts "Expected format: \"#{DEF_MSG_BODY_FORMAT}\""
|
156
|
-
return false
|
157
|
-
end
|
158
|
-
end
|
159
|
-
true
|
160
|
-
end
|
161
106
|
end
|
162
107
|
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.8
|
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: 2018-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,10 +74,11 @@ files:
|
|
74
74
|
- lib/githook/config.rb
|
75
75
|
- lib/githook/context.rb
|
76
76
|
- lib/githook/tasks.rb
|
77
|
-
- lib/githook/tasks/commit
|
77
|
+
- lib/githook/tasks/1-pre-commit.rake
|
78
|
+
- lib/githook/tasks/2-prepare-commit-msg.rake
|
79
|
+
- lib/githook/tasks/3-commit-msg.rake
|
78
80
|
- lib/githook/tasks/install.rake
|
79
|
-
- lib/githook/tasks/
|
80
|
-
- lib/githook/tasks/prepare-commit-msg.rake
|
81
|
+
- lib/githook/tasks/run.rake
|
81
82
|
- lib/githook/tasks/setup.rake
|
82
83
|
- lib/githook/templates/config.rb
|
83
84
|
- lib/githook/templates/hooks/commit-msg
|
@@ -1,22 +0,0 @@
|
|
1
|
-
namespace :commit_msg do
|
2
|
-
desc "Check commit msg style"
|
3
|
-
task :check_msg do |t|
|
4
|
-
Githook::Util.log_task(t.name)
|
5
|
-
|
6
|
-
commit_msg_file = Githook::Util.commit_msg_file
|
7
|
-
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
8
|
-
puts "commit-msg:"
|
9
|
-
puts commit_msg.join("\n")
|
10
|
-
# can't use return in block
|
11
|
-
# can't "exit 0" in advance, else will abort later tasks
|
12
|
-
# but we can "exit 1" in advance
|
13
|
-
# exit 0 if Githook::Util.expected_msg_format?(commit_msg)
|
14
|
-
exit 1 unless Githook::Util.check_msg_format?(commit_msg)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
desc "Run all commit-msg hook tasks"
|
19
|
-
task :commit_msg do |t|
|
20
|
-
Githook::Util.log_task(t.name)
|
21
|
-
Githook::Util.run_tasks(t.name.to_sym)
|
22
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
namespace :prepare_commit_msg do
|
2
|
-
desc "Prepare commit msg"
|
3
|
-
task :prepare do |t|
|
4
|
-
Githook::Util.log_task(t.name)
|
5
|
-
|
6
|
-
commit_msg_file = Githook::Util.commit_msg_file
|
7
|
-
# can't use return in block
|
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)
|
11
|
-
|
12
|
-
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
13
|
-
if Githook::Util.commit_msg_empty?(commit_msg)
|
14
|
-
branch_name = Githook::Util.branch_name
|
15
|
-
pre_msg = Githook::Util.gen_pre_msg(branch_name)
|
16
|
-
puts "pre-msg:"
|
17
|
-
puts pre_msg
|
18
|
-
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
desc "Run all prepare-commit-msg hook tasks"
|
24
|
-
task :prepare_commit_msg do |t|
|
25
|
-
Githook::Util.log_task(t.name)
|
26
|
-
Githook::Util.run_tasks(t.name.to_sym)
|
27
|
-
end
|