git-hook 0.1.2 → 0.1.3

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: 83556fdb46bba0169e6b6b49af7bb3db20149749
4
- data.tar.gz: 444a35267d74f379355d450d378e968874250a89
3
+ metadata.gz: bd3b750cfbd6d3b3158be919978cccf9b9f3e391
4
+ data.tar.gz: 3063fc3f1b655c7062326457187e3e66fd7e8677
5
5
  SHA512:
6
- metadata.gz: f86dbbdd1c145db185b07dcfa4e6a9af0bb63a2350c32e7f910551c02edd85a813542c453ae1c1deb0f63c6947fd190a0588a63d589f0ff0c806c24616eac9f7
7
- data.tar.gz: b4726565873251918b6d45ba8140279aa70dd7c9e272247d0c6e9ddb140c7c7399c95b944aca5e2af94c609d4fe9683ae270810742270d92f0111cf068abfea1
6
+ metadata.gz: 3120664307138925564ad5f227cb54999010d103ac4c35d61f008be3047ed479e8ab0d20bc0059091e89618e07d40da3382fc0ea26b6fd72c9482b262a279392
7
+ data.tar.gz: 1b62f43c9006c333af6e8ab246c0ef5905dcd3636e6197c0c34482e95203c238c331a0f1086245c385aa250a1077f8df068a3bc62ff8af18127bbdc0460a9c30
@@ -13,7 +13,7 @@ namespace :commit_msg do
13
13
 
14
14
  unless Githook::Util.expected_msg_format?(commit_msg)
15
15
  puts "ERROR! commit failed, commit msg doesn't match the required format"
16
- puts "expected msg format: FEAUTER|BUG|MISC|REFACTOR #issue_num - Content"
16
+ puts "expected msg format: #{Githook::Util::MSG_FORMAT}"
17
17
  exit 1
18
18
  end
19
19
  end
@@ -1,28 +1,38 @@
1
- desc 'Setup hooks'
2
- task :setup do
3
- # setup 1, check whether has '.githook/hooks' and '.git' folder
1
+ desc 'Check where .githook folder exists'
2
+ task :check_githook_folder do
4
3
  hooks_path = '.githook/hooks'
5
4
  unless Dir.exists?(hooks_path)
6
5
  puts "There isn't a .githook/hooks folder."
7
6
  exit 1
8
7
  end
8
+ end
9
9
 
10
- git_path = '.git'
10
+ desc 'Check where .git folder exists'
11
+ task :check_git_folder do
12
+ git_path = '.git/hooks'
11
13
  unless Dir.exists?(git_path)
12
- puts "There isn't a .git folder."
14
+ puts "There isn't a .git/hooks folder."
13
15
  exit 1
14
16
  end
17
+ end
18
+
19
+ #################################################################
20
+
21
+ desc 'Setup hooks'
22
+ task :setup => [:check_githook_folder, :check_git_folder] do
23
+ # setup 1, check whether has '.githook/hooks' and '.git' folder
24
+ # => [:check_githook_folder, :check_git_folder]
15
25
 
16
26
  # setup 2, backup hooks
17
27
  puts "Backup old hooks:"
18
28
  Rake::Task[:backup].invoke
19
29
 
20
30
  # setup 3, copy hooks to .git/hooks
21
- FileUtils.cp_r(hooks_path, git_path)
31
+ FileUtils.cp_r('.githook/hooks', '.git')
22
32
  end
23
33
 
24
34
  desc 'Backup old hooks in .git/hooks'
25
- task :backup do
35
+ task :backup => :check_git_folder do
26
36
  has_backup = false
27
37
  Dir.glob('.git/hooks/*').each do |path|
28
38
  file_name = path.split('/').last
@@ -38,7 +48,7 @@ task :backup do
38
48
  end
39
49
 
40
50
  desc 'Clear backup hooks in .git/hooks'
41
- task :clear_backup do
51
+ task :clear_backup => :check_git_folder do
42
52
  backup = Dir.glob('.git/hooks/*.bak')
43
53
  Githook::Util.interactive_delete_files(backup, 'backup hooks')
44
54
  end
@@ -53,43 +63,50 @@ end
53
63
  # Githook::Util.interactive_delete_files(hooks, 'hooks')
54
64
  # end
55
65
 
56
- ALL_HOOKS = %w(
57
- applypatch_msg
58
- pre_applypatch
59
- post_applypatch
60
- pre_commit
61
- prepare_commit_msg
62
- commit_msg
63
- post_commit
64
- pre_rebase
65
- post_checkout
66
- post_merge
67
- pre_receive
68
- post_receive
69
- update
70
- post_update
71
- pre_auto_gc
72
- post_rewrite
73
- )
66
+ # all hooks
67
+ # https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
68
+ # ALL_HOOKS = %w(
69
+ # pre_commit
70
+ # prepare_commit_msg
71
+ # commit_msg
72
+ # post_commit
73
+ # pre_rebase
74
+ # post_checkout
75
+ # post_merge
76
+ # pre_push
77
+ # applypatch_msg
78
+ # pre_applypatch
79
+ # post_applypatch
80
+ # pre_receive
81
+ # post_receive
82
+ # update
83
+ # post_update
84
+ # )
85
+
86
+ desc 'Disable hooks: [HOOKS=pre_commit,commit_msg] githook disable'
87
+ task :disable => :check_git_folder do
88
+ target_hooks = (ENV['HOOKS'] || '').split(',')
89
+ target_hooks = Githook::Util.all_hooks if target_hooks.empty?
74
90
 
75
- desc 'Disable hooks: HOOKS=pre_commit,commit_msg githook disable'
76
- task :disable do
77
- target_hooks = (ENV['HOOKS'] || '').split(',') || ALL_HOOKS
78
91
  target_hooks.each do |hook|
79
92
  hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
93
+ disable_path = hook_path + '.disable'
80
94
  if File.file?(hook_path)
81
- disable_path = hook_path + '.disable'
82
95
  FileUtils.mv(hook_path, disable_path)
83
96
  puts "Disable #{hook} hook."
97
+ elsif File.file?(disable_path)
98
+ puts "#{hook} is already disabled, skip."
84
99
  else
85
100
  puts "#{hook} hook doesn't exist, skip."
86
101
  end
87
102
  end
88
103
  end
89
104
 
90
- desc 'Enable hooks: HOOKS=pre_commit,commit_msg githook enable'
91
- task :enable do
92
- target_hooks = (ENV['HOOKS'] || '').split(',') || ALL_HOOKS
105
+ desc 'Enable hooks: [HOOKS=pre_commit,commit_msg] githook enable'
106
+ task :enable => :check_git_folder do
107
+ target_hooks = (ENV['HOOKS'] || '').split(',')
108
+ target_hooks = Githook::Util.all_hooks if target_hooks.empty?
109
+
93
110
  target_hooks.each do |hook|
94
111
  hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
95
112
  disable_path = hook_path + '.disable'
@@ -105,10 +122,11 @@ task :enable do
105
122
  end
106
123
 
107
124
  desc 'List all hooks'
108
- task :list do
125
+ task :list => :check_git_folder do
109
126
  enabled_hooks = []
110
127
  disabled_hooks = []
111
- ALL_HOOKS.each do |hook|
128
+ all_hooks = Githook::Util.all_hooks
129
+ all_hooks.each do |hook|
112
130
  hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
113
131
  disable_path = hook_path + '.disable'
114
132
  if File.file?(hook_path)
@@ -37,6 +37,25 @@ module Githook
37
37
 
38
38
  #######################################################
39
39
 
40
+ # def self.enabled_hooks
41
+ # Dir.glob('.git/hooks/*')
42
+ # .map { |path| path.split('/').last }
43
+ # .reject { |name| name.include?('.') }
44
+ # .map { |name| name.gsub('-', '_') }
45
+ # end
46
+
47
+ # include enabled_hooks and disabled_hooks
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', '') }
53
+ .uniq
54
+ .map { |name| name.gsub('-', '_') }
55
+ end
56
+
57
+ #######################################################
58
+
40
59
  # check whether origin commit msg is empty
41
60
  def self.commit_msg_empty?(commit_msg_file)
42
61
  File.open(commit_msg_file, 'r') do |f|
@@ -91,7 +110,8 @@ module Githook
91
110
  commit_msg
92
111
  end
93
112
 
94
- MSG_FORMAT_REG = /^(FEATURE|BUG|MISC|REFACTOR)(\s#\d+)* - ([A-Z].*)/
113
+ MSG_FORMAT_REG = /^(FEATURE|BUG|MISC|REFACTOR|WIP)(\s#\d+)* - ([A-Z].*)/
114
+ MSG_FORMAT = "FEAUTER|BUG|MISC|REFACTOR|WIP #issue_num - Content"
95
115
  # check commit msg style
96
116
  def self.expected_msg_format?(commit_msg)
97
117
  commit_msg.start_with?('Merge branch') || MSG_FORMAT_REG.match(commit_msg)
@@ -1,3 +1,3 @@
1
1
  module Githook
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - baurine