|
| 1 | +#!/bin/sh |
| 2 | +set -e |
| 3 | + |
| 4 | +usage () { |
| 5 | + echo "usage: git cleanup [-nh]" >&2 |
| 6 | + echo >&2 |
| 7 | + echo "Deletes all branches that have already been merged into the main branch." >&2 |
| 8 | + echo "Removes those branches both locally and in the origin remote. Will be " >&2 |
| 9 | + echo "most conservative with deletions." >&2 |
| 10 | + echo >&2 |
| 11 | + echo "Options:" >&2 |
| 12 | + echo "-n Dry-run" >&2 |
| 13 | + echo "-s Squashed" >&2 |
| 14 | + echo "-l Local branches only, don't touch the remotes" >&2 |
| 15 | + echo "-v Be verbose (show what's skipped)" >&2 |
| 16 | + echo "-h Show this help" >&2 |
| 17 | +} |
| 18 | + |
| 19 | +dryrun=0 |
| 20 | +remotes=1 |
| 21 | +squashed=0 |
| 22 | +verbose=0 |
| 23 | +while getopts nlsvh flag; do |
| 24 | + case "$flag" in |
| 25 | + n) dryrun=1;; |
| 26 | + l) remotes=0;; |
| 27 | + s) squashed=1;; |
| 28 | + v) verbose=1;; |
| 29 | + h) usage; exit 2;; |
| 30 | + esac |
| 31 | +done |
| 32 | +shift $(($OPTIND - 1)) |
| 33 | + |
| 34 | +# |
| 35 | +# This will clean up any branch (both locally and remotely) that has been |
| 36 | +# merged into any of the known "trunks". Trunks are any of: |
| 37 | +# |
| 38 | +# - main (local) + origin/main |
| 39 | +# - master (local) + origin/master |
| 40 | +# |
| 41 | + |
| 42 | +safegit () { |
| 43 | + if [ "$dryrun" -eq 1 ]; then |
| 44 | + echo git "$@" |
| 45 | + else |
| 46 | + git "$@" |
| 47 | + fi |
| 48 | +} |
| 49 | + |
| 50 | +# |
| 51 | +# The Algorithm[tm]: |
| 52 | +# - Find the smallest set of common ancestors for those trunks. (There can |
| 53 | +# actually be multiple, although unlikely.) |
| 54 | +# - For each local branch, check if any of the common ancestors contains it, |
| 55 | +# but not vice-versa (prevents newly-created branches from being deleted) |
| 56 | +# - Idem for each remote branch |
| 57 | +# |
| 58 | + |
| 59 | +find_common_base () { |
| 60 | + if [ $# -eq 1 ]; then |
| 61 | + git sha "$1" |
| 62 | + else |
| 63 | + git merge-base "$1" "$2" |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +find_branch_base () { |
| 68 | + branch="$1" |
| 69 | + base_point="" |
| 70 | + |
| 71 | + if git local-branch-exists "$branch"; then |
| 72 | + base_point=$(find_common_base "$branch" $base_point) |
| 73 | + fi |
| 74 | + |
| 75 | + if git remote-branch-exists origin "$branch"; then |
| 76 | + base_point=$(find_common_base "origin/$branch" $base_point) |
| 77 | + fi |
| 78 | + |
| 79 | + if [ -n "$base_point" ]; then |
| 80 | + echo "$base_point" |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +main="$(git main-branch)" |
| 85 | + |
| 86 | +find_bases () { |
| 87 | + find_branch_base "$main" |
| 88 | +} |
| 89 | + |
| 90 | +bases=$(find_bases) |
| 91 | + |
| 92 | +# |
| 93 | +# The Clean Squashed Algorithm[tm] |
| 94 | +# - Create a temporary dangling squashed commit with git commit-tree |
| 95 | +# - Then use git cherry to check if the squashed commit has already been |
| 96 | +# applied to the main branch |
| 97 | +# - If it has, then delete the branch |
| 98 | +# |
| 99 | + |
| 100 | +clean_squashed () { |
| 101 | + branch="$1" |
| 102 | + |
| 103 | + # Find the merge base for this branch |
| 104 | + merge_base=$(git merge-base "$main" "$branch") |
| 105 | + |
| 106 | + # Get the tree object of the branch |
| 107 | + branch_tree="$(git rev-parse "$branch^{tree}")" |
| 108 | + |
| 109 | + # Create a squashed commit object of the branch tree with parent |
| 110 | + # of $base with a commit message of "_" |
| 111 | + dangling_squashed_commit="$(git commit-tree "$branch_tree" -p "$merge_base" -m _)" |
| 112 | + |
| 113 | + # Show a summary of what has yet to be applied |
| 114 | + cherry_commit="$(git cherry "$main" "$dangling_squashed_commit")" |
| 115 | + |
| 116 | + if [ "$cherry_commit" = "- $dangling_squashed_commit" ]; then |
| 117 | + # If "- <commit-sha>", (ex. - "- 851cb44727") this means the |
| 118 | + # commit is in main and can be dropped if you rebased |
| 119 | + # against main |
| 120 | + safegit branch -D "$branch" |
| 121 | + elif [ $verbose -eq 1 ]; then |
| 122 | + # If "+ <commit-sha>", (ex. - "+ 851cb44727") this means the |
| 123 | + # commit still needs to be kept so that it will be applied to |
| 124 | + # main |
| 125 | + echo "Skipped $branch (no similar squash found)" |
| 126 | + fi |
| 127 | +} |
| 128 | + |
| 129 | +for branch in $(git local-branches \ |
| 130 | + | grep -vxF "$main"); do |
| 131 | + for base in $bases; do |
| 132 | + if git contains "$base" "$branch"; then |
| 133 | + if ! git contains "$branch" "$base"; then |
| 134 | + # Actually delete |
| 135 | + if ! safegit branch -D "$branch"; then |
| 136 | + echo "Errors deleting local branch $branch" >&2 |
| 137 | + fi |
| 138 | + break |
| 139 | + fi |
| 140 | + else |
| 141 | + # This is the case where the branches are in fact legit |
| 142 | + # local WIP branches or they are squashed merges, and we |
| 143 | + # need to check if they have been squashed-merged into |
| 144 | + # main. NOTE - this assumes main is up-to-date locally |
| 145 | + if [ "$squashed" -eq 1 ]; then |
| 146 | + clean_squashed "$branch" |
| 147 | + fi |
| 148 | + fi |
| 149 | + done |
| 150 | +done |
| 151 | + |
| 152 | +# Pruning first will remove any remote tracking branches that don't exist in |
| 153 | +# the remote anymore anyway. |
| 154 | + |
| 155 | +#XXX: FIXME: This gave trouble, as it tried to remove branches from Heroku remotes... :( |
| 156 | +#for remote in $(git remote); do |
| 157 | +for remote in origin; do |
| 158 | + safegit remote prune "$remote" >/dev/null 2>/dev/null |
| 159 | + |
| 160 | + if [ $remotes -eq 1 ]; then |
| 161 | + branches_to_remove="" |
| 162 | + for branch in $(git remote-branches "$remote" | grep -vEe "/($main)\$"); do |
| 163 | + for base in $bases; do |
| 164 | + if git contains "$base" "$branch"; then |
| 165 | + if ! git contains "$branch" "$base"; then |
| 166 | + branchname=$(echo "$branch" | cut -d/ -f2-) |
| 167 | + branches_to_remove="$branches_to_remove $branchname" |
| 168 | + break |
| 169 | + fi |
| 170 | + fi |
| 171 | + done |
| 172 | + done |
| 173 | + |
| 174 | + if [ -n "$branches_to_remove" ]; then |
| 175 | + if ! safegit push "$remote" --delete $branches_to_remove; then |
| 176 | + echo "Errors deleting branches $branches_to_remove from remote '$remote'" >&2 |
| 177 | + fi |
| 178 | + fi |
| 179 | + fi |
| 180 | +done |
| 181 | + |
| 182 | +# Delete any remaining local remote-tracking branches of remotes that are gone |
| 183 | +# This is an atypical situation that has occurred to me personally after having |
| 184 | +# used the command: |
| 185 | +# |
| 186 | +# $ hub merge <some-github-url> |
| 187 | +# |
| 188 | +branches_to_remove="" |
| 189 | +for branch in $(git remote-branches); do |
| 190 | + for base in $bases; do |
| 191 | + if git contains "$base" "$branch"; then |
| 192 | + if ! git contains "$branch" "$base"; then |
| 193 | + branches_to_remove="$branches_to_remove $branch" |
| 194 | + break |
| 195 | + fi |
| 196 | + fi |
| 197 | + done |
| 198 | +done |
| 199 | + |
| 200 | +if [ -n "$branches_to_remove" ]; then |
| 201 | + safegit branch -dr $branches_to_remove |
| 202 | +fi |
0 commit comments