From 57346617a5cb28fbd2617133c5abaed8c3fac67d Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Fri, 4 Apr 2025 15:10:47 -0700 Subject: [PATCH] tools/ai/repo-structure: fix on mac/zsh --- tools/ai/repo-structure.sh | 98 +++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/tools/ai/repo-structure.sh b/tools/ai/repo-structure.sh index 5cedd14d1..26bae5565 100644 --- a/tools/ai/repo-structure.sh +++ b/tools/ai/repo-structure.sh @@ -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 '' 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 '' # 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/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 '' } -# 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