<commit_before>import os

import sh
import yaml
from path import path

from cloudify import ctx
from cloudify import exceptions
from cloudify.decorators import operation

from common import bake


def _git():
    repo_location = ctx.instance.runtime_properties['repo_location']
    return bake(sh.git).bake(
        '--no-pager',
        '--git-dir', path(repo_location) / '.git',
        '--work-tree', repo_location)


@operation
def clone(location, organization, repo, branch, **_):
    git = bake(sh.git)
    location = os.path.expanduser(location)
    repo_location = os.path.join(location, repo)
    ctx.instance.runtime_properties['repo_location'] = repo_location
    if os.path.isdir(repo_location):
        return
    git.clone('<EMAIL>:{}/{}.git'.format(organization, repo),
              repo_location,
              '-b', branch).wait()


@operation
def configure(commit_msg_resource_path, git_config, **_):
    repo_location = path(ctx.instance.runtime_properties['repo_location'])

    # configure commit-msg hook
    commit_msg_hook_path = repo_location / '.git' / 'hooks' / 'commit-msg'
    if commit_msg_hook_path.exists():
        ctx.logger.warn('{} already exits, skipping hook installation.'
                        .format(commit_msg_hook_path))
    else:
        commit_msg_hook = ctx.get_resource(commit_msg_resource_path)
        with open(commit_msg_hook_path, 'w') as f:
            f.write(commit_msg_hook)
        os.chmod(commit_msg_hook_path, 0755)

    # git config
    git = _git()
    for key, value in git_config.items():
        git.config(key, value).wait()


@operation
def pull(**_):
    git = _git()
    git.pull(prune=True, tags=True).wait()


@operation
def status(**_):
    git = _git()
    git('rev-parse', '--abbrev-ref', 'HEAD').wait()
    git.status(s=True).wait()


@operation
def checkout(repo_type, branch, **_):
    git = _git()
    branches_file_path = os.path.expanduser(branch)
    if not os.path.exists(branches_file_path):
        branches_dir = os.path.expanduser(ctx.node.properties['branches_dir'])
        branches_file_path = os.path.join(branches_dir, branch)
    if os.path.exists(branches_file_path):
        with open(branches_file_path) as f:
            branches = yaml.safe_load(f.read())
        name = ctx.node.properties['name']
        if name not in branches:
            return
        branch = branches[name]
    elif repo_type == 'misc':
        return
    elif repo_type not in ['core', 'plugin']:
        raise exceptions.NonRecoverableError('Unhandled repo type: {}'
                                             .format(repo_type))
    elif not branch:
        raise exceptions.NonRecoverableError('Branch is not defined')
    elif branch.startswith('.'):
        branch = _fix_branch_name(repo_type, branch)<commit_msg>when using branches file, fallback to default branch, if repo is not included<commit_after>import os

import sh
import yaml
from path import path

from cloudify import ctx
from cloudify import exceptions
from cloudify.decorators import operation

from common import bake


def _git():
    repo_location = ctx.instance.runtime_properties['repo_location']
    return bake(sh.git).bake(
        '--no-pager',
        '--git-dir', path(repo_location) / '.git',
        '--work-tree', repo_location)


@operation
def clone(location, organization, repo, branch, **_):
    git = bake(sh.git)
    location = os.path.expanduser(location)
    repo_location = os.path.join(location, repo)
    ctx.instance.runtime_properties['repo_location'] = repo_location
    if os.path.isdir(repo_location):
        return
    git.clone('<EMAIL>:{}/{}.git'.format(organization, repo),
              repo_location,
              '-b', branch).wait()


@operation
def configure(commit_msg_resource_path, git_config, **_):
    repo_location = path(ctx.instance.runtime_properties['repo_location'])

    # configure commit-msg hook
    commit_msg_hook_path = repo_location / '.git' / 'hooks' / 'commit-msg'
    if commit_msg_hook_path.exists():
        ctx.logger.warn('{} already exits, skipping hook installation.'
                        .format(commit_msg_hook_path))
    else:
        commit_msg_hook = ctx.get_resource(commit_msg_resource_path)
        with open(commit_msg_hook_path, 'w') as f:
            f.write(commit_msg_hook)
        os.chmod(commit_msg_hook_path, 0755)

    # git config
    git = _git()
    for key, value in git_config.items():
        git.config(key, value).wait()


@operation
def pull(**_):
    git = _git()
    git.pull(prune=True, tags=True).wait()


@operation
def status(**_):
    git = _git()
    git('rev-parse', '--abbrev-ref', 'HEAD').wait()
    git.status(s=True).wait()


@operation
def checkout(repo_type, branch, **_):
    git = _git()
    branches_file_path = os.path.expanduser(branch)
    if not os.path.exists(branches_file_path):
        branches_dir = os.path.expanduser(ctx.node.properties['branches_dir'])
        branches_file_path = os.path.join(branches_dir, branch)
    if os.path.exists(branches_file_path):
        with open(branches_file_path) as f:
            branches = yaml.safe_load(f.read())
        name = ctx.node.properties['name']
        if name in branches:
            branch = branches[name]
        else:
            branch = ctx.node.properties['branch']
    elif repo_type == 'misc':
        return
    elif repo_type not in ['core', 'plugin']:
        raise exceptions.NonRecoverableError('Unhandled repo type: {}'
                                             .format(repo_type))
    elif not branch:
        raise exceptions.NonRecoverableError('Branch is not defined')
    elif branch.startswith('.'):
        branch = _fix_branch_name(repo_type, branch)
{'fraction_non_alphanumeric': 0.12333566923615978, 'fraction_numerical': 0.001401541695865452, 'mean_word_length': 2.934527911784976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7650378', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1738, 'n_words': 380}