diff --git a/bin/git-delete-squashed-branches b/bin/git-delete-squashed-branches index 6d1b6bbe..6eee178c 100755 --- a/bin/git-delete-squashed-branches +++ b/bin/git-delete-squashed-branches @@ -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 diff --git a/helper/git-extra-utility b/helper/git-extra-utility index 6ed6943d..439365f2 100755 --- a/helper/git-extra-utility +++ b/helper/git-extra-utility @@ -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) - 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 diff --git a/tests/git-delete-squashed-branches.bats b/tests/git-delete-squashed-branches.bats new file mode 100644 index 00000000..c2c89b23 --- /dev/null +++ b/tests/git-delete-squashed-branches.bats @@ -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" +}