Skip to content

Commit 3f660a7

Browse files
committed
Log standard output/error output in debug mode
In order to make debugging hook runs easier, include output from the standard output/error streams when the OVERCOMMIT_DEBUG environment variable is set.
1 parent e701ff8 commit 3f660a7

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* Add `rewritten_commits` helper to `PostRewrite` hooks
1717
* Add `gemfile` option to configuration file which allows a `Gemfile` to be
1818
loaded by Bundler to enforce particular gem versions during hook runs
19+
* Add support for `OVERCOMMIT_DEBUG` environment variable which toggles the
20+
display of additional verbose output from executed commands
1921

2022
## 0.26.0
2123

lib/overcommit/cli.rb

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def initialize(arguments, input, logger)
99
@input = input
1010
@log = logger
1111
@options = {}
12+
13+
Overcommit::Utils.log = logger
1214
end
1315

1416
def run

lib/overcommit/utils.rb

+18-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class Version < Gem::Version
2525
end
2626

2727
class << self
28+
# @return [Overcommit::Logger] logger with which to send debug output
29+
attr_accessor :log
30+
2831
def script_path(script)
2932
File.join(Overcommit::HOME, 'libexec', script)
3033
end
@@ -162,11 +165,20 @@ def execute(args, splittable_args = nil)
162165
'Cannot pipe commands with the `execute` helper'
163166
end
164167

165-
if splittable_args
166-
Overcommit::CommandSplitter.execute(args, splittable_args)
167-
else
168-
Overcommit::Subprocess.spawn(args)
169-
end
168+
result =
169+
if splittable_args
170+
log.debug(args.join(' ') + " ... (#{splittable_args.length} splittable args)")
171+
Overcommit::CommandSplitter.execute(args, splittable_args)
172+
else
173+
log.debug(args.join(' '))
174+
Overcommit::Subprocess.spawn(args)
175+
end
176+
177+
log.debug("EXIT STATUS: #{result.status}")
178+
log.debug("STDOUT: #{result.stdout.inspect}")
179+
log.debug("STDERR: #{result.stderr.inspect}")
180+
181+
result
170182
end
171183

172184
# Execute a command in a subprocess, returning immediately.
@@ -182,6 +194,7 @@ def execute_in_background(args)
182194
'Cannot pipe commands with the `execute_in_background` helper'
183195
end
184196

197+
log.debug("Spawning background task: #{args.join(' ')}")
185198
Subprocess.spawn_detached(args)
186199
end
187200

template-dir/hooks/overcommit-hook

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ end
5050

5151
begin
5252
logger = Overcommit::Logger.new(STDOUT)
53+
Overcommit::Utils.log = logger
5354

5455
# Ensure master hook is up-to-date
5556
installer = Overcommit::Installer.new(logger)

0 commit comments

Comments
 (0)