Skip to content

Workspace checkout reports an existing default branch as missing on repositories with many branches #9195

Open

Description

@Abdullah-Uppal

Summary

The workspace checkout script reports the workspace default branch as missing when the repository has a large number of branches, even though the branch exists. Implement then fails with:

The workspace default branch "develop" does not exist in https://bitbucket.org/<workspace>/<repo>.git. The repository's default branch is "master". Update it in Workspace settings → Repository.

The message happens for every branch, including the repository's own default branch (master above). Planning shows the same problem as a warning and falls back to the remote default branch.

Cause

WorkspaceCheckoutScript() in pkg/components/runner/workspace_agent_resources.go runs with set -euo pipefail and checks the branch like this:

heads="$(git ls-remote --heads "${REPO_URL}")" || exit 1
if [ -n "$requested_branch" ] && printf '%s\n' "$heads" | awk '{print $2}' | grep -Fxq "refs/heads/${requested_branch}"; then

grep -q exits on the first match. When the ls-remote output is larger than the pipe buffer, printf or awk is still writing and gets SIGPIPE. Under pipefail, the pipeline then reports failure, so a branch that matched is treated as missing.

Reproduction

set -euo pipefail
heads="$(printf '%040d\trefs/heads/develop\n' 0; for i in $(seq 1 5000); do printf '%040d\trefs/heads/feature/branch-%d-with-a-longish-name\n' 0 $i; done)"
printf '%s\n' "$heads" | awk '{print $2}' | grep -Fxq refs/heads/develop && echo found || echo "not found"
# prints: not found

Fix

Let grep read all of its input:

printf '%s\n' "$heads" | awk '{print $2}' | grep -Fx "refs/heads/${requested_branch}" >/dev/null

I have this fix locally, with a regression test in workspace_checkout_test.go. The test creates a bare repo with 3,000 branches and checks that the requested branch is checked out. I can open a PR.

Environment

  • Bitbucket repository with several hundred branches
  • Local development setup (make dev.up, Docker Fleet Manager runner)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions