import yaml
from git import Repo


def get_git_commit_info(repo_path):
    repo = Repo(repo_path)
    head_commit = repo.head.commit
    branch_name = repo.active_branch.name
    return {
        'branch': branch_name,
        'message': head_commit.message,
        'author': head_commit.author.name,
        'date': head_commit.committed_datetime.strftime("%Y-%m-%d %H:%M:%S")
    }


def write_commit_info_to_config(commit_info, config_file):
    with open(config_file, 'r+') as file:
        config = yaml.safe_load(file)
        config['extra']['commit_info'] = commit_info
        file.seek(0)
        yaml.dump(config, file)


if __name__ == "__main__":
    repo_path = '.'
    config_file = 'mkdocs.yml'
    commit_info = get_git_commit_info(repo_path)
    write_commit_info_to_config(commit_info, config_file)