diff --git a/scripts/common.sh b/scripts/common.sh index fc9bdb4..a5e7f70 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -1928,18 +1928,21 @@ function get_push_list { fi base_commit=$(diff -u <(git rev-list --first-parent "$1" 2>/dev/null) <(git rev-list --first-parent "$2" 2>/dev/null) | sed -ne 's/^ //p' | head -1 || true) + # diff prints no context lines when both histories are identical (a fresh + # branch with no commits of its own) — fall back to the merge base + if [ -z "$base_commit" ]; then + base_commit=$(git merge-base "$1" "$2" 2>/dev/null || true) + fi if [ -n "$base_commit" ]; then push_list=$(commit_list 999 "tab" "$base_commit..HEAD") history_from="${base_commit::7}" + elif git rev-parse --verify --quiet "$origin/$2" >/dev/null 2>&1; then + push_list=$(commit_list 999 "tab" "$origin/$2..HEAD") + history_from="$origin/$2" else - # If no base commit found and origin_name is empty, use HEAD - if [ -z "$origin" ]; then - push_list="" - history_from="" - else - push_list=$(commit_list 999 "tab" "$origin/$2..HEAD") - history_from="$origin/$2" - fi + # No common base and no remote main branch to compare against + push_list=$(commit_list 999 "tab") + history_from="HEAD" fi } diff --git a/tests/test_repository_helpers.bats b/tests/test_repository_helpers.bats index c1618bc..779dd27 100644 --- a/tests/test_repository_helpers.bats +++ b/tests/test_repository_helpers.bats @@ -113,6 +113,33 @@ teardown() { [ -n "$push_list" ] } +@test "get_push_list: empty new branch without remote main yields no commits and no errors" { + setup_remote_repo + git push -q origin --delete main 2>/dev/null || true + git update-ref -d refs/remotes/origin/main 2>/dev/null || true + + create_test_branch "develop" + + output=$(get_push_list "develop" "main" "origin" 2>&1) + [[ "$output" != *"fatal"* ]] + get_push_list "develop" "main" "origin" 2>/dev/null + [ -z "$push_list" ] +} + +@test "get_push_list: new branch without remote main lists only its own commits" { + setup_remote_repo + git update-ref -d refs/remotes/origin/main 2>/dev/null || true + + create_test_branch "develop" + make_test_commit "dev.txt" "Dev commit" + + output=$(get_push_list "develop" "main" "origin" 2>&1) + [[ "$output" != *"fatal"* ]] + get_push_list "develop" "main" "origin" 2>/dev/null + count=$(echo -e "$push_list" | wc -l | tr -d ' ') + [ "$count" -eq 1 ] +} + @test "get_push_list: shows multiple unpushed commits" { setup_remote_repo