Skip to content

Instantly share code, notes, and snippets.

@mnofresno
Created August 13, 2024 20:38
Show Gist options
  • Save mnofresno/e4b1d037f97c513a6aef0ee5f0e48d01 to your computer and use it in GitHub Desktop.
Save mnofresno/e4b1d037f97c513a6aef0ee5f0e48d01 to your computer and use it in GitHub Desktop.
BASH script to check all the current directory's git repos statuses (It reports them in a fancy ascii-art table)
#!/bin/bash
# Function to check the git status
check_git_status() {
local dir="$1"
cd "$dir" || return
git_status=$(git status --porcelain 2>/dev/null)
if [[ -z "$git_status" ]]; then
echo "Clean"
else
if echo "$git_status" | grep -q "^ M"; then
echo "Dirty"
else
echo "Stagging"
fi
fi
}
# Function to print the table header
print_header() {
echo "+----------------------------+----------------+"
echo "| Repository | Status |"
echo "+----------------------------+----------------+"
}
# Function to print a table row
print_row() {
printf "| %-26s | %-14s |\n" "$1" "$2"
}
# Function to print the table footer
print_footer() {
echo "+----------------------------+----------------+"
}
# Main function to find git repos and their statuses
main() {
local target_dir="$1"
if [[ -z "$target_dir" ]]; then
echo "Usage: $0 <directory>"
exit 1
fi
if [[ ! -d "$target_dir" ]]; then
echo "Error: Directory '$target_dir' does not exist."
exit 1
fi
cd "$target_dir" || exit
print_header
for dir in */ ; do
if [ -d "$dir/.git" ]; then
status=$(check_git_status "$dir")
print_row "${dir%/}" "$status"
fi
done
print_footer
}
# Run the main function with the specified directory
main "$1"
@mnofresno
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment