-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_context.sh
More file actions
executable file
·168 lines (142 loc) · 4.73 KB
/
collect_context.sh
File metadata and controls
executable file
·168 lines (142 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
set -euo pipefail
# --- Functions ---
# Prints usage information to stderr and exits with the provided code
usage() {
cat >&2 << 'EOF'
Usage: collect_context.sh [-h] [-d <dir>] [-c]
Collects project context including Git information and file contents.
Options:
-h, --help Show this help message and exit
-d <dir> Target repository directory (default: current directory)
-c, --copy Copy output directly to the system clipboard
Examples:
collect_context.sh # Run in current directory
collect_context.sh -d /path/to/project # Run in specific directory
collect_context.sh -c # Copy output to clipboard
collect_context.sh -d /path/to/project -c # Run in directory and copy to clipboard
EOF
exit "${1:-0}"
}
# Routes stdin to the system clipboard using xclip
pipe_to_clipboard() {
# xclip is required and checked in dependencies
cat | xclip -selection clipboard
}
# Checks for required dependencies and exits if any are missing
check_dependencies() {
local deps=("git" "file" "awk" "xclip")
for dep in "${deps[@]}"; do
if ! command -v "$dep" >/dev/null 2>&1; then
echo "Error: Required command '$dep' is not installed." >&2
exit 1
fi
done
}
# Gathers file contents, respecting .gitignore
gather_file_context() {
# 1. Use `git ls-files` to get all relevant files.
# -c = cached (files Git knows about)
# -o = others (untracked files)
# --exclude-standard = respect .gitignore
#
# 2. Pipe the list directly into the `while` loop.
# This is more efficient than storing it in a variable.
git ls-files -c -o --exclude-standard | while IFS= read -r file; do
# 3. Filter token-heavy text files using a case statement
case "$file" in
*lock.json|*.lock|*-lock.yaml|*.min.js) continue ;;
esac
# 4. Check the file's encoding.
local encoding
encoding=$(file -L --mime-encoding -b "$file")
# 5. If the encoding is NOT binary, we can safely print it.
# This is a "deny-list" approach, which is far more robust
# than trying to "allow-list" all possible text types.
if [[ "$encoding" != "binary" ]]; then
echo "### File: \`$file\`"
# 6. Extract extension efficiently using short-circuit evaluation
local ext="${file##*.}"
[[ "$ext" == "$file" ]] && ext=""
# 7. Add syntax highlighting hints
echo '```'"$ext"
# 8. Prevent markdown syntax breakage by ensuring trailing newline
awk 1 "$file"
echo '```'
echo
fi
done
}
# Gathers Git information and prints it to standard output
gather_git_context() {
# Check if we're in a git repo
if ! git rev-parse --is-inside-work-tree &> /dev/null; then
echo "Not a git repository. Skipping git context." >&2
return
fi
echo "### Git Status (Short)"
echo '```'
git status --short
echo '```'
echo
echo "### Git Diff (Unstaged)"
echo '```'
git --no-pager diff
echo '```'
echo
echo "### Git Diff (Staged)"
echo '```'
git --no-pager diff --staged
echo '```'
echo
echo "### Git Log (Last 5)"
echo '```'
git --no-pager log -n 5 --oneline
echo '```'
echo
}
# --- Main Logic ---
main() {
local target_dir="."
local auto_copy=false
# Parse command line arguments using getopts
while getopts ":hd:c" opt; do
case $opt in
h)
usage 0
;;
d)
target_dir="$OPTARG"
;;
c)
auto_copy=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage 1
;;
esac
done
# Shift parsed options so remaining arguments are accessible if needed
shift $((OPTIND-1))
# Navigate to the target directory
cd "$target_dir" || { echo "Error: Cannot access directory $target_dir" >&2; exit 1; }
# Check dependencies
check_dependencies
echo "Gathering project context..." >&2
# Execute context gathering based on the -c flag
if [[ "$auto_copy" == true ]]; then
{ gather_git_context; gather_file_context; } | pipe_to_clipboard
echo "Context successfully copied to clipboard." >&2
else
gather_git_context
gather_file_context
fi
echo "Context output complete." >&2
}
# Pass all script arguments to the main function
main "$@"