Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions bin/git-delete-squashed-branches
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ else
git checkout "$targetBranch"
fi

defaultBranch=$(git_extra_default_branch)

git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do
case "$branch" in
"$targetBranch"|"$defaultBranch"|main|master|trunk|svn)
continue
;;
esac

mergeBase=$(git merge-base "$targetBranch" "$branch")

if [[ $(git cherry "$targetBranch" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]]; then
Expand Down
4 changes: 2 additions & 2 deletions helper/git-extra-utility
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ git_extra_mktemp() {

git_extra_default_branch() {
local extras_default_branch init_default_branch
extras_default_branch=$(git config --get git-extras.default-branch)
Comment thread
hyperupcall marked this conversation as resolved.
Resolved
init_default_branch=$(git config --get init.defaultBranch)
extras_default_branch=$(git config --get --default '' git-extras.default-branch)
init_default_branch=$(git config --get --default '' init.defaultBranch)
if [ -n "$extras_default_branch" ]; then
echo "$extras_default_branch"
elif [ -n "$init_default_branch" ]; then
Expand Down
111 changes: 111 additions & 0 deletions tests/git-delete-squashed-branches.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# shellcheck shell=bash

source "$BATS_TEST_DIRNAME/test_util.sh"

setup_file() {
test_util.setup_file
test_util.install_command delete-squashed-branches
}

setup() {
test_util.cd_test

test_util.git_init
echo "base" > base.txt
git add base.txt
git commit -q -m "Initial commit"
}

@test "delete-squashed-branches deletes squash-merged branch and preserves default and unmerged branches" {
git checkout -q -b feat-squashed
echo "feat" > feat.txt
git add feat.txt
git commit -q -m "Feature commit"

git checkout -q main
git merge -q --squash feat-squashed
git commit -q -m "Squash feat-squashed"

git checkout -q -b feat-unmerged
echo "wip" > wip.txt
git add wip.txt
git commit -q -m "WIP commit"

git checkout -q main
run git delete-squashed-branches
assert_success

run git branch --list
assert_line -p "main"
assert_line -p "feat-unmerged"
refute_line -p "feat-squashed"
}

@test "delete-squashed-branches preserves main when main changes were squashed into another branch" {
git checkout -q -b release
echo "rel" > release.txt
git add release.txt
git commit -q -m "Release commit"

git checkout -q main
echo "fix" > hotfix.txt
git add hotfix.txt
git commit -q -m "Hotfix on main"

git checkout -q release
git merge -q --squash main
git commit -q -m "Squash main hotfix into release"

git checkout -q -b feat-squashed
echo "extra" > extra.txt
git add extra.txt
git commit -q -m "Extra commit"

git checkout -q release
git merge -q --squash feat-squashed
git commit -q -m "Squash feat-squashed into release"

run git delete-squashed-branches
assert_success

run git branch --list
assert_line -p "main"
assert_line -p "release"
refute_line -p "feat-squashed"
}

@test "delete-squashed-branches preserves custom init.defaultBranch and git-extras.default-branch" {
git branch -m main develop
git config init.defaultBranch develop

git checkout -q -b release
echo "rel" > release.txt
git add release.txt
git commit -q -m "Release commit"

git checkout -q develop
echo "dev" > dev.txt
git add dev.txt
git commit -q -m "Commit on develop"

git checkout -q release
git merge -q --squash develop
git commit -q -m "Squash develop into release"

run git delete-squashed-branches
assert_success

run git branch --list
assert_line -p "develop"
assert_line -p "release"

git branch -m develop primary
git config git-extras.default-branch primary

run git delete-squashed-branches release
assert_success

run git branch --list
assert_line -p "primary"
assert_line -p "release"
}
Loading