git-hook 0.1.3 → 0.1.4
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 +190 -2
- data/art/check_branch_name.gif +0 -0
- data/art/disable_enable_list_hooks.gif +0 -0
- data/art/install_setup_hooks.gif +0 -0
- data/art/main_hooks.gif +0 -0
- data/lib/githook/tasks/commit-msg.rake +6 -10
- data/lib/githook/tasks/install.rake +1 -1
- data/lib/githook/tasks/pre-commit.rake +10 -4
- data/lib/githook/tasks/prepare-commit-msg.rake +8 -6
- data/lib/githook/tasks/setup.rake +10 -10
- data/lib/githook/templates/config.rb +12 -5
- data/lib/githook/templates/tasks/task.rake +14 -3
- data/lib/githook/util.rb +66 -24
- data/lib/githook/version.rb +1 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffd94b9dc21eb07eeba7437c31123e9d09bc58a9
|
4
|
+
data.tar.gz: 7e8a1c295e60c722b55b755d40a165987ef36bcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b8919653e6c9655c8423fb1552d8b264a9b6c9377407a34e0357b78ac5694148d885d880c518557060c32fb5ac3789e261f71fdf1c6c7bbca993915c09499ef
|
7
|
+
data.tar.gz: 1c1ce9090adcccf38cae79627f1c7b143c020a0954479adec5f23dad60bad85d63776a1429482149194ac7faa500eae7322710e462a222d514073cf029c70ef6
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ A ruby gem that help to setup git hooks easily, base on Rake, inspired from Capi
|
|
14
14
|
- [x] prepare-commit-msg hook tasks
|
15
15
|
- [x] commit-msg hook tasks
|
16
16
|
- [x] implement as a gem
|
17
|
-
- [
|
17
|
+
- [x] more document
|
18
18
|
|
19
19
|
## Installation
|
20
20
|
|
@@ -34,11 +34,199 @@ Or install it yourself as:
|
|
34
34
|
|
35
35
|
## Usage
|
36
36
|
|
37
|
+
Help:
|
38
|
+
|
37
39
|
$ githook help
|
40
|
+
Usage: githook task_name
|
41
|
+
|
42
|
+
task_name:
|
43
|
+
install -- Init githook, create .githook folder, prepare template files
|
44
|
+
setup -- Setup hooks, copy hooks from .githook/hooks to .git/hooks
|
45
|
+
backup -- Backup old hooks in .git/hooks
|
46
|
+
clearup -- Clear backup hooks in .git/hooks
|
47
|
+
disable -- Disable hooks: [HOOKS=pre_commit,commit_msg] githook disable
|
48
|
+
enable -- Enable hooks: [HOOKS=pre_commit,commit_msg] githook enable
|
49
|
+
list -- List all hooks
|
50
|
+
version -- Version
|
51
|
+
help -- Help
|
52
|
+
|
53
|
+
Getting started:
|
54
|
+
|
38
55
|
$ githook install
|
39
56
|
$ githook setup
|
40
57
|
|
41
|
-
|
58
|
+
Demos:
|
59
|
+
|
60
|
+
1. install, setup hooks
|
61
|
+
|
62
|
+

|
63
|
+
|
64
|
+
1. pre-commit, prepare-commit-msg, commit-msg hooks
|
65
|
+
|
66
|
+

|
67
|
+
|
68
|
+
1. disable, enable, list hooks
|
69
|
+
|
70
|
+

|
71
|
+
|
72
|
+
## Principle
|
73
|
+
|
74
|
+
Git will call some hooks when you commit, merge, rebase code, etc. The hooks place in `.git/hooks` folder, there are many default sample hooks, you need remove the `.sample` appendix to make it works if you want to execute the hook. The 3 hooks are most useful for us: pre-commit, prepare-commit-msg, commit-msg.
|
75
|
+
|
76
|
+
[See more hooks introduction in git official website - Customizing Git - Git Hooks](https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks).
|
77
|
+
|
78
|
+
The default behavior of these 3 hooks are a few, but we can add more additional behaviors.
|
79
|
+
|
80
|
+
In `pre-commit`, we add the following code:
|
81
|
+
|
82
|
+
# custom pre-commit hooks
|
83
|
+
githook pre_commit
|
84
|
+
if [ $? -ne 0 ]; then
|
85
|
+
exit 1
|
86
|
+
fi
|
87
|
+
|
88
|
+
We will execute `githook pre_commit` in `pre-commit`, if it fails, it will abort later operations.
|
89
|
+
|
90
|
+
What does `githook pre_commit` do? don't forget the githook base on Rake, so the `pre_commit` is a task defined in `tasks/pre-commit.rake`:
|
91
|
+
|
92
|
+
desc 'run all pre-commit hook tasks'
|
93
|
+
task :pre_commit do |t|
|
94
|
+
Githook::Util.log_task(t.name)
|
95
|
+
Githook::Util.run_tasks(t.name.to_sym)
|
96
|
+
end
|
97
|
+
|
98
|
+
It nearly does nothing but just calls `Githook::Util.run_tasks(:pre_commit)`, let's continue to dive into `run_tasks()`, it is defined in `util.rb`:
|
99
|
+
|
100
|
+
def self.run_tasks(hook_stage)
|
101
|
+
tasks = fetch(hook_stage, [])
|
102
|
+
tasks.each do |task|
|
103
|
+
if Rake::Task.task_defined?(task)
|
104
|
+
Rake::Task[task].invoke
|
105
|
+
else
|
106
|
+
puts "#{task} task doesn't exist."
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
So it will get the value of `:pre_commit`, if it is an empty array, then nothing will happen, else, it will traverse this array, execute all the tasks in this array one by one.
|
112
|
+
|
113
|
+
Where do we define the `:pre_commit` value? It is defined by user in `.githook/config.rb`, let's see how it looks like by default:
|
114
|
+
|
115
|
+
set :pre_commit, fetch(:pre_commit, []).push(
|
116
|
+
# uncomment following lines if it is a ruby project
|
117
|
+
# 'pre_commit:rubocop',
|
118
|
+
# 'pre_commit:rspec',
|
119
|
+
|
120
|
+
# uncomment following lines if it is a java project built by gradle
|
121
|
+
# 'pre_commit:checkstyle'
|
122
|
+
)
|
123
|
+
set :prepare_commit_msg, fetch(:prepare_commit_msg, []).push(
|
124
|
+
# comment following lines if you want to skip it
|
125
|
+
'prepare_commit_msg:prepare'
|
126
|
+
)
|
127
|
+
set :commit_msg, fetch(:commit_msg, []).push(
|
128
|
+
# comment following lines if you want to skip it
|
129
|
+
'commit_msg:check_msg'
|
130
|
+
)
|
131
|
+
|
132
|
+
We use `set` method to set the value. It seems the `:pre_commit` value is an empty array by default, all items are commented, so `githook pre_commit` in `pre-commit` hook will do nothing, but if your project is a ruby or rails project, there are 2 default tasks are prepared for `:pre_commit`, they are `pre_commit:rubocop` and `pre_commit:rspec`, if you uncomment the above 2 lines code, then `pre_commit:rubocp` and `pre_commit:rspec` will be executed when you commit code, of course, you can define your customized task and add to some hook (later I will introduce how to add a customized task).
|
133
|
+
|
134
|
+
How the default tasks `pre_commit:rubocop` and `pre_commit:rspec` looks like, they are defined in `tasks/pre-commit.rake`:
|
135
|
+
|
136
|
+
namespace :pre_commit do
|
137
|
+
desc 'check ruby code style by rubocop'
|
138
|
+
task :rubocop do |t|
|
139
|
+
Githook::Util.log_task(t.name)
|
140
|
+
exit 1 unless system("bundle exec rubocop")
|
141
|
+
end
|
142
|
+
|
143
|
+
desc 'test ruby code by rspec'
|
144
|
+
task :rspec do |t|
|
145
|
+
Githook::Util.log_task(t.name)
|
146
|
+
exit 1 unless system("bundle exec rspec")
|
147
|
+
end
|
148
|
+
|
149
|
+
desc 'check java code style by checkstyle'
|
150
|
+
task :checkstyle do |t|
|
151
|
+
Githook::Util.log_task(t.name)
|
152
|
+
exit 1 unless system("./gradlew checkstyle")
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
The `pre_commit:rubocop` just simply runs `bundle exec rubocop` command to check ruby code style, while `pre_commit:rspec` runs `bundle exec rspec` to test ruby code.
|
157
|
+
|
158
|
+
At last, let's see what do `prepare_commit_msg:prepare` and `commit_msg:check_msg` tasks do?
|
159
|
+
|
160
|
+
The `prepare_commit_msg:prepare` is defined in `tasks/prepare-commit-msg.rake`, it is executed in `prepare-commit-msg` hook. It will check whether the commit message is empty, if it is, it will help generate the commit message according the branch name, for example, if the branch name is `feature/24_add_help_task`, then the auto generated commit message is "FEATURE #24 - Add help task".
|
161
|
+
|
162
|
+
namespace :prepare_commit_msg do
|
163
|
+
desc 'prepare commit msg'
|
164
|
+
task :prepare do |t|
|
165
|
+
Githook::Util.log_task(t.name)
|
166
|
+
|
167
|
+
commit_msg_file = Githook::Util.commit_msg_file
|
168
|
+
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
169
|
+
if Githook::Util.commit_msg_empty?(commit_msg)
|
170
|
+
branch_name = Githook::Util.branch_name
|
171
|
+
pre_msg = Githook::Util.gen_pre_msg(branch_name)
|
172
|
+
puts "pre-msg:"
|
173
|
+
puts pre_msg
|
174
|
+
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
The `commit_msg:check_msg` is defined in `tasks/commit-msg.rake`, it is executed in `commit-msg` hook after you save the commit message. It will check your commit message style, if it doesn't match the expected format, then this commit will be aborted. In default, our expected commit message summary format is "FEAUTER|BUG|MISC|REFACTOR|WIP #issue_num - Summary", if you want to another format, you need to define yourself task to replace the default behavior.
|
180
|
+
|
181
|
+
namespace :commit_msg do
|
182
|
+
desc 'check commit msg style'
|
183
|
+
task :check_msg do |t|
|
184
|
+
Githook::Util.log_task(t.name)
|
185
|
+
|
186
|
+
commit_msg_file = Githook::Util.commit_msg_file
|
187
|
+
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
188
|
+
puts "commit-msg:"
|
189
|
+
puts commit_msg.join("\n")
|
190
|
+
exit 1 unless Githook::Util.check_msg_format?(commit_msg)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
## Customize yourself task
|
195
|
+
|
196
|
+
The default `pre_commit:rubocp`, `pre_commit:rspec` tasks will help you check ruby code style and test ruby code before commit, the `prepare_commit_msg:prepare` task will help auto generate commit message according branch name when commit code, the `commit_msg:check_msg` task will help check commit message style after save the commit message.
|
197
|
+
|
198
|
+
If you don't like these default behaviors, for example you have your own commit message style, or you want to add more checks, for example you want to be more strict in the branch name, you can't name a branch arbitrarily, it must follow some rules, it should be any of `develop/staging/master`, `/^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/`, let's try to implement this custom task.
|
199
|
+
|
200
|
+
We can define it in any rake file in `.githook/tasks` folder, there is already an empty `task.rake` file, so let's just define in it. It should work in `pre-commit` hook, so let's define it in `:pre_commit` namespace.
|
201
|
+
|
202
|
+
# .githook/tasks/task.rake
|
203
|
+
namespace :pre_commit do
|
204
|
+
desc 'Check branch name style'
|
205
|
+
task :check_branch_name do
|
206
|
+
expected_branch_reg = /^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/
|
207
|
+
branch_name = Githook::Util.branch_name
|
208
|
+
if branch_name.include?('/')
|
209
|
+
valid = expected_branch_reg.match(branch_name)
|
210
|
+
else
|
211
|
+
valid = %w(develop staging master).include?(branch_name)
|
212
|
+
end
|
213
|
+
unless valid
|
214
|
+
puts "Branch name #{branch_name} doesn't match the expected foramt."
|
215
|
+
exit 1
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
Finally, don't forget to enable this task in `.githook/config.rb`:
|
221
|
+
|
222
|
+
# .githook/config.rb
|
223
|
+
set :pre_commit, fetch(:pre_commit, []).push(
|
224
|
+
'pre_commit:check_branch_name'
|
225
|
+
)
|
226
|
+
|
227
|
+
Demo:
|
228
|
+
|
229
|
+

|
42
230
|
|
43
231
|
## License
|
44
232
|
|
Binary file
|
Binary file
|
Binary file
|
data/art/main_hooks.gif
ADDED
Binary file
|
@@ -1,26 +1,22 @@
|
|
1
1
|
namespace :commit_msg do
|
2
2
|
desc 'check commit msg style'
|
3
3
|
task :check_msg do |t|
|
4
|
-
Githook::Util.
|
4
|
+
Githook::Util.log_task(t.name)
|
5
5
|
|
6
|
-
commit_msg_file =
|
6
|
+
commit_msg_file = Githook::Util.commit_msg_file
|
7
7
|
commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
|
8
|
-
puts "commit-msg:
|
8
|
+
puts "commit-msg:"
|
9
|
+
puts commit_msg.join("\n")
|
9
10
|
# can't use return in block
|
10
11
|
# can't "exit 0" in advance, else will abort later tasks
|
11
12
|
# but we can "exit 1" in advance
|
12
13
|
# exit 0 if Githook::Util.expected_msg_format?(commit_msg)
|
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: #{Githook::Util::MSG_FORMAT}"
|
17
|
-
exit 1
|
18
|
-
end
|
14
|
+
exit 1 unless Githook::Util.check_msg_format?(commit_msg)
|
19
15
|
end
|
20
16
|
end
|
21
17
|
|
22
18
|
desc 'run all commit-msg hook tasks'
|
23
19
|
task :commit_msg do |t|
|
24
|
-
Githook::Util.
|
20
|
+
Githook::Util.log_task(t.name)
|
25
21
|
Githook::Util.run_tasks(t.name.to_sym)
|
26
22
|
end
|
@@ -1,19 +1,25 @@
|
|
1
1
|
namespace :pre_commit do
|
2
2
|
desc 'check ruby code style by rubocop'
|
3
3
|
task :rubocop do |t|
|
4
|
-
Githook::Util.
|
4
|
+
Githook::Util.log_task(t.name)
|
5
5
|
exit 1 unless system("bundle exec rubocop")
|
6
6
|
end
|
7
7
|
|
8
|
-
desc 'test by rspec'
|
8
|
+
desc 'test ruby code by rspec'
|
9
9
|
task :rspec do |t|
|
10
|
-
Githook::Util.
|
10
|
+
Githook::Util.log_task(t.name)
|
11
11
|
exit 1 unless system("bundle exec rspec")
|
12
12
|
end
|
13
|
+
|
14
|
+
desc 'check java code style by checkstyle'
|
15
|
+
task :checkstyle do |t|
|
16
|
+
Githook::Util.log_task(t.name)
|
17
|
+
exit 1 unless system("./gradlew checkstyle")
|
18
|
+
end
|
13
19
|
end
|
14
20
|
|
15
21
|
desc 'run all pre-commit hook tasks'
|
16
22
|
task :pre_commit do |t|
|
17
|
-
Githook::Util.
|
23
|
+
Githook::Util.log_task(t.name)
|
18
24
|
Githook::Util.run_tasks(t.name.to_sym)
|
19
25
|
end
|
@@ -1,18 +1,20 @@
|
|
1
1
|
namespace :prepare_commit_msg do
|
2
2
|
desc 'prepare commit msg'
|
3
3
|
task :prepare do |t|
|
4
|
-
Githook::Util.
|
4
|
+
Githook::Util.log_task(t.name)
|
5
5
|
|
6
|
-
commit_msg_file =
|
6
|
+
commit_msg_file = Githook::Util.commit_msg_file
|
7
7
|
# can't use return in block
|
8
8
|
# can't "exit 0" in advance, else will abort later tasks
|
9
9
|
# but we can "exit 1" in advance
|
10
10
|
# exit 0 unless Githook::Util.commit_msg_empty?(commit_msg_file)
|
11
11
|
|
12
|
-
|
13
|
-
|
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
|
14
15
|
pre_msg = Githook::Util.gen_pre_msg(branch_name)
|
15
|
-
puts "pre-msg:
|
16
|
+
puts "pre-msg:"
|
17
|
+
puts pre_msg
|
16
18
|
Githook::Util.prefill_msg(commit_msg_file, pre_msg)
|
17
19
|
end
|
18
20
|
end
|
@@ -20,6 +22,6 @@ end
|
|
20
22
|
|
21
23
|
desc 'run all prepare-commit-msg hook tasks'
|
22
24
|
task :prepare_commit_msg do |t|
|
23
|
-
Githook::Util.
|
25
|
+
Githook::Util.log_task(t.name)
|
24
26
|
Githook::Util.run_tasks(t.name.to_sym)
|
25
27
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
desc 'Check
|
1
|
+
desc 'Check whether .githook/hooks folder exists'
|
2
2
|
task :check_githook_folder do
|
3
3
|
hooks_path = '.githook/hooks'
|
4
4
|
unless Dir.exists?(hooks_path)
|
@@ -7,7 +7,7 @@ task :check_githook_folder do
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
desc 'Check
|
10
|
+
desc 'Check whether .git/hooks folder exists'
|
11
11
|
task :check_git_folder do
|
12
12
|
git_path = '.git/hooks'
|
13
13
|
unless Dir.exists?(git_path)
|
@@ -18,7 +18,7 @@ end
|
|
18
18
|
|
19
19
|
#################################################################
|
20
20
|
|
21
|
-
desc 'Setup hooks'
|
21
|
+
desc 'Setup hooks, copy hooks from .githook/hooks to .git/hooks'
|
22
22
|
task :setup => [: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]
|
@@ -44,19 +44,19 @@ task :backup => :check_git_folder do
|
|
44
44
|
has_backup = true
|
45
45
|
end
|
46
46
|
|
47
|
-
puts "
|
47
|
+
puts "You can run 'githook clearup' 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 :clearup => :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
56
|
# later I think we don't need to clear hooks, use disable/enable replace them
|
57
57
|
# desc 'clear all hooks (include backup) in .git/hooks'
|
58
|
-
# task :clear => :
|
59
|
-
# Githook::Util.
|
58
|
+
# task :clear => :clearup do |t|
|
59
|
+
# Githook::Util.log_task(t.name)
|
60
60
|
|
61
61
|
# hooks = Dir.glob('.git/hooks/*')
|
62
62
|
# .reject { |path| path.split('/').last.include?('.') }
|
@@ -150,7 +150,7 @@ TASKS_NAME = %w(
|
|
150
150
|
install
|
151
151
|
setup
|
152
152
|
backup
|
153
|
-
|
153
|
+
clearup
|
154
154
|
disable
|
155
155
|
enable
|
156
156
|
list
|
@@ -159,12 +159,12 @@ TASKS_NAME = %w(
|
|
159
159
|
)
|
160
160
|
desc 'Help'
|
161
161
|
task :help do
|
162
|
-
puts
|
163
162
|
puts "Usage: githook task_name"
|
164
163
|
puts
|
165
164
|
puts "task_name:"
|
165
|
+
left_len = TASKS_NAME.map(&:length).max + 2
|
166
166
|
TASKS_NAME.each do |task_name|
|
167
167
|
task = Rake::Task[task_name]
|
168
|
-
puts " #{task_name.ljust(
|
168
|
+
puts " #{task_name.ljust(left_len)} -- #{task.comment}"
|
169
169
|
end
|
170
170
|
end
|
@@ -1,11 +1,18 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
|
1
|
+
set :pre_commit, fetch(:pre_commit, []).push(
|
2
|
+
# uncomment following lines if it is a ruby project
|
3
|
+
# 'pre_commit:rubocop',
|
4
|
+
# 'pre_commit:rspec',
|
5
|
+
|
6
|
+
# uncomment following lines if it is a java project built by gradle
|
7
|
+
# 'pre_commit:checkstyle'
|
8
|
+
|
9
|
+
# 'pre_commit:check_branch_name'
|
10
|
+
)
|
6
11
|
set :prepare_commit_msg, fetch(:prepare_commit_msg, []).push(
|
12
|
+
# comment following lines if you want to skip it
|
7
13
|
'prepare_commit_msg:prepare'
|
8
14
|
)
|
9
15
|
set :commit_msg, fetch(:commit_msg, []).push(
|
16
|
+
# comment following lines if you want to skip it
|
10
17
|
'commit_msg:check_msg'
|
11
18
|
)
|
@@ -1,8 +1,19 @@
|
|
1
1
|
# define yourself tasks here
|
2
2
|
# or put them in a new .rake file
|
3
|
-
|
3
|
+
|
4
4
|
# namespace :pre_commit do
|
5
|
-
#
|
6
|
-
#
|
5
|
+
# desc 'Check branch name style'
|
6
|
+
# task :check_branch_name do
|
7
|
+
# expected_branch_reg = /^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/
|
8
|
+
# branch_name = Githook::Util.branch_name
|
9
|
+
# if branch_name.include?('/')
|
10
|
+
# valid = expected_branch_reg.match(branch_name)
|
11
|
+
# else
|
12
|
+
# valid = %w(develop staging master).include?(branch_name)
|
13
|
+
# end
|
14
|
+
# unless valid
|
15
|
+
# puts "Branch name #{branch_name} doesn't match the expected foramt."
|
16
|
+
# exit 1
|
17
|
+
# end
|
7
18
|
# end
|
8
19
|
# end
|
data/lib/githook/util.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Githook
|
2
2
|
class Util
|
3
|
-
def self.
|
3
|
+
def self.log_task(task_name)
|
4
4
|
puts "[#{Time.now.strftime('%H:%m:%S')}] #{task_name.gsub('_', ' ')}"
|
5
5
|
end
|
6
6
|
|
@@ -56,21 +56,52 @@ module Githook
|
|
56
56
|
|
57
57
|
#######################################################
|
58
58
|
|
59
|
-
|
60
|
-
|
59
|
+
def self.commit_msg_file
|
60
|
+
'.git/COMMIT_EDITMSG'
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.branch_name
|
64
|
+
`git symbolic-ref --short HEAD`.strip
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.get_commit_msg(commit_msg_file)
|
68
|
+
commit_msg = []
|
69
|
+
# trim begining empty lines
|
61
70
|
File.open(commit_msg_file, 'r') do |f|
|
62
71
|
f.readlines.each do |line|
|
63
|
-
|
64
|
-
|
72
|
+
next if line[0] == '#'
|
73
|
+
next if commit_msg.empty? && line.strip.empty?
|
74
|
+
commit_msg << line
|
65
75
|
end
|
66
76
|
end
|
77
|
+
# trim redundant tail empty lines
|
78
|
+
unless commit_msg.empty?
|
79
|
+
last_not_empty_line = 0
|
80
|
+
commit_msg.each_with_index do |line, index|
|
81
|
+
last_not_empty_line = index unless line.strip.empty?
|
82
|
+
end
|
83
|
+
commit_msg = commit_msg[0..last_not_empty_line]
|
84
|
+
end
|
85
|
+
# remove every line right blank space, include "\n"
|
86
|
+
commit_msg.map(&:rstrip)
|
87
|
+
end
|
88
|
+
|
89
|
+
# check whether origin commit msg is empty
|
90
|
+
def self.commit_msg_empty?(commit_msg_arr)
|
91
|
+
commit_msg_arr.each do |line|
|
92
|
+
return false unless line.strip.empty?
|
93
|
+
end
|
67
94
|
true
|
68
95
|
end
|
69
96
|
|
70
|
-
|
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*)/
|
71
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.
|
72
103
|
def self.gen_pre_msg(branch_name)
|
73
|
-
match_group =
|
104
|
+
match_group = DEF_BRANCH_NAME_REG.match(branch_name)
|
74
105
|
if match_group
|
75
106
|
issue_type = match_group[1].upcase
|
76
107
|
issue_num = match_group[2]
|
@@ -96,25 +127,36 @@ module Githook
|
|
96
127
|
end
|
97
128
|
end
|
98
129
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
130
|
+
DEF_MSG_SUMMARY_REG = /^(FEATURE|BUG|MISC|REFACTOR|WIP)(\s#\d+)* - ([A-Z].*)[^.]$/
|
131
|
+
DEF_MSG_SUMMARY_FORMAT = "FEAUTER|BUG|MISC|REFACTOR|WIP #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
|
109
150
|
end
|
110
|
-
commit_msg
|
111
|
-
end
|
112
151
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
118
160
|
end
|
119
161
|
end
|
120
162
|
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.4
|
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-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,6 +67,10 @@ files:
|
|
67
67
|
- LICENSE.txt
|
68
68
|
- README.md
|
69
69
|
- Rakefile
|
70
|
+
- art/check_branch_name.gif
|
71
|
+
- art/disable_enable_list_hooks.gif
|
72
|
+
- art/install_setup_hooks.gif
|
73
|
+
- art/main_hooks.gif
|
70
74
|
- bin/githook
|
71
75
|
- build-install.sh
|
72
76
|
- githook.gemspec
|
@@ -106,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
110
|
version: '0'
|
107
111
|
requirements: []
|
108
112
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.6.
|
113
|
+
rubygems_version: 2.6.14
|
110
114
|
signing_key:
|
111
115
|
specification_version: 4
|
112
116
|
summary: Setup git hooks easily
|