| 1 |
|
| 2 |
#' Resolve a Remote |
| 3 |
#' |
| 4 |
#' Given a remote specification, resolve it into an renv package record that |
| 5 |
#' can be used for download and installation (e.g. with [install]). |
| 6 |
#' |
| 7 |
#' @param spec A remote specification. This should be a string, conforming |
| 8 |
#' to the Remotes specification as defined in |
| 9 |
#' <https://remotes.r-lib.org/articles/dependencies.html>. |
| 10 |
#' |
| 11 |
remote <- function(spec) { |
| 12 |
renv_scope_error_handler() |
| 13 |
renv_remotes_resolve(spec) |
| 14 |
} |
| 15 |
|
| 16 |
# take a short-form remotes spec, parse that into a remote, |
| 17 |
# and generate a corresponding package record |
| 18 |
renv_remotes_resolve <- function(spec, latest = FALSE, infer = FALSE) { |
| 19 |
|
| 20 |
# check for already-resolved specs |
| 21 |
if (is.null(spec) || is.list(spec)) |
| 22 |
return(spec) |
| 23 |
|
| 24 |
# check for a package name prefix and remove it |
| 25 |
regexps <- .standard_regexps() |
| 26 |
pattern <- sprintf("^%s=", regexps$valid_package_name) |
| 27 |
spec <- sub(pattern, "", spec) |
| 28 |
|
| 29 |
# remove a trailing slash |
| 30 |
# https://github.com/rstudio/renv/issues/1135 |
| 31 |
spec <- gsub("/+$", "", spec, perl = TRUE) |
| 32 |
|
| 33 |
# check if we should infer the package version |
| 34 |
# from a locally-installed copy of the package |
| 35 |
infer <- |
| 36 |
infer && |
| 37 |
grepl(renv_regexps_package_name(), spec) && |
| 38 |
renv_package_installed(spec) |
| 39 |
|
| 40 |
if (infer) |
| 41 |
spec <- paste(spec, renv_package_version(spec), sep = "@") |
| 42 |
|
| 43 |
# check for archive URLs -- this is a bit hacky |
| 44 |
if (grepl("^(?:file|https?)://", spec)) { |
| 45 |
for (suffix in c(".zip", ".tar.gz", ".tgz", "/tarball")) |
| 46 |
if (endsWith(spec, suffix)) |
| 47 |
return(renv_remotes_resolve_url(spec, quiet = TRUE)) |
| 48 |
} |
| 49 |
|
| 50 |
# remove github prefix |
| 51 |
spec <- gsub("^https?://(?:www\\.)?github\\.com/", "", spec) |
| 52 |
|
| 53 |
# check for paths to existing local files |
| 54 |
first <- substring(spec, 1L, 1L) |
| 55 |
local <- first %in% c("~", "/", ".") || renv_path_absolute(spec) |
| 56 |
|
| 57 |
if (local) { |
| 58 |
record <- catch(renv_remotes_resolve_path(spec)) |
| 59 |
if (!inherits(record, "error")) |
| 60 |
return(record) |
| 61 |
} |
| 62 |
|
| 63 |
# check for explicit local remotes |
| 64 |
if (grepl("^local::", spec)) { |
| 65 |
spec <- substring(spec, 8L) |
| 66 |
record <- catch(renv_remotes_resolve_path(spec)) |
| 67 |
if (!inherits(record, "error")) |
| 68 |
return(record) |
| 69 |
} |
| 70 |
|
| 71 |
# check for requests to install local packages -- note that depending on how |
| 72 |
# the R package was built / generated, it's possible that it might not adhere |
| 73 |
# to the "typical" R package names, so we try to be a bit flexible here |
| 74 |
ext <- "(?:\\.tar\\.gz|\\.tgz|\\.zip)$" |
| 75 |
if (grepl(ext, spec, perl = TRUE)) { |
| 76 |
pathlike <- tryCatch(file.exists(spec), condition = identity) |
| 77 |
if (identical(pathlike, TRUE)) { |
| 78 |
return(renv_remotes_resolve_path(spec)) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
# define error handler (tag error with extra context when possible) |
| 83 |
error <- function(e) { |
| 84 |
|
| 85 |
# build error message |
| 86 |
fmt <- "failed to resolve remote '%s'" |
| 87 |
prefix <- sprintf(fmt, spec) |
| 88 |
message <- paste(prefix, e$message, sep = " -- ") |
| 89 |
|
| 90 |
# otherwise, propagate the error |
| 91 |
stop(simpleError(message = message, call = e$call)) |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
# attempt the parse |
| 96 |
withCallingHandlers( |
| 97 |
renv_remotes_resolve_impl(spec, latest), |
| 98 |
error = error |
| 99 |
) |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
renv_remotes_resolve_impl <- function(spec, latest = FALSE) { |
| 104 |
|
| 105 |
remote <- renv_remotes_parse(spec) |
| 106 |
|
| 107 |
# fixup for bioconductor |
| 108 |
isbioc <- |
| 109 |
identical(remote$type, "repository") && |
| 110 |
identical(remote$repository, "bioc") |
| 111 |
|
| 112 |
if (isbioc) |
| 113 |
remote$type <- "bioc" |
| 114 |
|
| 115 |
# treat HEAD refs as an implicit request to use the default branch |
| 116 |
# of the associated remote repository |
| 117 |
# https://github.com/rstudio/renv/issues/2040 |
| 118 |
if (identical(remote$ref, "HEAD")) |
| 119 |
remote$ref <- NULL |
| 120 |
|
| 121 |
resolved <- switch( |
| 122 |
remote$type, |
| 123 |
bioc = renv_remotes_resolve_bioc(remote), |
| 124 |
bitbucket = renv_remotes_resolve_bitbucket(remote), |
| 125 |
gitlab = renv_remotes_resolve_gitlab(remote), |
| 126 |
github = renv_remotes_resolve_github(remote), |
| 127 |
repository = renv_remotes_resolve_repository(remote, latest), |
| 128 |
git = renv_remotes_resolve_git(remote), |
| 129 |
url = renv_remotes_resolve_url(remote$url, quiet = TRUE), |
| 130 |
stopf("unknown remote type '%s'", remote$type %||% "<NA>") |
| 131 |
) |
| 132 |
|
| 133 |
# ensure that attributes on the record are preserved, but drop NULL entries |
| 134 |
for (key in names(resolved)) |
| 135 |
if (is.null(resolved[[key]])) |
| 136 |
resolved[[key]] <- NULL |
| 137 |
|
| 138 |
resolved |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
renv_remotes_parse_impl <- function(spec, pattern, fields) { |
| 143 |
|
| 144 |
matches <- regexec(pattern, spec, perl = TRUE) |
| 145 |
strings <- regmatches(spec, matches)[[1]] |
| 146 |
if (empty(strings)) |
| 147 |
stopf("'%s' is not a valid remote", spec) |
| 148 |
|
| 149 |
if (length(fields) != length(strings)) |
| 150 |
stop("internal error: field length mismatch in renv_remotes_parse_impl") |
| 151 |
|
| 152 |
names(strings) <- fields |
| 153 |
remote <- as.list(strings) |
| 154 |
lapply(remote, function(item) if (nzchar(item)) item) |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
renv_remotes_parse_repos <- function(spec) { |
| 159 |
|
| 160 |
pattern <- paste0( |
| 161 |
"^", # start |
| 162 |
"(?:([^:]+)::)?", # optional repository name |
| 163 |
"([[:alnum:].]+)", # package name |
| 164 |
"(?:@([[:digit:]_.-]+))?", # optional package version |
| 165 |
"$" |
| 166 |
) |
| 167 |
|
| 168 |
fields <- c("spec", "repository", "package", "version") |
| 169 |
renv_remotes_parse_impl(spec, pattern, fields) |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
renv_remotes_parse_remote <- function(spec) { |
| 174 |
|
| 175 |
pattern <- paste0( |
| 176 |
"^", |
| 177 |
"(?:([[:alpha:]][[:alnum:].]*[[:alnum:]])=)?", # optional package name |
| 178 |
"(?:([^@:]+)(?:@([^:]+))?::)?", # optional prefix, providing type + host |
| 179 |
"([^/#@:]+)", # a username |
| 180 |
"(?:[/]([^@#:]+))?", # a repository (allow sub-repositories) |
| 181 |
"(?:[:]([^@#]+))?", # optional subdirectory |
| 182 |
"(?:[#]([^@]+))?", # optional hash (e.g. pull request) |
| 183 |
"(?:[@](.+))?", # optional ref (e.g. branch or commit) |
| 184 |
"$" |
| 185 |
) |
| 186 |
|
| 187 |
fields <- c( |
| 188 |
"spec", "package", "type", |
| 189 |
"host", "user", "repo", |
| 190 |
"subdir", "pull", "ref" |
| 191 |
) |
| 192 |
|
| 193 |
remote <- renv_remotes_parse_impl(spec, pattern, fields) |
| 194 |
if (!nzchar(remote$repo)) |
| 195 |
stopf("'%s' is not a valid remote", spec) |
| 196 |
|
| 197 |
renv_remotes_parse_finalize(remote) |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
renv_remotes_parse_gitssh <- function(spec) { |
| 202 |
|
| 203 |
pattern <- paste0( |
| 204 |
"^", |
| 205 |
"(?:([[:alpha:]][[:alnum:].]*[[:alnum:]])=)?", # optional package name |
| 206 |
"(?:(git)::)?", # optional git prefix |
| 207 |
"(", # url start |
| 208 |
"([^@]+)@", # user (typically, 'git') |
| 209 |
"([^:]+):", # host |
| 210 |
"([^:#@]+)", # the rest of the repo url |
| 211 |
")", # url end |
| 212 |
"(?::([^@#:]+))?", # optional sub-directory |
| 213 |
"(?:#([^@#:]+))?", # optional hash (e.g. pull request) |
| 214 |
"(?:@([^@#:]+))?", # optional ref (e.g. branch or commit) |
| 215 |
"$" |
| 216 |
) |
| 217 |
|
| 218 |
fields <- c( |
| 219 |
"spec", "package", "type", "url", |
| 220 |
"user", "host", "repo", |
| 221 |
"subdir", "pull", "ref" |
| 222 |
) |
| 223 |
|
| 224 |
remote <- renv_remotes_parse_impl(spec, pattern, fields) |
| 225 |
if (!nzchar(remote$repo)) |
| 226 |
stopf("'%s' is not a valid remote", spec) |
| 227 |
|
| 228 |
remote$type <- remote$type %||% "git" |
| 229 |
renv_remotes_parse_finalize(remote) |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
renv_remotes_parse_git <- function(spec) { |
| 234 |
|
| 235 |
hostpattern <- paste0( |
| 236 |
"(", |
| 237 |
"(?:(?:(?!-))(?:xn--|_{1,1})?[a-z0-9-]{0,61}[a-z0-9]{1,1}\\.)*", |
| 238 |
"(?:xn--)?", |
| 239 |
"(?:[a-z0-9][a-z0-9\\-]{0,60}|[a-z0-9-]{1,30}\\.[a-z]{2,})", |
| 240 |
")" |
| 241 |
) |
| 242 |
|
| 243 |
pattern <- paste0( |
| 244 |
"^", |
| 245 |
"(?:([[:alpha:]][[:alnum:].]*[[:alnum:]])=)?", # optional package name |
| 246 |
"(?:(git)::)?", # optional git prefix |
| 247 |
"(", # URL start |
| 248 |
"(?:(https?|git|ssh)://)?", # protocol |
| 249 |
"(?:([^@]+)@)?", # login (probably git) |
| 250 |
hostpattern, # host |
| 251 |
"[/:]([\\w_.-]+)", # a username |
| 252 |
"(?:/([^@#:]+?))?", # a repository (allow sub-repositories) |
| 253 |
"(?:\\.(git))?", # optional .git extension |
| 254 |
")", # URL end |
| 255 |
"(?::([^@#:]+))?", # optional sub-directory |
| 256 |
"(?:#([^@#:]+))?", # optional hash (e.g. pull request) |
| 257 |
"(?:@([^@#:]+))?", # optional ref (e.g. branch or commit) |
| 258 |
"$" |
| 259 |
) |
| 260 |
|
| 261 |
fields <- c( |
| 262 |
"spec", "package", "type", |
| 263 |
"url", "protocol", "login", "host", "user", "repo", "ext", |
| 264 |
"subdir", "pull", "ref" |
| 265 |
) |
| 266 |
|
| 267 |
remote <- renv_remotes_parse_impl(spec, pattern, fields) |
| 268 |
if (!nzchar(remote$repo)) |
| 269 |
stopf("'%s' is not a valid remote", spec) |
| 270 |
|
| 271 |
# If type has not been found & repo looks like a git repo, set it as git |
| 272 |
# (note that this parser also accepts entries which are not truly git |
| 273 |
# references, so we try to "fix up" after the fact) |
| 274 |
if ("git" %in% c(remote$login, remote$type, remote$ext, remote$protocol)) |
| 275 |
remote$type <- tolower(remote$type %||% "git") |
| 276 |
|
| 277 |
renv_remotes_parse_finalize(remote) |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
# NOTE: to avoid ambiguity with git remote specs, we require URL |
| 282 |
# remotes to begin with a 'url::' prefix |
| 283 |
renv_remotes_parse_url <- function(spec) { |
| 284 |
|
| 285 |
pattern <- paste0( |
| 286 |
"^", |
| 287 |
"(?:([[:alpha:]][[:alnum:].]*[[:alnum:]])=)?", # optional package name |
| 288 |
"(url)::", # type (required for URL remotes) |
| 289 |
"((https?)://([^:]+))", # url, protocol, path |
| 290 |
"(?::([^@#:]+))?", # optional subdir |
| 291 |
"$" |
| 292 |
) |
| 293 |
|
| 294 |
fields <- c("spec", "package", "type", "url", "protocol", "path", "subdir") |
| 295 |
remote <- renv_remotes_parse_impl(spec, pattern, fields) |
| 296 |
if (!nzchar(remote$url)) |
| 297 |
stopf("'%s' is not a valid remote", spec) |
| 298 |
|
| 299 |
renv_remotes_parse_finalize(remote) |
| 300 |
} |
| 301 |
|
| 302 |
renv_remotes_parse_finalize <- function(remote) { |
| 303 |
|
| 304 |
# default remote type is github |
| 305 |
remote$type <- tolower(remote$type %||% "github") |
| 306 |
|
| 307 |
# custom finalization for different remote types |
| 308 |
case( |
| 309 |
remote$type == "github" ~ renv_remotes_parse_finalize_github(remote), |
| 310 |
TRUE ~ remote |
| 311 |
) |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
renv_remotes_parse_finalize_github <- function(remote) { |
| 316 |
|
| 317 |
# split repo spec into pieces |
| 318 |
repo <- remote$repo %||% "" |
| 319 |
parts <- strsplit(repo, "/", fixed = TRUE)[[1]] |
| 320 |
if (length(parts) < 2) |
| 321 |
return(remote) |
| 322 |
|
| 323 |
# form subdir from tail of repo |
| 324 |
remote$repo <- paste(head(parts, n = 1L), collapse = "/") |
| 325 |
remote$subdir <- paste(tail(parts, n = -1L), collapse = "/") |
| 326 |
|
| 327 |
# return modified remote |
| 328 |
remote |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
renv_remotes_parse <- function(spec) { |
| 333 |
|
| 334 |
remote <- catch(renv_remotes_parse_repos(spec)) |
| 335 |
if (!inherits(remote, "error")) { |
| 336 |
remote$type <- "repository" |
| 337 |
return(remote) |
| 338 |
} |
| 339 |
|
| 340 |
remote <- catch(renv_remotes_parse_remote(spec)) |
| 341 |
if (!inherits(remote, "error")) { |
| 342 |
remote$type <- remote$type %||% "github" |
| 343 |
return(remote) |
| 344 |
} |
| 345 |
|
| 346 |
remote <- catch(renv_remotes_parse_gitssh(spec)) |
| 347 |
if (!inherits(remote, "error")) { |
| 348 |
remote$type <- remote$type %||% "git" |
| 349 |
return(remote) |
| 350 |
} |
| 351 |
|
| 352 |
remote <- catch(renv_remotes_parse_url(spec)) |
| 353 |
if (!inherits(remote, "error")) { |
| 354 |
remote$type <- remote$type %||% "url" |
| 355 |
return(remote) |
| 356 |
} |
| 357 |
|
| 358 |
remote <- catch(renv_remotes_parse_git(spec)) |
| 359 |
if (!inherits(remote, "error")) { |
| 360 |
remote$type <- remote$type %||% "git" |
| 361 |
return(remote) |
| 362 |
} |
| 363 |
|
| 364 |
stopf("failed to parse remote spec '%s'", spec) |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
renv_remotes_resolve_bioc_version <- function(version) { |
| 369 |
|
| 370 |
# initialize Bioconductor |
| 371 |
renv_bioconductor_init() |
| 372 |
BiocManager <- renv_scope_biocmanager() |
| 373 |
|
| 374 |
# handle versions like 'release' and 'devel' |
| 375 |
versions <- BiocManager$.version_map() |
| 376 |
row <- versions[versions$BiocStatus == version, ] |
| 377 |
if (nrow(row)) |
| 378 |
return(row$Bioc) |
| 379 |
|
| 380 |
# otherwise, use the default version |
| 381 |
BiocManager$version() |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
renv_remotes_resolve_bioc_plain <- function(remote) { |
| 386 |
|
| 387 |
list( |
| 388 |
Package = remote$package, |
| 389 |
Version = remote$version, |
| 390 |
Source = "Bioconductor" |
| 391 |
) |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
renv_remotes_resolve_bioc <- function(remote) { |
| 396 |
|
| 397 |
# if we parsed this as a repository remote, use that directly |
| 398 |
if (!is.null(remote$package)) |
| 399 |
return(renv_remotes_resolve_bioc_plain(remote)) |
| 400 |
|
| 401 |
# otherwise, this was parsed as a regular remote, declaring the package |
| 402 |
# should be obtained from a particular Bioconductor release |
| 403 |
package <- remote$repo |
| 404 |
biocversion <- renv_remotes_resolve_bioc_version(remote$user) |
| 405 |
biocrepos <- renv_bioconductor_repos(version = biocversion) |
| 406 |
record <- renv_available_packages_latest(package, repos = biocrepos) |
| 407 |
|
| 408 |
# update fields |
| 409 |
record$Source <- "Bioconductor" |
| 410 |
record$Repository <- NULL |
| 411 |
|
| 412 |
# return the resolved record |
| 413 |
record |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
renv_remotes_resolve_bitbucket <- function(remote) { |
| 418 |
|
| 419 |
user <- remote$user |
| 420 |
repo <- remote$repo |
| 421 |
subdir <- remote$subdir |
| 422 |
ref <- remote$ref %||% getOption("renv.bitbucket.default_branch", "master") |
| 423 |
|
| 424 |
host <- remote$host %||% config$bitbucket.host() |
| 425 |
|
| 426 |
# scope authentication |
| 427 |
renv_scope_auth(repo) |
| 428 |
|
| 429 |
# get commit sha for ref |
| 430 |
fmt <- "%s/repositories/%s/%s/commit/%s" |
| 431 |
origin <- renv_retrieve_origin(host) |
| 432 |
url <- sprintf(fmt, origin, user, repo, ref) |
| 433 |
|
| 434 |
destfile <- renv_scope_tempfile("renv-bitbucket-") |
| 435 |
download(url, destfile = destfile, type = "bitbucket", quiet = TRUE) |
| 436 |
json <- renv_json_read(file = destfile) |
| 437 |
sha <- json$hash |
| 438 |
|
| 439 |
# get DESCRIPTION file |
| 440 |
parts <- c(if (nzchar(subdir %||% "")) subdir, "DESCRIPTION") |
| 441 |
descpath <- paste(parts, collapse = "/") |
| 442 |
fmt <- "%s/repositories/%s/%s/src/%s/%s" |
| 443 |
origin <- renv_retrieve_origin(host) |
| 444 |
url <- sprintf(fmt, origin, user, repo, ref, descpath) |
| 445 |
|
| 446 |
destfile <- renv_scope_tempfile("renv-description-") |
| 447 |
download(url, destfile = destfile, type = "bitbucket", quiet = TRUE) |
| 448 |
desc <- renv_dcf_read(destfile) |
| 449 |
|
| 450 |
list( |
| 451 |
Package = desc$Package, |
| 452 |
Version = desc$Version, |
| 453 |
Source = "Bitbucket", |
| 454 |
RemoteType = "bitbucket", |
| 455 |
RemoteHost = host, |
| 456 |
RemoteUsername = user, |
| 457 |
RemoteRepo = repo, |
| 458 |
RemoteSubdir = subdir, |
| 459 |
RemoteRef = ref, |
| 460 |
RemoteSha = sha |
| 461 |
) |
| 462 |
|
| 463 |
} |
| 464 |
|
| 465 |
renv_remotes_resolve_repository <- function(remote, latest) { |
| 466 |
|
| 467 |
package <- remote$package |
| 468 |
if (package %in% renv_packages_base()) |
| 469 |
return(renv_remotes_resolve_base(package)) |
| 470 |
|
| 471 |
version <- remote$version |
| 472 |
repository <- remote$repository |
| 473 |
|
| 474 |
if (latest && is.null(version)) { |
| 475 |
remote <- renv_available_packages_latest(package) |
| 476 |
version <- remote$Version |
| 477 |
repository <- remote$Repository |
| 478 |
} |
| 479 |
|
| 480 |
list( |
| 481 |
Package = package, |
| 482 |
Version = version, |
| 483 |
Source = "Repository", |
| 484 |
Repository = repository |
| 485 |
) |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
renv_remotes_resolve_base <- function(package) { |
| 490 |
|
| 491 |
list( |
| 492 |
Package = package, |
| 493 |
Version = renv_package_version(package), |
| 494 |
Source = "R" |
| 495 |
) |
| 496 |
|
| 497 |
} |
| 498 |
|
| 499 |
renv_remotes_resolve_github_sha_pull <- function(host, user, repo, pull) { |
| 500 |
|
| 501 |
# scope authentication |
| 502 |
renv_scope_auth(repo) |
| 503 |
|
| 504 |
# make request |
| 505 |
fmt <- "%s/repos/%s/%s/pulls/%s" |
| 506 |
origin <- renv_retrieve_origin(host) |
| 507 |
url <- sprintf(fmt, origin, user, repo, pull) |
| 508 |
jsonfile <- renv_scope_tempfile("renv-json-") |
| 509 |
download(url, destfile = jsonfile, type = "github", quiet = TRUE) |
| 510 |
|
| 511 |
# read resulting JSON |
| 512 |
json <- renv_json_read(jsonfile) |
| 513 |
json$head$sha |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
renv_remotes_resolve_github_sha_ref <- function(host, user, repo, ref) { |
| 518 |
|
| 519 |
# scope authentication |
| 520 |
renv_scope_auth(repo) |
| 521 |
|
| 522 |
# build url for github commits endpoint |
| 523 |
fmt <- "%s/repos/%s/%s/commits/%s" |
| 524 |
origin <- renv_retrieve_origin(host) |
| 525 |
|
| 526 |
ref <- ref %||% renv_remotes_resolve_github_ref(host, user, repo) |
| 527 |
url <- sprintf(fmt, origin, user, repo, ref %||% "main") |
| 528 |
|
| 529 |
# prepare headers |
| 530 |
headers <- c(Accept = "application/vnd.github.sha") |
| 531 |
|
| 532 |
# make request to endpoint |
| 533 |
shafile <- renv_scope_tempfile("renv-sha-") |
| 534 |
download( |
| 535 |
url, |
| 536 |
destfile = shafile, |
| 537 |
type = "github", |
| 538 |
quiet = TRUE, |
| 539 |
headers = headers |
| 540 |
) |
| 541 |
|
| 542 |
# read downloaded content |
| 543 |
sha <- renv_file_read(shafile) |
| 544 |
|
| 545 |
# check for JSON response (in case our headers weren't sent) |
| 546 |
if (nchar(sha) > 40L) { |
| 547 |
json <- renv_json_read(text = sha) |
| 548 |
sha <- json$sha |
| 549 |
} |
| 550 |
|
| 551 |
sha |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
renv_remotes_resolve_github_modules <- function(host, user, repo, subdir, sha) { |
| 556 |
|
| 557 |
# form path to .gitmodules file |
| 558 |
subdir <- subdir %||% "" |
| 559 |
parts <- c( |
| 560 |
if (nzchar(subdir)) URLencode(subdir), |
| 561 |
".gitmodules" |
| 562 |
) |
| 563 |
|
| 564 |
path <- paste(parts, collapse = "/") |
| 565 |
|
| 566 |
# scope authentication |
| 567 |
renv_scope_auth(repo) |
| 568 |
|
| 569 |
# add headers |
| 570 |
headers <- c(Accept = "application/vnd.github.raw") |
| 571 |
|
| 572 |
# get the file contents |
| 573 |
fmt <- "%s/repos/%s/%s/contents/%s?ref=%s" |
| 574 |
origin <- renv_retrieve_origin(host) |
| 575 |
url <- sprintf(fmt, origin, user, repo, path, sha) |
| 576 |
jsonfile <- renv_scope_tempfile("renv-json-") |
| 577 |
status <- suppressWarnings( |
| 578 |
catch( |
| 579 |
download(url, destfile = jsonfile, type = "github", quiet = TRUE, headers = headers) |
| 580 |
) |
| 581 |
) |
| 582 |
|
| 583 |
# just return a status code whether or not submodules are included |
| 584 |
!inherits(status, "error") |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
renv_remotes_resolve_github_description <- function(url, host, user, repo, subdir, sha) { |
| 589 |
|
| 590 |
# form DESCRIPTION path |
| 591 |
subdir <- subdir %||% "" |
| 592 |
parts <- c( |
| 593 |
if (nzchar(subdir)) URLencode(subdir), |
| 594 |
"DESCRIPTION" |
| 595 |
) |
| 596 |
|
| 597 |
descpath <- paste(parts, collapse = "/") |
| 598 |
|
| 599 |
# scope authentication |
| 600 |
renv_scope_auth(repo) |
| 601 |
|
| 602 |
# add headers |
| 603 |
headers <- c( |
| 604 |
Accept = "application/vnd.github.raw", |
| 605 |
renv_download_auth_github(url) |
| 606 |
) |
| 607 |
|
| 608 |
# get the DESCRIPTION contents |
| 609 |
fmt <- "%s/repos/%s/%s/contents/%s?ref=%s" |
| 610 |
origin <- renv_retrieve_origin(host) |
| 611 |
url <- sprintf(fmt, origin, user, repo, descpath, sha) |
| 612 |
destfile <- renv_scope_tempfile("renv-json-") |
| 613 |
download(url, destfile = destfile, type = "github", quiet = TRUE, headers = headers) |
| 614 |
|
| 615 |
# try to read the file; detect JSON versus raw content in case |
| 616 |
# headers were not sent for some reason |
| 617 |
contents <- renv_file_read(destfile) |
| 618 |
if (substring(contents, 1L, 1L) == "{") { |
| 619 |
json <- renv_json_read(text = contents) |
| 620 |
contents <- renv_base64_decode(json$content) |
| 621 |
} |
| 622 |
|
| 623 |
# normalize newlines |
| 624 |
contents <- gsub("\r\n", "\n", contents, fixed = TRUE) |
| 625 |
|
| 626 |
# read as DCF |
| 627 |
renv_dcf_read(text = contents) |
| 628 |
|
| 629 |
} |
| 630 |
|
| 631 |
renv_remotes_resolve_github_ref <- function(host, user, repo) { |
| 632 |
|
| 633 |
tryCatch( |
| 634 |
renv_remotes_resolve_github_ref_impl(host, user, repo), |
| 635 |
error = function(e) { |
| 636 |
warning(e) |
| 637 |
getOption("renv.github.default_branch", default = "main") |
| 638 |
} |
| 639 |
) |
| 640 |
|
| 641 |
} |
| 642 |
|
| 643 |
renv_remotes_resolve_github_ref_impl <- function(host, user, repo) { |
| 644 |
|
| 645 |
# scope authentication |
| 646 |
renv_scope_auth(repo) |
| 647 |
|
| 648 |
# build url to repos endpoint |
| 649 |
fmt <- "%s/repos/%s/%s" |
| 650 |
origin <- renv_retrieve_origin(host) |
| 651 |
url <- sprintf(fmt, origin, user, repo) |
| 652 |
|
| 653 |
# download JSON data at endpoint |
| 654 |
jsonfile <- renv_scope_tempfile("renv-github-ref-", fileext = ".json") |
| 655 |
download(url, destfile = jsonfile, type = "github", quiet = TRUE) |
| 656 |
json <- renv_json_read(jsonfile) |
| 657 |
|
| 658 |
# read default branch |
| 659 |
json$default_branch %||% getOption("renv.github.default_branch", default = "main") |
| 660 |
|
| 661 |
} |
| 662 |
|
| 663 |
renv_remotes_resolve_github <- function(remote) { |
| 664 |
|
| 665 |
# resolve the reference associated with this repository |
| 666 |
host <- remote$host %||% config$github.host() |
| 667 |
user <- remote$user |
| 668 |
repo <- remote$repo |
| 669 |
spec <- remote$spec |
| 670 |
subdir <- remote$subdir |
| 671 |
|
| 672 |
# resolve ref |
| 673 |
ref <- remote$ref %||% renv_remotes_resolve_github_ref(host, user, repo) |
| 674 |
|
| 675 |
# handle '*release' refs |
| 676 |
if (identical(ref, "*release")) |
| 677 |
ref <- renv_remotes_resolve_github_release(host, user, repo, spec) |
| 678 |
|
| 679 |
# resolve the sha associated with the ref / pull |
| 680 |
pull <- remote$pull %||% "" |
| 681 |
sha <- case( |
| 682 |
nzchar(pull) ~ renv_remotes_resolve_github_sha_pull(host, user, repo, pull), |
| 683 |
nzchar(ref) ~ renv_remotes_resolve_github_sha_ref(host, user, repo, ref) |
| 684 |
) |
| 685 |
|
| 686 |
# if an abbreviated sha was provided as the ref, expand it here |
| 687 |
if (nzchar(ref) && startsWith(sha, ref)) |
| 688 |
ref <- sha |
| 689 |
|
| 690 |
# check whether the repository has a .gitmodules file; if so, then we'll have |
| 691 |
# to use a plain 'git' client to retrieve the package |
| 692 |
modules <- renv_remotes_resolve_github_modules(host, user, repo, subdir, sha) |
| 693 |
|
| 694 |
# construct full url |
| 695 |
origin <- fsub("api.github.com", "github.com", renv_retrieve_origin(host)) |
| 696 |
parts <- c(origin, user, repo) |
| 697 |
url <- paste(parts, collapse = "/") |
| 698 |
|
| 699 |
# read DESCRIPTION |
| 700 |
desc <- renv_remotes_resolve_github_description(url, host, user, repo, subdir, sha) |
| 701 |
|
| 702 |
list( |
| 703 |
Package = desc$Package, |
| 704 |
Version = desc$Version, |
| 705 |
Source = if (modules) "git" else "GitHub", |
| 706 |
RemoteType = if (modules) "git" else "github", |
| 707 |
RemoteUrl = if (modules) url, |
| 708 |
RemoteHost = host, |
| 709 |
RemoteUsername = user, |
| 710 |
RemoteRepo = repo, |
| 711 |
RemoteSubdir = subdir, |
| 712 |
RemoteRef = ref, |
| 713 |
RemoteSha = sha |
| 714 |
) |
| 715 |
|
| 716 |
} |
| 717 |
|
| 718 |
renv_remotes_resolve_github_release <- function(host, user, repo, spec) { |
| 719 |
|
| 720 |
# scope authentication |
| 721 |
renv_scope_auth(repo) |
| 722 |
|
| 723 |
# build url for github releases endpoint |
| 724 |
fmt <- "%s/repos/%s/%s/releases?per_page=1" |
| 725 |
origin <- renv_retrieve_origin(host) |
| 726 |
url <- sprintf(fmt, origin, user, repo) |
| 727 |
|
| 728 |
# prepare headers |
| 729 |
headers <- c(Accept = "application/vnd.github.raw+json") |
| 730 |
|
| 731 |
# make request to endpoint |
| 732 |
releases <- renv_scope_tempfile("renv-releases-") |
| 733 |
download( |
| 734 |
url = url, |
| 735 |
destfile = releases, |
| 736 |
type = "github", |
| 737 |
quiet = TRUE, |
| 738 |
headers = headers |
| 739 |
) |
| 740 |
|
| 741 |
# get reference associated with this tag |
| 742 |
json <- renv_json_read(releases) |
| 743 |
if (empty(json)) { |
| 744 |
fmt <- "could not find any releases associated with remote '%s'" |
| 745 |
stopf(fmt, sub("[*]release$", "", spec)) |
| 746 |
} |
| 747 |
|
| 748 |
json[[1L]][["tag_name"]] |
| 749 |
|
| 750 |
} |
| 751 |
|
| 752 |
renv_remotes_resolve_git <- function(remote) { |
| 753 |
|
| 754 |
package <- remote$package %||% basename(remote$repo) |
| 755 |
url <- remote$url |
| 756 |
subdir <- remote$subdir |
| 757 |
|
| 758 |
# handle git ref |
| 759 |
pull <- remote$pull %||% "" |
| 760 |
ref <- remote$ref %||% "" |
| 761 |
|
| 762 |
# resolve ref from pull if set |
| 763 |
if (nzchar(pull)) |
| 764 |
ref <- renv_remotes_resolve_git_pull(ref) |
| 765 |
|
| 766 |
record <- list( |
| 767 |
Package = package, |
| 768 |
Version = "<unknown>", |
| 769 |
Source = "git", |
| 770 |
RemoteType = "git", |
| 771 |
RemoteUrl = url, |
| 772 |
RemoteSubdir = subdir, |
| 773 |
RemoteRef = ref |
| 774 |
) |
| 775 |
|
| 776 |
desc <- renv_remotes_resolve_git_description(record) |
| 777 |
|
| 778 |
record$Package <- desc$Package |
| 779 |
record$Version <- desc$Version |
| 780 |
|
| 781 |
record |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
renv_remotes_resolve_git_sha_ref <- function(record) { |
| 786 |
|
| 787 |
renv_git_preflight() |
| 788 |
|
| 789 |
origin <- record$RemoteUrl |
| 790 |
ref <- record$RemoteRef %||% record$RemoteSha |
| 791 |
args <- c("ls-remote", origin, ref) |
| 792 |
|
| 793 |
output <- local({ |
| 794 |
renv_scope_auth(record) |
| 795 |
renv_scope_git_auth() |
| 796 |
renv_system_exec("git", args, "checking git remote") |
| 797 |
}) |
| 798 |
|
| 799 |
if (empty(output)) |
| 800 |
return("") |
| 801 |
|
| 802 |
# format of output is, for example: |
| 803 |
# |
| 804 |
# $ git ls-remote https://github.com/rstudio/renv refs/tags/0.14.0 |
| 805 |
# 20ca74bdcc3c87848e5665effa2fc8ee8b039c69 refs/tags/0.14.0 |
| 806 |
# |
| 807 |
# take first line of output, split on tab character, and take leftmost entry |
| 808 |
strsplit(output[[1L]], "\t", fixed = TRUE)[[1L]][[1L]] |
| 809 |
|
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
renv_remotes_resolve_git_description <- function(record) { |
| 814 |
|
| 815 |
path <- renv_scope_tempfile("renv-git-") |
| 816 |
ensure_directory(path) |
| 817 |
|
| 818 |
# TODO: is there a cheaper way for us to accomplish this? |
| 819 |
# it'd be nice if we could retrieve the contents of a single |
| 820 |
# file, without needing to pull an entire repository branch |
| 821 |
local({ |
| 822 |
renv_scope_options(renv.verbose = FALSE) |
| 823 |
renv_retrieve_git_impl(record, path) |
| 824 |
}) |
| 825 |
|
| 826 |
# subdir may be NULL |
| 827 |
subdir <- record$RemoteSubdir |
| 828 |
desc <- renv_description_read(path, subdir = subdir) |
| 829 |
|
| 830 |
desc |
| 831 |
} |
| 832 |
|
| 833 |
renv_remotes_resolve_git_pull <- function(pr) { |
| 834 |
fmt <- "pull/%1$s/head:pull/%1$s" |
| 835 |
sprintf(fmt, pr) |
| 836 |
} |
| 837 |
|
| 838 |
renv_remotes_resolve_gitlab_ref <- function(host, user, repo) { |
| 839 |
|
| 840 |
tryCatch( |
| 841 |
renv_remotes_resolve_gitlab_ref_impl(host, user, repo), |
| 842 |
error = function(e) { |
| 843 |
warning(e) |
| 844 |
getOption("renv.gitlab.default_branch", default = "master") |
| 845 |
} |
| 846 |
) |
| 847 |
|
| 848 |
} |
| 849 |
|
| 850 |
renv_remotes_resolve_gitlab_ref_impl <- function(host, user, repo) { |
| 851 |
|
| 852 |
# scope authentication |
| 853 |
renv_scope_auth(repo) |
| 854 |
|
| 855 |
# get list of available branches |
| 856 |
fmt <- "%s/api/v4/projects/%s/repository/branches" |
| 857 |
origin <- renv_retrieve_origin(host) |
| 858 |
id <- URLencode(paste(user, repo, sep = "/"), reserved = TRUE) |
| 859 |
url <- sprintf(fmt, origin, id) |
| 860 |
|
| 861 |
destfile <- renv_scope_tempfile("renv-gitlab-commits-") |
| 862 |
download(url, destfile = destfile, type = "gitlab", quiet = TRUE) |
| 863 |
json <- renv_json_read(file = destfile) |
| 864 |
|
| 865 |
# iterate through and find the default |
| 866 |
for (info in json) |
| 867 |
if (identical(info$default, TRUE)) |
| 868 |
return(info$name) |
| 869 |
|
| 870 |
# if no default was found, use master branch |
| 871 |
# (for backwards compatibility with existing projects) |
| 872 |
getOption("renv.gitlab.default_branch", default = "master") |
| 873 |
|
| 874 |
} |
| 875 |
|
| 876 |
renv_remotes_resolve_gitlab <- function(remote) { |
| 877 |
|
| 878 |
host <- remote$host %||% config$gitlab.host() |
| 879 |
user <- remote$user |
| 880 |
repo <- remote$repo |
| 881 |
subdir <- remote$subdir |
| 882 |
|
| 883 |
ref <- remote$ref %||% renv_remotes_resolve_gitlab_ref(host, user, repo) |
| 884 |
|
| 885 |
parts <- c(if (nzchar(subdir %||% "")) subdir, "DESCRIPTION") |
| 886 |
descpath <- URLencode(paste(parts, collapse = "/"), reserved = TRUE) |
| 887 |
|
| 888 |
# scope authentication |
| 889 |
renv_scope_auth(repo) |
| 890 |
|
| 891 |
# retrieve sha associated with this ref |
| 892 |
fmt <- "%s/api/v4/projects/%s/repository/commits/%s" |
| 893 |
origin <- renv_retrieve_origin(host) |
| 894 |
id <- URLencode(paste(user, repo, sep = "/"), reserved = TRUE) |
| 895 |
ref <- URLencode(ref, reserved = TRUE) |
| 896 |
url <- sprintf(fmt, origin, id, ref) |
| 897 |
|
| 898 |
destfile <- renv_scope_tempfile("renv-gitlab-commits-") |
| 899 |
download(url, destfile = destfile, type = "gitlab", quiet = TRUE) |
| 900 |
json <- renv_json_read(file = destfile) |
| 901 |
sha <- json$id |
| 902 |
|
| 903 |
# retrieve DESCRIPTION file |
| 904 |
fmt <- "%s/api/v4/projects/%s/repository/files/%s/raw?ref=%s" |
| 905 |
origin <- renv_retrieve_origin(host) |
| 906 |
id <- URLencode(paste(user, repo, sep = "/"), reserved = TRUE) |
| 907 |
url <- sprintf(fmt, origin, id, descpath, ref) |
| 908 |
|
| 909 |
destfile <- renv_scope_tempfile("renv-description-") |
| 910 |
download(url, destfile = destfile, type = "gitlab", quiet = TRUE) |
| 911 |
desc <- renv_dcf_read(destfile) |
| 912 |
|
| 913 |
list( |
| 914 |
Package = desc$Package, |
| 915 |
Version = desc$Version, |
| 916 |
Source = "GitLab", |
| 917 |
RemoteType = "gitlab", |
| 918 |
RemoteHost = host, |
| 919 |
RemoteUsername = user, |
| 920 |
RemoteRepo = repo, |
| 921 |
RemoteSubdir = subdir, |
| 922 |
RemoteRef = ref, |
| 923 |
RemoteSha = sha |
| 924 |
) |
| 925 |
|
| 926 |
} |
| 927 |
|
| 928 |
renv_remotes_resolve_url <- function(url, quiet = FALSE) { |
| 929 |
|
| 930 |
hash <- md5(url) |
| 931 |
ext <- fileext(url, default = ".tar.gz") |
| 932 |
name <- paste(hash, ext, sep = "") |
| 933 |
path <- renv_paths_source("url", name) |
| 934 |
|
| 935 |
ensure_parent_directory(path) |
| 936 |
download(url, path, quiet = quiet) |
| 937 |
|
| 938 |
desc <- renv_description_read(path) |
| 939 |
|
| 940 |
list( |
| 941 |
Package = desc$Package, |
| 942 |
Version = desc$Version, |
| 943 |
Source = "URL", |
| 944 |
RemoteType = "url", |
| 945 |
RemoteUrl = url, |
| 946 |
Path = path |
| 947 |
) |
| 948 |
|
| 949 |
} |
| 950 |
|
| 951 |
renv_remotes_resolve_path <- function(path) { |
| 952 |
|
| 953 |
# if this package lives within one of the cellar paths, |
| 954 |
# then treat it as a cellar source |
| 955 |
roots <- renv_cellar_roots() |
| 956 |
for (root in roots) |
| 957 |
if (renv_path_within(path, root)) |
| 958 |
return(renv_remotes_resolve_path_cellar(path)) |
| 959 |
|
| 960 |
# first, check for a common extension |
| 961 |
if (renv_archive_type(path) %in% c("tar", "zip")) |
| 962 |
return(renv_remotes_resolve_path_impl(path)) |
| 963 |
|
| 964 |
# otherwise, if this is the path to a package project, use the sources as-is |
| 965 |
if (renv_project_type(path) == "package") |
| 966 |
return(renv_remotes_resolve_path_impl(path)) |
| 967 |
|
| 968 |
stopf("there is no package at path '%s'", path) |
| 969 |
|
| 970 |
} |
| 971 |
|
| 972 |
renv_remotes_resolve_path_cellar <- function(path) { |
| 973 |
|
| 974 |
desc <- renv_description_read(path) |
| 975 |
list( |
| 976 |
Package = desc$Package, |
| 977 |
Version = desc$Version, |
| 978 |
Source = "Cellar", |
| 979 |
Cacheable = FALSE |
| 980 |
) |
| 981 |
|
| 982 |
} |
| 983 |
|
| 984 |
renv_remotes_resolve_path_impl <- function(path) { |
| 985 |
|
| 986 |
desc <- renv_description_read(path) |
| 987 |
list( |
| 988 |
Package = desc$Package, |
| 989 |
Version = desc$Version, |
| 990 |
Source = "Local", |
| 991 |
RemoteType = "local", |
| 992 |
RemoteUrl = path, |
| 993 |
Cacheable = FALSE |
| 994 |
) |
| 995 |
|
| 996 |
} |