<commit_before>#!/bin/bash

# Fail if there are any unset variables and whenever a command returns a
# non-zero exit code.
set -eu

# If the variable `$DEBUG` is set, then print the shell commands as we execute.
if [ -n "${DEBUG:-}" ]; then
  set -x
  export DEBUG
fi

info() {
  case "${TERM:-}" in
    *term | xterm-* | rxvt | screen | screen-*)
      printf -- "   \033[1;32m%s: \033[1;37m%s\033[0m\n" "${program}" "$1"
      ;;
    *)
      printf -- "   %s: %s\n" "${program}" "$1"
      ;;
  esac
  return 0
}

warn() {
  case "${TERM:-}" in
    *term | xterm-* | rxvt | screen | screen-*)
      >&2 echo -e "   \033[1;32m${program}: \033[1;33mWARN \033[1;37m$1\033[0m"
      ;;
    *)
      # shellcheck disable=2154
      >&2 echo "   ${pkg_name}: WARN $1"
      ;;
  esac
  return 0
}

exit_with() {
  case "${TERM:-}" in
    *term | xterm-* | rxvt | screen | screen-*)
      >&2 printf -- "\033[1;31mERROR: \033[1;37m%s\033[0m\n" "$1"
      ;;
    *)
      >&2 printf -- "ERROR: %s\n" "$1"
      ;;
  esac
  exit "$2"
}

program=$(basename "$0")
rf_version="0.4.1"

# Fix commit range in Travis, if set.
# See: https://github.com/travis-ci/travis-ci/issues/4596
if [ -n "${TRAVIS_COMMIT_RANGE:-}" ]; then
  TRAVIS_COMMIT_RANGE="${TRAVIS_COMMIT_RANGE/.../..}"
fi

info "Checking for rustfmt"
if ! command -v rustfmt >/dev/null; then
  exit_with "Program \`rustfmt' not found on PATH, aborting" 1
fi

failed="$(mktemp -t "$(basename "$0")-failed-XXXX")"
# shellcheck disable=2154
trap 'code=$?; rm -f $failed; exit $code' INT TERM EXIT

if [[ -n "${LINT_ALL:-}" ]]; then
  cmd="find components -type f -name '*.rs'"
  info "Linting all files, selecting files via: '$cmd'"
elif [[ $(git diff --name-only | wc -l) -gt 0 ]]; then
  cmd="git diff --name-only"
  info "Unstaged changes detected, selecting files via: '$cmd'"
elif [[ $(git diff --name-only --cached | wc -l) -gt 0 ]]; then
  cmd="git diff --name-only --cached"
  info "Staged changes detected, selecting files via: '$cmd'"
else
  treeish="${1:-${TRAVIS_COMMIT_RANGE:-${TRAVIS_COMMIT:-HEAD}}}"
  cmd="git diff-tree --no-commit-id --name-only -r $treeish"
  info "Selecting files from Git via: '$cmd'"
fi

eval "$cmd" | while read -r file; do
  case "${file##*.}" in
    rs)
      if [ ! -e "$file" ]; then
        # skip files which were deleted
        break
      fi
      if echo "$file" | grep -q "components/builder-protocol/src/message" >/dev/null; then
        info "Skipping generated Rust code file $file"
        break
      fi
      info "Running rustfmt on $file"
      set +e
      output="$(rustfmt --skip-children --write-mode diff "$file" 2>&1)"<commit_msg>Add missing quotes<commit_after>#!/bin/bash

# Fail if there are any unset variables and whenever a command returns a
# non-zero exit code.
set -eu

# If the variable `$DEBUG` is set, then print the shell commands as we execute.
if [ -n "${DEBUG:-}" ]; then
  set -x
  export DEBUG
fi

info() {
  case "${TERM:-}" in
    *term | xterm-* | rxvt | screen | screen-*)
      printf -- "   \033[1;32m%s: \033[1;37m%s\033[0m\n" "${program}" "$1"
      ;;
    *)
      printf -- "   %s: %s\n" "${program}" "$1"
      ;;
  esac
  return 0
}

warn() {
  case "${TERM:-}" in
    *term | xterm-* | rxvt | screen | screen-*)
      >&2 echo -e "   \033[1;32m${program}: \033[1;33mWARN \033[1;37m$1\033[0m"
      ;;
    *)
      # shellcheck disable=2154
      >&2 echo "   ${pkg_name}: WARN $1"
      ;;
  esac
  return 0
}

exit_with() {
  case "${TERM:-}" in
    *term | xterm-* | rxvt | screen | screen-*)
      >&2 printf -- "\033[1;31mERROR: \033[1;37m%s\033[0m\n" "$1"
      ;;
    *)
      >&2 printf -- "ERROR: %s\n" "$1"
      ;;
  esac
  exit "$2"
}

program=$(basename "$0")
rf_version="0.4.1"

# Fix commit range in Travis, if set.
# See: https://github.com/travis-ci/travis-ci/issues/4596
if [ -n "${TRAVIS_COMMIT_RANGE:-}" ]; then
  TRAVIS_COMMIT_RANGE="${TRAVIS_COMMIT_RANGE/.../..}"
fi

info "Checking for rustfmt"
if ! command -v rustfmt >/dev/null; then
  exit_with "Program \`rustfmt' not found on PATH, aborting" 1
fi

failed="$(mktemp -t "$(basename "$0")-failed-XXXX")"
# shellcheck disable=2154
trap 'code=$?; rm -f "$failed"; exit $code' INT TERM EXIT


if [[ -n "${LINT_ALL:-}" ]]; then
  cmd="find components -type f -name '*.rs'"
  info "Linting all files, selecting files via: '$cmd'"
elif [[ $(git diff --name-only | wc -l) -gt 0 ]]; then
  cmd="git diff --name-only"
  info "Unstaged changes detected, selecting files via: '$cmd'"
elif [[ $(git diff --name-only --cached | wc -l) -gt 0 ]]; then
  cmd="git diff --name-only --cached"
  info "Staged changes detected, selecting files via: '$cmd'"
else
  treeish="${1:-${TRAVIS_COMMIT_RANGE:-${TRAVIS_COMMIT:-HEAD}}}"
  cmd="git diff-tree --no-commit-id --name-only -r $treeish"
  info "Selecting files from Git via: '$cmd'"
fi

eval "$cmd" | while read -r file; do
  case "${file##*.}" in
    rs)
      if [ ! -e "$file" ]; then
        # skip files which were deleted
        break
      fi
      if echo "$file" | grep -q "components/builder-protocol/src/message" >/dev/null; then
        info "Skipping generated Rust code file $file"
        break
      fi
      info "Running rustfmt on $file"
      set +e
      output="$(rustfmt --skip-children --write-mode diff "$file" 2>&1)"
{'fraction_non_alphanumeric': 0.18941398865784498, 'fraction_numerical': 0.034026465028355386, 'mean_word_length': 2.6590594744121714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '68082', 'n_tokens_mistral': 2082, 'n_tokens_neox': 1972, 'n_words': 587}