Created
October 1, 2024 01:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example usages
Given that the directory
projects/
has multiple sub directories that are git repositories:bash check-all-repos.bash projects/