<commit_before>#!/usr/bin/env bash

# Update version number and prepare for publishing the new version

set -e

# Empty to run the rest of the line and "echo" for a dry run
CMD_PREFIX=

# Add a quote if this is a dry run
QUOTE=

NEW_VERSION=

UPDATE_ONLY=false

function echo_err
{
    echo "$@" 1>&2;
}

function usage
{
      echo "Usage: $0 [parameters]"
      echo "      -v | --version <version>"
      echo "      -d | --dry-run print the commands without executing them"
      echo "      -u | --update-only only update the version"
      echo "      -h | --help print this information and exit"
      echo
      echo "For example: $0 -v 1.2.3"
}

function process_arguments
{
    while [ "$1" != "" ]; do
        case $1 in
            -v | --version )
                shift
                NEW_VERSION=${1:-}
                if ! [[ "${NEW_VERSION}" =~ [0-9]+\.[0-9]+\.[0-9]+(\-.+)? ]]; then
                  echo_err "You must supply a new version after -v or --version"
                  echo_err "For example:"
                  echo_err "  1.2.3"
                  echo_err "  1.2.3-rc1"
                  echo_err ""
                  usage; return 1
                fi
                ;;
            -d | --dry-run )
                CMD_PREFIX=echo
                echo "Dry Run"
                echo ""
                ;;
            -u | --update-only )
                UPDATE_ONLY=true
                echo "Only update version"
                echo ""
                ;;
            -h | --help )
                usage; return 0
                ;;
            * )
              usage; return 1
        esac
        shift || true
    done
}

# Intentionally make pushd silent
function pushd
{
    command pushd "$@" > /dev/null
}

# Intentionally make popd silent
function popd
{
    command popd "$@" > /dev/null
}

# Check if one version is less than or equal than other
# Example:
# ver_lte 1.2.3 1.2.3 && echo "yes" || echo "no" # yes
# ver_lte 1.2.3 1.2.4 && echo "yes" || echo "no" # yes
# ver_lte 1.2.4 1.2.3 && echo "yes" || echo "no" # no
function ver_lte
{
    [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}

# Extract the last entry or entry for a given version
# The function is not currently used in this file.
# Examples:
#   changelog_last_entry
#   changelog_last_entry 1.10.0
#
function changelog_last_entry
{
    sed -e "1,/^${1}/d" -e '/^=/d' -e '/^$/d' -e '/^[0-9]/,$d' CHANGELOG.md
}

function verify_dependencies
{
    # Test if the gnu grep is installed
    if ! grep --version | grep -q GNU
    then
        echo_err "GNU grep is required for this script"
        echo_err "You can install it using the following command:"
        echo_err ""
        echo_err "brew install grep --with-default-names"
        return 1
    fi

    if [ "${UPDATE_ONLY}" = true ]; then
      return 0;
    fi

    if [[ -z "$(type -t git-changelog)" ]]
    then
        echo_err "git-extras packages is not installed."
        echo_err "You can install it using the following command:"
        echo_err ""
        echo_err "brew install git-extras"
        return 1
    fi
}

# Replace old string only if it is present in the file, otherwise return 1
function safe_replace
{
    local old=$1
    local new=$2
    local file=$3

    grep -q "${old}" "${file}" || { echo_err "${old} was not found in ${file}"; return 1; }

    ${CMD_PREFIX} sed -E -i '.bak' "${QUOTE}s/${old}/${new}/${QUOTE}" "${file}"
}

function update_version
{
    if [ -z "${NEW_VERSION}" ]; then
        usage; return 1
    fi

    # Enter git root
    pushd $(git rev-parse --show-toplevel)<commit_msg>Fix `update_version` script linux compatiblity<commit_after>#!/usr/bin/env bash

# Update version number and prepare for publishing the new version

set -e

# Empty to run the rest of the line and "echo" for a dry run
CMD_PREFIX=

# Add a quote if this is a dry run
QUOTE=

NEW_VERSION=

UPDATE_ONLY=false

function echo_err
{
    echo "$@" 1>&2;
}

function usage
{
      echo "Usage: $0 [parameters]"
      echo "      -v | --version <version>"
      echo "      -d | --dry-run print the commands without executing them"
      echo "      -u | --update-only only update the version"
      echo "      -h | --help print this information and exit"
      echo
      echo "For example: $0 -v 1.2.3"
}

function process_arguments
{
    while [ "$1" != "" ]; do
        case $1 in
            -v | --version )
                shift
                NEW_VERSION=${1:-}
                if ! [[ "${NEW_VERSION}" =~ [0-9]+\.[0-9]+\.[0-9]+(\-.+)? ]]; then
                  echo_err "You must supply a new version after -v or --version"
                  echo_err "For example:"
                  echo_err "  1.2.3"
                  echo_err "  1.2.3-rc1"
                  echo_err ""
                  usage; return 1
                fi
                ;;
            -d | --dry-run )
                CMD_PREFIX=echo
                echo "Dry Run"
                echo ""
                ;;
            -u | --update-only )
                UPDATE_ONLY=true
                echo "Only update version"
                echo ""
                ;;
            -h | --help )
                usage; return 0
                ;;
            * )
              usage; return 1
        esac
        shift || true
    done
}

# Intentionally make pushd silent
function pushd
{
    command pushd "$@" > /dev/null
}

# Intentionally make popd silent
function popd
{
    command popd > /dev/null
}

# Check if one version is less than or equal than other
# Example:
# ver_lte 1.2.3 1.2.3 && echo "yes" || echo "no" # yes
# ver_lte 1.2.3 1.2.4 && echo "yes" || echo "no" # yes
# ver_lte 1.2.4 1.2.3 && echo "yes" || echo "no" # no
function ver_lte
{
    [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}

# Extract the last entry or entry for a given version
# The function is not currently used in this file.
# Examples:
#   changelog_last_entry
#   changelog_last_entry 1.10.0
#
function changelog_last_entry
{
    sed -e "1,/^${1}/d" -e '/^=/d' -e '/^$/d' -e '/^[0-9]/,$d' CHANGELOG.md
}

function verify_dependencies
{
    # Test if the gnu grep is installed
    if ! grep --version | grep -q GNU
    then
        echo_err "GNU grep is required for this script"
        echo_err "You can install it using the following command:"
        echo_err ""
        echo_err "brew install grep --with-default-names"
        return 1
    fi

    if [ "${UPDATE_ONLY}" = true ]; then
      return 0;
    fi

    if [[ -z "$(type -t git-changelog)" ]]
    then
        echo_err "git-extras packages is not installed."
        echo_err "You can install it using the following command:"
        echo_err ""
        echo_err "brew install git-extras"
        return 1
    fi
}

# Replace old string only if it is present in the file, otherwise return 1
function safe_replace
{
    local old=$1
    local new=$2
    local file=$3

    grep -q "${old}" "${file}" || { echo_err "${old} was not found in ${file}"; return 1; }

    ${CMD_PREFIX} sed -i.bak -e "${QUOTE}s/${old}/${new}/${QUOTE}" -- "${file}"  && rm -- "${file}.bak"
}

function update_version
{
    if [ -z "${NEW_VERSION}" ]; then
        usage; return 1
    fi

    # Enter git root
    pushd $(git rev-parse --show-toplevel)
{'fraction_non_alphanumeric': 0.12912995594713655, 'fraction_numerical': 0.01789647577092511, 'mean_word_length': 1.6987369985141159, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '2848412', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2306, 'n_words': 780}