Modify whatsdirty/whatsdirty
Some checks failed
Build-Test-Publish / build (linux/arm64) (push) Failing after 9s
Build-Test-Publish / build (linux/amd64) (push) Failing after 24s
Build-Test-Publish / test-install-from-scratch (linux/amd64) (push) Has been skipped
Build-Test-Publish / test-install-from-scratch (linux/arm64) (push) Has been skipped

This commit is contained in:
Your Name 2025-06-25 19:06:36 +12:00
parent bf4021464f
commit 31dcb29555

View File

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