Skip to content

Instantly share code, notes, and snippets.

@guyrt
Created April 3, 2025 17:47
Show Gist options
  • Save guyrt/eef5803492d5c80e88928918dafdbe92 to your computer and use it in GitHub Desktop.
Save guyrt/eef5803492d5c80e88928918dafdbe92 to your computer and use it in GitHub Desktop.
Quick script to find active branches in a repo. Mostly a Gemini2.5 test
#!/bin/bash
# --- Configuration ---
REPO_PATH="."
# Calculate the timestamp for 7 days ago from the current time
# Using the context's current time: Thursday, April 3, 2025 at 10:30:56 AM PDT
# If running this later, 'date -d "7 days ago" +%s' will use the actual current time.
# For consistency with the request context:
# CURRENT_DATE_STR="2025-04-03 10:30:56 PDT"
# ONE_WEEK_AGO_TIMESTAMP=$(date -d "$CURRENT_DATE_STR - 7 days" +%s)
# Or simply use the relative date for current execution:
ONE_WEEK_AGO_TIMESTAMP=$(date -d "7 days ago" +%s)
# --- End Configuration ---
# Function to print usage
usage() {
echo "Usage: $0 [-p <repo_path>]"
echo " -p <repo_path> : Path to the git repository (default: ./foo)"
exit 1
}
# --- Parse Command Line Options ---
while getopts ":p:" opt; do
case ${opt} in
p )
REPO_PATH=$OPTARG
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
usage
;;
: )
echo "Invalid option: -$OPTARG requires an argument" 1>&2
usage
;;
esac
done
shift $((OPTIND -1))
# --- Validation ---
if [ ! -d "$REPO_PATH" ]; then
echo "Error: Repository directory '$REPO_PATH' not found."
exit 1
fi
if [ ! -d "$REPO_PATH/.git" ]; then
echo "Error: '$REPO_PATH' does not appear to be a git repository."
exit 1
fi
# --- Main Logic ---
echo "== Searching for branches active in '$REPO_PATH' since $(date -d @$ONE_WEEK_AGO_TIMESTAMP '+%Y-%m-%d %H:%M:%S %Z (%A)') =="
echo "--------------------------------------------------------------------------------"
# Store the current directory
ORIGINAL_DIR=$(pwd)
# Change to the repository directory; exit if it fails
cd "$REPO_PATH" || { echo "Error: Failed to change directory to '$REPO_PATH'"; exit 1; }
#git fetch
# Define a separator character unlikely to appear in branch names or commit messages
# Using Tab character for better parsing robustness
SEPARATOR=$'\t'
# Use git for-each-ref to get data for local and remote branches
# Format: Short Branch Name<SEP>Author Date (ISO Strict)<SEP>Author Name<SEP>Commit Subject
git for-each-ref \
--format="%(refname:short)${SEPARATOR}%(authordate:iso-strict)${SEPARATOR}%(authorname)${SEPARATOR}%(subject)" \
refs/heads refs/remotes \
| while IFS="$SEPARATOR" read -r branch_name commit_date_iso author_name commit_subject; do
# Skip empty lines just in case
[[ -z "$branch_name" ]] && continue
# Convert the ISO date string to a Unix timestamp
# Add error suppression in case date format is unexpected
commit_timestamp=$(date -d "$commit_date_iso" +%s 2>/dev/null)
# Check if date conversion was successful and if the commit is recent enough
if [[ -n "$commit_timestamp" && "$commit_timestamp" -ge "$ONE_WEEK_AGO_TIMESTAMP" ]]; then
# Output the details for recent branches
printf "Branch: %s\n" "$branch_name"
printf " Date: %s\n" "$commit_date_iso"
printf " Author: %s\n" "$author_name"
printf " Message: %s\n" "$commit_subject"
printf -- "--------------------------------------------------------------------------------\n"
fi
done
# Return to the original directory silently
cd "$ORIGINAL_DIR" > /dev/null
echo "== Search complete =="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment