Skip to content

Instantly share code, notes, and snippets.

@qq99
Created June 18, 2024 22:44
Show Gist options
  • Save qq99/46f5f1caaf6d78848aeeb5f26418afa6 to your computer and use it in GitHub Desktop.
Save qq99/46f5f1caaf6d78848aeeb5f26418afa6 to your computer and use it in GitHub Desktop.
recent_git_branches
# add this to your bashrc or other shell configuration to make it even faster
alias gb='recent_git_branches'
#!/bin/bash
# provides you a list of 10 most recent git branches to pick through interactively
# useful if you forget what you named your branches or dislike typing long branch names
# drop this in /usr/local/bin, probably you need to chmod +x it too
# should work on ubuntus/wsl2, unsure about OSX
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to format time difference
format_time_difference() {
local seconds=$1
if ((seconds < 60)); then
echo "${seconds} seconds ago"
elif ((seconds < 3600)); then
echo "$((seconds / 60)) minutes ago"
elif ((seconds < 86400)); then
echo "$((seconds / 3600)) hours ago"
else
echo "$((seconds / 86400)) days ago"
fi
}
# Check if the current directory is a Git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Not a Git repository. Please run this script inside a Git repository."
exit 1
fi
# Get a list of the 10 most recent local branches and their last commit dates
branches=($(git for-each-ref --sort='-committerdate' --format='%(refname:short)' --count=10 refs/heads/))
# Function to switch to the selected branch
switch_to_branch() {
if [[ $selected -ge 0 && $selected -lt ${#branches[@]} ]]; then
branch=${branches[selected]}
git checkout "$branch"
echo -e "\nSwitched to ${GREEN}$branch${NC}"
exit 0
fi
}
# Function to cleanup and exit gracefully
cleanup_and_exit() {
tput cnorm # Restore cursor visibility
exit 0
}
# Trap the Exit signal (Escape key)
trap cleanup_and_exit EXIT
# Hide the cursor for a cleaner interface
tput civis
# Selected branch index
selected=0
# Read user input and handle keyboard events
while true; do
# Display the branches with colored branch names and dates
clear
echo "Recent branches (most recent 10):"
for i in "${!branches[@]}"; do
branch=${branches[i]}
last_commit_date=$(git log -n 1 --format="%ci" "$branch" | xargs -I {} date --date={} +"%s")
current_date=$(date +"%s")
time_difference=$((current_date - last_commit_date))
formatted_time=$(format_time_difference "$time_difference")
if ((i == selected)); then
echo -e "${GREEN}[${i}] ${branch}${NC}, Last commit date: ${BLUE}${formatted_time}${NC}"
else
echo -e " ${GREEN}[${i}] ${branch}${NC}, Last commit date: ${BLUE}${formatted_time}${NC}"
fi
done
# Read a single character silently
read -rsn3 key
case "$key" in
$'\x1b[A') # up arrow
((selected--));;
$'\x1b[B') # down arrow
((selected++));;
"") # Enter key (newline)
switch_to_branch
;;
esac
# Prevent negative or out-of-range index
if ((selected < 0)); then
selected=0
elif ((selected >= ${#branches[@]})); then
selected=$(( ${#branches[@]} - 1 ))
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment