Skip to content

Commit 18c907c

Browse files
committed
Allow running without specified files and conditionally output file-related content
- Remove error exit when no files specified to support git-only context generation - Wrap file processing sections in conditional checks using file count - Omit empty "Files" header in markdown format when no files present - Maintain formatting structure while skipping file blocks when unneeded - Preserve existing git information collection capabilities without file dependencies
1 parent 8bb758c commit 18c907c

File tree

1 file changed

+107
-146
lines changed

1 file changed

+107
-146
lines changed

context

Lines changed: 107 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,6 @@ find_files() {
113113
echo "$all_files" | grep -v "^$" | sort | uniq
114114
}
115115

116-
# Handle empty file list
117-
if [ ${#FILES[@]} -eq 0 ]; then
118-
echo "Error: No files specified. Use --files=<pattern> or provide filenames directly."
119-
exit 1
120-
fi
121-
122116
ALL_FILES=$(find_files)
123117
TOTAL_SIZE=0
124118

@@ -145,8 +139,10 @@ case $FORMAT in
145139
md)
146140
echo "# Code Context"
147141
echo ""
148-
echo "## Files"
149-
echo ""
142+
if [ ${#FILES[@]} -gt 0 ]; then
143+
echo "## Files"
144+
echo ""
145+
fi
150146
;;
151147
json)
152148
echo "{"
@@ -159,113 +155,104 @@ case $FORMAT in
159155
;;
160156
esac
161157

162-
# Process each file
158+
# Process each file (only if files were specified)
163159
file_count=0
164-
for file in $ALL_FILES; do
165-
if [ ! -f "$file" ]; then
166-
if [ "$VERBOSE" = true ]; then
167-
echo "Warning: File not found: $file" >&2
168-
fi
169-
continue
170-
fi
171-
172-
file_size=$(wc -c < "$file")
173-
TOTAL_SIZE=$((TOTAL_SIZE + file_size))
174-
truncated=false
175-
file_content=""
176-
177-
# Check if we need to truncate the file
178-
if [ $TRUNCATE_SIZE_BYTES -gt 0 ] && [ $file_size -gt $TRUNCATE_SIZE_BYTES ]; then
179-
file_content=$(head -c $TRUNCATE_SIZE_BYTES "$file")
180-
truncated=true
181-
else
182-
file_content=$(cat "$file")
183-
fi
184-
185-
# Get file summary if needed
186-
file_summary=""
187-
if [ "$SUMMARY" = true ]; then
188-
file_summary=$(head -n 20 "$file" | grep -E "^(//|#|/*) " | head -n 5 | sed 's/^[\/\#\* ]*//')
189-
fi
190-
191-
# Format the output based on chosen format
192-
case $FORMAT in
193-
md)
194-
echo "### $file"
195-
if [ "$SHOW_FILE_SIZES" = true ]; then
196-
echo ""
197-
echo "Size: $(human_readable_size $file_size)"
198-
echo ""
160+
if [ ${#FILES[@]} -gt 0 ]; then
161+
for file in $ALL_FILES; do
162+
if [ ! -f "$file" ]; then
163+
if [ "$VERBOSE" = true ]; then
164+
echo "Warning: File not found: $file" >&2
199165
fi
200-
201-
if [ "$SUMMARY" = true ] && [ -n "$file_summary" ]; then
202-
echo ""
203-
echo "Summary:"
204-
echo "$file_summary"
166+
continue
167+
fi
168+
169+
file_size=$(wc -c < "$file")
170+
TOTAL_SIZE=$((TOTAL_SIZE + file_size))
171+
truncated=false
172+
file_content=""
173+
174+
# Check if we need to truncate the file
175+
if [ $TRUNCATE_SIZE_BYTES -gt 0 ] && [ $file_size -gt $TRUNCATE_SIZE_BYTES ]; then
176+
file_content=$(head -c $TRUNCATE_SIZE_BYTES "$file")
177+
truncated=true
178+
else
179+
file_content=$(cat "$file")
180+
fi
181+
182+
# Get file summary if needed
183+
file_summary=""
184+
if [ "$SUMMARY" = true ]; then
185+
file_summary=$(head -n 20 "$file" | grep -E "^(//|#|/*) " | head -n 5 | sed 's/^[\/\#\* ]*//')
186+
fi
187+
188+
# Format the output based on chosen format
189+
case $FORMAT in
190+
md)
191+
if [ "$SHOW_FILE_SIZES" = true ]; then
192+
echo "Size: $(human_readable_size $file_size)"
193+
echo ""
194+
fi
195+
196+
if [ "$SUMMARY" = true ] && [ -n "$file_summary" ]; then
197+
echo "Summary:"
198+
echo "$file_summary"
199+
echo ""
200+
fi
201+
202+
echo "``````${file##*.} $file"
203+
echo "$file_content"
204+
if [ "$truncated" = true ]; then
205+
echo ""
206+
echo "... [File truncated due to size limit] ..."
207+
fi
208+
echo "``````"
205209
echo ""
206-
fi
207-
208-
echo ""
209-
echo '```'${file##*.}
210-
echo "$file_content"
211-
if [ "$truncated" = true ]; then
210+
;;
211+
json)
212+
# Escape special characters for JSON
213+
file_content_json=$(echo "$file_content" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
214+
if [ $file_count -gt 0 ]; then
215+
echo " ,"
216+
fi
217+
echo " {"
218+
echo " \"path\": \"$file\","
219+
if [ "$SHOW_FILE_SIZES" = true ]; then
220+
echo " \"size\": $file_size,"
221+
fi
222+
if [ "$SUMMARY" = true ] && [ -n "$file_summary" ]; then
223+
file_summary_json=$(echo "$file_summary" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
224+
echo " \"summary\": \"$file_summary_json\","
225+
fi
226+
echo " \"content\": \"$file_content_json\""
227+
if [ "$truncated" = true ]; then
228+
echo " \"truncated\": true"
229+
fi
230+
echo -n " }"
231+
;;
232+
text)
233+
echo "=== $file ==="
234+
if [ "$SHOW_FILE_SIZES" = true ]; then
235+
echo "Size: $(human_readable_size $file_size)"
236+
fi
237+
238+
if [ "$SUMMARY" = true ] && [ -n "$file_summary" ]; then
239+
echo "Summary:"
240+
echo "$file_summary"
241+
fi
242+
243+
echo "-----"
244+
echo "$file_content"
245+
if [ "$truncated" = true ]; then
246+
echo "... [File truncated due to size limit] ..."
247+
fi
248+
echo "-----"
212249
echo ""
213-
echo "... [File truncated due to size limit] ..."
214-
fi
215-
echo '```'
216-
echo ""
217-
;;
218-
json)
219-
# Escape special characters for JSON
220-
file_content_json=$(echo "$file_content" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
221-
if [ $file_count -gt 0 ]; then
222-
echo " ,"
223-
fi
224-
echo " {"
225-
echo " \"path\": \"$file\","
226-
if [ "$SHOW_FILE_SIZES" = true ]; then
227-
echo " \"size\": $file_size,"
228-
fi
229-
if [ "$SUMMARY" = true ] && [ -n "$file_summary" ]; then
230-
file_summary_json=$(echo "$file_summary" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
231-
echo " \"summary\": \"$file_summary_json\","
232-
fi
233-
echo " \"content\": \"$file_content_json\""
234-
if [ "$truncated" = true ]; then
235-
echo " \"truncated\": true"
236-
fi
237-
echo -n " }"
238-
;;
239-
text)
240-
echo "=== $file ==="
241-
if [ "$SHOW_FILE_SIZES" = true ]; then
242-
echo "Size: $(human_readable_size $file_size)"
243-
fi
244-
245-
if [ "$SUMMARY" = true ] && [ -n "$file_summary" ]; then
246-
echo "Summary:"
247-
echo "$file_summary"
248-
fi
249-
250-
echo "-----"
251-
echo "$file_content"
252-
if [ "$truncated" = true ]; then
253-
echo "... [File truncated due to size limit] ..."
254-
fi
255-
echo "-----"
256-
echo ""
257-
;;
258-
esac
259-
260-
file_count=$((file_count + 1))
261-
262-
# Note: Dependency resolution disabled for now
263-
if [ "$INCLUDE_DEPS" = true ] && [ $DEPTH -gt 0 ]; then
264-
if [ "$VERBOSE" = true ]; then
265-
echo "Note: Dependency resolution is currently disabled" >&2
266-
fi
267-
fi
268-
done
250+
;;
251+
esac
252+
253+
file_count=$((file_count + 1))
254+
done
255+
fi
269256

270257
# Include git information if requested
271258
if [ "$INCLUDE_GIT" = true ] && command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
@@ -324,11 +311,9 @@ fi
324311

325312
# Include git ls-files output if requested and we're in a git repo
326313
if [ "$INCLUDE_LS_FILES" = true ] && command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
327-
# Get the list of files tracked by git
328314
GIT_FILES=$(git ls-files | sort)
329315
GIT_FILES_COUNT=$(echo "$GIT_FILES" | wc -l)
330316

331-
# Apply limit if needed
332317
if [ $GIT_FILES_COUNT -gt $LS_FILES_LIMIT ]; then
333318
GIT_FILES=$(echo "$GIT_FILES" | head -n $LS_FILES_LIMIT)
334319
GIT_FILES_TRUNCATED=true
@@ -347,8 +332,6 @@ if [ "$INCLUDE_LS_FILES" = true ] && command -v git >/dev/null 2>&1 && git rev-p
347332
echo "- ... (and $(($GIT_FILES_COUNT - $LS_FILES_LIMIT)) more files)"
348333
fi
349334
echo ""
350-
echo "Total files in repository: $GIT_FILES_COUNT"
351-
echo ""
352335
;;
353336
json)
354337
echo " ,"
@@ -380,8 +363,6 @@ if [ "$INCLUDE_LS_FILES" = true ] && command -v git >/dev/null 2>&1 && git rev-p
380363
echo "* ... (and $(($GIT_FILES_COUNT - $LS_FILES_LIMIT)) more files)"
381364
fi
382365
echo ""
383-
echo "Total files in repository: $GIT_FILES_COUNT"
384-
echo ""
385366
;;
386367
esac
387368
fi
@@ -391,7 +372,6 @@ if [ "$FORMAT" = "json" ]; then
391372
echo ""
392373
echo " ]"
393374

394-
# Include prompt if specified
395375
if [ -n "$PROMPT_FILE" ] && [ -f "$PROMPT_FILE" ]; then
396376
prompt_content=$(cat "$PROMPT_FILE" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
397377
echo " ,"
@@ -400,43 +380,24 @@ if [ "$FORMAT" = "json" ]; then
400380

401381
echo "}"
402382
else
403-
# Add context generation footer
404-
case $FORMAT in
405-
md)
406-
echo "## Context Information"
407-
echo ""
408-
echo "Generated on: $(date)"
409-
echo "Total files: $file_count"
410-
echo "Total size: $(human_readable_size $TOTAL_SIZE)"
411-
echo ""
412-
413-
# Include prompt template if specified
414-
if [ -n "$PROMPT_FILE" ] && [ -f "$PROMPT_FILE" ]; then
383+
# Include prompt template if specified
384+
if [ -n "$PROMPT_FILE" ] && [ -f "$PROMPT_FILE" ]; then
385+
case $FORMAT in
386+
md)
415387
echo "## Instructions for LLM"
416388
echo ""
417389
cat "$PROMPT_FILE"
418390
echo ""
419-
fi
420-
;;
421-
text)
422-
echo "CONTEXT INFORMATION"
423-
echo "------------------"
424-
echo ""
425-
echo "Generated on: $(date)"
426-
echo "Total files: $file_count"
427-
echo "Total size: $(human_readable_size $TOTAL_SIZE)"
428-
echo ""
429-
430-
# Include prompt template if specified
431-
if [ -n "$PROMPT_FILE" ] && [ -f "$PROMPT_FILE" ]; then
391+
;;
392+
text)
432393
echo "INSTRUCTIONS FOR LLM"
433394
echo "-------------------"
434395
echo ""
435396
cat "$PROMPT_FILE"
436397
echo ""
437-
fi
438-
;;
439-
esac
398+
;;
399+
esac
400+
fi
440401
fi
441402

442403
exit 0

0 commit comments

Comments
 (0)