fix: summary in git_repos_status.sh

This commit is contained in:
Saravana Dhandapani 2025-06-25 11:46:15 -07:00
parent 3621202548
commit f25724dbd1

View File

@ -7,38 +7,82 @@ YELLOW='\033[1;33m'
CYAN='\033[0;36m' CYAN='\033[0;36m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
# Find all git repos # Arrays for summary
find . -type d -name ".git" | while read gitdir; do repos=()
uncommitted_counts=()
ahead_counts=()
behind_counts=()
total_uncommitted=0
total_ahead=0
# Find all git repos (POSIX compatible)
gitdirs=$(find . -type d -name ".git")
for gitdir in $gitdirs; do
repo=$(dirname "$gitdir") repo=$(dirname "$gitdir")
cd "$repo" || continue cd "$repo" || continue
echo -e "${CYAN}Repository: $repo${NC}" echo -e "${CYAN}📁 Repository: $repo${NC}"
# Latest 2 commits # Latest 2 commits
echo -e " ${YELLOW}Latest 2 commits:${NC}" echo -e " ${YELLOW}📜 Latest 2 commits:${NC}"
git log --oneline -2 --decorate git --no-pager log --oneline -2 --decorate
# Uncommitted file count # Uncommitted file count
uncommitted=$(git status --porcelain | wc -l | tr -d ' ') uncommitted=$(git status --porcelain | wc -l | tr -d ' ')
if [ "$uncommitted" -eq 0 ]; then if [ "$uncommitted" -eq 0 ]; then
echo -e " ${GREEN}Uncommitted files: $uncommitted${NC}" echo -e " ${GREEN}Uncommitted files: $uncommitted${NC}"
else else
echo -e " ${RED}Uncommitted files: $uncommitted${NC}" echo -e " ${RED}🗂️ Uncommitted files: $uncommitted${NC}"
fi fi
# Check for unpushed commits # Check for unpushed commits
git remote update > /dev/null 2>&1 git remote update > /dev/null 2>&1
# Only check if upstream exists
if git rev-parse --abbrev-ref --symbolic-full-name @{u} > /dev/null 2>&1; then
ahead=$(git rev-list --count @{u}..HEAD 2>/dev/null) ahead=$(git rev-list --count @{u}..HEAD 2>/dev/null)
behind=$(git rev-list --count HEAD..@{u} 2>/dev/null) behind=$(git rev-list --count HEAD..@{u} 2>/dev/null)
if [ "$ahead" -gt 0 ]; then
echo -e " ${YELLOW}Locally committed (not pushed): $ahead commit(s)${NC}"
else else
echo -e " ${GREEN}All local commits are pushed${NC}" ahead=0
behind=0
fi
if [ "$ahead" -gt 0 ]; then
echo -e " ${YELLOW}⬆️ Locally committed (not pushed): $ahead commit(s)${NC}"
else
echo -e " ${GREEN}✅ All local commits are pushed${NC}"
fi fi
if [ "$behind" -gt 0 ]; then if [ "$behind" -gt 0 ]; then
echo -e " ${RED}Behind remote by $behind commit(s)${NC}" echo -e " ${RED}⚠️ Behind remote by $behind commit(s)${NC}"
fi fi
echo -e "${CYAN}----------------------------------------${NC}" echo -e "${CYAN}──────────────────────────────────────────────${NC}"
# Store for summary
repos+=("$repo")
uncommitted_counts+=("$uncommitted")
ahead_counts+=("$ahead")
behind_counts+=("$behind")
# Add to totals
total_uncommitted=$((total_uncommitted + uncommitted))
total_ahead=$((total_ahead + ahead))
cd - > /dev/null || exit cd - > /dev/null || exit
done done
# Print summary table
printf "\n${CYAN}📊 Summary Table${NC}\n"
printf "${YELLOW}%-35s │ %-13s │ %-11s │ %-8s${NC}\n" "Repository" "🗂️ Uncommitted" "⬆️ Unpushed" "⚠️ Behind"
printf "%0.s─" {1..75}; printf "\n"
for i in "${!repos[@]}"; do
repo="${repos[$i]}"
uncommitted="${uncommitted_counts[$i]}"
ahead="${ahead_counts[$i]}"
behind="${behind_counts[$i]}"
printf "%-35s │ %-13s │ %-11s │ %-8s\n" "$repo" "$uncommitted" "$ahead" "$behind"
done
# Print overall totals
printf "%0.s─" {1..75}; printf "\n"
printf "${GREEN}🟢 Overall: ${NC}🗂️ %d uncommitted file(s), ⬆️ %d unpushed commit(s)\n" "$total_uncommitted" "$total_ahead"