git-hooks 0.5.0 → 0.6.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1eb1b43db79e2fee7f1e80b606a5f179d9ef6e01
4
- data.tar.gz: 4c406f7fa4c304979c38479a7b7e3aee4e3775d1
3
+ metadata.gz: 75034e9d934e19aee6f2737af93677be3aff90cd
4
+ data.tar.gz: a619eaf1a0d4e4b67b13c14537ec937592ee29bf
5
5
  SHA512:
6
- metadata.gz: 8e738a492bf589e366da29a5355a85c54c5d7990e369b0479f2cd3a325b7c78b619bd1d0df26fb25932fdcc19ca30ae600699ffd9b9689f58b26d57f897a4eb8
7
- data.tar.gz: ce5edbedf28705755913ef38b183ad64d8e60fe58dec7837be37350ff0779aad3c49213932ced1c7d755f476bc9e355b1142150702ee9904c05374c902dcebbe
6
+ metadata.gz: 5695f03af3ffbde6a7991a0f9845fca7d5184b329485a9c2e66254eebf339f13c8905c6c3d7b882cff61d6fc6ba7e5be3e8fb2a9ac7d337f5cef56d362db30c0
7
+ data.tar.gz: 43154d5d0cb3f0ca6efcf6ab6c205f749f128034fc2386039efc6cfb4a4fdc0d18815861c7fa31ede8e15654079c6615b3e4a8d8f4dad54d710aa008c1993be4
data/README.md CHANGED
@@ -63,7 +63,24 @@ application's start-up code (e.g. `config/environments/development.rb` or
63
63
  GitHooks.validate_hooks!
64
64
  ```
65
65
 
66
- This will force `git_hooks` installation before your application's start.
66
+ This will force `git_hooks` installation before you can run your application. Be
67
+ sure not to call `GitHooks#validate_hooks!` on production environments though!
68
+
69
+ ### Problems with ruby version
70
+
71
+ If you run `git` under other systems such as `gitk` or Emacs' `Magit`, you may
72
+ encounter problems with the ruby version being used to run `GitHooks`. This
73
+ happens because those applications don't source the `~/.bashrc` file, which is
74
+ required by ruby version managers such as `Rbenv` and `Rvm`.
75
+
76
+ In order to fix this problem, you can install the hooks by passing your ruby
77
+ path to the `--ruby_path` option. For example:
78
+
79
+ ```sh
80
+ $ git_hooks install pre-commit --ruby_path `which ruby`
81
+ ```
82
+
83
+ You can also manually edit the `.git/hooks/{hook-name}` file though.
67
84
 
68
85
  ## Contributing
69
86
 
@@ -6,13 +6,15 @@ module GitHooks
6
6
  class CLI < Thor
7
7
  desc 'install HOOK', 'Install some hook'
8
8
  option :force, type: :boolean
9
+ option :ruby_path, type: :string
9
10
  long_desc <<-LONGDESC
10
11
  Install some hook:
11
12
 
12
13
  $ git_hooks install pre-commit
13
14
  LONGDESC
14
15
  def install(hook)
15
- GitHooks::Installer.new(hook).install(options[:force])
16
+ GitHooks::Installer.new(hook, options[:ruby_path])
17
+ .install(options[:force])
16
18
  end
17
19
 
18
20
  desc 'Init GitHooks on current folder', 'Create a configuration file'
@@ -1,8 +1,11 @@
1
1
  module GitHooks
2
2
  module Exceptions
3
- class UnknowHookPresent < RuntimeError
3
+ class UnknownHookPresent < RuntimeError
4
4
  def initialize(hook)
5
- super "There is a unknown #{hook} hook. If you are sure, use --force."
5
+ super <<-HEREDOC
6
+ There is already a #{hook} hook installed.
7
+ If you want to override it, use the --force option.
8
+ HEREDOC
6
9
  end
7
10
  end
8
11
  end
@@ -1,33 +1,44 @@
1
1
  module GitHooks
2
2
  class Installer
3
- attr_accessor :hook
4
-
5
3
  HOOK_SAMPLE_FILE = 'hook.sample'
6
4
 
7
- def initialize(hook)
5
+ def initialize(hook, ruby_path = nil)
8
6
  @hook = hook
7
+ @ruby_path = ruby_path
9
8
  end
10
9
 
11
10
  def install(force = false)
12
- return true if installed?
11
+ throw Exceptions::UnknownHookPresent.new(hook) if !force && installed?
12
+
13
+ hook_script = hook_template
14
+ hook_script.gsub!('/usr/bin/env ruby', ruby_path) if ruby_path
15
+
16
+ File
17
+ .open(hook_path, 'w')
18
+ .write(hook_script)
13
19
 
14
- FileUtils.symlink(
15
- hook_template_path, File.join('.git', 'hooks', hook), force: force
16
- )
17
- rescue Errno::EEXIST
18
- raise GitHooks::Exceptions::UnknowHookPresent, hook
20
+ FileUtils.chmod(0775, hook_path)
19
21
  end
20
22
 
21
23
  def installed?
22
- hook_file = File.join(Dir.pwd, '.git', 'hooks', hook)
23
-
24
- File.symlink?(hook_file) && File.realpath(hook_file) == hook_template_path
24
+ File.exist?(hook_path) &&
25
+ File.read(hook_path).match(/GitHooks.execute_pre_commits/)
25
26
  end
26
27
 
27
28
  private
28
29
 
30
+ attr_accessor :hook, :ruby_path
31
+
32
+ def hook_template
33
+ File.read(hook_template_path)
34
+ end
35
+
29
36
  def hook_template_path
30
37
  File.join(GitHooks.base_path, HOOK_SAMPLE_FILE)
31
38
  end
39
+
40
+ def hook_path
41
+ File.join('.git', 'hooks', hook)
42
+ end
32
43
  end
33
44
  end
@@ -1,3 +1,3 @@
1
1
  module GitHooks
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0.pre1'
3
3
  end
@@ -3,68 +3,55 @@ describe GitHooks::Installer do
3
3
 
4
4
  let(:hook) { 'pre-commit' }
5
5
 
6
- let(:hook_real_path) do
6
+ let(:hook_template_path) do
7
7
  File.join(GitHooks.base_path, 'hook.sample')
8
8
  end
9
9
 
10
- let(:absolute_path) do
11
- File.join(GitHooks.base_path, '.git', 'hooks', hook)
10
+ let(:hook_path) do
11
+ File.join('.git', 'hooks', hook)
12
12
  end
13
13
 
14
14
  let(:git_hook_path) { ".git/hooks/#{hook}" }
15
+ let(:file) { instance_double(File, write: true) }
16
+
17
+ before do
18
+ allow(File).to receive(:open).and_return(file)
19
+ allow(FileUtils).to receive(:chmod).and_return(true)
20
+ end
15
21
 
16
22
  describe '#install' do
17
23
  subject(:install) { installer.install }
18
24
 
25
+ let(:installed?) { false }
26
+
19
27
  before do
20
- allow(installer).to receive(:installed?).and_return(false)
21
- allow(FileUtils).to receive(:symlink).and_return(true)
28
+ allow(installer).to receive(:installed?)
29
+ .and_return(installed?)
22
30
  end
23
31
 
24
- it 'creates symlink' do
25
- expect(FileUtils)
26
- .to receive(:symlink)
27
- .with(hook_real_path, git_hook_path, force: false)
32
+ it 'creates a new file' do
33
+ expect(File).to receive(:open)
34
+ .with(hook_path, 'w')
35
+
36
+ expect(file).to receive(:write)
37
+ .with(/GitHooks.execute_pre_commits/)
28
38
 
29
39
  install
30
40
  end
31
41
 
32
42
  it { is_expected.to be_truthy }
33
43
 
34
- context 'when there is a unknown hook' do
35
- before do
36
- allow(FileUtils).to receive(:symlink).and_raise(Errno::EEXIST)
37
- end
38
-
39
- it 'raises an error' do
40
- expect { install }.to raise_error(
41
- GitHooks::Exceptions::UnknowHookPresent
42
- )
43
- end
44
- end
45
-
46
44
  context 'when there is a unknown hook but I want to force installation' do
47
- subject(:install) { installer.install true }
45
+ subject(:install) { installer.install(true) }
48
46
 
49
- it 'creates symlink with force option' do
50
- expect(FileUtils)
51
- .to receive(:symlink)
52
- .with(hook_real_path, git_hook_path, force: true)
53
-
54
- install
55
- end
47
+ let(:installed?) { true }
56
48
 
57
- it { is_expected.to be_truthy }
58
- end
59
-
60
- context 'when hook is already installed' do
61
- before do
62
- allow(installer).to receive(:installed?).and_return(true)
63
- end
49
+ it 'creates symlink with force option' do
50
+ expect(File).to receive(:open)
51
+ .with(hook_path, 'w')
64
52
 
65
- it 'does not create symlink' do
66
- expect(FileUtils)
67
- .to_not receive(:symlink)
53
+ expect(file).to receive(:write)
54
+ .with(/GitHooks.execute_pre_commits/)
68
55
 
69
56
  install
70
57
  end
@@ -76,29 +63,22 @@ describe GitHooks::Installer do
76
63
  describe '#installed?' do
77
64
  subject(:installed?) { installer.installed? }
78
65
 
79
- let(:symlink?) { true }
80
-
81
66
  before do
82
- allow(File).to receive(:symlink?).and_return(symlink?)
83
- allow(File).to receive(:realpath).and_return(hook_real_path)
84
- end
85
-
86
- it 'validates file is a symlink' do
87
- expect(File).to receive(:symlink?).with(absolute_path)
88
- installed?
67
+ allow(File).to receive(:read)
68
+ .and_return('GitHooks.execute_pre_commits')
89
69
  end
90
70
 
91
71
  it { is_expected.to be_truthy }
92
72
 
93
- context 'when file is not a symlink' do
94
- let(:symlink?) { false }
95
-
96
- it { is_expected.to be_falsy }
73
+ it do
74
+ expect(File).to receive(:exist?).with(hook_path)
75
+ installed?
97
76
  end
98
77
 
99
- context 'when is not the GitHooks file' do
78
+ context 'when the hook does not validate pre_commits' do
100
79
  before do
101
- allow(File).to receive(:realpath).and_return('/tmp/foo')
80
+ allow(File).to receive(:read)
81
+ .and_return('echo yolo')
102
82
  end
103
83
 
104
84
  it { is_expected.to be_falsy }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael da Silva Almeida
@@ -193,9 +193,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
193
193
  version: '0'
194
194
  required_rubygems_version: !ruby/object:Gem::Requirement
195
195
  requirements:
196
- - - ">="
196
+ - - ">"
197
197
  - !ruby/object:Gem::Version
198
- version: '0'
198
+ version: 1.3.1
199
199
  requirements: []
200
200
  rubyforge_project:
201
201
  rubygems_version: 2.2.2