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

Accept common URLs as repo spec in parse_github_repo_spec(); closes #90 #109

Merged
merged 2 commits into from Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,3 +1,7 @@
# 1.1.0.9000

* Accept browser, HTTPS, or SSH URL in GitHub repo specification,
@jennybc, #90, #109

# 1.1.0

Expand Down
38 changes: 33 additions & 5 deletions R/install-github.R
Expand Up @@ -221,11 +221,16 @@ github_resolve_ref.github_release <- function(x, params) {
params
}

#' Parse a concise GitHub repo specification
#' Parse a GitHub repo specification
#'
#' The current format is:
#' \code{[username/]repo[/subdir][#pull|@ref|@*release]}
#' The \code{*release} suffix represents the latest release.
#' HTTPS and SSH remote URLs and the repo's browser URL, e.g.,
#' \code{https://github.com/r-lib/remotes.git} or
#' \code{git@github.com:r-lib/remotes.git} or
#' \code{https://github.com/r-lib/remotes}, are also acceptable
#' ways to specify the simple case of \code{username/repo}.
#'
#' @param repo Character scalar, the repo specification.
#' @return List with members: \code{username}, \code{repo}, \code{subdir}
Expand All @@ -235,12 +240,35 @@ github_resolve_ref.github_release <- function(x, params) {
#' @export
#' @examples
#' parse_github_repo_spec("metacran/crandb")
#' parse_github_repo_spec("jeroenooms/curl@v0.9.3")
#' parse_github_repo_spec("jeroen/curl@v0.9.3")
#' parse_github_repo_spec("jimhester/covr#47")
#' parse_github_repo_spec("hadley/dplyr@*release")
#' parse_github_repo_spec("mangothecat/remotes@550a3c7d3f9e1493a2ba")
#' parse_github_repo_spec("tidyverse/dplyr@*release")
#' parse_github_repo_spec("r-lib/remotes@550a3c7d3f9e1493a2ba")
#' parse_github_repo_spec("https://github.com/jeroen/curl.git")
#' parse_github_repo_spec("git@github.com:metacran/crandb.git")
#' parse_github_repo_spec("https://github.com/jimhester/covr")
#' parse_github_repo_spec("https://github.example.com/user/repo.git")
#' parse_github_repo_spec("git@github.example.com:user/repo.git")
#'
#' \dontrun{
#' ## browser-style URLs cannot be used to specify additional info
#' parse_github_repo_spec("https://github.com/r-lib/remotes/pull/108")
#' }

parse_github_repo_spec <- function(repo) {

re <- "github[^/:]*[/:]([^/]+)/([^/]+)/?(.*)$"
m <- regexec(re, repo)
match <- regmatches(repo, m)[[1]]
if (length(match) > 0) {
if (nchar(match[4]) > 0) {
stop(
"A browser URL must end with repo name, e.g., 'https://github.com/r-lib/remotes'.",
call. = FALSE)
}
repo <- paste(match[2], gsub("\\.git", "", match[3]), sep = "/")
}

username_rx <- "(?:([^/]+)/)?"
repo_rx <- "([^/@#]+)"
subdir_rx <- "(?:/([^@#]*[^@#/])/?)?"
Expand All @@ -249,7 +277,7 @@ parse_github_repo_spec <- function(repo) {
release_rx <- "(?:@([*]release))"
ref_or_pull_or_release_rx <- sprintf("(?:%s|%s|%s)?", ref_rx, pull_rx, release_rx)
github_rx <- sprintf("^(?:%s%s%s%s|(.*))$",
username_rx, repo_rx, subdir_rx, ref_or_pull_or_release_rx)
username_rx, repo_rx, subdir_rx, ref_or_pull_or_release_rx)

param_names <- c("username", "repo", "subdir", "ref", "pull", "release", "invalid")
replace <- stats::setNames(sprintf("\\%d", seq_along(param_names)), param_names)
Expand Down
23 changes: 19 additions & 4 deletions man/parse_github_repo_spec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions tests/testthat/test-install-github.R
Expand Up @@ -327,3 +327,45 @@ test_that("parse_github_repo_spec trailing slash, issue #54", {
parse_github_repo_spec("foo/bar/baz")
)
})

test_that("parse_github_repo_spec accepts HTTPS, SSH, and browser URLs (github.com and GHE)", {
expect_identical(
parse_github_repo_spec("jeroen/curl"),
parse_github_repo_spec("https://github.com/jeroen/curl.git")
)
expect_identical(
parse_github_repo_spec("metacran/crandb"),
parse_github_repo_spec("git@github.com:metacran/crandb.git")
)
expect_identical(
parse_github_repo_spec("jimhester/covr"),
parse_github_repo_spec("https://github.com/jimhester/covr")
)
expect_identical(
parse_github_repo_spec("jennybc/foo"),
parse_github_repo_spec("https://github.ubc.ca/jennybc/foo.git")
)
expect_identical(
parse_github_repo_spec("jennybc/foo"),
parse_github_repo_spec("git@github.ubc.ca:jennybc/foo.git")
)
expect_identical(
parse_github_repo_spec("jennybc/foo"),
parse_github_repo_spec("https://github.ubc.ca/jennybc/foo")
)
})

test_that("parse_github_repo_spec insists that browser URLs ends with repo", {
expect_error(
parse_github_repo_spec("https://github.com/r-lib/remotes/commit/975bed7"),
"must end with repo name"
)
expect_error(
parse_github_repo_spec("https://github.com/r-lib/remotes/pull/108"),
"must end with repo name"
)
expect_error(
parse_github_repo_spec("https://github.com/r-lib/remotes/releases/tag/1.0.0"),
"must end with repo name"
)
})