Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could pkgdown have a way to detect default branch? #2597

Closed
olivroy opened this issue May 29, 2024 · 6 comments · Fixed by #2623
Closed

Could pkgdown have a way to detect default branch? #2597

olivroy opened this issue May 29, 2024 · 6 comments · Fixed by #2623

Comments

@olivroy
Copy link
Collaborator

olivroy commented May 29, 2024

For "source" links of documentation pages

Let's say I read documentation online and spot a typo.

In the vignette for example: https://pkgdown.r-lib.org/articles/pkgdown.html

I click on the link on top to go edit the vignette

image

This is the one: https://github.com/r-lib/pkgdown/blob/HEAD/vignettes/pkgdown.Rmd

However, I cannot edit it easily :

image

I have to select the main branch on the top left corner.

If the link were https://github.com/r-lib/pkgdown/blob/main/vignettes/pkgdown.Rmd, would remove one step from the process.

usethis has usethis:::git_default_branch() for this. So if something similar could be called once, to create all the links, it would be awesome.

I understand pkgdown doesn't have git related dependencies, but it would be possible via a system call? This would have the biggest impact on the R pkgdown ecosystem.

Worst case scenario (opt-in), but the impact would be fairly minimal across pkgdown ecosytems would be to have a way to specify what is the default branch in config?

@hadley
Copy link
Member

hadley commented May 29, 2024

Hmmm, I don't think we can do this generally, but I think we could optimise for the common case, looking for a branch named either main or master, and using that if present. That would cover 99% of cases, I think.

@hadley
Copy link
Member

hadley commented May 29, 2024

Maybe something like this:

default_branch <- function(pkg) {
  if (Sys.which("git") == "") {
    return("HEAD")
  }

  if (has_branch(pkg$src_path, "main")) {
    "main"
  } else if (has_branch(pkg$src_path, "master")) {
    "master"
  } else {
    "HEAD"
  }
}

has_branch <- function(repo, branch) {
  withr::local_dir(repo)
  tryCatch(
    {
      git("show-ref", paste0("refs/heads/", branch), echo = FALSE, echo_cmd = FALSE)
      TRUE
    },
    error = function(e) {
      FALSE
    }
  )
}

@olivroy
Copy link
Collaborator Author

olivroy commented May 29, 2024

Sounds like a good idea!

@hadley
Copy link
Member

hadley commented May 29, 2024

Alternatively, we could use the REST API: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#get-a-repository

@maelle
Copy link
Collaborator

maelle commented May 30, 2024

@hadley
Copy link
Member

hadley commented May 30, 2024

@maelle FWIW that code is for determining what the default branch is for new repos

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 a pull request may close this issue.

3 participants