mirror of
https://github.com/tj/git-extras.git
synced 2026-09-10 07:26:17 -04:00
fix(branch): prevent git delete-merged-branches from deleting default branch (#1132)
This commit is contained in:
parent
09b77e8ed6
commit
afc5fcd761
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
branches=$(git branch --no-color --merged | grep -vE "^(\*|\+)" | grep -v "$(git_extra_default_branch)" | grep -v svn)
|
||||
default_branch=$(git_extra_default_branch)
|
||||
branches=$(git branch --no-color --merged | sed 's/^[ *+]*//' | grep -Fvx -e "$default_branch" -e "main" -e "master" -e "trunk" -e "svn")
|
||||
if [ -n "$branches" ]
|
||||
then
|
||||
echo "$branches" | xargs git branch -d
|
||||
|
|
|
|||
|
|
@ -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 | sed 's/^[ *+]*//' | grep -Fvx -e "$default_branch" -e "main" -e "master" -e "trunk" -e "svn"
|
||||
|
|
|
|||
|
|
@ -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 | sed 's/^[ *+]*//' | grep -Fvx -e "$default_branch" -e "main" -e "master" -e "trunk" -e "svn"
|
||||
|
|
|
|||
|
|
@ -13,6 +13,14 @@ git_extra_default_branch() {
|
|||
echo "$extras_default_branch"
|
||||
elif [ -n "$init_default_branch" ]; then
|
||||
echo "$init_default_branch"
|
||||
elif git show-ref --verify --quiet refs/heads/main; then
|
||||
echo "main"
|
||||
elif git show-ref --verify --quiet refs/heads/master; then
|
||||
echo "master"
|
||||
elif git show-ref --verify --quiet refs/heads/trunk; then
|
||||
echo "trunk"
|
||||
elif git show-ref --verify --quiet refs/heads/svn; then
|
||||
echo "svn"
|
||||
else
|
||||
echo "main"
|
||||
fi
|
||||
|
|
|
|||
70
tests/git-delete-merged-branches.bats
Normal file
70
tests/git-delete-merged-branches.bats
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
# 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 merged-branch
|
||||
git branch unmerged-branch
|
||||
git checkout unmerged-branch
|
||||
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 "merged-branch"
|
||||
assert_success
|
||||
}
|
||||
|
||||
@test "show-unmerged-branches lists unmerged branches" {
|
||||
run git show-unmerged-branches
|
||||
assert_output "unmerged-branch"
|
||||
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 "unmerged-branch"
|
||||
refute_line -p "merged-branch"
|
||||
}
|
||||
|
||||
@test "delete-merged-branches when checked out on feature branch protects default branch" {
|
||||
git branch merged-feature
|
||||
git checkout unmerged-branch
|
||||
run git delete-merged-branches
|
||||
assert_success
|
||||
|
||||
run git branch --list
|
||||
assert_line -p "main"
|
||||
assert_line -p "unmerged-branch"
|
||||
refute_line -p "merged-feature"
|
||||
}
|
||||
|
||||
@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"
|
||||
}
|
||||
Loading…
Reference in a new issue