-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass local and remote branch names as arguments / env variables to pre-push #320
Comments
Yes, lefthook doesn't pass STDIN now. Could you describe your case? |
I am using a pre-push hook to statically analyze the source code using PHPStan. Since it is a legacy code base, the code is not fully typehinted, and there are several thousand existing issues. We want to ensure that new changes does not introduce additional issues, while we are are slowly fixing the existing errors. To determine the merge base, we need access to the local ref name. |
Wow! This looks reasonable. Could you share your Does PHPStan support analyzing only the given files? I mean, you could run it only for files that were touched in commits that are being pushed. There is a pre-push:
commands:
phpstan:
run: phpstan {push_files} If it doesn't help, and using STDIN is the only option, I would ask you to provide some reproducible code+lefthook.yml set, so I could test STDIN passing while developing a feature. |
I am not sure whether static analysis is possible with just a subset of files. Changes in a file can affect code in unmodified files too. Also, the hook is supposed to report only new issues. Because, sometimes the modifieds may have hundreds of issues and it may not be feasible to fix them all in a single go. There is an existing issue about the missing STDIN. I have created a test repo with the static analysis check I had to hard code the values for |
Thank you a lot! I will start working on it soon! (wow, you made a repo for this case! I appreciate it a lot!) |
I thought a little bit and came up with the following solution:
So, it would look like this: pre-push:
stdin: true # default would be false
scripts:
"my-script.sh":
runner: bash @JoyceBabu , @ThisIsMissEm what do you think about it? |
The Users may use scripts/executables provided by third parties in their hook, that might behave differently depending on whether a stdin in available or not. |
Btw, why not pass copies of stdin to every hooks always by default? |
@Envek , there is a problem with LFS pre-push hook, which waits for input if stdin is provided. I guess lefthook users don't expect the tool to prompt for input by default, so it's better to have an option for those who need it. I am afraid we will have bugs otherwise, like, constant waiting for input when you are git-pushing from VScode/any other git wrapper. |
Wait, where is input prompting came from? How it is related to the stdin contents that git itself produces? |
Hmm, doesn't it prompt refs from user? Hell... seems like I misunderstood the issue. I need some time to investigate why we can't pass stdin from lefthook to scripts/commands. |
Hey! I have prepared and tested STDIN passing, but there is a limitation. You should not use Can you test the change? Does it work for you? #324 gh pr checkout 324
make # or `go build`
cp lefthook ${GOPATH}/bin |
The process is hanging for me, even without the I have updated the test repo to read the values from stdin. |
The script worked after commenting the following lines from if [ -t 1 ] ; then
exec < /dev/tty ; # <- enables interactive shell
fi It is now working with Thank you for fixing this. |
Oh, wow! I'll take a look and try to fix the script template. This makes sense. |
Yes, I found the PR that introduced this |
Hey @JoyceBabu ! I've updated the PR. Sorry for a long wait. But I can't make it work with |
I never push to more than one target at a time, so Thanks a lot for fixing this. |
Unfortunately I still have the origin issue. Should mean I have a pre-push command that requires to get the originally passed arguments by Git. Is that somehow possible? |
Yes, arguments passed by Git should be passed explicitly (see docs for details)
Is it helpful? If not, could you provide more details about what you are trying to achieve? |
Oh. I'm sorry that I missed that part. Very nice. Though, it is not fully working for me. I have this example configuration: pre-push:
commands:
test:
run: echo All arguments together are: {0}
test-2:
run: echo The arguments separated are: {1} {2} {3} {4} The output of doing an actual
According to the The very first line of the output that is not from any of my commands ( |
Hey @weilbith this is not all. The arguments are provided via STDIN:
So you need to read from STDIN to receive those lines. Use |
Ah, interesting. I think the But I'm afraid I'm too stupid to make it work. Having this simple bash command pre-push:
commands:
test:
interactive: true
run: xargs -0 printf '%s'; exit 1 The output is just:
Using my "actual" command I want to make work, it is the same result. It waits for ever. Btw: thank you very much for the great support! 🙏🏾 |
I'm sorry, but may I ping you @mrexox 🙈 |
Hey @weilbith! Sorry for such a delay. I've tried to handle pre-push locally, and I found that I've made a mistake with the later comment. Here is my example:
The config # lefthook.yml
pre-push:
follow: true # need to use follow to get stdin from git push
scripts:
"script.sh":
runner: bash The hook #!/usr/bin/env bash
remote="$1"
url="$2"
echo 'Hello there!'
echo remote $remote
echo url $url
read local_ref local_sha remote_ref remote_sha
echo local_ref $local_ref
echo local_sha $local_sha
echo remote_ref $remote_ref
echo remote_sha $remote_sha
exit 0 I forgot that there is a special option to Also I guess |
I got a hang up while running BTW: I'm using the latest version: 1.3.10 My pre-push script: #!/bin/sh
while read local_ref local_oid remote_ref remote_oid
do
echo $local_ref
echo $local_oid
echo $remote_ref
echo $remote_oid
echo '----------------------------------'
# Break is important here, to stop another loop
break;
done
exit 0 |
Is it possible to access the local / remote ref names in the
pre-push
hook?Git passes the information as STDIN to the
pre-push
hook. But lefthook is not passing it to the actual hook.The text was updated successfully, but these errors were encountered: