[fix] detect remote default branch instead of hardcoding main (#573)#574
Merged
danielmeppiel merged 2 commits intomicrosoft:mainfrom Apr 4, 2026
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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#refas "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
branchkwarg 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
approved these changes
Apr 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
apm installfailed when a dependency repo uses any default branch otherthan
main(e.g.master,develop). Before this fix, a clone with--branch=mainwas always attempted, producing:After this fix, when no
#refis given inapm.yml, git clones without--branch(letting the remote HEAD decide), and the actual default branchname is read from
repo.active_branch.namefor subsequent operations.Why
resolve_git_referencedefaulted to"main"whendep_ref.referencewasempty and passed it as
--branch=mainto every shallow clone, breaking anyrepo 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 toref = dep_ref.reference or Noneclone_kwargswithbranchonly whenrefis set,matching the existing pattern in
download_subdirectory_packageref_namefromrepo.active_branch.nameinstead of the hardcoded string
except GitCommandErrorfallback,raiseimmediately whenrefisNone-- the failure is not a branch-not-found issue and the ref-lookuploop that follows would be nonsensical without a target ref
"main"as its fallbackvia a local
effective_ref = ref or "main"Test
test_resolve_git_reference_no_ref_uses_remote_head: mocksRepo.clone_fromwith
active_branch.name = 'master', asserts the resolvedref_nameis'master'and thatclone_fromwas called without abranchkwargtest_resolve_git_reference_branch(explicit#main) continues to passpython -m pytest tests/unit/ -q --ignore=tests/unit/test_audit_report.py-- 3527 passed
test_credential_fill_used_when_no_env_token) isunrelated to this change and fails on the unmodified main branch too