git-hook 0.1.1 → 0.1.2

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: 65d3e00ec19974b20450b6c052f4bbbc57eca0a1
4
- data.tar.gz: 89525dbe7fededbc2bee68b46b29c03489535125
3
+ metadata.gz: 83556fdb46bba0169e6b6b49af7bb3db20149749
4
+ data.tar.gz: 444a35267d74f379355d450d378e968874250a89
5
5
  SHA512:
6
- metadata.gz: 7ec386606137eeb958e85cc1398649be9050cb570d644e81bb10e7a8539495c708d0418576c0cd9bfd0b96346b8c3027d4084e2e0cc17c3756d3025bce3d3ba9
7
- data.tar.gz: 9cdb75174577473f3632cfb992d08a15a9e1930fd23b7551e2a16f99199275f0d1bed68e5153e0463110a2ee051220ddf5f3dad66c6eee3980c9095431e4d003
6
+ metadata.gz: f86dbbdd1c145db185b07dcfa4e6a9af0bb63a2350c32e7f910551c02edd85a813542c453ae1c1deb0f63c6947fd190a0588a63d589f0ff0c806c24616eac9f7
7
+ data.tar.gz: b4726565873251918b6d45ba8140279aa70dd7c9e272247d0c6e9ddb140c7c7399c95b944aca5e2af94c609d4fe9683ae270810742270d92f0111cf068abfea1
data/README.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  A ruby gem that help to setup git hooks easily, base on Rake, inspired from Capistrano.
4
4
 
5
+ ## TODO
6
+
7
+ - [x] install task
8
+ - [x] setup hooks task
9
+ - [x] clear hooks task
10
+ - [x] disable/enable hooks task
11
+ - [x] list hooks task
12
+ - [x] version/help task
13
+ - [x] pre-commit hook tasks
14
+ - [x] prepare-commit-msg hook tasks
15
+ - [x] commit-msg hook tasks
16
+ - [x] implement as a gem
17
+ - [ ] more document
18
+
5
19
  ## Installation
6
20
 
7
21
  Add this line to your application's Gemfile:
@@ -20,6 +34,7 @@ Or install it yourself as:
20
34
 
21
35
  ## Usage
22
36
 
37
+ $ githook help
23
38
  $ githook install
24
39
  $ githook setup
25
40
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  require "githook"
4
4
 
5
- task_name = ARGV[0]
6
- begin
5
+ task_name = ARGV[0] || 'help'
6
+ if Rake::Task.task_defined?(task_name)
7
7
  Rake::Task[task_name].invoke
8
- rescue => e
9
- puts "Error: #{e.message}"
8
+ else
9
+ puts "Error: #{task_name} task doesn't exist"
10
10
  end
@@ -1,3 +1,11 @@
1
- if ARGV[0] != "install"
2
- load ".githook/config.rb"
1
+ # we must be very careful to load outside ruby code
2
+ # because they are out of our control
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"
5
+ begin
6
+ load ".githook/config.rb"
7
+ rescue => e
8
+ puts "Error: #{e.message} in .githook/config.rb"
9
+ exit 1
10
+ end
3
11
  end
@@ -1,11 +1,23 @@
1
+ # https://stackoverflow.com/questions/8781263/access-rake-task-description-from-within-task
2
+ # enable get task description
3
+ # you aslo can see it in Rake source code: lib/rake/application.rb#select_tasks_to_show
4
+ Rake::TaskManager.record_task_metadata = true
5
+
1
6
  # load rake files from lib
2
- rake_files_pattern = File.dirname(__FILE__) + "/tasks/**/*.rake"
3
- # => gems/git-hook-0.1.1/lib/githook/tasks/**/*.rake
7
+ rake_files_pattern = File.dirname(__FILE__) + "/tasks/*.rake"
8
+ # => gems/git-hook-0.1.1/lib/githook/tasks/*.rake
4
9
  Dir.glob(rake_files_pattern).each { |r| load r }
5
10
 
6
- # load rake files from outside project, defined by developer
7
- # in case the outside rake file has some errors,
8
- # not load them when target task is "install"
9
- if ARGV[0] != "install"
10
- Dir.glob(".githook/tasks/**/*.rake").each { |r| load r }
11
+ # we must be very careful to load outside ruby code
12
+ # because they are out of our control
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|
16
+ begin
17
+ load rake
18
+ rescue => e
19
+ puts "Error: #{e.message} in #{rake}"
20
+ exit 1
21
+ end
22
+ end
11
23
  end
@@ -6,11 +6,16 @@ namespace :commit_msg do
6
6
  commit_msg_file = '.git/COMMIT_EDITMSG'
7
7
  commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
8
8
  puts "commit-msg: #{commit_msg}"
9
- exit 0 if Githook::Util.expected_msg_format?(commit_msg)
9
+ # can't use return in block
10
+ # can't "exit 0" in advance, else will abort later tasks
11
+ # but we can "exit 1" in advance
12
+ # exit 0 if Githook::Util.expected_msg_format?(commit_msg)
10
13
 
11
- puts "ERROR! commit failed, commit msg doesn't match the required format"
12
- puts "expected msg format: FEAUTER|BUG|MISC|REFACTOR #issue_num - Content"
13
- exit 1
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: FEAUTER|BUG|MISC|REFACTOR #issue_num - Content"
17
+ exit 1
18
+ end
14
19
  end
15
20
  end
16
21
 
@@ -1,4 +1,5 @@
1
- task :install do |t|
1
+ desc 'Init githook, prepare template files'
2
+ task :install do
2
3
  # step 1, check whether Dir.pwd is in git repo root folder
3
4
  git_path = ".git"
4
5
  unless Dir.exists?(git_path)
@@ -5,12 +5,16 @@ namespace :prepare_commit_msg do
5
5
 
6
6
  commit_msg_file = '.git/COMMIT_EDITMSG'
7
7
  # can't use return in block
8
- exit 0 unless Githook::Util.commit_msg_empty?(commit_msg_file)
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)
9
11
 
10
- branch_name = `git symbolic-ref --short HEAD`
11
- pre_msg = Githook::Util.gen_pre_msg(branch_name)
12
- puts "pre-msg: #{pre_msg}"
13
- Githook::Util.prefill_msg(commit_msg_file, pre_msg)
12
+ if Githook::Util.commit_msg_empty?(commit_msg_file)
13
+ branch_name = `git symbolic-ref --short HEAD`
14
+ pre_msg = Githook::Util.gen_pre_msg(branch_name)
15
+ puts "pre-msg: #{pre_msg}"
16
+ Githook::Util.prefill_msg(commit_msg_file, pre_msg)
17
+ end
14
18
  end
15
19
  end
16
20
 
@@ -1,31 +1,28 @@
1
- desc 'setup hooks'
2
- task :setup do |t|
3
- Githook::Util.log(t.name)
4
-
5
- # setup 1, check whether has '.git-hooks/hooks' and '.git' folder
1
+ desc 'Setup hooks'
2
+ task :setup do
3
+ # setup 1, check whether has '.githook/hooks' and '.git' folder
6
4
  hooks_path = '.githook/hooks'
7
5
  unless Dir.exists?(hooks_path)
8
- puts "There isn't .git-hooks/hooks folder."
6
+ puts "There isn't a .githook/hooks folder."
9
7
  exit 1
10
8
  end
11
9
 
12
10
  git_path = '.git'
13
11
  unless Dir.exists?(git_path)
14
- puts "There isn't .git folder."
12
+ puts "There isn't a .git folder."
15
13
  exit 1
16
14
  end
17
15
 
18
16
  # setup 2, backup hooks
19
- Rake::Task[:backup_hooks].invoke
17
+ puts "Backup old hooks:"
18
+ Rake::Task[:backup].invoke
20
19
 
21
20
  # setup 3, copy hooks to .git/hooks
22
21
  FileUtils.cp_r(hooks_path, git_path)
23
22
  end
24
23
 
25
- desc 'backup old hooks in .git/hooks'
26
- task :backup_hooks do |t|
27
- Githook::Util.log(t.name)
28
-
24
+ desc 'Backup old hooks in .git/hooks'
25
+ task :backup do
29
26
  has_backup = false
30
27
  Dir.glob('.git/hooks/*').each do |path|
31
28
  file_name = path.split('/').last
@@ -40,19 +37,116 @@ task :backup_hooks do |t|
40
37
  puts "you can run 'rake clear_backup' to delete these backup" if has_backup
41
38
  end
42
39
 
43
- desc 'clear backup hooks in .git/hooks'
44
- task :clear_backup do |t|
45
- Githook::Util.log(t.name)
46
-
40
+ desc 'Clear backup hooks in .git/hooks'
41
+ task :clear_backup do
47
42
  backup = Dir.glob('.git/hooks/*.bak')
48
43
  Githook::Util.interactive_delete_files(backup, 'backup hooks')
49
44
  end
50
45
 
51
- desc 'clear all hooks (include backup) in .git/hooks'
52
- task :clear => :clear_backup do |t|
53
- Githook::Util.log(t.name)
46
+ # later I think we don't need to clear hooks, use disable/enable replace them
47
+ # desc 'clear all hooks (include backup) in .git/hooks'
48
+ # task :clear => :clear_backup do |t|
49
+ # Githook::Util.log(t.name)
50
+
51
+ # hooks = Dir.glob('.git/hooks/*')
52
+ # .reject { |path| path.split('/').last.include?('.') }
53
+ # Githook::Util.interactive_delete_files(hooks, 'hooks')
54
+ # end
55
+
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
+ )
54
74
 
55
- hooks = Dir.glob('.git/hooks/*')
56
- .reject { |path| path.split('/').last.include?('.') }
57
- Githook::Util.interactive_delete_files(hooks, 'hooks')
75
+ desc 'Disable hooks: HOOKS=pre_commit,commit_msg githook disable'
76
+ task :disable do
77
+ target_hooks = (ENV['HOOKS'] || '').split(',') || ALL_HOOKS
78
+ target_hooks.each do |hook|
79
+ hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
80
+ if File.file?(hook_path)
81
+ disable_path = hook_path + '.disable'
82
+ FileUtils.mv(hook_path, disable_path)
83
+ puts "Disable #{hook} hook."
84
+ else
85
+ puts "#{hook} hook doesn't exist, skip."
86
+ end
87
+ end
88
+ end
89
+
90
+ desc 'Enable hooks: HOOKS=pre_commit,commit_msg githook enable'
91
+ task :enable do
92
+ target_hooks = (ENV['HOOKS'] || '').split(',') || ALL_HOOKS
93
+ target_hooks.each do |hook|
94
+ hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
95
+ disable_path = hook_path + '.disable'
96
+ if File.file?(hook_path)
97
+ puts "#{hook} hook is arleady enabled, skip."
98
+ elsif File.file?(disable_path)
99
+ FileUtils.mv(disable_path, hook_path)
100
+ puts "Enable #{hook} hook."
101
+ else
102
+ puts "#{hook} hook doesn't exist, skip."
103
+ end
104
+ end
105
+ end
106
+
107
+ desc 'List all hooks'
108
+ task :list do
109
+ enabled_hooks = []
110
+ disabled_hooks = []
111
+ ALL_HOOKS.each do |hook|
112
+ hook_path = File.join('.git/hooks', hook.gsub('_', '-'))
113
+ disable_path = hook_path + '.disable'
114
+ if File.file?(hook_path)
115
+ enabled_hooks << hook
116
+ elsif File.file?(disable_path)
117
+ disabled_hooks << hook
118
+ end
119
+ end
120
+ puts "Enabled hooks:"
121
+ enabled_hooks.each { |h| puts " * #{h}" }
122
+ puts "Disabled hooks:"
123
+ disabled_hooks.each { |h| puts " * #{h}" }
124
+ end
125
+
126
+ desc 'Version'
127
+ task :version do
128
+ puts Githook::VERSION
129
+ end
130
+
131
+ TASKS_NAME = %w(
132
+ install
133
+ setup
134
+ backup
135
+ clear_backup
136
+ disable
137
+ enable
138
+ list
139
+ version
140
+ help
141
+ )
142
+ desc 'Help'
143
+ task :help do
144
+ puts
145
+ puts "Usage: githook task_name"
146
+ puts
147
+ puts "task_name:"
148
+ TASKS_NAME.each do |task_name|
149
+ task = Rake::Task[task_name]
150
+ puts " #{task_name.ljust(13)} -- #{task.comment}"
151
+ end
58
152
  end
@@ -7,7 +7,11 @@ module Githook
7
7
  def self.run_tasks(hook_stage)
8
8
  tasks = fetch(hook_stage, [])
9
9
  tasks.each do |task|
10
- Rake::Task[task].invoke
10
+ if Rake::Task.task_defined?(task)
11
+ Rake::Task[task].invoke
12
+ else
13
+ puts "#{task} task doesn't exist."
14
+ end
11
15
  end
12
16
  end
13
17
 
@@ -1,3 +1,3 @@
1
1
  module Githook
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
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-02 00:00:00.000000000 Z
11
+ date: 2017-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler