Install the default branch through `archive/HEAD` instead of trying `main` then `master`

Open Beginner friendly
#1,179 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
73/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start in crates/quarto-source-fetch/src/target.rs, where plain owner/repo targets currently try the main and master archives. Read the archive download and redirect handling, then check how the resolved commit is recorded. Done means a plain repository installs from its GitHub default branch, including repositories whose default branch has another name.

Written by the indexing model from the issue text.

Description

Description
Context

For a plain owner/repo, crates/quarto-source-fetch/src/target.rs tries archive/refs/heads/main, then archive/refs/heads/master.
When the default branch has another name, q2 either fails, or installs a branch that isn't the default.

https://github.com/mcanouil/q2-default-branch-demo shows the second case.
Its default branch is develop, with the demo extension at version 2.0.0.
An older main branch has the same extension at version 1.0.0.

$ q2 add mcanouil/q2-default-branch-demo --trust --no-prompt
The following changes will be made in .:
  Demo  [Install]      1.0.0  (filter)

q2 installs version 1.0.0 from main.
The record it writes has no ref and no commit, so nothing shows later that the install came from a branch that isn't the default:

version: 1
source: mcanouil/q2-default-branch-demo
kind: github
files:
  _extension.yml: b57dd1f9313c1b622ed19918e48134119762128b5612b709918317c10b63cef3
  demo.lua: 2150321a3a3a341aebde685226d403639b195ff507c1a45b9b6ab23fc4ee5f26
Proposal

Download https://github.com/<owner>/<repo>/archive/HEAD.tar.gz for the default branch.
GitHub answers with a redirect that names the commit at the head of the default branch:

$ curl -sI https://github.com/mcanouil/q2-default-branch-demo/archive/HEAD.tar.gz | grep -i '^location'
location: https://codeload.github.com/mcanouil/q2-default-branch-demo/tar.gz/0cb98bf577f198e0417d163bc589e8dfb9ae2e96

archive/refs/heads/main redirects to .../tar.gz/refs/heads/main, with no commit.

Why

It removes the guess at the branch name, and needs no API call or token.

Benefits

q2 add owner/repo installs the default branch of any repository, whatever its name.
q2 learns the installed commit for free, which https://github.com/quarto-dev/q2/issues/1178 needs.
Other tools that install from GitHub, such as Quarto Wizard, can use the same URL and get the same commit for the same input.

Related:

Dominant language
Rust
Stars
302
Forks
22
Avg merge
5h 2m
Merged PRs (30d)
213

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quarto-dev/q2

All issues in quarto-dev/q2

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.