how to get project path in hook script post-commit?(git)
Asked Answered
T

2

14

I want to call a Script that is located in the repository.

I could of course do the following:

#!/bin/sh
../../myscript.sh

but I think thats not nice ;)

so how do I get the path of my project within the post-commit script?

Tempo answered 9/3, 2011 at 16:10 Comment(0)
H
24

When you're dealing with a non-bare repository, the post-commit1 hook is run with a current working directory of the working tree of the repository. So, you don't need the ../../.

If you want the full path of the script, you could always do:

SCRIPT=$(readlink -nf myscript.sh)

... or you could use git rev-parse --show-toplevel to get an absolute path to the working directory:

SCRIPT=$(git rev-parse --show-toplevel)/myscript.sh

1 ... but note that this is not true of some other hooks, e.g. in a post-receive hook in a non-bare repository, the current working directory is the .git directory.

Hedve answered 9/3, 2011 at 16:25 Comment(1)
thank you. I did not know that it was already in the right directory, could have tried it before to figure it out by myself.. ;)Tempo
T
0

Is this still vaild? my post-commit contains

#!/bin/sh
exec ./../../myscript.sh

and works.
Using just exec myscript.sh or myscript.sh doesn't work.

Moreover, even myscript.sh returns the /.git/hooks folder if asked for pwd. Using $GIT_WORK_TREE points to ~. I currently see no robust way pointing to the repo root.

Tache answered 19/1, 2023 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.