Skip to content

Feature: prefer local branch resolution in .scripts/semver.sh (fallback to remote only when needed) #1383

Open

Description

@marcuscastelo

Summary

Make the semver script robust when running on local feature/topic branches by preferring local branch SHAs first and falling back to remote lookups only when necessary. This reduces reliance on remote queries (git ls-remote / GitHub API) and avoids failures like:

Error: branch 'marcuscastelo/issue1382' does not exist in remote https://github.com/marcuscastelo/macroflows

Motivation

Currently .scripts/semver.sh attempts to resolve branch SHAs via remote lookups which can:

  • Fail when the local branch exists only in the local clone (e.g. feature branches not pushed yet).
  • Unnecessarily consume GitHub API or remote git queries, contributing to rate limit/quota usage.

We want the script to:

  1. Try resolving branch/commit info locally (fast, offline-capable).
  2. If local resolution fails, fall back to remote lookups as a secondary option.

Current behavior

  • get_sha_for_branch calls git ls-remote "$REPO_URL" "refs/heads/$branch" and exits with an error if the branch is not present remotely.
  • This causes the script to fail for local-only branches and increases remote request usage.

Example observed error:

Error: branch 'marcuscastelo/issue1382' does not exist in remote https://github.com/marcuscastelo/macroflows

Proposed solution

Change .scripts/semver.sh so that get_sha_for_branch:

  1. First attempts a local resolution:
    • Use git rev-parse --verify "refs/heads/$branch" or git show-ref --verify -- "refs/heads/$branch" to get a local SHA.
    • If the branch name may include remote/namespace prefixes, also try git rev-parse --verify "$branch" and git rev-parse --verify "origin/$branch" as sensible local fallbacks.
  2. If local resolution fails, then perform the remote git ls-remote lookup (current behavior).
  3. When falling back to remote, prefer git ls-remote over GitHub API calls where possible to reduce API quota usage. Only use GitHub API for operations that require it (e.g., listing branches for rc discovery), and add retry/backoff as already present.

Additionally:

  • Update get_current_branch and other functions to prefer local information when available.
  • Add robust error messages that clearly indicate whether a local or remote lookup failed.
  • Add unit-testable helper functions (where reasonable for a shell script) or at least a test-mode (--test) verification path that simulates local vs remote behavior.

Implementation notes

Suggested implementation changes inside .scripts/semver.sh:

  • Update get_sha_for_branch() to something like:
    1. Try git rev-parse --verify --quiet "refs/heads/$branch" -> if found, use that SHA.
    2. Try git rev-parse --verify --quiet "$branch" (handles detached HEAD/refs) -> if found, use it.
    3. Fallback to git ls-remote "$REPO_URL" "refs/heads/$branch" (current behavior).
  • When falling back to remote, keep the existing error handling and messages.
  • Where commit counts are calculated (e.g., get_commit_count_between), prefer git rev-list --count <from>..<to> when both SHAs are available locally; otherwise, fall back to the existing GitHub compare endpoint with retry/backoff.

Acceptance criteria

  • Running .scripts/semver.sh on a local-only branch no longer fails with "branch ... does not exist in remote ..." — it produces a dev/rc version as expected.
  • Remote lookups only happen if necessary (local resolution fails).
  • Existing behaviors (rc/stable discovery) remain unchanged for branches pushed remotely.
  • Error handling and debug logs remain informative and preserve current retry/backoff logic for remote calls.
  • Tests or manual verification steps documented in the PR description.

Files to change

  • .scripts/semver.sh — prefer local branch resolution, add local-based commit count logic where applicable.

Suggested follow-ups

  • Add a small integration test that creates a local branch and runs the script to assert it resolves a dev version without pushing.
  • Document the fallback behavior in docs/ or the script header.

Activity

  1. modified the milestones: v0.15.0, v0.16.0 on Dec 1, 2025
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

    featureRequest or implement a new feature

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions