#!/usr/bin/env python3
#
# Helper script for committing data to git and pushing upstream
#
# Copyright (c) 2017, Intel Corporation.
#
# SPDX-License-Identifier: GPL-2.0-only
#

import argparse
import logging
import os
import re
import sys

# Import oe and bitbake libs
scripts_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(scripts_path, 'lib'))
import scriptpath
scriptpath.add_bitbake_lib_path()
scriptpath.add_oe_lib_path()

from oeqa.utils.git import GitRepo, GitError
from oeqa.utils.metadata import metadata_from_bb
import oeqa.utils.gitarchive as gitarchive

# Setup logging
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
log = logging.getLogger()


def parse_args(argv):
    """Parse command line arguments"""
    parser = argparse.ArgumentParser(
            description="Commit data to git and push upstream",
            formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('--debug', '-D', action='store_true',
                        help="Verbose logging")
    parser.add_argument('--git-dir', '-g', required=True,
                        help="Local git directory to use")
    parser.add_argument('--no-create', action='store_true',
                        help="If GIT_DIR is not a valid Git repository, do not "
                             "try to create one")
    parser.add_argument('--bare', action='store_true',
                        help="Initialize a bare repository when creating a "
                             "new one")
    parser.add_argument('--push', '-p', nargs='?', default=False, const=True,
                        help="Push to remote")
    parser.add_argument('--branch-name', '-b',
                        default='{hostname}/{branch}/{machine}',
                        help="Git branch name (pattern) to use")
    parser.add_argument('--no-tag', action='store_true',
                        help="Do not create Git tag")
    parser.add_argument('--tag-name', '-t',
                        default='{hostname}/{branch}/{machine}/{commit_count}-g{commit}/{tag_number}',
                        help="Tag name (pattern) to use")
    parser.add_argument('--commit-msg-subject',
                        default='Results of {branch}:{commit} on {hostname}',
                        help="Subject line (pattern) to use in the commit message")
    parser.add_argument('--commit-msg-body',
                        default='branch: {branch}\ncommit: {commit}\nhostname: {hostname}',
                        help="Commit message body (pattern)")
    parser.add_argument('--tag-msg-subject',
                        default='Test run #{tag_number} of {branch}:{commit} on {hostname}',
                        help="Subject line (pattern) of the tag message")
    parser.add_argument('--tag-msg-body',
                        default='',
                        help="Tag message body (pattern)")
    parser.add_argument('--exclude', action='append', default=[],
                        help="Glob to exclude files from the commit. Relative "
                             "to DATA_DIR. May be specified multiple times")
    parser.add_argument('--notes', nargs=2, action='append', default=[],
                        metavar=('GIT_REF', 'FILE'),
                        help="Add a file as a note under refs/notes/GIT_REF. "
                             "{branch_name} in GIT_REF will be expanded to the "
                             "actual target branch name (specified by "
                             "--branch-name). This option may be specified "
                             "multiple times.")
    parser.add_argument('data_dir', metavar='DATA_DIR',
                        help="Data to commit")
    return parser.parse_args(argv)

def get_nested(d, list_of_keys):
    try:
        for k in list_of_keys:
            d = d[k]
        return d
    except KeyError:
        return ""

def main(argv=None):
    args = parse_args(argv)
    if args.debug:
        log.setLevel(logging.DEBUG)

    try:
        # Get keywords to be used in tag and branch names and messages
        metadata = metadata_from_bb()
        keywords = {'hostname': get_nested(metadata, ['hostname']),
                    'branch': get_nested(metadata, ['layers', 'meta', 'branch']),
                    'commit': get_nested(metadata, ['layers', 'meta', 'commit']),
                    'commit_count': get_nested(metadata, ['layers', 'meta', 'commit_count']),
                    'machine': get_nested(metadata, ['config', 'MACHINE'])}

        gitarchive.gitarchive(args.data_dir, args.git_dir, args.no_create, args.bare,
                              args.commit_msg_subject.strip(), args.commit_msg_body, args.branch_name,
                              args.no_tag, args.tag_name, args.tag_msg_subject, args.tag_msg_body,
                              args.exclude, args.notes, args.push, keywords, log)

    except gitarchive.ArchiveError as err:
        log.error(str(err))
        return 1

    return 0

if __name__ == "__main__":
    sys.exit(main())
