Last active
April 9, 2026 10:13
-
-
Save bluishoul/87653d859fd3cb53f612aaaf63dfec94 to your computer and use it in GitHub Desktop.
Cleanup unchanged forked repos using gh cli
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
| #!/usr/bin/env zsh | |
| # Array to store repos with no changes | |
| unchanged_forks=() | |
| # Use process substitution < <() so the array stays available after the loop | |
| while read p_repo f_repo; do | |
| echo "---------------------------------------------------" | |
| echo "π Checking Fork: $f_repo" | |
| # 1. Fetch branch names | |
| p_branch=$(gh repo view "$p_repo" --json defaultBranchRef --jq '.defaultBranchRef.name' 2>/dev/null) | |
| f_branch=$(gh repo view "$f_repo" --json defaultBranchRef --jq '.defaultBranchRef.name' 2>/dev/null) | |
| if [[ -z "$p_branch" || -z "$f_branch" ]]; then | |
| echo " β SKIP: Could not retrieve branch names." | |
| continue | |
| fi | |
| # 2. API Comparison | |
| compare_url="repos/$f_repo/compare/${p_repo%/*}:${p_branch}...${f_repo%/*}:${f_branch}" | |
| res=$(gh api "$compare_url" --jq '{status: .status, ahead: .ahead_by}' 2>/dev/null) | |
| if [[ -z "$res" ]]; then | |
| echo " β ERROR: API 404 for $f_repo" | |
| continue | |
| fi | |
| diff_status=$(echo "$res" | jq -r .status) | |
| ahead_count=$(echo "$res" | jq -r .ahead) | |
| # 3. Collect if no changes (ahead is 0) | |
| if [[ "$ahead_count" == "0" ]]; then | |
| echo " β Found unchanged repo: $f_repo" | |
| unchanged_forks+=("$f_repo") | |
| else | |
| echo " πΈ Modified: Ahead by $ahead_count" | |
| fi | |
| done < <(gh repo list --fork --limit 100 --json nameWithOwner,parent --jq '.[] | select(.parent != null) | "\(.parent.owner.login)/\(.parent.name) \(.nameWithOwner)"') | |
| # --- Final Prompt --- | |
| if [[ ${#unchanged_forks[@]} -eq 0 ]]; then | |
| echo "\n⨠No unchanged forks found." | |
| exit 0 | |
| fi | |
| echo "\nβ οΈ The following ${#unchanged_forks[@]} forks have NO personal changes:" | |
| printf ' - %s\n' "${unchanged_forks[@]}" | |
| # In Zsh, we read from the terminal specifically to avoid issues with the pipe | |
| echo -n "\nβ Delete these ${#unchanged_forks[@]} forks? (y/N): " | |
| read -r confirm | |
| if [[ "$confirm" =~ ^[Yy]$ ]]; then | |
| for repo in "${unchanged_forks[@]}"; do | |
| echo "ποΈ Deleting $repo..." | |
| gh repo delete "$repo" --yes | |
| done | |
| echo "β Cleanup complete." | |
| else | |
| echo "β Cancelled." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment