diff --git a/scripts/branch.sh b/scripts/branch.sh index 6352c3b..61dd5c4 100644 --- a/scripts/branch.sh +++ b/scripts/branch.sh @@ -448,7 +448,7 @@ function branch_script { "$main_branch"|"$current_branch"|master|main|develop) continue ;; esac merged_to_delete+=("$_mb") - done < <(git branch --merged --format='%(refname:short)') + done < <(git branch --merged HEAD --format='%(refname:short)') for branch_to_delete in "${merged_to_delete[@]}"; do delete_output=$(git branch -d "$branch_to_delete" 2>&1) diff --git a/tests/test_branch_operations.bats b/tests/test_branch_operations.bats index 351c769..033966d 100644 --- a/tests/test_branch_operations.bats +++ b/tests/test_branch_operations.bats @@ -99,6 +99,24 @@ teardown() { [ "$status" -ne 0 ] } +@test "branch deletion: merged-branch listing used by gitb branch delete works" { + create_test_branch "feature/merged" + make_test_commit "feature.txt" "Feature work" + git checkout main + git merge feature/merged --no-edit + + run git branch --merged HEAD --format='%(refname:short)' + [ "$status" -eq 0 ] + [[ "$output" == *"feature/merged"* ]] +} + +@test "branch deletion: --merged is never followed directly by another flag" { + # `--merged`/`--no-merged` take an optional commit, so git parses a + # following `--format=...` as that commit: "malformed object name". + run grep -rnE -- '--(no-)?merged +-' "${GITBASHER_ROOT}/scripts" + [ "$status" -ne 0 ] +} + # ===== Remote branch tests ===== @test "remote branches: can fetch from remote" {