#!/usr/bin/python3

"""
    This hook happens after commit
    (it is post-commit in pre-commit you do not know commit hash)

    This hook creates .git/[commit_hash].sql difference file
    Between developer database and remote branch database
    Later during push this file will be applied to branch database in pre-push hook

    Also commit will revert if diff file generation fails

    To allow empty commits use:
    git commit --allow-empty -a -m "my commit message"
"""

import sys
import os
import socket
import subprocess
from util import color, cmd, diff_file_path, get_branch_name, get_commit_hash, get_exec_data, pg_sync, undo_commit

# # No looping
# if not os.path.exists('.commit'):
#     sys.exit(0)
# cmd(['rm', '.commit'])

get_exec_data('DEV_POST_COMMIT_HOOK')

branch_name = get_branch_name()

diff_file = diff_file_path('push', local=False)

pg_sync(pg_from_env='pg_dev', 
        pg_to_env='pg_branch', 
        pg_from_db=branch_name, 
        pg_to_db=branch_name, 
        pg_sql_file=diff_file,
        pg_create_from_db=True,
        pg_create_to_db=True,
        pg_apply=False)

# cmd(['git', 'add', diff_file])
# cmd(['git', 'commit', '--amend', '-C', 'HEAD', '--no-verify'])

# Create new branch and commit (?)
#   Maybe post-checkout hook will do it