Home > OS >  How do I get current project directory inside a git hook?
How do I get current project directory inside a git hook?

Time:01-18

I have a pre-commit hook running in my flutter project directory.

#!/bin/sh

cd D: || exit
cd AndroidProjects/json_test/ || exit

flutter pub run 

It is as follows. I am running the git command (git commit) in local terminal at project directory, so D:AndroidProjects/json_test/> git commit And then the hook is executed.

Is there a way I get project path in which I am running git commit, right to the hook, without hardcoding it inside?

CodePudding user response:

As the githooks documentation says:

Before Git invokes a hook, it changes its working directory to either $GIT_DIR in a bare repository or the root of the working tree in a non-bare repository.

(Read the linked documentation for exceptions.)

You may therefore inspect the current working directory to find the top level of the working tree, assuming a non-bare repository.

  •  Tags:  
  • Related