From e18797f55b2a0dd1dc154d1ae36d13a4a5634a2b Mon Sep 17 00:00:00 2001 From: Pawel Rucki <12943682+pawelru@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:57:15 +0200 Subject: [PATCH] better handle very old and latest snapshots --- R/get_ref.R | 10 +++++++++- R/utils.R | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/R/get_ref.R b/R/get_ref.R index 7d9f22c..54f1774 100644 --- a/R/get_ref.R +++ b/R/get_ref.R @@ -596,6 +596,9 @@ get_avail_date <- function(remote_ref, start = get_release_date(remote_ref)) { } #' @rdname get_avail_date +#' +#' @importFrom pkgcache ppm_snapshots +#' #' @export #' #' @examplesIf Sys.getenv("R_USER_CACHE_DIR", "") != "" @@ -606,8 +609,13 @@ get_avail_date.remote_ref_cran <- function(remote_ref, start = get_release_date( i <- 0 date <- start while (i <= max_iter) { + # there are some gaps in the snapshots so it's important to at first find the closest date ppm_url <- get_ppm_snapshot_by_date(date) - date <- unname(as.Date(sub(".*/", "", ppm_url))) + date <- `if`( + grepl("/latest$", ppm_url), + tail(pkgcache::ppm_snapshots(), 1)$date, + unname(as.Date(sub(".*/", "", ppm_url))) + ) data <- available.packages( repos = ppm_url, filters = list(function(db) db[db[, "Package"] == remote_ref$package, ]) diff --git a/R/utils.R b/R/utils.R index 874d91d..060272e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -36,6 +36,9 @@ get_ppm_snapshot_by_date <- function(date = NA) { if (date >= tail(pkgcache::ppm_snapshots(), 1)$date) { return(pkgcache::repo_resolve("PPM@latest")) } + if (date <= head(pkgcache::ppm_snapshots(), 1)$date) { + return(pkgcache::repo_resolve(sprintf("PPM@%s", head(pkgcache::ppm_snapshots(), 1)$date))) + } tryCatch( pkgcache::repo_resolve(sprintf("PPM@%s", as.character(as.Date(date) + 1))), error = function(err) {