Skip to content

Instantly share code, notes, and snippets.

@gsouf
Created October 1, 2024 01:29
Show Gist options
  • Save gsouf/2fa75f34ce2d9450e0b360085bd18377 to your computer and use it in GitHub Desktop.
Save gsouf/2fa75f34ce2d9450e0b360085bd18377 to your computer and use it in GitHub Desktop.
list all uncommitted and unpushed changed in all repos of a given directory
#!/bin/bash
check_repo_status() {
local dir="$1"
# Check if it's a git repository
if ! git -C "$dir" rev-parse --is-inside-work-tree > /dev/null 2>&1; then
return
fi
# Get repository name
repo_name=$(basename "$dir")
# Check for uncommitted changes
uncommitted_files=$(git -C "$dir" status --porcelain)
# Check for unpushed branches
unpushed_branches=$(git -C "$dir" for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads | awk '$2 == "[gone]" || $2 ~ /ahead/ {print $1}')
# Display results
if [ -n "$uncommitted_files" ] || [ -n "$unpushed_branches" ]; then
echo "[$repo_name]"
if [ -n "$uncommitted_files" ]; then
echo "uncommitted files:"
echo "$uncommitted_files" | sed 's/^/ /'
fi
if [ -n "$unpushed_branches" ]; then
if [ -n "$uncommitted_files" ]; then
echo
fi
echo "unpushed branches:"
echo "$unpushed_branches" | sed 's/^/ /'
fi
echo
fi
}
check_all_repos() {
local base_dir="$1"
# Check if directory exists
if [ ! -d "$base_dir" ]; then
echo "Error: Directory '$base_dir' does not exist."
exit 1
fi
# Iterate over all directories in the base directory
for dir in "$base_dir"/*/; do
if [ -d "$dir" ]; then
check_repo_status "$dir"
fi
done
}
# Call the function with the base directory passed as an argument
# If no argument is provided, use the current directory
base_dir="${1:-.}"
check_all_repos "$base_dir"
@gsouf
Copy link
Author

gsouf commented Oct 1, 2024

example usages

Given that the directory projects/ has multiple sub directories that are git repositories:

bash check-all-repos.bash projects/

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