<commit_before>#!/bin/bash
#
# like ack or ag, but for search and replace. operates from the root
# of a git repo, and fails if not run within a git repo. shows a preview
# of the replacements to be made, and prompts to continue.
#
# $ agr '(\w+)_factory' '\1_factory_factory' [yes|no]
#
# the third argument, when yes, commits with prompting, when no, exits
# without prompting to commit, and when absent, prompts before committing.
#
# depends on silver-searcher (ag) and sed.
#

set -e

while true; do
    [ $(pwd) = "/" ] && echo failed to find git root && exit 1
    ls .git >/dev/null 2>&1 && break || cd ..
done

pattern=$1
replacement=$2
doit=$3
matches=$(ag -s ${pattern})

echo "${matches}" | while read name; do
    path=$(echo $name|cut -d: -f1)
    num=$(echo $name|cut -d: -f2)
    echo ${path}:${num} $(sed -n "${num}p" "${path}") '=>' $(sed -r "${num}s:${pattern}:${replacement}:g" "$path")
done

if [ "${doit}" = "no" ]; then
    proceed=n
elif [ "${doit}" = "yes" ]; then
    proceed=y
else
    echo proceed? y/n
    read -n1 proceed
    echo ""
fi

if [ "${proceed}" = "y" ]; then
    echo "${matches}" | while read name; do
        path=$(echo $name|cut -d: -f1)
        num=$(echo $name|cut -d: -f2)<commit_msg>bugfix agr.sh preview<commit_after>#!/bin/bash
#
# like ack or ag, but for search and replace. operates from the root
# of a git repo, and fails if not run within a git repo. shows a preview
# of the replacements to be made, and prompts to continue.
#
# $ agr '(\w+)_factory' '\1_factory_factory' [yes|no]
#
# the third argument, when yes, commits with prompting, when no, exits
# without prompting to commit, and when absent, prompts before committing.
#
# depends on silver-searcher (ag) and sed.
#

set -e

while true; do
    [ $(pwd) = "/" ] && echo failed to find git root && exit 1
    ls .git >/dev/null 2>&1 && break || cd ..
done

pattern=$1
replacement=$2
doit=$3
matches=$(ag -s "${pattern}")

echo "${matches}" | while read name; do
    path=$(echo $name|cut -d: -f1)
    num=$(echo $name|cut -d: -f2)
    echo ${path}:${num} $(sed -n "${num}p" "${path}") '=>' $(sed -rn "${num}s:${pattern}:${replacement}:gp" "$path")
done

if [ "${doit}" = "no" ]; then
    proceed=n
elif [ "${doit}" = "yes" ]; then
    proceed=y
else
    echo proceed? y/n
    read -n1 proceed
    echo ""
fi

if [ "${proceed}" = "y" ]; then
    echo "${matches}" | while read name; do
        path=$(echo $name|cut -d: -f1)
        num=$(echo $name|cut -d: -f2)
{'fraction_non_alphanumeric': 0.18376413570274636, 'fraction_numerical': 0.009693053311793215, 'mean_word_length': 3.2414383561643834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6902200', 'n_tokens_mistral': 895, 'n_tokens_neox': 874, 'n_words': 321}