Dean Hiller
Dean Hiller

Reputation: 20192

Git: How to get the user doing a push in the post-receive hook?

I have a post-receive hook setup to tell Hudson to build after which Hudson merges the changes back into the master branch if the build succeeds without errors. However this causes the process to start again and I end up in an infinite loop.

How do I get the user so I can prevent Git from doing a build when hudson merges back?

I tried the below but $user wasn't set:

$user = ENV['USER']

Upvotes: 0

Views: 282

Answers (1)

Dean Hiller
Dean Hiller

Reputation: 20192

Well, IF you are using ssh to connect, you can use $USER as that seems to work. If that doesn't work on your ssh, just run "ssh someuser@your-git-address env" to get a list of environment variables as all those will work.

My script for preventing users to push changes to master(BUT now I need to figure out how they can fix it so they can push and get their changes off of the master with a clean pull but moving their changes to another branch or something)...

#!/bin/sh
# <oldrev> <newrev> <refname>
# update a blame tree
while read oldrev newrev ref
do
    echo "STARTING [$oldrev $newrev $ref]"

if [ $ref == "refs/heads/master" ] && [ $USER != "hudson" ]
then
    echo "YOU CANNOT COMMIT STUFF TO MASTER BRANCH"
    echo "TO CORRECT THIS run"
    echo "git reset --soft HEAD^"
    echo "git branch -c <branch name> then run"
    echo "git push <reponame> <branch name>"
    echo "and hudson will take and push to master IF it passes the tests"
    exit 1;
else
    echo "This is hudson, allowing commit to master"
fi

done

similar answer here but with 2 scripts..

How can I stop myself from committing to the master branch in git?

Upvotes: 1

Related Questions