question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pre-commit hook doesn't work when using #!/bin/bash on windows

See original GitHub issue

Description

Basically if a pre-commit hook uses #!/bin/bash rather than #!/bin/sh then it works on the command line (in all of cmd, git bash and powershell) but causes this error in the github app:

Commit failed - exit code 1 received, with output: 'error: cannot spawn
C:/Users/Public/github-desktop-issue/githooks/pre-commit: No such file or directory'

Version

  • GitHub Desktop: 1.4.0
  • Operating system: Microsoft Windows [Version 10.0.17134.285]

Steps to Reproduce

  1. Clone this repository: https://bitbucket.org/Jamesernator/github-desktop-issue
  2. Change the githooks directory using git config core.gitHooks githooks while in the repo directory
  3. Change the example file somehow
  4. Try to commit in Github Desktop
  5. Error occurs because of #!/bin/bash
  6. EXTRA: Try committing in a regular shell, this works per normal

Expected Behavior

I expected the git hook to work like it did in all three regular shells.

Actual Behavior

The error mentioned in the top of the post occurs.

Additional Information

capture

Logs

2018-09-12.desktop.production.log

Non-Solutions Tried:

Although #!/bin/sh works on windows (as it uses bash) it’s a non-solution for user’s on other operating systems as #!/bin/sh does not in general point to #!/bin/bash.

Creating a trivial #!/bin/sh file that uses bash ./pre-commit.bash doesn’t work Windows as bash isn’t found when run in the github app pre-commit hook.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Kurt-von-Lavencommented, Jun 26, 2021

@maystar, add %LOCALAPPDATA%\Programs\Git\bin to your user’s path via Settings (Win + I) > Edit environment variables for your account > User variables for … > Path > Edit…. %LOCALAPPDATA% expands to C:\Users\<user>\AppData\Local\.

1reaction
shiftkeycommented, Sep 20, 2018

This script worked for me on Windows 10 without a Git for Windows installation on the path:

#!/bin/sh
if [ "$(expr match $(uname -s) 'MINGW64_NT*')" == "10" ]; then
  echo "You may commit on Windows"
  exit 2
else
  echo "You cannot commit on this non-Windows OS"
  exit 3
fi

Setting .git/hooks/precommit to this script and making a commit would error (as intended) but with the expected message:

I think uname -s is a fine enough approach here, but maybe older versions of Windows report the numbers differently:

screen shot 2018-09-20 at 11 11 36 am
Read more comments on GitHub >

github_iconTop Results From Across the Web

Git pre-commit hook is not running on Windows - Stack Overflow
A Git hook is an executable script; however, you are using a command prompt script ( . cmd ) and not a shell...
Read more >
pre-commit
Run pre-commit install to install pre-commit into your git hooks. pre-commit will now run on every commit. Every time you clone a project...
Read more >
Learn How to Setup Git Hooks in GitKraken Client
Once the terminal windows is open, change directory to .git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file...
Read more >
Git hooks, practical uses (yes, even on Windows) - tygertec
sample . To enable it, just rename it to pre-commit by removing the .sample extension and make the script executable ( chmod + ......
Read more >
Commit hooks are skipped due to PATH issues - Jira - Atlassian
Committing from the command line or IDE works ok (pre-commit runs). ... despite Sourcetree being a "git GUI", it is still running shell...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found