#!/usr/bin/env python3

import os
import subprocess
import sys

count = os.environ.get('GIT_PUSH_OPTION_COUNT')
if count is None:
    sys.exit(0)

try:
    count = int(count)
except ValueError:
    sys.stderr.write(f'`GIT_PUSH_OPTION_COUNT` was not an integer: {count}\n')
    sys.exit(1)

def git(*args):
    subprocess.check_call(['git'] + args)

for i in range(count):
    push_option = os.environ.get(f'GIT_PUSH_OPTION_{i}')
    if push_option is None:
        sys.stderr.write(f'`GIT_PUSH_OPTION_{i}` is not set?\n')
        sys.exit(1)

    subprocess.check_call([
        'git',
        'config',
        # XXX(git-2.46.0): use `git config set`
        # 'set',
        '-f', f'{os.environ["GIT_DIR"]}/push-option-log',
        # '--append',
        '--add',
        'ghostflow.push',
        push_option,
    ])
