Skip to content

Commit 9f2ed49

Browse files
committed
Refactor shell scripts with shellcheck improvements and quoting
- Add proper quoting for variables in context and git-context scripts - Use shellcheck recommended fixes for safer shell scripting - Improve readability and prevent potential shell expansion issues - Ensure consistent and safer command execution
1 parent eb9716c commit 9f2ed49

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

context

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ done
5454
convert_to_bytes() {
5555
local size=$1
5656
local num=${size%[KMG]*}
57-
local unit=${size#$num}
57+
local unit=${size#"$num"}
5858

5959
case $unit in
6060
KB|K) echo $((num * 1024)) ;;
6161
MB|M) echo $((num * 1024 * 1024)) ;;
6262
GB|G) echo $((num * 1024 * 1024 * 1024)) ;;
63-
*) echo $num ;;
63+
*) echo "$num" ;;
6464
esac
6565
}
6666

@@ -75,7 +75,8 @@ find_files() {
7575
all_files="$all_files"$'\n'"$pattern"
7676
else
7777
local find_cmd="find . -type f -path \"$pattern\""
78-
local found_files=$(eval $find_cmd)
78+
local found_files
79+
found_files=$(eval "$find_cmd")
7980
if [ -n "$found_files" ]; then
8081
all_files="$all_files"$'\n'"$found_files"
8182
fi
@@ -100,12 +101,12 @@ human_readable_size() {
100101
local unit_index=0
101102
local size_float=$size
102103

103-
while [ $(echo "$size_float >= 1024" | bc -l) -eq 1 ] && [ $unit_index -lt 4 ]; do
104+
while [ "$(echo "$size_float >= 1024" | bc -l)" -eq 1 ] && [ $unit_index -lt 4 ]; do
104105
size_float=$(echo "scale=2; $size_float / 1024" | bc -l)
105106
unit_index=$((unit_index + 1))
106107
done
107108

108-
size_float=$(echo $size_float | sed 's/\.0*$//')
109+
size_float=${size_float%.0*}
109110
echo "$size_float ${units[$unit_index]}"
110111
}
111112

@@ -121,7 +122,7 @@ if [ "$INCLUDE_GIT" = true ] && command -v git >/dev/null 2>&1 && git rev-parse
121122
echo ""
122123
echo "Recent commits:"
123124
echo ""
124-
git log -n 3 --pretty=format:"* %h: %s (%an, %ar)" | while read line; do
125+
git log -n 3 --pretty=format:"* %h: %s (%an, %ar)" | while read -r line; do
125126
echo "$line"
126127
done
127128
echo ""
@@ -150,15 +151,15 @@ for file in $ALL_FILES; do
150151
file_size=$(wc -c < "$file")
151152
TOTAL_SIZE=$((TOTAL_SIZE + file_size))
152153

153-
if [ $TOTAL_SIZE -gt $MAX_SIZE_BYTES ]; then
154-
echo "Error: Total context size exceeds maximum allowed size ($(human_readable_size $MAX_SIZE_BYTES)). Reduce file scope or increase --max-size." >&2
154+
if [ $TOTAL_SIZE -gt "$MAX_SIZE_BYTES" ]; then
155+
echo "Error: Total context size exceeds maximum allowed size ($(human_readable_size "$MAX_SIZE_BYTES")). Reduce file scope or increase --max-size." >&2
155156
exit 1
156157
fi
157158

158159
file_content=$(cat "$file")
159160

160161
if [ "$SHOW_FILE_SIZES" = true ]; then
161-
echo "### $file ($(human_readable_size $file_size))"
162+
echo "### $file ($(human_readable_size "$file_size"))"
162163
else
163164
echo "### $file"
164165
fi

git-context

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fi
9292
if [ $RECENT_COMMITS -gt 0 ]; then
9393
echo "## Recent Commits (for reference)"
9494
echo ""
95-
git log -n $RECENT_COMMITS --pretty=format:"- %h: %s (%an, %ar)" | while read line; do
95+
git log -n $RECENT_COMMITS --pretty=format:"- %h: %s (%an, %ar)" | while read -r line; do
9696
echo "$line"
9797
done
9898
echo ""

0 commit comments

Comments
 (0)