Repository: etcd-io/etcd
File: scripts/markdown_diff_lint.sh (Shell)

Code before:
#!/usr/bin/env bash
# This script runs markdownlint-cli2 on changed files.
# Usage: ./markdown_lint.sh

set -eo pipefail

if ! command markdownlint-cli2 dummy.md &>/dev/null; then
  echo "markdownlint-cli2 needs to be installed."
  echo "Please refer to https://github.com/DavidAnson/markdownlint-cli2?tab=readme-ov-file#install for installation instructions."
  exit 1
fi

COLOR_RED='\033[0;31m'
COLOR_BOLD='\033[1m'
COLOR_ORANGE='\033[0;33m'
COLOR_NONE='\033[0m' # No Color

function log_error {
  echo -n -e "${COLOR_BOLD}${COLOR_RED}$*${COLOR_NONE}\n"
}

function log_warning {
  echo -n -e "${COLOR_ORANGE}$*${COLOR_NONE}\n"
}

if [ -z "${PULL_BASE_SHA}" ]; then
  echo "Empty base reference (\$PULL_BASE_SHA), assuming: main"
  PULL_BASE_SHA=main
fi

if [ -z "${PULL_PULL_SHA}" ]; then
  PULL_PULL_SHA="$(git rev-parse HEAD)"
  echo "Empty pull reference (\$PULL_PULL_SHA), assuming: ${PULL_PULL_SHA}"
fi

MD_LINT_URL_PREFIX="https://github.com/DavidAnson/markdownlint/blob/main/doc/"

mapfile -t changed_files < <(git diff "${PULL_BASE_SHA}" --name-only)
declare -A files_with_failures start_ranges end_ranges

for file in "${changed_files[@]}"; do
  if ! [[ "$file" =~ .md$ ]]; then
    continue
  fi

  # Find start and end ranges from changed files.
  start_ranges=()
  end_ranges=()
  # From https://github.com/paleite/eslint-plugin-diff/blob/46c5bcf296e9928db19333288457bf2805aad3b9/src/git.ts#L8-L27

Review comment (suggestion):
We can benefit from sourcing `scripts/test_lib.sh`, which defines these functions and color codes. Therefore, we can remove these definitions.

Code after:
#!/usr/bin/env bash
# This script runs markdownlint-cli2 on changed files.
# Usage: ./markdown_lint.sh


# We source ./scripts/test_lib.sh, it sets the log functions and color variables.
source ./scripts/test_lib.sh

# When we source ./scripts/test_lib.sh, it has the line set -u which treats unset variables as errors.
# We need to unset the variable to avoid the error.
set +u -eo pipefail

if ! command markdownlint-cli2 dummy.md &>/dev/null; then
  log_error "markdownlint-cli2 needs to be installed."
  log_error "Please refer to https://github.com/DavidAnson/markdownlint-cli2?tab=readme-ov-file#install for installation instructions."
  exit 1
fi


if [ -z "${PULL_BASE_SHA}" ]; then
  echo "Empty base reference (\$PULL_BASE_SHA), assuming: main"
  PULL_BASE_SHA=main
fi

if [ -z "${PULL_PULL_SHA}" ]; then
  PULL_PULL_SHA="$(git rev-parse HEAD)"
  echo "Empty pull reference (\$PULL_PULL_SHA), assuming: ${PULL_PULL_SHA}"
fi

MD_LINT_URL_PREFIX="https://github.com/DavidAnson/markdownlint/blob/main/doc/"

mapfile -t changed_files < <(git diff "${PULL_BASE_SHA}" --name-only)
declare -A files_with_failures start_ranges end_ranges

for file in "${changed_files[@]}"; do
  if ! [[ "$file" =~ .md$ ]]; then
    continue
  fi

  # Find start and end ranges from changed files.
  start_ranges=()
  end_ranges=()
  # From https://github.com/paleite/eslint-plugin-diff/blob/46c5bcf296e9928db19333288457bf2805aad3b9/src/git.ts#L8-L27
  ranges=$(git diff "${PULL_BASE_SHA}" \
           --diff-algorithm=histogram \
           --diff-filter=ACM \
           --find-renames=100% \
           --no-ext-diff \
           --relative \