Skip to content

Instantly share code, notes, and snippets.

@dhinojosa
Last active February 29, 2024 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhinojosa/75ede4f21865b533579028b386479031 to your computer and use it in GitHub Desktop.
Save dhinojosa/75ede4f21865b533579028b386479031 to your computer and use it in GitHub Desktop.
Find all the repositories in your home directory and tells you what needs to be updated. Run with ./git_repo_check <dir1> <dir 2>
#!/bin/bash
# Function to check a single git repository
check_git_repo() {
local dir=$1
cd "$dir" || return
# Check for uncommitted changes
uncommitted_changes=$(git status --porcelain)
if [ ! -z "$uncommitted_changes" ]; then
echo "[UNCOMMITTED CHANGES] $dir"
((uncommitted_repos++))
return
fi
# Check for unpushed commits
unpushed_commits=$(git log --branches --not --remotes)
if [ ! -z "$unpushed_commits" ]; then
echo "[UNPUSHED COMMITS] $dir"
((unpushed_repos++))
return
fi
# Increment clean repositories counter
((clean_repos++))
# Report clean
echo "[CLEAN] $dir"
}
# Initialize counters
total_repos=0
clean_repos=0
uncommitted_repos=0
unpushed_repos=0
# Function to find git repositories in a directory
find_git_repos() {
local search_dir=$1
find "$search_dir" -type d -name .git
}
# Check if any arguments are provided
if [ $# -eq 0 ]; then
echo "Usage: git_repo_check.sh <dir1> [dir2] [dir3] ..."
echo "Specify one or more directories to scan for git repositories."
exit 1
fi
echo "Scanning specified directories..."
declare -a repo_list
for arg in "$@"; do
if [ -d "$arg" ]; then
while IFS= read -r repo; do
repo_list+=("$repo")
done < <(find_git_repos "$arg")
else
echo "Warning: Directory not found or not a directory: $arg"
fi
done
# Check each repository
for repo in "${repo_list[@]}"; do
dir=$(dirname "$repo")
# Increment total repositories counter
((total_repos++))
# Check the git repository
check_git_repo "$dir"
done
# Print statistics
echo "==================================="
echo "Total Repositories: $total_repos"
echo "Clean Repositories: $clean_repos"
echo "Repositories with Uncommitted Changes: $uncommitted_repos"
echo "Repositories with Unpushed Commits: $unpushed_repos"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment