diff --git a/lib/bash/gh/lib_gh.sh b/lib/bash/gh/lib_gh.sh index cccfa72..7ae895d 100644 --- a/lib/bash/gh/lib_gh.sh +++ b/lib/bash/gh/lib_gh.sh @@ -120,7 +120,7 @@ gh_infer_repo_from_origin() { gh_detect_default_branch() { local repo_dir="$1" local result_var="${2:-}" - local default_branch + local detected_branch if [[ -z "$repo_dir" || -z "$result_var" ]]; then log_error "Usage: gh_detect_default_branch " @@ -128,10 +128,10 @@ gh_detect_default_branch() { fi assert_variable_name "$result_var" - if default_branch="$(git -C "$repo_dir" symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null)"; then - default_branch="${default_branch#origin/}" - if [[ -n "$default_branch" ]]; then - printf -v "$result_var" '%s' "$default_branch" + if detected_branch="$(git -C "$repo_dir" symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null)"; then + detected_branch="${detected_branch#origin/}" + if [[ -n "$detected_branch" ]]; then + printf -v "$result_var" '%s' "$detected_branch" return 0 fi fi @@ -154,7 +154,7 @@ gh_detect_default_branch() { gh_repo_default_branch() { local repo="$1" local result_var="${2:-}" - local default_branch status + local remote_default_branch status if [[ -z "$repo" || -z "$result_var" ]]; then log_error "Usage: gh_repo_default_branch " @@ -163,18 +163,18 @@ gh_repo_default_branch() { assert_variable_name "$result_var" gh_require_cli || return 1 - default_branch="$(gh repo view "$repo" --json defaultBranchRef --jq .defaultBranchRef.name 2>/dev/null)" + remote_default_branch="$(gh repo view "$repo" --json defaultBranchRef --jq .defaultBranchRef.name 2>/dev/null)" status=$? if ((status != 0)); then gh_report_command_failure "$status" repo view "$repo" --json defaultBranchRef --jq .defaultBranchRef.name return $? fi - if [[ -z "$default_branch" ]]; then + if [[ -z "$remote_default_branch" ]]; then log_error "GitHub repository '$repo' does not report a default branch." return 1 fi - printf -v "$result_var" '%s' "$default_branch" + printf -v "$result_var" '%s' "$remote_default_branch" } __gh_api_failure_retryable() { diff --git a/lib/bash/gh/tests/lib_gh.bats b/lib/bash/gh/tests/lib_gh.bats index 1b9f46f..68b7db1 100644 --- a/lib/bash/gh/tests/lib_gh.bats +++ b/lib/bash/gh/tests/lib_gh.bats @@ -194,6 +194,19 @@ EOF [ "$branch" = "main" ] } +@test "gh_detect_default_branch supports default_branch as the result variable name" { + local repo_dir="$TEST_TMPDIR/repo" + local default_branch="" + + init_git_repo "$repo_dir" + printf 'base\n' > "$repo_dir/data.txt" + commit_all "$repo_dir" "Initial commit" + + gh_detect_default_branch "$repo_dir" default_branch + + [ "$default_branch" = "main" ] +} + @test "gh_detect_default_branch reports failure when no default branch can be detected" { local repo_dir="$TEST_TMPDIR/repo" local branch="sentinel" @@ -225,6 +238,23 @@ EOF [ "$branch" = "develop" ] } +@test "gh_repo_default_branch supports default_branch as the result variable name" { + local default_branch="" + + create_fake_gh <<'EOF' +#!/usr/bin/env bash +if [[ "$1" == "repo" && "$2" == "view" ]]; then + printf 'develop\n' + exit 0 +fi +exit 99 +EOF + + gh_repo_default_branch "owner/repo" default_branch + + [ "$default_branch" = "develop" ] +} + @test "gh_api_with_retry retries retryable API pressure once" { create_fake_gh <<'EOF' #!/usr/bin/env bash