Created
May 12, 2025 09:55
-
-
Save ledex/97aa6d9609cff81976d3f1b0c999dee5 to your computer and use it in GitHub Desktop.
This script gets all your commits on the main/master branch from all subfolders in the given base-directory. Useful for getting an overview of your work, i.e., for creating job references.
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 | |
# ============================================================================== | |
# Script: get_my_commits.sh | |
# Description: | |
# Finds and displays Git commits by a specific author on the main/master | |
# branch across multiple Git repositories within a base directory. | |
# Excludes merge commits. | |
# | |
# Usage: | |
# ./get_my_commits.sh [base_directory] [author_email] | |
# | |
# Arguments: | |
# base_directory (Optional): Path to directory containing Git repos. Defaults to '~/dev/sources'. | |
# author_email (Optional): Author's email. Defaults to global Git config user.email. | |
# ============================================================================== | |
# --- Configuration --- | |
DEFAULT_BASE_DIR="$HOME/dev/sources" | |
BASE_DIR_INPUT="${1:-$DEFAULT_BASE_DIR}" | |
# Use 'eval' to correctly expand ~ in the path, if present | |
eval BASE_DIR="$BASE_DIR_INPUT" | |
# Attempt to retrieve the email from global Git config if not provided as $2 | |
AUTHOR_EMAIL="${2:-$(git config --global --get user.email)}" | |
# --- Pre-run Checks --- | |
if [ -z "$AUTHOR_EMAIL" ]; then | |
echo "Error: Author email could not be determined." >&2 | |
echo "Configure Git globally (git config --global user.email \"[email protected]\") or provide email as the second argument." >&2 | |
exit 1 | |
fi | |
if [ ! -d "$BASE_DIR" ]; then | |
echo "Error: Base directory '$BASE_DIR' does not exist or is not a directory." >&2 | |
exit 1 | |
fi | |
# --- Main Logic --- | |
echo "Searching for commits by author '$AUTHOR_EMAIL' in Git repositories under '$BASE_DIR'..." | |
echo "============================================================================" | |
# Find direct subdirectories within the base directory | |
find "$BASE_DIR" -maxdepth 1 -mindepth 1 -type d | while IFS= read -r repo_candidate_path; do | |
repo_name=$(basename "$repo_candidate_path") | |
# Process each potential repository in a subshell to isolate cd and errors | |
( | |
cd "$repo_candidate_path" || exit 1 # Exit subshell if cd fails | |
# Check if it's a Git repository | |
if [ -e ".git" ]; then | |
echo # Blank line for readability | |
echo "--- Repository: $repo_name ---" | |
target_branch="" | |
branch_checked="" | |
# Check for local 'main' or 'master' branch | |
if git show-ref --verify --quiet refs/heads/main; then | |
target_branch="main" | |
branch_checked="main" | |
elif git show-ref --verify --quiet refs/heads/master; then | |
target_branch="master" | |
branch_checked="master" | |
else | |
echo "Info: Neither 'main' nor 'master' branch found locally in '$repo_name'." | |
exit 0 # No action needed for this repo | |
fi | |
echo "Checking branch '$branch_checked' for commits by '$AUTHOR_EMAIL'..." | |
# Execute git log with filters and formatting | |
commit_logs=$(git log "$target_branch" --author="$AUTHOR_EMAIL" --no-merges --pretty=format:"[%h] %ad | %s (%an)" --date=short) | |
if [ -n "$commit_logs" ]; then | |
echo "$commit_logs" | |
else | |
echo "No non-merge commits found for '$AUTHOR_EMAIL' on branch '$branch_checked'." | |
fi | |
fi | |
) # End of subshell | |
done # End of find loop | |
# --- Script Completion --- | |
echo "" | |
echo "============================================================================" | |
echo "Script finished searching repositories in '$BASE_DIR'." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment