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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cbbda040e9b08f7a73cb7a7bce3adb0e18a05b3
4
- data.tar.gz: e4a79c0201970aefc8d2002273f75b1236117b6e
3
+ metadata.gz: ebab726ddd8101a28247bc96a20628711c56f22f
4
+ data.tar.gz: c29c99fa2fcb931101290d18f40c3a75c025f713
5
5
  SHA512:
6
- metadata.gz: 22a7f13fdfd6ab50078d3516940770f5ca7beb96ae37b02ea8fde843e91924703b9857bf4c33a5d46a510b3b8442f0a97bdaafe867dfd0581fcbf1fd36f178f7
7
- data.tar.gz: 00549414373fe0b38dc1d77fb0a7bdd6cc2672ceb8c056833d425ecf9bc718bf06504ca7b9b6da4e7d66d44cef5f28e1dbb927c15a48c0c06d2b5beedb14be6d
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
- clearup -- Clear backup hooks in .git/hooks
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).
@@ -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)/})
@@ -17,9 +17,3 @@ namespace :pre_commit do
17
17
  exit 1 unless system("./gradlew checkstyle")
18
18
  end
19
19
  end
20
-
21
- desc "Run all pre-commit hook tasks"
22
- task :pre_commit do |t|
23
- Githook::Util.log_task(t.name)
24
- Githook::Util.run_tasks(t.name.to_sym)
25
- end
@@ -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
@@ -0,0 +1,8 @@
1
+ desc "Run hook tasks"
2
+ task :run do |t|
3
+ hook = ENV["HOOK"]
4
+ if hook
5
+ Githook::Util.log_task(hook)
6
+ Githook::Util.run_tasks(hook.to_sym)
7
+ end
8
+ 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 clearup' to delete these backup." if has_backup
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 :clearup => :check_git_folder do
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
- clearup
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:prepare"
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:check_msg"
18
+ "commit_msg:check_msg_for_ekohe_format"
18
19
  )
@@ -24,4 +24,4 @@ test "" = "$(grep '^Signed-off-by: ' "$1" |
24
24
  }
25
25
 
26
26
  # custom commit-msg hooks
27
- githook commit_msg
27
+ HOOK=commit_msg githook run
@@ -22,7 +22,7 @@ allownonascii=$(git config --bool hooks.allownonascii)
22
22
  exec 1>&2
23
23
 
24
24
  # custom pre-commit hooks
25
- githook pre_commit
25
+ HOOK=pre_commit githook run
26
26
  if [ $? -ne 0 ]; then
27
27
  exit 1
28
28
  fi
@@ -36,4 +36,4 @@ esac
36
36
  # grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
37
37
 
38
38
  # custom prepare-commit-msg hooks
39
- githook prepare_commit_msg
39
+ HOOK=prepare_commit_msg githook run
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Githook
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
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.7
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: 2017-11-06 00:00:00.000000000 Z
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-msg.rake
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/pre-commit.rake
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