Last active
September 4, 2023 12:50
-
-
Save matthiasseghers/688e55bdeeac22e8a335ab09aaa47fed to your computer and use it in GitHub Desktop.
Git scripts
This file contains 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 | |
# Define the default branch (e.g., 'main' or 'master') | |
default_branch="main" | |
# Define the stale branch age threshold in days | |
stale_age_days=90 | |
# List all branches except the default branch | |
for branch in $(git branch -r --no-merged $default_branch | grep -vE "origin/$default_branch"); do | |
# Get the last commit date for the branch | |
last_commit_date=$(git log -n 1 --format="%cd" --date=relative $branch) | |
# Check if the branch is older than the stale age threshold | |
if [[ "$last_commit_date" == *months* || "$last_commit_date" == *years* ]]; then | |
# Branch is considered stale | |
echo "Stale branch: $branch (last commit: $last_commit_date)" | |
# Check if the branch is associated with a closed issue or pull request | |
associated_issues=$(hub issue --format='%I %s' -a "$branch" -s closed) | |
if [[ -n "$associated_issues" ]]; then | |
echo "Associated closed issues/PRs:" | |
echo "$associated_issues" | |
fi | |
# Optionally, delete the branch if you're sure it's safe to do so | |
# git push origin --delete $branch | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment