From 31dcb29555b3ddcacbc4d205f1bf6738d9313fb3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 25 Jun 2025 19:06:36 +1200 Subject: [PATCH] Modify whatsdirty/whatsdirty --- whatsdirty/whatsdirty | 87 +++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/whatsdirty/whatsdirty b/whatsdirty/whatsdirty index b2d8244..99fc363 100755 --- a/whatsdirty/whatsdirty +++ b/whatsdirty/whatsdirty @@ -33,7 +33,8 @@ handle_directory_argument() { if [ -z "$arg" ]; then # No argument - try to load from config - local saved_dir=$(load_directory) + local saved_dir + saved_dir=$(load_directory) if [ -z "$saved_dir" ]; then echo "Error: No directory specified and no saved directory found." >&2 echo "Usage: $0 " >&2 @@ -43,7 +44,8 @@ handle_directory_argument() { echo "$saved_dir" else # Argument provided - validate and save - local canonical_dir=$(validate_directory "$arg") + local canonical_dir + canonical_dir=$(validate_directory "$arg") if [ -z "$canonical_dir" ]; then echo "Error: Directory '$arg' not found" >&2 exit 1 @@ -56,14 +58,17 @@ handle_directory_argument() { # === Git Repository Functions === get_last_commit_hours() { - local last_commit_timestamp=$(git log -1 --format="%ct" 2>/dev/null) + local last_commit_timestamp + last_commit_timestamp=$(git log -1 --format="%ct" 2>/dev/null) if [ -n "$last_commit_timestamp" ]; then - local current_timestamp=$(date +%s) + local current_timestamp + current_timestamp=$(date +%s) local diff_seconds=$((current_timestamp - last_commit_timestamp)) # Convert to hours with decimal precision using awk (more portable than bc) - local hours_ago=$(awk "BEGIN {printf \"%.1f\", $diff_seconds / 3600}") + local hours_ago + hours_ago=$(awk "BEGIN {printf \"%.1f\", $diff_seconds / 3600}") # Ensure leading zero for values less than 1 if [[ $hours_ago =~ ^\. ]]; then @@ -77,9 +82,12 @@ get_last_commit_hours() { } count_git_changes() { - local staged=$(git diff --cached --numstat 2>/dev/null | wc -l) - local unstaged=$(git diff --numstat 2>/dev/null | wc -l) - local untracked=$(git ls-files --others --exclude-standard 2>/dev/null | wc -l) + local staged + staged=$(git diff --cached --numstat 2>/dev/null | wc -l) + local unstaged + unstaged=$(git diff --numstat 2>/dev/null | wc -l) + local untracked + untracked=$(git ls-files --others --exclude-standard 2>/dev/null | wc -l) echo $((staged + unstaged + untracked)) } @@ -87,7 +95,8 @@ get_changed_files_list() { local changed_files="" # Get staged files - local staged_files=$(git diff --cached --name-only 2>/dev/null) + local staged_files + staged_files=$(git diff --cached --name-only 2>/dev/null) if [ -n "$staged_files" ]; then while IFS= read -r file; do changed_files="${changed_files} [staged] $file\n" @@ -95,7 +104,8 @@ get_changed_files_list() { fi # Get unstaged modified files - local unstaged_files=$(git diff --name-only 2>/dev/null) + local unstaged_files + unstaged_files=$(git diff --name-only 2>/dev/null) if [ -n "$unstaged_files" ]; then while IFS= read -r file; do changed_files="${changed_files} [modified] $file\n" @@ -103,7 +113,8 @@ get_changed_files_list() { fi # Get untracked files - local untracked=$(git ls-files --others --exclude-standard 2>/dev/null) + local untracked + untracked=$(git ls-files --others --exclude-standard 2>/dev/null) if [ -n "$untracked" ]; then while IFS= read -r file; do changed_files="${changed_files} [untracked] $file\n" @@ -118,12 +129,14 @@ get_changed_files_list() { analyze_repository() { local repo_path="$1" - local repo_name=$(basename "$repo_path") + local repo_name + repo_name=$(basename "$repo_path") cd "$repo_path" || return 1 # Get commit info - local hours_ago=$(get_last_commit_hours) + local hours_ago + hours_ago=$(get_last_commit_hours) local hours_num local hours_label local hours_formatted @@ -139,13 +152,15 @@ analyze_repository() { fi # Count changes - local total_changes=$(count_git_changes) + local total_changes + total_changes=$(count_git_changes) # Determine status and collect data - if [ $total_changes -eq 0 ]; then + if [ "$total_changes" -eq 0 ]; then echo "C|${hours_num}|${total_changes}|${repo_name}|${hours_formatted}" else - local changed_files=$(get_changed_files_list) + local changed_files + changed_files=$(get_changed_files_list) echo "D|${hours_num}|${total_changes}|${repo_name}|${hours_formatted}|||${changed_files}" fi } @@ -162,7 +177,7 @@ print_dirty_repositories() { if grep -q "^D|" "$temp_file" 2>/dev/null; then printf "\n=== DIRTY REPOSITORIES ===\n\n" - grep "^D|" "$temp_file" | sort -t'|' -k3,3nr | while IFS='|' read -r status hours changes repo hours_fmt separator file_list; do + grep "^D|" "$temp_file" | sort -t'|' -k3,3nr | while IFS='|' read -r _ _ changes repo hours_fmt _ file_list; do printf "%-40s %15s ${RED}%-10s${NC} ${RED}%-10s${NC}\n" "$repo" "$hours_fmt" "$changes" "Dirty" if [ -n "$file_list" ]; then echo -e "$file_list" @@ -176,7 +191,7 @@ print_clean_repositories() { if grep -q "^C|" "$temp_file" 2>/dev/null; then printf "\n=== CLEAN REPOSITORIES ===\n\n" - grep "^C|" "$temp_file" | sort -t'|' -k2,2n | while IFS='|' read -r status hours changes repo hours_fmt; do + grep "^C|" "$temp_file" | sort -t'|' -k2,2n | while IFS='|' read -r _ _ changes repo hours_fmt; do printf "%-40s %15s %-10s %-10s\n" "$repo" "$hours_fmt" "$changes" "Clean" done fi @@ -192,7 +207,8 @@ scan_repositories() { [ ! -d "$folder" ] && continue if [ -d "$folder/.git" ]; then - local repo_data=$(analyze_repository "$folder") + local repo_data + repo_data=$(analyze_repository "$folder") if [ -n "$repo_data" ]; then echo "$repo_data" >> "$temp_file" fi @@ -203,10 +219,12 @@ scan_repositories() { main() { # Handle directory argument - local DIR=$(handle_directory_argument "$1") + local DIR + DIR=$(handle_directory_argument "$1") # Validate directory still exists (for loaded configs) - local validated_dir=$(validate_directory "$DIR") + local validated_dir + validated_dir=$(validate_directory "$DIR") if [ -z "$validated_dir" ]; then echo "Error: Saved directory '$DIR' no longer exists" >&2 rm -f "$CONFIG_FILE" @@ -215,8 +233,9 @@ main() { DIR="$validated_dir" # Create temp file for results - local TEMP_FILE=$(mktemp) - trap "rm -f $TEMP_FILE" EXIT + local TEMP_FILE + TEMP_FILE=$(mktemp) + trap 'rm -f "$TEMP_FILE"' EXIT # Scan repositories scan_repositories "$DIR" "$TEMP_FILE" @@ -231,12 +250,12 @@ main() { handle_autocomplete() { local cur_word="${COMP_WORDS[COMP_CWORD]}" - local prev_word="${COMP_WORDS[COMP_CWORD-1]}" # If we're completing the first argument after 'whatsdirty' if [ ${COMP_CWORD} -eq 1 ]; then # Suggest common directories and saved directory - local saved_dir=$(load_directory) + local saved_dir + saved_dir=$(load_directory) local suggestions="" # Add saved directory if it exists @@ -256,18 +275,22 @@ handle_autocomplete() { # Directory completion for current word if [ -n "$cur_word" ]; then - # Use compgen for directory completion - local dir_completions=($(compgen -d -- "$cur_word")) + # Use mapfile for directory completion + local dir_completions + mapfile -t dir_completions < <(compgen -d -- "$cur_word") # Add our suggestions that match the current word - local matching_suggestions=($(echo "$suggestions" | tr ' ' '\n' | grep "^$cur_word" | sort -u)) + local matching_suggestions + mapfile -t matching_suggestions < <(echo "$suggestions" | tr ' ' '\n' | grep "^$cur_word" | sort -u) # Combine and deduplicate - COMPREPLY=($(printf '%s\n' "${dir_completions[@]}" "${matching_suggestions[@]}" | sort -u)) + mapfile -t COMPREPLY < <(printf '%s\n' "${dir_completions[@]}" "${matching_suggestions[@]}" | sort -u) else # No current word, suggest all our directories plus general directory completion - local dir_completions=($(compgen -d)) - local all_suggestions=($(echo "$suggestions" | tr ' ' '\n' | sort -u)) + local dir_completions + mapfile -t dir_completions < <(compgen -d) + local all_suggestions + mapfile -t all_suggestions < <(echo "$suggestions" | tr ' ' '\n' | sort -u) # Combine and deduplicate, but limit to reasonable number - COMPREPLY=($(printf '%s\n' "${all_suggestions[@]}" "${dir_completions[@]}" | sort -u | head -20)) + mapfile -t COMPREPLY < <(printf '%s\n' "${all_suggestions[@]}" "${dir_completions[@]}" | sort -u | head -20) fi fi }