git-hook 0.1.0 → 0.1.1

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: de19dce608753a5f44f2cded224fa71a86d4b9af
4
- data.tar.gz: e94a4ea4e8c749e6acaabd4987d5a6f2de2e46b7
3
+ metadata.gz: 65d3e00ec19974b20450b6c052f4bbbc57eca0a1
4
+ data.tar.gz: 89525dbe7fededbc2bee68b46b29c03489535125
5
5
  SHA512:
6
- metadata.gz: 976300f7c1cb7826321132a9f095adc76d85660d4c6ea432e45bfa92b3b85bf8c8e508f9d5eb2dd70e2ed3bc1f5c35475c4da2fbc8108b1f11dd0a3a1a2f961e
7
- data.tar.gz: 9429b31337657ded853d3205291254cecec72eaf7da31741a802c0bbfd3c961c42d8363c6cfacde8bdd8a2051fa41b694cced74867725baffd32eb1578e211e4
6
+ metadata.gz: 7ec386606137eeb958e85cc1398649be9050cb570d644e81bb10e7a8539495c708d0418576c0cd9bfd0b96346b8c3027d4084e2e0cc17c3756d3025bce3d3ba9
7
+ data.tar.gz: 9cdb75174577473f3632cfb992d08a15a9e1930fd23b7551e2a16f99199275f0d1bed68e5153e0463110a2ee051220ddf5f3dad66c6eee3980c9095431e4d003
data/.gitignore CHANGED
@@ -10,3 +10,9 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ # built gem file
15
+ *.gem
16
+
17
+ # hooks
18
+ .githook
data/README.md CHANGED
@@ -1,15 +1,13 @@
1
1
  # Githook
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/githook`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A ruby gem that help to setup git hooks easily, base on Rake, inspired from Capistrano.
6
4
 
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
9
  ```ruby
12
- gem 'githook'
10
+ gem 'git-hook'
13
11
  ```
14
12
 
15
13
  And then execute:
@@ -18,24 +16,15 @@ And then execute:
18
16
 
19
17
  Or install it yourself as:
20
18
 
21
- $ gem install githook
19
+ $ gem install git-hook
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/githook. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
23
+ $ githook install
24
+ $ githook setup
36
25
 
26
+ More: TODO
37
27
 
38
28
  ## License
39
29
 
40
30
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
data/bin/githook ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "githook"
4
+
5
+ task_name = ARGV[0]
6
+ begin
7
+ Rake::Task[task_name].invoke
8
+ rescue => e
9
+ puts "Error: #{e.message}"
10
+ end
data/build-install.sh ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ # for build and install easier to test in local
4
+ rm -f *.gem
5
+ gem build githook.gemspec
6
+ target=`ls *.gem`
7
+ gem install $target
data/lib/githook.rb CHANGED
@@ -1,4 +1,9 @@
1
+ require "rake"
1
2
  require "githook/version"
3
+ require "githook/context"
4
+ require "githook/util"
5
+ require "githook/tasks"
6
+ require "githook/config"
2
7
 
3
8
  module Githook
4
9
  # Your code goes here...
@@ -0,0 +1,3 @@
1
+ if ARGV[0] != "install"
2
+ load ".githook/config.rb"
3
+ end
@@ -0,0 +1,21 @@
1
+ module Githook
2
+ class Context
3
+ def self.set(key, value)
4
+ @env ||= {}
5
+ @env[key] = value
6
+ end
7
+
8
+ def self.fetch(key, def_val)
9
+ @env ||= {}
10
+ @env[key] || def_val
11
+ end
12
+ end
13
+ end
14
+
15
+ def set(key, value)
16
+ Githook::Context.set(key, value)
17
+ end
18
+
19
+ def fetch(key, def_val)
20
+ Githook::Context.fetch(key, def_val)
21
+ end
@@ -0,0 +1,11 @@
1
+ # 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
4
+ Dir.glob(rake_files_pattern).each { |r| load r }
5
+
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
+ end
@@ -0,0 +1,21 @@
1
+ namespace :commit_msg do
2
+ desc 'check commit msg style'
3
+ task :check_msg do |t|
4
+ Githook::Util.log(t.name)
5
+
6
+ commit_msg_file = '.git/COMMIT_EDITMSG'
7
+ commit_msg = Githook::Util.get_commit_msg(commit_msg_file)
8
+ puts "commit-msg: #{commit_msg}"
9
+ exit 0 if Githook::Util.expected_msg_format?(commit_msg)
10
+
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
+ end
15
+ end
16
+
17
+ desc 'run all commit-msg hook tasks'
18
+ task :commit_msg do |t|
19
+ Githook::Util.log(t.name)
20
+ Githook::Util.run_tasks(t.name.to_sym)
21
+ end
@@ -0,0 +1,22 @@
1
+ task :install do |t|
2
+ # step 1, check whether Dir.pwd is in git repo root folder
3
+ git_path = ".git"
4
+ unless Dir.exists?(git_path)
5
+ puts "It isn't in a git repo root folder."
6
+ exit 1
7
+ end
8
+
9
+ # step 2, check whether '.githook' folder already exists
10
+ githook_path = ".githook"
11
+ if Dir.exists?(githook_path)
12
+ print ".githook already exists, do you want to override it? [y/(n)]: "
13
+ choice = STDIN.gets
14
+ exit 0 if ["n", "N", "\n"].include?(choice[0])
15
+ FileUtils.rm_r(githook_path)
16
+ end
17
+
18
+ # setup 3, copy templates to .githook
19
+ templates_path = File.expand_path("../../templates", __FILE__)
20
+ FileUtils.cp_r(templates_path, githook_path)
21
+ puts "Create .githook folder."
22
+ end
@@ -0,0 +1,19 @@
1
+ namespace :pre_commit do
2
+ desc 'check ruby code style by rubocop'
3
+ task :rubocop do |t|
4
+ Githook::Util.log(t.name)
5
+ exit 1 unless system("bundle exec rubocop")
6
+ end
7
+
8
+ desc 'test by rspec'
9
+ task :rspec do |t|
10
+ Githook::Util.log(t.name)
11
+ exit 1 unless system("bundle exec rspec")
12
+ end
13
+ end
14
+
15
+ desc 'run all pre-commit hook tasks'
16
+ task :pre_commit do |t|
17
+ Githook::Util.log(t.name)
18
+ Githook::Util.run_tasks(t.name.to_sym)
19
+ end
@@ -0,0 +1,21 @@
1
+ namespace :prepare_commit_msg do
2
+ desc 'prepare commit msg'
3
+ task :prepare do |t|
4
+ Githook::Util.log(t.name)
5
+
6
+ commit_msg_file = '.git/COMMIT_EDITMSG'
7
+ # can't use return in block
8
+ exit 0 unless Githook::Util.commit_msg_empty?(commit_msg_file)
9
+
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)
14
+ end
15
+ end
16
+
17
+ desc 'run all prepare-commit-msg hook tasks'
18
+ task :prepare_commit_msg do |t|
19
+ Githook::Util.log(t.name)
20
+ Githook::Util.run_tasks(t.name.to_sym)
21
+ end
@@ -0,0 +1,58 @@
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
6
+ hooks_path = '.githook/hooks'
7
+ unless Dir.exists?(hooks_path)
8
+ puts "There isn't .git-hooks/hooks folder."
9
+ exit 1
10
+ end
11
+
12
+ git_path = '.git'
13
+ unless Dir.exists?(git_path)
14
+ puts "There isn't .git folder."
15
+ exit 1
16
+ end
17
+
18
+ # setup 2, backup hooks
19
+ Rake::Task[:backup_hooks].invoke
20
+
21
+ # setup 3, copy hooks to .git/hooks
22
+ FileUtils.cp_r(hooks_path, git_path)
23
+ end
24
+
25
+ desc 'backup old hooks in .git/hooks'
26
+ task :backup_hooks do |t|
27
+ Githook::Util.log(t.name)
28
+
29
+ has_backup = false
30
+ Dir.glob('.git/hooks/*').each do |path|
31
+ file_name = path.split('/').last
32
+ next if file_name.include?('.')
33
+
34
+ appendix = ".#{Time.now.strftime("%Y%m%d%H%m%S")}.bak"
35
+ puts "Backup old #{file_name} to #{file_name}#{appendix}"
36
+ FileUtils.cp(path, "#{path}#{appendix}")
37
+ has_backup = true
38
+ end
39
+
40
+ puts "you can run 'rake clear_backup' to delete these backup" if has_backup
41
+ end
42
+
43
+ desc 'clear backup hooks in .git/hooks'
44
+ task :clear_backup do |t|
45
+ Githook::Util.log(t.name)
46
+
47
+ backup = Dir.glob('.git/hooks/*.bak')
48
+ Githook::Util.interactive_delete_files(backup, 'backup hooks')
49
+ end
50
+
51
+ desc 'clear all hooks (include backup) in .git/hooks'
52
+ task :clear => :clear_backup do |t|
53
+ Githook::Util.log(t.name)
54
+
55
+ hooks = Dir.glob('.git/hooks/*')
56
+ .reject { |path| path.split('/').last.include?('.') }
57
+ Githook::Util.interactive_delete_files(hooks, 'hooks')
58
+ end
@@ -0,0 +1,11 @@
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
+ # )
6
+ set :prepare_commit_msg, fetch(:prepare_commit_msg, []).push(
7
+ 'prepare_commit_msg:prepare'
8
+ )
9
+ set :commit_msg, fetch(:commit_msg, []).push(
10
+ 'commit_msg:check_msg'
11
+ )
@@ -0,0 +1,27 @@
1
+ #!/bin/sh
2
+ #
3
+ # An example hook script to check the commit log message.
4
+ # Called by "git commit" with one argument, the name of the file
5
+ # that has the commit message. The hook should exit with non-zero
6
+ # status after issuing an appropriate message if it wants to stop the
7
+ # commit. The hook is allowed to edit the commit message file.
8
+ #
9
+ # To enable this hook, rename this file to "commit-msg".
10
+
11
+ # Uncomment the below to add a Signed-off-by line to the message.
12
+ # Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13
+ # hook is more suited to it.
14
+ #
15
+ # SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16
+ # grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17
+
18
+ # This example catches duplicate Signed-off-by lines.
19
+
20
+ test "" = "$(grep '^Signed-off-by: ' "$1" |
21
+ sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
22
+ echo >&2 Duplicate Signed-off-by lines.
23
+ exit 1
24
+ }
25
+
26
+ # custom commit-msg hooks
27
+ githook commit_msg
@@ -0,0 +1,55 @@
1
+ #!/bin/sh
2
+ #
3
+ # An example hook script to verify what is about to be committed.
4
+ # Called by "git commit" with no arguments. The hook should
5
+ # exit with non-zero status after issuing an appropriate message if
6
+ # it wants to stop the commit.
7
+ #
8
+ # To enable this hook, rename this file to "pre-commit".
9
+
10
+ if git rev-parse --verify HEAD >/dev/null 2>&1
11
+ then
12
+ against=HEAD
13
+ else
14
+ # Initial commit: diff against an empty tree object
15
+ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16
+ fi
17
+
18
+ # If you want to allow non-ASCII filenames set this variable to true.
19
+ allownonascii=$(git config --bool hooks.allownonascii)
20
+
21
+ # Redirect output to stderr.
22
+ exec 1>&2
23
+
24
+ # custom pre-commit hooks
25
+ githook pre_commit
26
+ if [ $? -ne 0 ]; then
27
+ exit 1
28
+ fi
29
+
30
+ # Cross platform projects tend to avoid non-ASCII filenames; prevent
31
+ # them from being added to the repository. We exploit the fact that the
32
+ # printable range starts at the space character and ends with tilde.
33
+ if [ "$allownonascii" != "true" ] &&
34
+ # Note that the use of brackets around a tr range is ok here, (it's
35
+ # even required, for portability to Solaris 10's /usr/bin/tr), since
36
+ # the square bracket bytes happen to fall in the designated range.
37
+ test $(git diff --cached --name-only --diff-filter=A -z $against |
38
+ LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
39
+ then
40
+ cat <<\EOF
41
+ Error: Attempt to add a non-ASCII file name.
42
+
43
+ This can cause problems if you want to work with people on other platforms.
44
+
45
+ To be portable it is advisable to rename the file.
46
+
47
+ If you know what you are doing you can disable this check using:
48
+
49
+ git config hooks.allownonascii true
50
+ EOF
51
+ exit 1
52
+ fi
53
+
54
+ # If there are whitespace errors, print the offending file names and fail.
55
+ exec git diff-index --check --cached $against --
@@ -0,0 +1,39 @@
1
+ #!/bin/sh
2
+ #
3
+ # An example hook script to prepare the commit log message.
4
+ # Called by "git commit" with the name of the file that has the
5
+ # commit message, followed by the description of the commit
6
+ # message's source. The hook's purpose is to edit the commit
7
+ # message file. If the hook fails with a non-zero status,
8
+ # the commit is aborted.
9
+ #
10
+ # To enable this hook, rename this file to "prepare-commit-msg".
11
+
12
+ # This hook includes three examples. The first comments out the
13
+ # "Conflicts:" part of a merge commit.
14
+ #
15
+ # The second includes the output of "git diff --name-status -r"
16
+ # into the message, just before the "git status" output. It is
17
+ # commented because it doesn't cope with --amend or with squashed
18
+ # commits.
19
+ #
20
+ # The third example adds a Signed-off-by line to the message, that can
21
+ # still be edited. This is rarely a good idea.
22
+
23
+ case "$2,$3" in
24
+ merge,)
25
+ /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
26
+
27
+ # ,|template,)
28
+ # /usr/bin/perl -i.bak -pe '
29
+ # print "\n" . `git diff --cached --name-status -r`
30
+ # if /^#/ && $first++ == 0' "$1" ;;
31
+
32
+ *) ;;
33
+ esac
34
+
35
+ # SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
36
+ # grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
37
+
38
+ # custom prepare-commit-msg hooks
39
+ githook prepare_commit_msg
@@ -0,0 +1,8 @@
1
+ # define yourself tasks here
2
+ # or put them in a new .rake file
3
+ #
4
+ # namespace :pre_commit do
5
+ # task :checkstyle do
6
+ # # ...
7
+ # end
8
+ # end
@@ -0,0 +1,96 @@
1
+ module Githook
2
+ class Util
3
+ def self.log(task_name)
4
+ puts "[#{Time.now.strftime('%H:%m:%S')}] #{task_name.gsub('_', ' ')}"
5
+ end
6
+
7
+ def self.run_tasks(hook_stage)
8
+ tasks = fetch(hook_stage, [])
9
+ tasks.each do |task|
10
+ Rake::Task[task].invoke
11
+ end
12
+ end
13
+
14
+ #######################################################
15
+
16
+ def self.interactive_delete_files(path_arr, type)
17
+ if path_arr.length == 0
18
+ puts "There are no #{type}."
19
+ else
20
+ puts "There are following #{type}:"
21
+ puts path_arr
22
+ print "Are you sure want to delete all of them [(y)/n]: "
23
+ # https://stackoverflow.com/a/40643667/2998877
24
+ choice = STDIN.gets
25
+ if ["Y", "y", "\n"].include?(choice[0])
26
+ path_arr.each do |path|
27
+ FileUtils.rm(path)
28
+ puts "Delete #{path}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ #######################################################
35
+
36
+ # check whether origin commit msg is empty
37
+ def self.commit_msg_empty?(commit_msg_file)
38
+ File.open(commit_msg_file, 'r') do |f|
39
+ f.readlines.each do |line|
40
+ strip_line = line.strip
41
+ return false if (!strip_line.empty? && !strip_line.start_with?('#'))
42
+ end
43
+ end
44
+ true
45
+ end
46
+
47
+ BRANCH_NAME_REG = /^(feature|bug|hotfix|misc|refactor)\/(\d*)?(\w*)/
48
+ # generate pre msg according branch name
49
+ def self.gen_pre_msg(branch_name)
50
+ match_group = BRANCH_NAME_REG.match(branch_name)
51
+ if match_group
52
+ issue_type = match_group[1].upcase
53
+ issue_num = match_group[2]
54
+ issue_content = match_group[3]
55
+
56
+ issue_type = 'BUG' if issue_type == 'HOTFIX'
57
+ issue_num = " \##{issue_num}" unless issue_num.empty?
58
+ issue_content = issue_content.tr('_', ' ').strip.capitalize
59
+
60
+ "#{issue_type}#{issue_num} - #{issue_content}"
61
+ else
62
+ 'MISC - '
63
+ end
64
+ end
65
+
66
+ # write the pre msg at the begining of commit_msg_file
67
+ def self.prefill_msg(commit_msg_file, pre_msg)
68
+ File.open(commit_msg_file, 'r+') do |f|
69
+ ori_content = f.read
70
+ f.seek(0, IO::SEEK_SET)
71
+ f.puts pre_msg
72
+ f.puts ori_content
73
+ end
74
+ end
75
+
76
+ def self.get_commit_msg(commit_msg_file)
77
+ commit_msg = ''
78
+ File.open(commit_msg_file, 'r') do |f|
79
+ f.readlines.each do |line|
80
+ strip_line = line.strip
81
+ if !strip_line.empty? && !strip_line.start_with?('#')
82
+ commit_msg = line
83
+ break
84
+ end
85
+ end
86
+ end
87
+ commit_msg
88
+ end
89
+
90
+ MSG_FORMAT_REG = /^(FEATURE|BUG|MISC|REFACTOR)(\s#\d+)* - ([A-Z].*)/
91
+ # check commit msg style
92
+ def self.expected_msg_format?(commit_msg)
93
+ commit_msg.start_with?('Merge branch') || MSG_FORMAT_REG.match(commit_msg)
94
+ end
95
+ end
96
+ end
@@ -1,3 +1,3 @@
1
1
  module Githook
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - baurine
@@ -56,8 +56,7 @@ description: Help to setup git hooks easily, base on Rake, inspired from Capistr
56
56
  email:
57
57
  - 2008.hbl@gmail.com
58
58
  executables:
59
- - console
60
- - setup
59
+ - githook
61
60
  extensions: []
62
61
  extra_rdoc_files: []
63
62
  files:
@@ -68,10 +67,24 @@ files:
68
67
  - LICENSE.txt
69
68
  - README.md
70
69
  - Rakefile
71
- - bin/console
72
- - bin/setup
70
+ - bin/githook
71
+ - build-install.sh
73
72
  - githook.gemspec
74
73
  - lib/githook.rb
74
+ - lib/githook/config.rb
75
+ - lib/githook/context.rb
76
+ - lib/githook/tasks.rb
77
+ - lib/githook/tasks/commit-msg.rake
78
+ - lib/githook/tasks/install.rake
79
+ - lib/githook/tasks/pre-commit.rake
80
+ - lib/githook/tasks/prepare-commit-msg.rake
81
+ - lib/githook/tasks/setup.rake
82
+ - lib/githook/templates/config.rb
83
+ - lib/githook/templates/hooks/commit-msg
84
+ - lib/githook/templates/hooks/pre-commit
85
+ - lib/githook/templates/hooks/prepare-commit-msg
86
+ - lib/githook/templates/tasks/task.rake
87
+ - lib/githook/util.rb
75
88
  - lib/githook/version.rb
76
89
  homepage: https://github.com/baurine/githook
77
90
  licenses:
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "githook"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here