Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions actin/local/util/git-clean-stale-branches
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -euo pipefail

git fetch --prune
mapfile -t MERGED_DELETED_BRANCHES < <(git branch -vv | grep ': gone]' | awk '{print $1}')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit here, I don't think you need the <(...) construct, which presents a file descriptor for the given command output, because mapfile is meant to take stdin. In fact I thought you had to specify -u for that?

While this may (or may not...) work, I'd expect to see either:

git branch -vv | grep ': gone]' | awk '{print $1}' | mapfile -t MERGED_DELETED_BRANCHES

or

mapfile -t MERGED_DELETED_BRANCHES < $(git branch ...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems I can't use mapfile -t MERGED_DELETED_BRANCHES < $(git branch ...) since < to redirect to stdin expects a file descriptor, and I can't use git branch -vv | grep ': gone]' | awk '{print $1}' | mapfile -t MERGED_DELETED_BRANCHES because it scopes the MERGED_DELETED_BRANCHES variable to the pipeline. I could do mapfile -t MERGED_DELETED_BRANCHES <<< $(git branch ...) but it doesn't feel much better than what's there already.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Maybe the GNU version behaves differently. Still fine to merge :)


if [ ${#MERGED_DELETED_BRANCHES[@]} -eq 0 ]; then
echo "No merged or deleted branches."
exit 0
fi

echo "Merged or deleted branches (${#MERGED_DELETED_BRANCHES[@]}):"
printf ' - %s\n' "${MERGED_DELETED_BRANCHES[@]}"

read -r -p "Delete these? [y/N] " reply
echo ""
if [[ "$reply" =~ ^[Yy]$ ]]; then
git branch -D "${MERGED_DELETED_BRANCHES[@]}"
else
echo "Skipped."
fi