import subprocess

git_branches = subprocess.run(
    ['git', 'branch'],
    stdout=subprocess.PIPE,
    text=True,
).stdout.strip()


def git_add_worktree(worktree_name, branch_name, git_branches=git_branches):
    # FIXME: This should check for the exact branch name.
    # If NGAV-1000 already exists, an error is thrown when
    # NGAV-100 tries to be created
    existing_branch = branch_name in git_branches

    subprocess.run([
        'git',
        'worktree',
        'add',
        worktree_name,
        '--checkout' if existing_branch else '-b',
        branch_name
    ])


def git_remove_worktree(worktree_name):
    subprocess.run([
        'git',
        'worktree',
        'remove',
        worktree_name
    ])


def git_checkout_branch(branch_name):
    subprocess.run([
        'git',
        'checkout',
        '-b',
        branch_name
    ])


def git_current_branch():
    return subprocess.run(
        ['git', 'branch', '--show-current'],
        stdout=subprocess.PIPE,
        text=True,
    ).stdout


def git_commit(commit_message):
    subprocess.run(['git', 'commit', '-m', commit_message])
