hookspec 1.0.1 → 1.1.0
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 +4 -4
- data/README.md +37 -13
- data/lib/hookspec/git_env.rb +18 -51
- data/lib/hookspec/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 841483393b2aee20b0bf2547b0f8ccbfc36ccafe
|
4
|
+
data.tar.gz: c34b2e0bd560c367da38b76be5559c464bc1a6b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6dcf1d20a384fbae7c42fdafb9564d4649f50e4a66bb8d9ca4b161947a1e6c961adf0f68c810a6715547d7dc571d990f5600f31a149510b8e4377cd63717b073
|
7
|
+
data.tar.gz: 3b4cf3fe649ec433932608823b212390e9b0928fe596c80bc1c381a197a82fb71912683a624633cb5e9786681fcb6356641c353fe0c3d0257822ab53c712f69e
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@ hookspec
|
|
3
3
|
|
4
4
|
## Lastest version
|
5
5
|
|
6
|
-
v1.
|
6
|
+
v1.1.0(beta)
|
7
7
|
|
8
8
|
## About
|
9
9
|
|
@@ -25,15 +25,37 @@ After hookspec gem is installed, you can use 'hookspec' command. Execute the com
|
|
25
25
|
|
26
26
|
New a spec file under git\_hooks/[hook\_type]/ named like _FILENAME_\_spec.rb
|
27
27
|
|
28
|
-
|
29
|
-
require 'hookspec'
|
28
|
+
### Sample: Check push branch
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
```ruby
|
31
|
+
# assert branch is not master branch
|
32
|
+
require 'hookspec'
|
33
|
+
|
34
|
+
describe "push" do
|
35
|
+
let (:git_env) {HookSpec::GitEnv.new}
|
36
|
+
it 'master branch can not be pushed' do
|
37
|
+
expect(git_env).not_to be_branch('master')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Sample: Check with rubocop
|
43
|
+
|
44
|
+
`install rubocop first`
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
describe "rubocop check" do
|
48
|
+
let (:git_env) {HookSpec::GitEnv.new}
|
49
|
+
it 'rubocop must pass' do
|
50
|
+
git_env.diff(model: 'cached', filter: 'AM').each do |f|
|
51
|
+
if f.end_with?('.rb')
|
52
|
+
`rubocop #{f}`
|
53
|
+
expect($?).to be_nil
|
35
54
|
end
|
36
55
|
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
37
59
|
|
38
60
|
## Supported hook\_type
|
39
61
|
|
@@ -62,15 +84,17 @@ New a spec file under git\_hooks/[hook\_type]/ named like _FILENAME_\_spec.rb
|
|
62
84
|
|
63
85
|
#### how to validate include certain file or not
|
64
86
|
|
65
|
-
it '
|
66
|
-
expect(git_env.
|
87
|
+
it 'commit not delete certain files' do
|
88
|
+
expect(git_env.diff(model: 'cached', filter: 'D').not_to include_file('README.md')
|
67
89
|
end
|
68
90
|
|
69
|
-
|
91
|
+
* use git\_env.diff(model: `model_name`, filter: `filters`)
|
92
|
+
* filters: A|C|D|M|R|T|U|X|B
|
70
93
|
|
71
|
-
|
72
|
-
|
73
|
-
|
94
|
+
```ruby
|
95
|
+
# get cached file list with filter 'AM'
|
96
|
+
git_env.diff(model: 'cached', filter: 'AM')
|
97
|
+
```
|
74
98
|
|
75
99
|
#### how to get git configure
|
76
100
|
|
data/lib/hookspec/git_env.rb
CHANGED
@@ -4,84 +4,51 @@ module HookSpec
|
|
4
4
|
AGAINST = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
|
5
5
|
|
6
6
|
def initialize
|
7
|
-
@current_branch = nil
|
8
|
-
@cached_diffs_info = nil
|
9
|
-
@commit_diffs_info = nil
|
10
|
-
@local_diffs_info = nil
|
11
|
-
@all_files = nil
|
12
7
|
end
|
13
8
|
|
14
9
|
def current_branch
|
15
|
-
|
16
|
-
@current_branch = `git rev-parse --abbrev-ref HEAD`.chomp!
|
10
|
+
`git rev-parse --abbrev-ref HEAD`.chomp
|
17
11
|
end
|
18
12
|
|
19
13
|
def files
|
20
|
-
|
21
|
-
@all_files = `git ls-files`.chomp!.split
|
14
|
+
`git ls-files`.chomp.split
|
22
15
|
end
|
23
16
|
|
24
|
-
# diff
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
return @cached_diffs_info unless @cached_diffs_info.nil?
|
36
|
-
|
37
|
-
diff = `git diff --cached --name-status`.chomp!
|
38
|
-
@cached_diffs_info = classify(diff)
|
39
|
-
end
|
40
|
-
|
41
|
-
def diffs_of_local
|
42
|
-
return @local_diffs_info unless @local_diffs_info.nil?
|
43
|
-
|
44
|
-
diff = `git diff --name-status`.chomp!
|
45
|
-
@local_diffs_info = classify(diff)
|
17
|
+
# git diff {--cached} --name-only {--diff-filter=}
|
18
|
+
def diff model: nil, filter: nil
|
19
|
+
cmd = 'git diff '
|
20
|
+
if !model.nil? && model.downcase == 'cached'
|
21
|
+
cmd = cmd + '--cached '
|
22
|
+
end
|
23
|
+
cmd = cmd + '--name-only '
|
24
|
+
if !filter.nil?
|
25
|
+
cmd = cmd + '--diff-filter=' + filter
|
26
|
+
end
|
27
|
+
`#{cmd}`.chomp.split
|
46
28
|
end
|
47
29
|
|
48
|
-
|
49
30
|
# git confs
|
50
31
|
def conf_of conf_name
|
51
|
-
|
32
|
+
`git config #{conf_name}`.chomp
|
52
33
|
end
|
53
34
|
|
54
35
|
def white_spaces
|
55
36
|
against = get_against
|
56
37
|
|
57
|
-
|
38
|
+
`git diff-index --check --cached #{against} --`.chomp
|
58
39
|
end
|
59
40
|
|
60
41
|
def exec cmd
|
61
|
-
|
42
|
+
`#{cmd}`.chomp
|
62
43
|
end
|
63
44
|
|
64
45
|
private
|
65
46
|
|
66
|
-
def classify diffs
|
67
|
-
classified = {}
|
68
|
-
diffs.each_line(separator = $/) do |line|
|
69
|
-
Hash[*line.split].each_pair do |key, value|
|
70
|
-
if classified[key.to_sym].nil?
|
71
|
-
classified[key.to_sym] = [value]
|
72
|
-
else
|
73
|
-
classified[key.to_sym] << value
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
return classified
|
78
|
-
end
|
79
|
-
|
80
47
|
def get_against
|
81
48
|
if `git rev-parse --verify HEAD >/dev/null 2>&1`
|
82
|
-
|
49
|
+
'HEAD'
|
83
50
|
else
|
84
|
-
|
51
|
+
AGAINST
|
85
52
|
end
|
86
53
|
end
|
87
54
|
end
|
data/lib/hookspec/version.rb
CHANGED