overcommit icon indicating copy to clipboard operation
overcommit copied to clipboard

Unable to setup environment for pre-commit hook run: unknown switch

Open jerrygar96 opened this issue 2 years ago • 2 comments

Hooks fail when trying to stash changes with message

Unable to setup environment for pre-commit hook run
STDOUT:
STDERR:error: unknown switch `0'
usage: git stash save [-p | --patch] [-S | --staged]...
stash message

in stash_changes in file stash_unstaged_changes.rb changing stash_message to use Time.now.to_i instead of Time.now seems to fix the issue for me.

This issue only presents itself when not committing all changed files.

jerrygar96 avatar Apr 10 '24 18:04 jerrygar96

I hit this same error and tracked down the root cause — it's a Windows-only quoting bug, not really about Time.now specifically.

Why this only happens on Windows: stash_changes passes the stash message as one array element straight to Subprocess.spawn. On macOS/Linux, ChildProcess.build(*args) executes that directly with no shell involved, so the message stays one atomic argument no matter what it contains. On Windows, though, Subprocess.spawn routes everything through win32_prepare_args, which wraps whitespace-containing args in quotes and then runs a metacharacter-escaping regex over the result — and that regex ([()%!^"<>&|]) also matches the quote marks it just added, corrupting them:

def win32_prepare_args(args)
  args = args.map do |arg|
    arg = "\"#{arg}\"" if arg =~ /\s/       # 1. wrap in quotes
    arg.gsub(/[()%!^"<>&|]/, '^\0')         # 2. re-escapes those same quotes, breaking them
  end
  %w[cmd.exe /c] + [args.join(' ')]
end

Once the quotes are corrupted, cmd.exe no longer treats the message as one quoted string and splits it on whitespace — so a stash message like ...at 2024-04-10 12:34:56 -0700 reaches git stash save with -0700 as a separate, bare argument, which git rejects. Repro:

require "overcommit/subprocess"
msg = "Overcommit: Stash of repo state before hook run at #{Time.now}"
Overcommit::Subprocess.send(:win32_prepare_args, ["git", "stash", "save", "--keep-index", "--quiet", msg]).last
# => "cmd.exe /c git stash save --keep-index --quiet ^\"Overcommit: ... -0700^\""

This affects any argument through Subprocess.spawn on Windows with a space plus a --prefixed token, not just this one call site — so switching to Time.now.to_i only dodges it here.

Suggested fix: drop the cmd.exe /c wrapper entirely for spawn/spawn_detached. Modern childprocess (5.1.0, tested) resolves .bat/.cmd shims fine via ChildProcess.build(*args) without it, and it handles .exe targets natively too. I verified this removes the bug end-to-end (real commits auto-stashing dirty files now succeed) without breaking bundle exec rspec/rubocop hooks that resolve to .bat shims.

Happy to open a PR if useful.

bwilde-castle avatar Jul 17 '26 19:07 bwilde-castle

This was fixed by #898, which removes the Windows cmd.exe argument-joining wrapper and passes the original argument vector to ChildProcess. Its reproduction used the reported stash message with a -0700 offset and confirmed the failure before the fix and success afterward on Windows.

I verified that current main contains that fix and reran the regression for the exact space-containing, dash-prefixed stash argument locally (passing). The same regression and full suite also pass on Windows with Ruby 3.3, 3.4, and 4.0 in run 42761227542. Closing as already fixed.

sds avatar Nov 10 '26 05:11 sds