Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/bash/gh/lib_gh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ 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 <repo_dir> <result_variable_name>"
return 1
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
Expand All @@ -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 <owner/repo> <result_variable_name>"
Expand All @@ -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() {
Expand Down
30 changes: 30 additions & 0 deletions lib/bash/gh/tests/lib_gh.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading