Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the changed files path in git pre-receive hook

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.

like image 730
aBadAssCowboy Avatar asked Feb 09 '15 16:02

aBadAssCowboy


People also ask

What is pre-receive hook in remote repository?

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.

What is Git pre-receive hook?

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.

What is pre-receive hook declined in Git?

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.


1 Answers

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
like image 114
joozek Avatar answered Sep 21 '22 06:09

joozek