Skip to content

[fix] detect remote default branch instead of hardcoding main (#573)#574

Merged
danielmeppiel merged 2 commits intomicrosoft:mainfrom
edenfunf:fix/default-branch-detection
Apr 4, 2026
Merged

[fix] detect remote default branch instead of hardcoding main (#573)#574
danielmeppiel merged 2 commits intomicrosoft:mainfrom
edenfunf:fix/default-branch-detection

Conversation

@edenfunf
Copy link
Copy Markdown
Contributor

@edenfunf edenfunf commented Apr 4, 2026

What

apm install failed when a dependency repo uses any default branch other
than main (e.g. master, develop). Before this fix, a clone with
--branch=main was always attempted, producing:

fatal: Remote branch main not found in upstream origin

After this fix, when no #ref is given in apm.yml, git clones without
--branch (letting the remote HEAD decide), and the actual default branch
name is read from repo.active_branch.name for subsequent operations.

Why

resolve_git_reference defaulted to "main" when dep_ref.reference was
empty and passed it as --branch=main to every shallow clone, breaking any
repo whose default branch is not main. This is not intended behaviour --
the dependency declaration carries no branch, so APM should follow whatever
branch the remote advertises as HEAD.

Refs #573.

How

In resolve_git_reference (github_downloader.py):

  • ref = dep_ref.reference or "main" changed to ref = dep_ref.reference or None
  • Shallow clone: build clone_kwargs with branch only when ref is set,
    matching the existing pattern in download_subdirectory_package
  • After a no-ref clone, populate ref_name from repo.active_branch.name
    instead of the hardcoded string
  • In the except GitCommandError fallback, raise immediately when ref is
    None -- the failure is not a branch-not-found issue and the ref-lookup
    loop that follows would be nonsensical without a target ref
  • Artifactory fast-path (no remote to query) keeps "main" as its fallback
    via a local effective_ref = ref or "main"

Test

  • Added test_resolve_git_reference_no_ref_uses_remote_head: mocks Repo.clone_from
    with active_branch.name = 'master', asserts the resolved ref_name is
    'master' and that clone_from was called without a branch kwarg
  • Existing test_resolve_git_reference_branch (explicit #main) continues to pass
  • Full unit suite: python -m pytest tests/unit/ -q --ignore=tests/unit/test_audit_report.py
    -- 3527 passed
  • The one pre-existing failure (test_credential_fill_used_when_no_env_token) is
    unrelated to this change and fails on the unmodified main branch too

resolve_git_reference always passed --branch=main to git clone when no
explicit ref was specified in apm.yml, causing apm install to fail for
repos whose default branch is not main (e.g. master, develop).

Omit --branch when no ref is given so git uses the remote HEAD, then
read the actual default branch name from repo.active_branch.name. The
Artifactory fast-path, which cannot query a remote, keeps "main" as its
fallback.

Fixes microsoft#573
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes apm install for Git dependencies that do not use main as the remote default branch by avoiding --branch=main when no #ref is specified, and by reading the checked-out default branch name after cloning.

Changes:

  • Update resolve_git_reference() to treat a missing #ref as "use remote HEAD" (omit --branch) and to record the detected default branch name from the clone.
  • Preserve the existing Artifactory behavior by continuing to fall back to "main" when no remote can be queried.
  • Add a unit test asserting that no-ref dependencies clone without a branch kwarg and return the detected default branch name.
Show a summary per file
File Description
src/apm_cli/deps/github_downloader.py Stop defaulting missing refs to main; omit branch for shallow clones when no ref is provided; capture the remote default branch name post-clone.
tests/test_github_downloader.py Add regression test ensuring no-ref cloning does not pass branch and resolves ref_name from the repo's active branch.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

_clone_with_fallback catches all GitCommandError internally and
re-raises as RuntimeError, so the except GitCommandError block in
resolve_git_reference is pre-existing dead code. Remove the if not ref:
raise that was added inside it to avoid misleading readers into
thinking the guard does something.
@danielmeppiel danielmeppiel self-requested a review April 4, 2026 20:22
@danielmeppiel danielmeppiel merged commit 5925342 into microsoft:main Apr 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants