Repository: risingwavelabs/risingwave
File: scripts/check/check-trailing-spaces.sh (Shell)

Code before:
    -f | --fix)
        fix=true
        ;;
    -s | --skip-dir)
        shift
        if [ $# -eq 0 ]; then
            echo -e "${RED}${BOLD}$self: missing argument for --skip-dir${NONE}"
            print_help
            exit 1
        fi
        skip_dirs+=("$1")
        ;;
    -h | --help)
        print_help
        exit 0
        ;;
    *)
        echo -e "${RED}${BOLD}$self: invalid option \`$1\`\n${NONE}"
        print_help
        exit 1
        ;;
    esac
    shift
done

temp_file=$(mktemp)

echo -ne "${BLUE}"
# Build git pathspec exclusions. `--` separates revs/options from pathspecs.
# Default: search repo root, but skip `.cargo/` and `src/tests/regress/`.
git_grep_pathspec=(-- ':' ':!.cargo' ':!src/tests/regress')
if [ ${#skip_dirs[@]} -gt 0 ]; then
    for d in "${skip_dirs[@]}"; do
        # Normalize leading "./" to keep the pathspec consistent.
        d="${d#./}"
        git_grep_pathspec+=(":!${d}")
    done
fi

git grep -nIP --untracked '[[:space:]]+$' "${git_grep_pathspec[@]}" | tee "$temp_file" || true
echo -ne "${NONE}"

bad_files=$(cat "$temp_file" | cut -f1 -d ':' | sort -u)
rm "$temp_file"

# Portable in-place sed (GNU vs BSD/macOS).
sed_inplace() {
    local expr=$1
    local file=$2
    if sed --version >/dev/null 2>&1; then
        # GNU sed

Review comment (bug):
The sed command is now hard-coded to use BSD/macOS syntax (with the empty string argument after -i). This breaks compatibility with GNU sed which is used on Linux systems. The original version had portable sed logic that worked on both platforms. This will cause the --fix option to fail on Linux CI runners and developer machines.
```suggestion
        # Determine portable in-place edit flag for sed (GNU vs BSD/macOS).
        if sed --version >/dev/null 2>&1; then
            # GNU sed supports `-i` without a backup suffix.
            SED_INPLACE=(-i)
        else
            # BSD/macOS sed requires an explicit backup suffix argument.
            SED_INPLACE=(-i '')
        fi

        for file in $bad_files; do
            sed "${SED_INPLACE[@]}" -e's/[[:space:]]*$//' "$file"
```

Code after:
    -h | --help)
        print_help
        exit 0
        ;;
    *)
        echo -e "${RED}${BOLD}$self: invalid option \`$1\`\n${NONE}"
        print_help
        exit 1
        ;;
    esac
    shift
done

temp_file=$(mktemp)

echo -ne "${BLUE}"
git grep -nIP --untracked '[[:space:]]+$' -- ':!src/tests/regress/data' | tee $temp_file || true
echo -ne "${NONE}"

bad_files=$(cat $temp_file | cut -f1 -d ':' | sort -u)
rm $temp_file

if [ ! -z "$bad_files" ]; then
    if [[ $fix == true ]]; then
        for file in $bad_files; do
            sed -i '' -e's/[[:space:]]*$//' "$file"
        done

        echo
        echo -e "${GREEN}${BOLD}All trailing spaces listed above have been cleaned.${NONE}"
        exit 0
    else
        echo
        echo -e "${RED}${BOLD}Please clean all the trailing spaces listed above.${NONE}"
        echo -e "${BOLD}You can run '$self --fix' for convenience.${NONE}"
        exit 1
    fi
else
    echo -e "${GREEN}${BOLD}No trailing spaces found.${NONE}"
    exit 0
fi