-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(branch): prevent git delete-merged-branches from deleting default branch (#1132) #1269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
spacewander
merged 5 commits into
tj:main
from
vaibhavmashal:fix/prevent-deleting-default-branch-1132
Sep 21, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
afc5fcd
fix(branch): prevent git delete-merged-branches from deleting default…
vaibhavmashal 65e314c
fix(tests): remove UTF-8 BOM from bats test file
vaibhavmashal fab2c51
fix(branch): exclude current branch before trimming whitespace in del…
vaibhavmashal a09f4e5
fix(branch): preserve success exit status on empty branch list and ad…
vaibhavmashal 18912c2
test: fix master default branch test in git-delete-merged-branches.bats
vaibhavmashal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| git branch --no-color --merged | grep -v "\*" | grep -v "$(git_extra_default_branch)" | tr -d ' ' | ||
| default_branch=$(git_extra_default_branch) | ||
| git branch --no-color --merged | grep -vE "^(\*|\+)" | sed 's/^[ ]*//' | grep -Fvx -e "$default_branch" -e "main" -e "master" -e "trunk" -e "svn" || true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| git branch --no-color --no-merged | grep -v "\*" | grep -v "$(git_extra_default_branch)" | tr -d ' ' | ||
| default_branch=$(git_extra_default_branch) | ||
| git branch --no-color --no-merged | grep -vE "^(\*|\+)" | sed 's/^[ ]*//' | grep -Fvx -e "$default_branch" -e "main" -e "master" -e "trunk" -e "svn" || true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # shellcheck shell=bash | ||
|
|
||
| source "$BATS_TEST_DIRNAME/test_util.sh" | ||
|
|
||
| setup_file() { | ||
| test_util.setup_file | ||
| test_util.install_command delete-merged-branches | ||
| test_util.install_command show-merged-branches | ||
| test_util.install_command show-unmerged-branches | ||
| } | ||
|
|
||
| setup() { | ||
| test_util.cd_test | ||
|
|
||
| test_util.git_init | ||
| git commit --allow-empty -m "Initial commit" | ||
| git branch feature-merged | ||
| git branch feature-unmerged | ||
| git checkout feature-unmerged | ||
| git commit --allow-empty -m "Unmerged commit" | ||
| git checkout main | ||
| } | ||
|
|
||
| @test "show-merged-branches lists merged branches but not default branch" { | ||
| run git show-merged-branches | ||
| assert_output "feature-merged" | ||
| assert_success | ||
| } | ||
|
|
||
| @test "show-unmerged-branches lists unmerged branches" { | ||
| run git show-unmerged-branches | ||
| assert_output "feature-unmerged" | ||
| assert_success | ||
| } | ||
|
|
||
| @test "delete-merged-branches deletes merged branches and preserves default and unmerged branches" { | ||
| run git delete-merged-branches | ||
| assert_success | ||
|
|
||
| run git branch --list | ||
| assert_line -p "main" | ||
| assert_line -p "feature-unmerged" | ||
| refute_line -p "feature-merged" | ||
| } | ||
|
|
||
| @test "delete-merged-branches when checked out on feature branch protects default branch" { | ||
| git branch other-merged | ||
| git checkout feature-unmerged | ||
| run git delete-merged-branches | ||
| assert_success | ||
|
|
||
| run git branch --list | ||
| assert_line -p "main" | ||
| assert_line -p "feature-unmerged" | ||
| refute_line -p "other-merged" | ||
| } | ||
|
|
||
| @test "delete-merged-branches handles branch names with special characters" { | ||
| git branch "feature/foo+bar" | ||
| run git show-merged-branches | ||
| assert_line -p "feature/foo+bar" | ||
| assert_success | ||
|
|
||
| run git delete-merged-branches | ||
| assert_success | ||
|
|
||
| run git branch --list | ||
| assert_line -p "main" | ||
| refute_line -p "feature/foo+bar" | ||
| } | ||
|
|
||
| @test "delete-merged-branches protects master when master is the default branch" { | ||
| git checkout -b master | ||
| git branch -D main | ||
| run git show-merged-branches | ||
| assert_output "feature-merged" | ||
| assert_success | ||
|
|
||
| run git delete-merged-branches | ||
| assert_success | ||
|
|
||
| run git branch --list | ||
| assert_line -p "master" | ||
| assert_line -p "feature-unmerged" | ||
| refute_line -p "feature-merged" | ||
| } | ||
|
|
||
| @test "show-merged-branches, show-unmerged-branches, and delete-merged-branches succeed when no branches match" { | ||
| git branch -D feature-merged | ||
| git branch -D feature-unmerged | ||
|
|
||
| run git show-merged-branches | ||
| assert_success | ||
| assert_output "" | ||
|
|
||
| run git show-unmerged-branches | ||
| assert_success | ||
| assert_output "" | ||
|
|
||
| run git delete-merged-branches | ||
| assert_success | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's cover master branch too