Skip to content

Commit a0bbe5d

Browse files
committed
ls-files
1 parent 902e6d5 commit a0bbe5d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

context

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ SHOW_FILE_SIZES=false
1717
TRUNCATE_LARGE=""
1818
PROMPT_FILE=""
1919
VERBOSE=false
20+
INCLUDE_LS_FILES=true
21+
LS_FILES_LIMIT=100
2022

2123
# Parse arguments
2224
while [[ "$#" -gt 0 ]]; do
@@ -34,6 +36,9 @@ while [[ "$#" -gt 0 ]]; do
3436
--truncate-large=*) TRUNCATE_LARGE="${1#*=}"; shift ;;
3537
--prompt=*) PROMPT_FILE="${1#*=}"; shift ;;
3638
--verbose) VERBOSE=true; shift ;;
39+
--ls-files) INCLUDE_LS_FILES=true; shift ;;
40+
--no-ls-files) INCLUDE_LS_FILES=false; shift ;;
41+
--ls-files-limit=*) LS_FILES_LIMIT="${1#*=}"; shift ;;
3742
--help|-h)
3843
echo "Usage: ./context [options] [file1 file2 ...]"
3944
echo ""
@@ -51,6 +56,9 @@ while [[ "$#" -gt 0 ]]; do
5156
echo " --truncate-large=<size> Truncate files larger than specified size (e.g., \"50KB\")"
5257
echo " --prompt=<file> Include prompt template from specified file"
5358
echo " --verbose Show verbose output"
59+
echo " --ls-files Include git ls-files output (default: true)"
60+
echo " --no-ls-files Don't include git ls-files output"
61+
echo " --ls-files-limit=<num> Limit the number of files shown in ls-files (default: 100)"
5462
echo " --help, -h Show this help message"
5563
exit 0
5664
;;
@@ -314,6 +322,70 @@ if [ "$INCLUDE_GIT" = true ] && command -v git >/dev/null 2>&1 && git rev-parse
314322
esac
315323
fi
316324

325+
# Include git ls-files output if requested and we're in a git repo
326+
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
328+
GIT_FILES=$(git ls-files | sort)
329+
GIT_FILES_COUNT=$(echo "$GIT_FILES" | wc -l)
330+
331+
# Apply limit if needed
332+
if [ $GIT_FILES_COUNT -gt $LS_FILES_LIMIT ]; then
333+
GIT_FILES=$(echo "$GIT_FILES" | head -n $LS_FILES_LIMIT)
334+
GIT_FILES_TRUNCATED=true
335+
else
336+
GIT_FILES_TRUNCATED=false
337+
fi
338+
339+
case $FORMAT in
340+
md)
341+
echo "## Repository Files"
342+
echo ""
343+
echo "$GIT_FILES" | while read -r file; do
344+
echo "- $file"
345+
done
346+
if [ "$GIT_FILES_TRUNCATED" = true ]; then
347+
echo "- ... (and $(($GIT_FILES_COUNT - $LS_FILES_LIMIT)) more files)"
348+
fi
349+
echo ""
350+
echo "Total files in repository: $GIT_FILES_COUNT"
351+
echo ""
352+
;;
353+
json)
354+
echo " ,"
355+
echo " \"repository_files\": {"
356+
echo " \"count\": $GIT_FILES_COUNT,"
357+
echo " \"truncated\": $GIT_FILES_TRUNCATED,"
358+
echo " \"files\": ["
359+
first=true
360+
echo "$GIT_FILES" | while read -r file; do
361+
if [ "$first" = true ]; then
362+
first=false
363+
else
364+
echo ","
365+
fi
366+
echo -n " \"$file\""
367+
done
368+
echo ""
369+
echo " ]"
370+
echo " }"
371+
;;
372+
text)
373+
echo "REPOSITORY FILES"
374+
echo "---------------"
375+
echo ""
376+
echo "$GIT_FILES" | while read -r file; do
377+
echo "* $file"
378+
done
379+
if [ "$GIT_FILES_TRUNCATED" = true ]; then
380+
echo "* ... (and $(($GIT_FILES_COUNT - $LS_FILES_LIMIT)) more files)"
381+
fi
382+
echo ""
383+
echo "Total files in repository: $GIT_FILES_COUNT"
384+
echo ""
385+
;;
386+
esac
387+
fi
388+
317389
# Close JSON if needed
318390
if [ "$FORMAT" = "json" ]; then
319391
echo ""

docs/context.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ The `context` tool extracts relevant code and context from your codebase to send
3434
| `--show-file-sizes` | Include file sizes in output | False |
3535
| `--truncate-large=<size>` | Truncate files larger than specified size (e.g., "50KB") | None |
3636
| `--prompt=<file>` | Include prompt template from specified file | None |
37+
| `--ls-files` | Include git ls-files output | True |
38+
| `--no-ls-files` | Don't include git ls-files output | False |
39+
| `--ls-files-limit=<num>` | Limit the number of files shown in ls-files | 100 |
3740

3841
## Examples
3942

0 commit comments

Comments
 (0)