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.

j08691
  • 204,283
  • 31
  • 260
  • 272
Eric A. Meyer
  • 920
  • 6
  • 20
  • Is your git repository on a filesystem that is mounted with the `noexec` option? – jordanm Feb 05 '20 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 Feb 05 '20 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`), – Eric A. Meyer Feb 05 '20 at 18:26

1 Answers1

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

jeff
  • 1,998
  • 1
  • 16
  • 12