Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I return a message from a python git pre-receive hook prefixed error: instead of remote:

Tags:

git

python

hook

I have written a git pre-receive hook in python to abort the process if a user tries to push to a prohibited directory.

The function that does this takes a parameter of filepaths and iterates over them looking for the naughty dir, like this;

for filename in changed:
    pieces = filename.split('/')
    if(len(pieces) >3 and pieces[0] == 'application' and pieces[1] == 'modules' and pieces[3] == 'core'):
        # User is trying to push to application/modules/[MODULE_NAME]/core directory, they must be stopped!
        msg = 'You are pushing code that will be over-written by framework updates: ' + filename + '. THIS IS NOT ALLOWED!'
        print(msg)
        exit(1)

This works, and returns a message formatted like this;

remote: You are pushing code that will be overwritten..etc.

Which is followed by

error: failed to push some refs...etc.

What I would like to do however, is replace the print call and return the message as an error instead, so it reads;

error: You are pushing code will be overwritten...etc.

My reasoning is that I suspect different gui git clients will present errors differently (i.e. in red, with a pop-up, exclamation marks etc.) and they are more likely to be read by the person trying to push. Does anyone know how to do this please?

like image 391
charliefortune Avatar asked May 04 '26 04:05

charliefortune


1 Answers

You can't, as git's hook handling code simply takes every string that comes back (on sideband channel 2, specifically; that's where hook output winds up) and sticks the word remote: in front of it (see recv_sideband() in sideband.c). You could get it to print remote: error: and cross fingers that the gui will recognize that as an error string, perhaps. :-)

like image 101
torek Avatar answered May 06 '26 20:05

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!