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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebab726ddd8101a28247bc96a20628711c56f22f
4
- data.tar.gz: c29c99fa2fcb931101290d18f40c3a75c025f713
3
+ metadata.gz: 59be1b7b50d12a2c488354d0ae590fdf4d1f880b
4
+ data.tar.gz: 0f0788df757f05f8e2d1b8d73cbe82d60ee68198
5
5
  SHA512:
6
- metadata.gz: ef86bf172e83076e1af209611564b93b4e30345c65d95cf39c003df27c52f632ffa9ab6df3205a9d097f4bdb57d43dc36e4eb69e3494065dd654ff0b5e5114df
7
- data.tar.gz: 448237492eaa4dde373d7a2ffa93f4c38af944053f4cb728e1dbcfb773d53eda613cb44fe565a48ace7f1a2b73fedb0b1d676f37cdff7b0626834a1cd383ff59
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 -- Clear backup hooks in .git/hooks
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
- Demos:
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
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "githook"
3
+ require 'githook'
4
4
 
5
- task_name = ARGV[0] || "help"
5
+ task_name = ARGV[0] || 'help'
6
6
  if Rake::Task.task_defined?(task_name)
7
7
  Rake::Task[task_name].invoke
8
8
  else
@@ -1,18 +1,18 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
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 = "git-hook"
7
+ spec.name = 'git-hook'
8
8
  spec.version = Githook::VERSION
9
- spec.authors = ["baurine"]
10
- spec.email = ["2008.hbl@gmail.com"]
9
+ spec.authors = ['baurine']
10
+ spec.email = ['2008.hbl@gmail.com']
11
11
 
12
- spec.summary = %q{Setup git hooks easily}
13
- spec.description = %q{Help to setup git hooks easily, base on Rake, inspired from Capistrano.}
14
- spec.homepage = "https://github.com/baurine/githook"
15
- spec.license = "MIT"
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 = ["lib"]
31
+ spec.require_paths = ['lib']
32
32
 
33
- spec.add_development_dependency "bundler", "~> 1.14"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "rspec", "~> 3.0"
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
@@ -1,9 +1,9 @@
1
- require "rake"
2
- require "githook/version"
3
- require "githook/context"
4
- require "githook/util"
5
- require "githook/tasks"
6
- require "githook/config"
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...
@@ -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?(".git") && Dir.exist?(".githook") && ARGV[0] != "install"
4
+ if Dir.exist?('.git') && Dir.exist?('.githook') && ARGV[0] != 'install'
5
5
  begin
6
- load ".githook/config.rb"
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
@@ -4,11 +4,11 @@ module Githook
4
4
  @env ||= {}
5
5
  @env[key] = value
6
6
  end
7
-
7
+
8
8
  def self.fetch(key, def_val)
9
9
  @env ||= {}
10
10
  @env[key] || def_val
11
- end
11
+ end
12
12
  end
13
13
  end
14
14
 
@@ -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__) + "/tasks/*.rake"
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?(".git") && Dir.exist?(".githook") && ARGV[0] != "install"
15
- Dir.glob(".githook/tasks/**/*.rake").each do |rake|
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 "Check ruby code style by rubocop"
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 "Test ruby code by rspec"
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("bundle exec rspec")
11
+ exit 1 unless system('bundle exec rspec')
12
12
  end
13
13
 
14
- desc "Check java code style by checkstyle"
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("./gradlew checkstyle")
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 = "BUG" if issue_type == "HOTFIX"
16
+ issue_type = 'BUG' if issue_type == 'HOTFIX'
17
17
  issue_num = " \##{issue_num}" unless issue_num.empty?
18
- issue_content = issue_content.tr("_", " ").strip.capitalize
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 = "FEATURE"
37
+ issue_type = 'FEATURE'
38
38
  issue_num = "\##{issue_num}"
39
- issue_content = issue_content.tr("-", " ").strip.capitalize
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 "Prepare commit msg for ekohe type branch"
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 "pre-msg:"
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 "Prepare commit msg for gitlab type branch"
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 "pre-msg:"
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 = "FEAUTER|BUG|MISC|REFACTOR #issue_num - Summary"
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 = "- detail"
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?("Merge branch") || def_ekohe_msg_summary_reg.match(summary)
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 "Commit message the first line after summary should be blank."
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
- 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
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 "Check commit msg style for ekohe format"
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 "commit-msg:"
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 "Init githook, create .githook folder, 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
- git_path = ".git"
5
- unless Dir.exists?(git_path)
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 = ".githook"
12
- if Dir.exists?(githook_path)
13
- print ".githook already exists, do you want to override it? [y/(n)]: "
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 ["n", "N", "\n"].include?(choice[0])
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("../../templates", __FILE__)
20
+ templates_path = File.expand_path('../templates', __dir__)
21
21
  FileUtils.cp_r(templates_path, githook_path)
22
- puts "Create .githook folder."
22
+ puts 'Create .githook folder.'
23
23
  end
@@ -1,6 +1,6 @@
1
- desc "Run hook tasks"
2
- task :run do |t|
3
- hook = ENV["HOOK"]
1
+ desc 'Run hook tasks'
2
+ task :run do |_t|
3
+ hook = ENV['HOOK']
4
4
  if hook
5
5
  Githook::Util.log_task(hook)
6
6
  Githook::Util.run_tasks(hook.to_sym)
@@ -1,16 +1,16 @@
1
- desc "Check whether .githook/hooks folder exists"
1
+ desc 'Check whether .githook/hooks folder exists'
2
2
  task :check_githook_folder do
3
- hooks_path = ".githook/hooks"
4
- unless Dir.exists?(hooks_path)
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 "Check whether .git/hooks folder exists"
10
+ desc 'Check whether .git/hooks folder exists'
11
11
  task :check_git_folder do
12
- git_path = ".git/hooks"
13
- unless Dir.exists?(git_path)
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 "Setup hooks, copy hooks from .githook/hooks to .git/hooks"
22
- task :setup => [:check_githook_folder, :check_git_folder] do
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 "Backup old hooks:"
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(".githook/hooks", ".git")
31
+ FileUtils.cp_r('.githook/hooks', '.git')
32
32
  end
33
33
 
34
- desc "Backup old hooks in .git/hooks"
35
- task :backup => :check_git_folder do
34
+ desc 'Backup old hooks in .git/hooks'
35
+ task backup: :check_git_folder do
36
36
  has_backup = false
37
- Dir.glob(".git/hooks/*").each do |path|
38
- file_name = path.split("/").last
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("%Y%m%d%H%m%S")}.bak"
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 "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")
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 "Disable hooks: [HOOKS=pre_commit,commit_msg] githook disable"
77
- task :disable => :check_git_folder do
78
- target_hooks = (ENV["HOOKS"] || "").split(",")
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(".git/hooks", hook.gsub("_", "-"))
83
- disable_path = hook_path + ".disable"
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 "Enable hooks: [HOOKS=pre_commit,commit_msg] githook enable"
96
- task :enable => :check_git_folder do
97
- target_hooks = (ENV["HOOKS"] || "").split(",")
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(".git/hooks", hook.gsub("_", "-"))
102
- disable_path = hook_path + ".disable"
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 "List all hooks"
115
- task :list => :check_git_folder do
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(".git/hooks", hook.gsub("_", "-"))
121
- disable_path = hook_path + ".disable"
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 "Enabled hooks:"
128
+ puts 'Enabled hooks:'
129
129
  enabled_hooks.each { |h| puts " * #{h}" }
130
- puts "Disabled hooks:"
130
+ puts 'Disabled hooks:'
131
131
  disabled_hooks.each { |h| puts " * #{h}" }
132
132
  end
133
133
 
134
- desc "Version"
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 "Help"
149
+ ].freeze
150
+ desc 'Help'
151
151
  task :help do
152
- puts "Usage: githook task_name"
152
+ puts 'Usage: githook task_name'
153
153
  puts
154
- puts "task_name:"
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
- # "pre_commit:rubocop",
4
- # "pre_commit:rspec",
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
- # "pre_commit:checkstyle"
7
+ # 'pre_commit:checkstyle'
8
8
 
9
- # "pre_commit:check_branch_name"
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
- "prepare_commit_msg:prepare_for_ekohe_branch"
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
- "commit_msg:check_msg_for_ekohe_format"
19
+ 'commit_msg:check_msg_for_ekohe_format'
19
20
  )
@@ -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.gsub('_', ' ')}"
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.length == 0
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 "Are you sure want to delete all of them [y/(n)]: "
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 ["N", "n", "\n"].include?(choice[0])
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(".git/hooks/*")
50
- .map { |path| path.split("/").last }
51
- .select { |name| !name.include?(".") || name.include?(".disable") }
52
- .map { |name| name.gsub(".disable", "") }
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.gsub("-", "_") }
54
+ .map { |name| name.tr('-', '_') }
55
55
  end
56
56
 
57
57
  #######################################################
58
58
 
59
59
  def self.commit_msg_file
60
- ".git/COMMIT_EDITMSG"
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, "r") do |f|
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, "r+") do |f|
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
@@ -1,3 +1,3 @@
1
1
  module Githook
2
- VERSION = "0.1.8"
2
+ VERSION = '0.1.9'.freeze
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.8
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: 2018-09-02 00:00:00.000000000 Z
11
+ date: 2019-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler