<commit_before>#!/bin/bash

set -o errexit
set -o nounset

case $OSTYPE in
darwin*) SED=gsed;;
*) SED=sed;;
esac

release_date="$(date +%Y-%m-%d)"

verify_args() {
  echo "Cutting a release for version $version with commit $commit"
  while true; do
    read -rp "Is this correct (y/n)?" yn
    case $yn in
    [Yy]*) break;;
    [Nn]*) exit;;
    *) echo "Please answer y or n.";;
    esac
  done
}

update_version() {
  echo "$version" > VERSION
  echo "VERSION set to $version"
}

update_changelog() {
  # cut out empty "Fixed", "Added", or "Changed" sections
  remove_empty_sections

  local match="## \[unreleased\] \- unreleased"
  local heading="## [$version] - $release_date"
  $SED -i "/$match/c $heading" CHANGELOG.md
}

remove_empty_sections() {
  for section in "Fixed" "Added" "Changed"; do
    # check if first non-whitespace character after section heading is '-'
    local str_after
    local first_word
    str_after="$(SED "1,/### $section/d" <CHANGELOG.md)"
    # Do not put quotes around $str_after!
    first_word="$(echo $str_after | head -n1 | cut -d " " -f1)"
    if [[ "$first_word" != "-" ]]; then
      echo "Removing empty CHANGELOG section: $section"
      local line_num
      line_num="$(grep -n -m1 "$section" CHANGELOG.md | cut -d : -f 1)"
      $SED -i "$line_num{N;N;d}" CHANGELOG.md
    fi
  done
}

add_changelog_unreleased_section() {
  echo "Adding unreleased section to CHANGELOG"
  local replace='## [unreleased] - unreleased\n\n### Fixed\n\n\n### Added\n\n\n### Changed\n\n\n'
  $SED -i "/## \[$version\] \- $release_date/i $replace" CHANGELOG.md
}

check_for_commit_and_push() {
  printf "Would you like to push CHANGELOG updates to master (y/n)? "
  while true; do
    read -r yn
    case $yn in<commit_msg>More safely remove empty CHANGELOG entries (#1827)<commit_after>#!/bin/bash

set -o errexit
set -o nounset

case $OSTYPE in
darwin*) SED=gsed;;
*) SED=sed;;
esac

release_date="$(date +%Y-%m-%d)"

verify_args() {
  echo "Cutting a release for version $version with commit $commit"
  while true; do
    read -rp "Is this correct (y/n)?" yn
    case $yn in
    [Yy]*) break;;
    [Nn]*) exit;;
    *) echo "Please answer y or n.";;
    esac
  done
}

update_version() {
  echo "$version" > VERSION
  echo "VERSION set to $version"
}

update_changelog() {
  # cut out empty "Fixed", "Added", or "Changed" sections
  remove_empty_sections

  local match="## \[unreleased\] \- unreleased"
  local heading="## [$version] - $release_date"
  $SED -i "/$match/c $heading" CHANGELOG.md
}

remove_empty_sections() {
  local unreleased_section
  unreleased_section="$($SED -n "/## \[unreleased\] - unreleased/,/## \[/p" < CHANGELOG.md)"
  if [[ "$unreleased_section" != "" ]]; then
    for section in "Fixed" "Added" "Changed"; do
      local section_text
      local first
      section_text="$(echo "$unreleased_section" | $SED -n "/### $section/{:a;n;/##/b;p;ba}")"
      first="$(echo "$section_text" | $SED "/^\s*$/d" | cut -c 1)"
      if [[ "$first" == "" ]]; then
        echo "Removing empty CHANGELOG section: $section"
        local line_num
        line_num="$(grep -n -m1 "$section" CHANGELOG.md | cut -d : -f 1)"
        $SED -i "$line_num{N;N;d}" CHANGELOG.md
      fi
    done
  fi
}

add_changelog_unreleased_section() {
  echo "Adding unreleased section to CHANGELOG"
  local replace='## [unreleased] - unreleased\n\n### Fixed\n\n\n### Added\n\n\n### Changed\n\n\n'
  $SED -i "/## \[$version\] \- $release_date/i $replace" CHANGELOG.md
}

check_for_commit_and_push() {
  printf "Would you like to push CHANGELOG updates to master (y/n)? "
  while true; do
    read -r yn
    case $yn in
{'fraction_non_alphanumeric': 0.16831411312465677, 'fraction_numerical': 0.0032948929159802307, 'mean_word_length': 3.2213209733487833, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5779049', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1346, 'n_words': 381}