0

I cloned an existing project on my local.

I have a pre-commit bash script that is in the .git/hooks folder.

This is what it looks like:

#!/bin/bash
cd api && php artisan enforcer:check --githook

I am changing into the api folder since my git root is one level up and api contains the laravel framework where I can use the php artisan commands.

Whenever I commit something on the commandline, When I go to the line in my code that says:

$projectGitRoot = trim(shell_exec("git rev-parse --show-toplevel"));

it gives me fatal: Not a git repository: '.git' error.

Whenever I run the actual pre-commit on command line by doing ./.git/hooks/pre-commit, it works just fine.

I then changed the pre-commit to use Sudo and everything works (but this is not a good solution)

#!/bin/bash
cd api && sudo php artisan enforcer:check --githook

I have verified using whoami that in both cases it is the same user. I have tried chowning the .git folder with my commandline user. Nothing works.

What can I do? I looked at other questions like fatal: Not a git repository: '.git' error

but still very lost.

UPDATE:

It appears that the git pre-commit hook does not want to do anything with GIT. It cant even return a git status.

kratos
  • 2,465
  • 2
  • 27
  • 45
  • Does `api` folder is another local git repo or it's just a folder for laravel project? And where did you execute the command `$projectGitRoot = trim(shell_exec("git rev-parse --show-toplevel"));`, api folder or the parent folder? – Marina Liu Oct 13 '17 at 06:32
  • api folder is just for laravel. That is run in api folder since that is where the php artisan command will work. Thats why I did a 'cd api && php artisan...' because my understanding is that the git hook executes in the git root folder so I would need to do a cd into the api. Works ok with sudo. without it the same error. – kratos Oct 13 '17 at 06:37
  • What's the directory if you print the `$projectGitRoot` in pre-commit hook? – Marina Liu Oct 13 '17 at 06:48
  • /Users/jini/apps/laravel_apps/MyAppName Under this are folders like api, .git etc. – kratos Oct 13 '17 at 06:57
  • Try `unset GIT_DIR` - like the top answer here https://stackoverflow.com/questions/4043609/getting-fatal-not-a-git-repository-when-using-post-update-hook-to-execut – drizin Jun 08 '23 at 00:32

1 Answers1

0

I had the same error in my post-checkout hook:

fatal: not a git repository: '.git'

I fixed it by only running git commands in the working directory that the hook script started in. The first git rev-parse below is before pushd changes directories, and the second one is after popd switches back to the original directory.

#!/bin/bash
pushd $(git rev-parse --show-toplevel)/mydir
# Do something inside mydir.
./do_something.sh
# Run git commands in the original directory.
popd
git rev-parse --show-toplevel
egrubbs
  • 331
  • 1
  • 5