7

This is all on OS X Mojave.

I’m trying to block myself from mistakenly making commits to the master branch, because that is a thing I do a little too often, using the pre-commit Git hook from this SO answer, changed slightly because I use bash instead of sh. Every time I tried to run it, though, I got the following:

fatal: cannot exec '.git/hooks/pre-commit': Operation not permitted

I checked the permissions of the .git and .git/hooks directories. Both are drwxrwxrwx. The permissions on pre-commit itself are:

-rwxr-xr-x@  1 emeyer  staff    25 Feb  5 11:50 pre-commit

…which is the same as the pre-commit.sample file I copied over to pre-commit and then replaced the contents. I tried chmod +w but that didn’t fix it.

I decided to simplify my testing and replaced the contents of pre-commit with the following:

#!/bin/bash

echo "Test"

I still got the above-referenced Operation not permitted error. I also tried it with #!/bin/sh like in the SO answer’s example; same result.

If I try running the script directly, by typing ./pre-commit from the command line, I get a slightly different error: -bash: ./pre-commit: /bin/bash: bad interpreter: Operation not permitted. The error is consistent whether I use /bin/bash, /bin/sh, /usr/local/bin/bash, or /usr/local/bin/sh.

Googling, Binging, and SO-searching didn’t get me an answer that worked, so I’m asking here how to allow the operation, or whatever is needed.

3
  • Is your git repository on a filesystem that is mounted with the noexec option?
    – jordanm
    Commented Feb 5, 2020 at 17:15
  • 2
    Are you using MacOS? (The @ suggests that you are.) You're probably hitting the System Integrity Protection feature in Mojave.
    – torek
    Commented Feb 5, 2020 at 17:37
  • 1
    It is in fact OS X Mojave! I tried running pre-commit directly and got -bash: ./pre-commit: /bin/bash: bad interpreter: Operation not permitted (and the same basic result with sh), Commented Feb 5, 2020 at 18:26

2 Answers 2

15

The pre-commit file may have file metadata associated with it (the @ in your ls output suggests this), and that that file metadata may include the com.apple.quarantine attribute.

You should be able to confirm this using the following:

ls -l@ pre-commit

or

xattr -l pre-commit

And you should be able to remove the attribute with:

xattr -d com.apple.quarantine pre-commit

ref: https://stackoverflow.com/a/9952742/157515

2
  • 1
    That’s it! Thank you! Commented Feb 5, 2020 at 22:17
  • 2
    Also, today I learned what the @ meant in ls, so thank you twice! Commented Feb 5, 2020 at 23:12
0

Try the following commands:

  1. chmod +x .git-hooks/pre-push where .git-hooks/pre-push is your directory with already configured hooks.

  2. ln -s -f ../../.git-hooks/pre-push .git/hooks/pre-push The .git-hooks/pre-push directory already has the access permissions that were configured in the previous command

Not the answer you're looking for? Browse other questions tagged or ask your own question.