tools/ai/repo-structure: fix on mac/zsh

This commit is contained in:
Enrico Ros
2025-04-04 15:10:47 -07:00
parent 3a8bfb0bb1
commit 57346617a5
+59 -39
View File
@@ -31,7 +31,7 @@ done
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "Error: This script must be run from within a Git repository." >&2
echo "Please navigate to your cloned Big-AGI repository and try again." >&2
echo "Please navigate to your repository and try again." >&2
exit 1
fi
@@ -60,8 +60,9 @@ generate_structure() {
echo '<context name="directoryStructure" description="Below is a snapshot of this project root file structure (git ls-files) at the start of the conversation. This snapshot will NOT update during the conversation.">'
echo ""
# Create a temporary file for the file list
# Create temporary files
TMP_FILE=$(mktemp)
PROCESSED_DIRS_FILE=$(mktemp)
# Get all Git-tracked files, filtering hidden ones if needed
if [[ "$INCLUDE_HIDDEN" == false ]]; then
@@ -75,62 +76,81 @@ generate_structure() {
# Check if TMP_FILE is empty after filtering
if [[ ! -s "$TMP_FILE" ]]; then
echo "# (No files found matching criteria)"
rm "$TMP_FILE"
rm "$TMP_FILE" "$PROCESSED_DIRS_FILE"
echo '</context>' # Close the tag even if empty
return
fi
# Initialize an associative array (requires bash 4+) for directories
declare -A PROCESSED_DIRS
# Process each file
while IFS= read -r file; do
# Skip empty lines
if [[ -z "$file" ]]; then
continue
fi
# Split the path into components
IFS='/' read -ra PATH_PARTS <<< "$file"
# Initialize directory tracking
CURRENT_PATH=""
PATH_LENGTH=${#PATH_PARTS[@]}
# Process all directories in the path except the last part (which is the file)
for ((i=0; i<PATH_LENGTH-1; i++)); do
part=${PATH_PARTS[i]}
if [[ -z "$CURRENT_PATH" ]]; then
CURRENT_PATH="$part"
# Extract directory parts using dirname and basename
filename=$(basename "$file")
dirpath=$(dirname "$file")
# Process the directory path
if [[ "$dirpath" != "." ]]; then
# Split directory path into parts
current_path=""
dir_parts=""
# Use a safer method to split the path
IFS='/' dir_parts=($dirpath)
# Process each directory level
for ((i=0; i<${#dir_parts[@]}; i++)); do
part="${dir_parts[$i]}"
if [[ -z "$part" ]]; then
continue
fi
if [[ -z "$current_path" ]]; then
current_path="$part"
else
current_path="$current_path/$part"
fi
# Check if we've already processed this directory
if ! grep -q "^$current_path\$" "$PROCESSED_DIRS_FILE" 2>/dev/null; then
echo "$current_path" >> "$PROCESSED_DIRS_FILE"
indent=$((i * 2))
# Fix the printf issue by ensuring the format string doesn't start with "-"
if [ $indent -eq 0 ]; then
echo "- $part/"
else
printf "%${indent}s- %s/\n" "" "$part"
fi
fi
done
# Output the file with proper indentation
level=${#dir_parts[@]}
indent=$((level * 2))
# Fix the printf issue for files too
if [ $indent -eq 0 ]; then
echo "- $filename"
else
CURRENT_PATH="$CURRENT_PATH/$part"
printf "%${indent}s- %s\n" "" "$filename"
fi
# Check if we've already processed this directory
if [[ -z "${PROCESSED_DIRS[$CURRENT_PATH]}" ]]; then
PROCESSED_DIRS["$CURRENT_PATH"]=1 # Mark as processed
INDENT=$((i * 2)) # Indentation starts at 0 for the top level
printf "%${INDENT}s- %s/\n" "" "$part"
fi
done
# Output the file itself with proper indentation
INDENT=$(((PATH_LENGTH - 1) * 2))
printf "%${INDENT}s- %s\n" "" "${PATH_PARTS[-1]}"
else
# File is in the root directory - avoid printf issue
echo "- $filename"
fi
done < "$TMP_FILE"
# Clean up
rm "$TMP_FILE"
rm "$TMP_FILE" "$PROCESSED_DIRS_FILE"
# Close the XML-like tag
echo '</context>'
}
# Check bash version for associative arrays
if (( BASH_VERSINFO[0] < 4 )); then
echo "Error: This script requires Bash version 4 or higher for associative arrays." >&2
exit 1
fi
# Handle output to file, clipboard, or stdout
if [[ -n "$OUTPUT_FILE" ]]; then