From a3d83dee37c43bff97fcba58b9f928d550420c47 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 21 Dec 2025 17:07:31 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20file=20reading?= =?UTF-8?q?=20pipeline=20and=20fix=20compatibility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 💡 What: - Replaced `mapfile` (Bash 4+) with `while read` loop for Bash 3.2 compatibility (macOS). - Optimized file content reading by replacing `cat file | sed | tr` with `tr < file`. - Removed broken `sed 's// /g'` command which was causing data loss (empty output). 🎯 Why: - The script was crashing on macOS due to `mapfile`. - The broken `sed` command was causing all files to be skipped as empty. - Avoids 2 process forks per file, significantly reducing system overhead. 📊 Impact: - Fixes critical bug where output was empty. - Enables support for macOS (default Bash 3.2). - Reduces system calls by ~66% per file during content extraction. 🔬 Measurement: - Verified with `repro_bug.sh` that content is now correctly extracted. - Passed `test/test_basic.sh` and `test/test_features.sh`. --- .jules/bolt.md | 3 +++ codepack.sh | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..6eacc14 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-12-21 - [Bash Pipeline Optimization] +**Learning:** Avoid useless pipes like `cat | sed`. Using input redirection `tr < file` saved 2 process forks per file and fixed a data loss bug caused by `sed 's// /g'`. +**Action:** Always prefer redirection over `cat` for single file inputs. diff --git a/codepack.sh b/codepack.sh index 0d8ae2c..4159a9a 100755 --- a/codepack.sh +++ b/codepack.sh @@ -851,7 +851,9 @@ extract_files_content() { # Read file content and clean invalid characters local content="" if [[ -r "$file" && -s "$file" ]]; then - content=$(cat "$file" 2>/dev/null | sed 's// /g' 2>/dev/null | tr -cd '\11\12\15\40-\176' 2>/dev/null || echo "") + # Optimization: Use redirection < "$file" instead of cat to save a process fork + # Removed broken sed 's// /g' which caused empty output on some systems + content=$(tr -cd '\11\12\15\40-\176' < "$file" 2>/dev/null || echo "") fi debug_log "Content length: ${#content}" >&2 @@ -943,7 +945,12 @@ main() { echo "🗂️ Generation in progress, please wait ..." # Capture files list once to avoid double traversal - mapfile -t files < <(list_files_to_process "$directory") + # Using while loop instead of mapfile for Bash 3.2 compatibility (macOS) + files=() + while IFS= read -r file; do + files+=("$file") + done < <(list_files_to_process "$directory") + total_files=${#files[@]} formatted_total=$(format_number "$total_files") echo "Found $formatted_total files to process"