<commit_before># (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import re

from semver import parse_version_info

from ..fs import chdir
from ..subprocess import run_command
from .constants import get_root


def get_git_root():
    """
    Get root of git repo from current location.  Returns 'None' if not in a repo.
    """
    command = 'git rev-parse --show-toplevel'

    result = run_command(command, capture='both')
    if result.stdout:
        return result.stdout.strip()
    elif result.stderr:
        return None


def get_current_branch():
    """
    Get the current branch name.
    """
    command = 'git rev-parse --abbrev-ref HEAD'

    with chdir(get_root()):
        return run_command(command, capture='out').stdout.strip()


def content_changed(file_glob="*"):
    """
    Return the content changed in the current branch compared to `master`
    """
    with chdir(get_root()):
        output = run_command(f'git diff master -U0 -- "{file_glob}"', capture='out')
    return output.stdout


def files_changed(include_uncommitted=True):
    """
    Return the list of file changed in the current branch compared to `master`
    """
    with chdir(get_root()):
        # Use `--name-status` to include moved files
        name_status_result = run_command('git diff --name-status origin/master...', capture='out')

    name_status_lines = name_status_result.stdout.splitlines()

    changed_files = []
    for l in name_status_lines:
        files = l.split('\t')[1:]  # skip first element representing the type of change
        changed_files.extend(files)

    if include_uncommitted:
        with chdir(get_root()):
            # Use `--name-only` to include uncommitted files
            name_only_result = run_command('git diff --name-only master', capture='out')
        name_only_lines = name_only_result.stdout.splitlines()
        changed_files.extend(name_only_lines)

    return sorted([f for f in set(changed_files) if f])


def get_commits_since(check_name, target_tag=None, exclude_branch=None):
    """
    Get the list of commits from `target_tag` to `HEAD` for the given check
    """
    root = get_root()
    if check_name:
        target_path = os.path.join(root, check_name)
    else:
        target_path = root

    if exclude_branch is not None and check_name not in {".", None}:
        raise ValueError(f"Cannot exclude a branch from a non-root check {check_name}")
    elif exclude_branch is not None:
        command = f"git cherry -v {exclude_branch} HEAD {'' if target_tag is None else f'{target_tag} '}"
    else:
        command = f"git log --pretty=%s {'' if target_tag is None else f'{target_tag}... '}{target_path}"

    with chdir(root):
        return run_command(command, capture=True).stdout.splitlines()

<commit_msg>Fail releases for missing tags (#11593)<commit_after># (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import re

from semver import parse_version_info

from ..fs import chdir
from ..subprocess import run_command
from .constants import get_root


def get_git_root():
    """
    Get root of git repo from current location.  Returns 'None' if not in a repo.
    """
    command = 'git rev-parse --show-toplevel'

    result = run_command(command, capture='both')
    if result.stdout:
        return result.stdout.strip()
    elif result.stderr:
        return None


def get_current_branch():
    """
    Get the current branch name.
    """
    command = 'git rev-parse --abbrev-ref HEAD'

    with chdir(get_root()):
        return run_command(command, capture='out').stdout.strip()


def content_changed(file_glob="*"):
    """
    Return the content changed in the current branch compared to `master`
    """
    with chdir(get_root()):
        output = run_command(f'git diff master -U0 -- "{file_glob}"', capture='out')
    return output.stdout


def files_changed(include_uncommitted=True):
    """
    Return the list of file changed in the current branch compared to `master`
    """
    with chdir(get_root()):
        # Use `--name-status` to include moved files
        name_status_result = run_command('git diff --name-status origin/master...', capture='out')

    name_status_lines = name_status_result.stdout.splitlines()

    changed_files = []
    for l in name_status_lines:
        files = l.split('\t')[1:]  # skip first element representing the type of change
        changed_files.extend(files)

    if include_uncommitted:
        with chdir(get_root()):
            # Use `--name-only` to include uncommitted files
            name_only_result = run_command('git diff --name-only master', capture='out')
        name_only_lines = name_only_result.stdout.splitlines()
        changed_files.extend(name_only_lines)

    return sorted([f for f in set(changed_files) if f])


def get_commits_since(check_name, target_tag=None, exclude_branch=None):
    """
    Get the list of commits from `target_tag` to `HEAD` for the given check
    """
    root = get_root()
    if check_name:
        target_path = os.path.join(root, check_name)
    else:
        target_path = root

    if exclude_branch is not None and check_name not in {".", None}:
        raise ValueError(f"Cannot exclude a branch from a non-root check {check_name}")
    elif exclude_branch is not None:
        command = f"git cherry -v {exclude_branch} HEAD {'' if target_tag is None else f'{target_tag} '}"
    else:
        command = f"git log --pretty=%s {'' if target_tag is None else f'{target_tag}... '}{target_path}"

    with chdir(root):
        return run_command(command, capture=True, check=True).stdout.splitlines()


{'fraction_non_alphanumeric': 0.10989202368512713, 'fraction_numerical': 0.0033089515848136537, 'mean_word_length': 3.12275664034458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6340508', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1704, 'n_words': 536}