#!/usr/bin/env python

# https://github.com/git/git/blob/master/Documentation/githooks.txt#L181
# This hook is called by 'git push' and can be used to prevent a push from taking
# place.  The hook is called with two parameters which provide the name and
# location of the destination remote, if a named remote is not being used both
# values will be the same.
 
# Information about what is to be pushed is provided on the hook's standard
# input with lines of the form:
 
#   <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
 
# For instance, if the command +git push origin master:foreign+ were run the
# hook would receive a line like the following:
 
#   refs/heads/master 67890 refs/heads/foreign 12345
 
# although the full, 40-character SHA-1s would be supplied.  If the foreign ref
# does not yet exist the `<remote SHA-1>` will be 40 `0`.  If a ref is to be
# deleted, the `<local ref>` will be supplied as `(delete)` and the `<local
# SHA-1>` will be 40 `0`.  If the local commit was specified by something other
# than a name which could be expanded (such as `HEAD~`, or a SHA-1) it will be
# supplied as it was originally given.
 
# If this hook exits with a non-zero status, 'git push' will abort without
# pushing anything.  Information about why the push is rejected may be sent
# to the user by writing to standard error.
import gitta_hook
import sys
sys.argv.extend(sys.stdin.read().split())  # add stdin arguments
remote_dest_uri = sys.argv[1]  # generally, uri is the name of the remote
                              # but may instead be the url
remote_dest_url = sys.argv[2]  # remote destination location
kwargs = {'remote_dest_uri': remote_dest_uri, 
    'remote_dest_url': remote_dest_url}
if len(sys.argv) > 3:  # this can fail if pre-push is going to fail anyways?
    local_ref = sys.argv[3]  # there was no 4th argument in sys.argv. Had to
                            # add by reading from stdin
    local_sha1 = sys.argv[4]
    remote_ref = sys.argv[5]
    remote_sha1 = sys.argv[6]
    kwargs.update({'local_ref': local_ref, 'local_sha1': local_sha1,
                   'remote_ref': remote_ref, 'remote_sha1': remote_sha1})
gitta_hook.trigger(*sys.argv, **kwargs)

