overcommit 0.12.0 → 0.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24385419d31ec2658a5088bbc7805f993e78f55d
4
- data.tar.gz: 9532a577df43ec23f97c045f8539dd9d44a555f6
3
+ metadata.gz: f50342c0638e20584a726340b172131a24085be4
4
+ data.tar.gz: 64cb433bf1f877cda8257ae840bed4588594ca2b
5
5
  SHA512:
6
- metadata.gz: 6573a5e8d63690e215e693d6a0d1a920632a1687bb008c689b0aae0e4ab563cc7668214cbdfc2002eb55efb9f304334a7b82dd053d7f90829d33cdf915626276
7
- data.tar.gz: 0e32d2d386baf428288518982dbc52c1751d1d8a90e7e806f634f7e368aa0a698892b43f86ec684aa96168218ba66e8dff37455dbe475fe3e9bd25a069ff27b0
6
+ metadata.gz: 8520683843161c250dba3e94fb15e4222cc6f0e2d3d8c8e63763e2a6837a50c30ce478f6d86699817950f120fdb472eef0cd9816f48cbfc23876d2355bacec84
7
+ data.tar.gz: c961cadf99ce02e0766f68ea53b6bd4f7b403ecfa0c048821c130d38b4778066a70ae7945b27e6a3342b0271ba7014a56a4920722c7b594809c4a7b8772c7ce1
data/config/default.yml CHANGED
@@ -54,6 +54,12 @@ PreCommit:
54
54
  required: true
55
55
  quiet: true
56
56
 
57
+ BerksfileCheck:
58
+ description: 'Checking Berksfile lock'
59
+ include:
60
+ - 'Berksfile'
61
+ - 'Berksfile.lock'
62
+
57
63
  Brakeman:
58
64
  enabled: false
59
65
  description: 'Checking for security vulnerabilities'
@@ -92,6 +98,8 @@ PreCommit:
92
98
  description: 'Checking for hard tabs'
93
99
  exclude:
94
100
  - '**/*.go'
101
+ - '**/Makefile'
102
+ - '.gitmodules'
95
103
 
96
104
  ImageOptim:
97
105
  description: 'Checking for optimizable images'
data/lib/overcommit.rb CHANGED
@@ -10,6 +10,7 @@ require 'overcommit/hook_signer'
10
10
  require 'overcommit/hook_loader/base'
11
11
  require 'overcommit/hook_loader/built_in_hook_loader'
12
12
  require 'overcommit/hook_loader/plugin_hook_loader'
13
+ require 'overcommit/interrupt_handler'
13
14
  require 'overcommit/hook_runner'
14
15
  require 'overcommit/installer'
15
16
  require 'overcommit/logger'
@@ -8,7 +8,7 @@ module Overcommit::Hook::PreCommit
8
8
 
9
9
  applicable_files.each do |file|
10
10
  begin
11
- File.open(file) { |io| JSON.load(io) }
11
+ JSON.parse(IO.read(file))
12
12
  rescue JSON::ParserError => e
13
13
  output << "#{e.message} parsing #{file}"
14
14
  end
@@ -14,7 +14,7 @@ module Overcommit::HookContext
14
14
  store_merge_state
15
15
  store_cherry_pick_state
16
16
 
17
- if any_changes?
17
+ if !initial_commit? && any_changes?
18
18
  @changes_stashed = true
19
19
  `git stash save --keep-index --quiet #{<<-MSG}`
20
20
  "Overcommit: Stash of repo state before hook run at #{Time.now}"
@@ -28,7 +28,9 @@ module Overcommit::HookContext
28
28
  # Restore unstaged changes and reset file modification times so it appears
29
29
  # as if nothing ever changed.
30
30
  def cleanup_environment
31
- `git reset --hard` # Ensure working tree is clean before popping stash
31
+ unless initial_commit?
32
+ `git reset --hard &> /dev/null` # Ensure working tree is clean before popping stash
33
+ end
32
34
 
33
35
  if @changes_stashed
34
36
  `git stash apply --index --quiet`
@@ -43,8 +45,8 @@ module Overcommit::HookContext
43
45
  # Renames and deletions are ignored, since there should be nothing to check.
44
46
  def modified_files
45
47
  @modified_files ||=
46
- `git diff --cached --name-only --diff-filter=ACM --ignore-submodules=all`.
47
- split("\n").
48
+ `git diff --cached --name-only -z --diff-filter=ACM --ignore-submodules=all`.
49
+ split("\0").
48
50
  map { |relative_file| File.expand_path(relative_file) }
49
51
  end
50
52
 
@@ -57,10 +59,20 @@ module Overcommit::HookContext
57
59
 
58
60
  private
59
61
 
60
- # Returns whether there are any changes between the working tree or index
61
- # and HEAD.
62
+ # Returns whether there are any changes to the working tree, staged or
63
+ # otherwise.
62
64
  def any_changes?
63
- !`git diff --name-only --ignore-submodules=all HEAD`.strip.empty?
65
+ modified_files = `git status -z --untracked-files=no`.
66
+ split("\0").
67
+ map { |line| line.gsub(/[^\s]+\s+(.+)/, '\\1') }
68
+
69
+ modified_files.any?
70
+ end
71
+
72
+ # Returns whether the current git branch is empty (has no commits).
73
+ def initial_commit?
74
+ return @initial_commit unless @initial_commit.nil?
75
+ @initial_commit = !Overcommit::Utils.execute(%w[git rev-parse HEAD]).success?
64
76
  end
65
77
 
66
78
  DIFF_HUNK_REGEX = /
@@ -18,9 +18,11 @@ module Overcommit
18
18
 
19
19
  # Loads and runs the hooks registered for this {HookRunner}.
20
20
  def run
21
- @context.setup_environment
22
- load_hooks
23
- run_hooks
21
+ InterruptHandler.isolate_from_interrupts do
22
+ @context.setup_environment
23
+ load_hooks
24
+ run_hooks
25
+ end
24
26
  ensure
25
27
  @context.cleanup_environment
26
28
  end
@@ -33,13 +35,27 @@ module Overcommit
33
35
  if @hooks.any? { |hook| hook.run? || hook.skip? }
34
36
  log.bold "Running #{hook_script_name} hooks"
35
37
 
36
- statuses = @hooks.map { |hook| run_hook(hook) }.compact
38
+ interrupted = false
39
+
40
+ statuses = @hooks.map do |hook|
41
+ hook_status = run_hook(hook)
42
+
43
+ if hook_status == :interrupted
44
+ # Stop running any more hooks and assume a bad result
45
+ interrupted = true
46
+ break [:bad]
47
+ end
48
+
49
+ hook_status
50
+ end.compact
37
51
 
38
52
  log.log # Newline
39
53
 
40
54
  run_failed = statuses.include?(:bad)
41
55
 
42
- if run_failed
56
+ if interrupted
57
+ log.warning '⚠ Hook run interrupted by user'
58
+ elsif run_failed
43
59
  log.error "✗ One or more #{hook_script_name} hooks failed"
44
60
  else
45
61
  log.success "✓ All #{hook_script_name} hooks passed"
@@ -62,10 +78,19 @@ module Overcommit
62
78
  end
63
79
 
64
80
  begin
81
+ # Disable the interrupt handler during individual hook run so that
82
+ # Ctrl-C actually stops the current hook from being run, but doesn't
83
+ # halt the entire process.
84
+ InterruptHandler.disable!
65
85
  status, output = hook.run
66
86
  rescue => ex
67
87
  status = :bad
68
88
  output = "Hook raised unexpected error\n#{ex.message}"
89
+ rescue Interrupt
90
+ status = :interrupted
91
+ output = 'Hook was interrupted by Ctrl-C; restoring repo state...'
92
+ ensure
93
+ InterruptHandler.enable!
69
94
  end
70
95
 
71
96
  # Want to print the header in the event the result wasn't good so that the
@@ -109,6 +134,9 @@ module Overcommit
109
134
  when :bad
110
135
  log.error 'FAILED'
111
136
  print_report(output, :bold_error)
137
+ when :interrupted
138
+ log.error 'INTERRUPTED'
139
+ print_report(output, :bold_error)
112
140
  end
113
141
  end
114
142
 
@@ -0,0 +1,44 @@
1
+ require 'singleton'
2
+
3
+ # Provides a handler for interrupt signals (SIGINT), allowing the application to
4
+ # finish what it's currently working on.
5
+ class InterruptHandler
6
+ include Singleton
7
+
8
+ attr_accessor :isolate_signals, :signal_received
9
+
10
+ def initialize
11
+ self.isolate_signals = false
12
+ self.signal_received = false
13
+
14
+ Signal.trap('INT') do
15
+ if isolate_signals
16
+ self.signal_received = true
17
+ else
18
+ raise Interrupt
19
+ end
20
+ end
21
+ end
22
+
23
+ class << self
24
+ def disable!
25
+ instance.isolate_signals = false
26
+ end
27
+
28
+ def enable!
29
+ instance.isolate_signals = true
30
+ end
31
+
32
+ def isolate_from_interrupts
33
+ instance.signal_received = false
34
+ instance.isolate_signals = true
35
+ result = yield
36
+ instance.isolate_signals = false
37
+ result
38
+ end
39
+
40
+ def signal_received?
41
+ instance.signal_received
42
+ end
43
+ end
44
+ end
@@ -1,4 +1,4 @@
1
1
  # Defines the gem version.
2
2
  module Overcommit
3
- VERSION = '0.12.0'
3
+ VERSION = '0.13.0'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: overcommit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Causes Engineering
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-23 00:00:00.000000000 Z
12
+ date: 2014-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: childprocess
@@ -132,6 +132,7 @@ files:
132
132
  - lib/overcommit/hook_context/post_checkout.rb
133
133
  - lib/overcommit/installer.rb
134
134
  - lib/overcommit/configuration_loader.rb
135
+ - lib/overcommit/interrupt_handler.rb
135
136
  - lib/overcommit/hook_loader/built_in_hook_loader.rb
136
137
  - lib/overcommit/hook_loader/plugin_hook_loader.rb
137
138
  - lib/overcommit/hook_loader/base.rb