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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd3b750cfbd6d3b3158be919978cccf9b9f3e391
4
- data.tar.gz: 3063fc3f1b655c7062326457187e3e66fd7e8677
3
+ metadata.gz: ffd94b9dc21eb07eeba7437c31123e9d09bc58a9
4
+ data.tar.gz: 7e8a1c295e60c722b55b755d40a165987ef36bcf
5
5
  SHA512:
6
- metadata.gz: 3120664307138925564ad5f227cb54999010d103ac4c35d61f008be3047ed479e8ab0d20bc0059091e89618e07d40da3382fc0ea26b6fd72c9482b262a279392
7
- data.tar.gz: 1b62f43c9006c333af6e8ab246c0ef5905dcd3636e6197c0c34482e95203c238c331a0f1086245c385aa250a1077f8df068a3bc62ff8af18127bbdc0460a9c30
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
- - [ ] more document
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
- More: TODO
58
+ Demos:
59
+
60
+ 1. install, setup hooks
61
+
62
+ ![](./art/install_setup_hooks.gif)
63
+
64
+ 1. pre-commit, prepare-commit-msg, commit-msg hooks
65
+
66
+ ![](./art/main_hooks.gif)
67
+
68
+ 1. disable, enable, list hooks
69
+
70
+ ![](./art/disable_enable_list_hooks.gif)
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
+ ![](./art/check_branch_name.gif)
42
230
 
43
231
  ## License
44
232
 
Binary file
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.log(t.name)
4
+ Githook::Util.log_task(t.name)
5
5
 
6
- commit_msg_file = '.git/COMMIT_EDITMSG'
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: #{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.log(t.name)
20
+ Githook::Util.log_task(t.name)
25
21
  Githook::Util.run_tasks(t.name.to_sym)
26
22
  end
@@ -1,4 +1,4 @@
1
- desc 'Init githook, prepare template files'
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
4
  git_path = ".git"
@@ -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.log(t.name)
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.log(t.name)
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.log(t.name)
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.log(t.name)
4
+ Githook::Util.log_task(t.name)
5
5
 
6
- commit_msg_file = '.git/COMMIT_EDITMSG'
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
- if Githook::Util.commit_msg_empty?(commit_msg_file)
13
- branch_name = `git symbolic-ref --short HEAD`
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: #{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.log(t.name)
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 where .githook folder exists'
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 where .git folder exists'
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 "you can run 'rake clear_backup' to delete these backup" if has_backup
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 :clear_backup => :check_git_folder do
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 => :clear_backup do |t|
59
- # Githook::Util.log(t.name)
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
- clear_backup
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(13)} -- #{task.comment}"
168
+ puts " #{task_name.ljust(left_len)} -- #{task.comment}"
169
169
  end
170
170
  end
@@ -1,11 +1,18 @@
1
- # you can uncomment following lines if it is a ruby project
2
- # set :pre_commit, fetch(:pre_commit, []).push(
3
- # 'pre_commit:rubocop',
4
- # 'pre_commit:rspec',
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
- # task :checkstyle do
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
@@ -1,6 +1,6 @@
1
1
  module Githook
2
2
  class Util
3
- def self.log(task_name)
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
- # check whether origin commit msg is empty
60
- def self.commit_msg_empty?(commit_msg_file)
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
- strip_line = line.strip
64
- return false if (!strip_line.empty? && !strip_line.start_with?('#'))
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
- BRANCH_NAME_REG = /^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/
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 = BRANCH_NAME_REG.match(branch_name)
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
- def self.get_commit_msg(commit_msg_file)
100
- commit_msg = ''
101
- File.open(commit_msg_file, 'r') do |f|
102
- f.readlines.each do |line|
103
- strip_line = line.strip
104
- if !strip_line.empty? && !strip_line.start_with?('#')
105
- commit_msg = line
106
- break
107
- end
108
- end
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
- MSG_FORMAT_REG = /^(FEATURE|BUG|MISC|REFACTOR|WIP)(\s#\d+)* - ([A-Z].*)/
114
- MSG_FORMAT = "FEAUTER|BUG|MISC|REFACTOR|WIP #issue_num - Content"
115
- # check commit msg style
116
- def self.expected_msg_format?(commit_msg)
117
- commit_msg.start_with?('Merge branch') || MSG_FORMAT_REG.match(commit_msg)
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
@@ -1,3 +1,3 @@
1
1
  module Githook
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
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.3
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-03 00:00:00.000000000 Z
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.13
113
+ rubygems_version: 2.6.14
110
114
  signing_key:
111
115
  specification_version: 4
112
116
  summary: Setup git hooks easily