Skip to content

Commit 56123fe

Browse files
Create prune-gh-branches.sh
created a script to prune branches of a certain age (based on most recent edit)
1 parent d5a0ab5 commit 56123fe

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

scripts/prune-gh-branches.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
# Script: prune-gh-branches.sh
4+
# Requirements: Linux terminal (like WSL), gh and jq installed.
5+
# Also, please use ```gh auth login``` to login to github beforehand
6+
# Description: Deletes branches older than specified number of months (based on most recent edit)
7+
# Usage: Run the script with the number of months as an argument.
8+
# Example: ./prune-gh-branches.sh 18
9+
10+
# Check if the number of months is provided
11+
if [ -z "$1" ]; then
12+
echo "Usage: $0 <number_of_months>"
13+
echo "Example: ./prune-gh-branches.sh 18 (to delete branches older than 18 months)"
14+
exit 1
15+
fi
16+
17+
# Set the repository name
18+
REPO="thecourseforum/theCourseForum2"
19+
20+
# Get the number of months from the user input
21+
MONTHS=$1
22+
23+
# Get the current date in ISO format
24+
CURRENT_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
25+
26+
# Calculate the date X months ago
27+
X_MONTHS_AGO=$(date -u -d "$MONTHS months ago" +"%Y-%m-%dT%H:%M:%SZ")
28+
29+
# Fetch all branches
30+
BRANCHES=$(gh api repos/$REPO/branches --paginate | jq -r '.[].name')
31+
32+
# Check if there are any branches
33+
if [ -z "$BRANCHES" ]; then
34+
echo "No branches found in the repository."
35+
exit 0
36+
fi
37+
38+
# Initialize an empty array to store old branches
39+
OLD_BRANCHES=()
40+
41+
# Loop through each branch and fetch the last commit date
42+
echo "Checking branches for last commit date..."
43+
for BRANCH in $BRANCHES; do
44+
# Fetch the last commit date for the branch
45+
LAST_COMMIT_DATE=$(gh api repos/$REPO/branches/$BRANCH | jq -r '.commit.commit.committer.date')
46+
47+
# Check if the last commit date is older than X months
48+
if [[ "$LAST_COMMIT_DATE" < "$X_MONTHS_AGO" ]]; then
49+
OLD_BRANCHES+=("$BRANCH $LAST_COMMIT_DATE")
50+
fi
51+
done
52+
53+
# Check if there are any old branches
54+
if [ ${#OLD_BRANCHES[@]} -eq 0 ]; then
55+
echo "No branches older than $MONTHS months found."
56+
exit 0
57+
fi
58+
59+
# Print the list of old branches with their last commit date
60+
echo "The following branches were last edited at least $MONTHS months ago:"
61+
echo "Branch Name | Last Commit Date"
62+
echo "-----------------------------"
63+
for BRANCH_INFO in "${OLD_BRANCHES[@]}"; do
64+
echo "$BRANCH_INFO"
65+
done
66+
echo ""
67+
68+
# Ask for confirmation
69+
read -p "Do you want to delete these branches? (y/n): " CONFIRM
70+
if [[ $CONFIRM != "y" && $CONFIRM != "Y" ]]; then
71+
echo "Aborting. No branches were deleted."
72+
exit 0
73+
fi
74+
75+
# Delete the branches
76+
for BRANCH_INFO in "${OLD_BRANCHES[@]}"; do
77+
BRANCH_NAME=$(echo "$BRANCH_INFO" | awk '{print $1}')
78+
echo "Deleting branch: $BRANCH_NAME"
79+
gh api -X DELETE repos/$REPO/git/refs/heads/$BRANCH_NAME
80+
done
81+
82+
echo "Done! All specified branches have been deleted."

0 commit comments

Comments
 (0)