Skip to content

Commit b2e7767

Browse files
committed
Handle branch names safely in cleanup script
1 parent 2ca66fd commit b2e7767

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

bin/common/.local/bin/git-cleanup-branches

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
git fetch --prune &>/dev/null
66

7-
for branch in $(git branch -vv | grep -o -E '^[\* ]+\S+' | tr -d '* '); do
8-
upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "${branch}@{upstream}" 2>/dev/null)
9-
if [ -n "${upstream}" ]; then
10-
if ! git show-ref --quiet "refs/remotes/${upstream}"; then
11-
echo "Deleting branch '${branch}'"
12-
git branch -D "${branch}"
7+
# Use `git for-each-ref` to safely handle branch names with spaces and avoid
8+
# parsing the output of `git branch`.
9+
git for-each-ref --format='%(refname:short)\t%(upstream:short)' refs/heads |
10+
while IFS=$'\t' read -r branch upstream; do
11+
if [ -n "$upstream" ] && ! git show-ref --quiet "refs/remotes/$upstream"; then
12+
echo "Deleting branch '$branch'"
13+
git branch -D "$branch"
1314
fi
14-
fi
15-
done
15+
done

0 commit comments

Comments
 (0)