I'm writing a git pre-receive hook on the remote repo to make sure pushed code is consistent with our company's internal guidelines.
I'm able to find all the files that are to be checked when a pre-receive hook is fired, however, I don't have the path to these files to open them using normal file operations(e.g. cat git_working_directory/file_name
would throw No such file or directory
error). The application which validates the code requires the file path as an argument so that it can open the file and run its checks.
Consider this scenario: the developer created a new file and pushed it to the server and the pre-receive hook is triggered. At this stage the new file is not saved on to the remote's working directory because the pre-receive hook is still running.
I'm wondering if there is a temporary location where the files are saved in git as soon as they are pushed so that I can pass that directory to the application to run the checks?
Update:
I could checkout to a temporary location and run the checks there, this could be an option but considering the fact that developers frequently push, sometimes even at the same time and the repo being very big, this option doesn't seem to be feasible. I'm looking more for a solution where I can just use the path to the file if it is somehow available.
Pre-receive hooks enforce rules for contributions before commits may be pushed to a repository. Pre-receive hooks run tests on code pushed to a repository to ensure contributions meet repository or organization policy. If the commit contents pass the tests, the push will be accepted into the repository.
The pre-receive hook is executed every time somebody uses git push to push commits to the repository. It should always reside in the remote repository that is the destination of the push, not in the originating repository.
Your commits were rejected by the pre-receive hook of that repo (that's a user-configurable script that is intended to analyze incoming commits and decide if they are good enough to be accepted into the repo). It is also a good idea to ask that person to update the hook, so it would print the reasons for the rejection.
I'm wondering if there is a temporary location where the files are saved in git as soon as they are pushed so that I can pass that directory to the application to run the checks?
No, there is no such place. Those files are stored as blobs and can be reduced to deltas and/or compressed, so there's no guarantee they'll be available anywhere in 'ready-to-consume' state.
The application which checks for the standards requires the file path as an argument so that it can open the file and run its checks.
If you're on linux you could just point /dev/stdin
as input file and put the files through pipe.
#!/bin/sh
while read oldrev newrev refname; do
git diff --name-only $oldrev $newrev | while read file; do
git show $newrev:$file | validate /dev/stdin || exit 1
done
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With