bashgitgithooksmacos-mojave

Git hooks: '.git/hooks/pre-commit': Operation not permitted


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.


Solution

  • 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