From dbfedb6782531eabe7b9c19d21fcc9704e6e8a25 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Sun, 30 Jan 2022 16:20:02 -0800 Subject: [PATCH 1/3] Use a join, instead of line diff, to compare package info --- R/compare.R | 70 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/R/compare.R b/R/compare.R index 459f2c9..ff68098 100644 --- a/R/compare.R +++ b/R/compare.R @@ -30,7 +30,10 @@ #' @examplesIf FALSE #' session_diff() -session_diff <- function(old = "local", new = "clipboard", ...) { +session_diff <- function(old = "local", new = "clipboard", + packages = c("diff", "merge"), ...) { + + packages <- match.arg(packages) oldname <- get_symbol_name(substitute(old)) newname <- get_symbol_name(substitute(new)) @@ -41,7 +44,7 @@ session_diff <- function(old = "local", new = "clipboard", ...) { ret <- list( old = old, new = new, - diff = session_diff_text(old$text, new$text) + diff = session_diff_text(old$text, new$text, packages) ) class(ret) <- c("session_diff", "list") @@ -205,7 +208,9 @@ beginning <- function(x) { trimws(substr(paste0(x123, sep = "\n"), 1, 100)) } -session_diff_text <- function(old, new) { +session_diff_text <- function(old, new, packages = c("diff", "merge")) { + packages <- match.arg(packages) + old <- enc2utf8(old) new <- enc2utf8(new) @@ -219,10 +224,16 @@ session_diff_text <- function(old, new) { old <- diff_fix_lines(old, min) new <- diff_fix_lines(new, min) - # expand thinner package info to match the wider one + package_fixup_fun <- switch( + packages, + # expands package info with fewer columns to match one with more + diff = expand_diff_text, + # full-joins package info to concentrate each diff in a single row + merge = merge_packages + ) # do not error, in case we cannot parse sessioninfo output suppressWarnings(tryCatch({ - exp <- expand_diff_text(old, new) + exp <- package_fixup_fun(old, new) old <- exp$old new <- exp$new }, error = function(e) NULL)) @@ -324,6 +335,55 @@ expand_diff_text <- function(old, new) { list(old = old, new = new) } +merge_packages <- function(old, new) { + opkgs <- parse_pkgs(old) + npkgs <- parse_pkgs(new) + + if (is.null(opkgs) || is.null(opkgs)) return(list(old = old, new = new)) + + names_to_keep <- c("package", "version", "source") + opkgs$pkgs <- opkgs$pkgs[names_to_keep] + npkgs$pkgs <- npkgs$pkgs[names_to_keep] + names(opkgs$pkgs) <- c("package", "old_version", "old_source") + names(npkgs$pkgs) <- c("package", "new_version", "new_source") + + merge_res <- merge(opkgs$pkgs, npkgs$pkgs, all = TRUE) + merge_res <- with(merge_res, + data.frame( + package = package, + "v!=" = mark(is_different(old_version, new_version)), + old_version = old_version, + new_version = new_version, + "s!=" = mark(is_different(old_source, new_source)), + old_source = old_source, + new_source = new_source, + stringsAsFactors = FALSE, + row.names = NULL, + check.names = FALSE + ) + ) + + oldopts <- options(cli.num_colors = 1) + on.exit(options(oldopts), add = TRUE) + fmt <- format_df(merge_res) + + old <- insert_instead(old, opkgs$begin, opkgs$end, fmt) + new <- insert_instead(new, npkgs$begin, npkgs$end, fmt) + + list(old = old, new = new) +} + +is_different <- function(x, y) { + x_na <- is.na(x) + y_na <- is.na(y) + no_na <- !x_na & !y_na + xor(x_na, y_na) | (no_na & (x != y)) +} + +mark <- function(x, mark = ">>") { + ifelse(x, mark, "") +} + insert_instead <- function(orig, from, to, new) { pre <- if (from > 1) orig[1:(from-1)] pst <- if (to < length(orig)) orig[(to+1):length(orig)] From 910478ac765ba304b51f209038a68193c523553f Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Sun, 30 Jan 2022 17:03:59 -0800 Subject: [PATCH 2/3] Document the new `packages` argument --- R/compare.R | 4 ++++ man/session_diff.Rd | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/R/compare.R b/R/compare.R index ff68098..b3391ff 100644 --- a/R/compare.R +++ b/R/compare.R @@ -4,6 +4,10 @@ #' @param old,new A `session_info` object (the return value of #' [session_info()]), or a pointer to [session_info()] output. See details #' below. +#' @param packages How to compare the package info for `old` and `new`: +#' * `"diff"`: line diffs, in the style of `diff -u` +#' * `"merge"`: merge the information into one data frame, with one row per +#' package. Only package `version` and `source` are compared. #' @param ... Passed to any new [session_info()] calls. #' #' @details diff --git a/man/session_diff.Rd b/man/session_diff.Rd index aeff36c..8704d45 100644 --- a/man/session_diff.Rd +++ b/man/session_diff.Rd @@ -4,13 +4,25 @@ \alias{session_diff} \title{Compare session information from two sources} \usage{ -session_diff(old = "local", new = "clipboard", ...) +session_diff( + old = "local", + new = "clipboard", + packages = c("diff", "merge"), + ... +) } \arguments{ \item{old, new}{A \code{session_info} object (the return value of \code{\link[=session_info]{session_info()}}), or a pointer to \code{\link[=session_info]{session_info()}} output. See details below.} +\item{packages}{How to compare the package info for \code{old} and \code{new}: +\itemize{ +\item \code{"diff"}: line diffs, in the style of \code{diff -u} +\item \code{"merge"}: merge the information into one data frame, with one row per +package. Only package \code{version} and \code{source} are compared. +}} + \item{...}{Passed to any new \code{\link[=session_info]{session_info()}} calls.} } \description{ From 0e02f862d1bc007236c6b71b602e2811e930f42d Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Tue, 25 Apr 2023 21:35:56 -0700 Subject: [PATCH 3/3] Tweak regex --- R/github-actions.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/github-actions.R b/R/github-actions.R index f7944c5..bd22cff 100644 --- a/R/github-actions.R +++ b/R/github-actions.R @@ -82,7 +82,7 @@ get_session_info_gha <- function(url) { owner = dat$owner, repo = dat$repo, job_id = dat$job_id ) timestamped_lines <- unlist(strsplit(raw_log$message, split = "\r\n|\n")) - lines <- sub("^[^\\s]+\\s+", "", timestamped_lines, perl = TRUE) + lines <- sub("^[^\\s]+\\s", "", timestamped_lines, perl = TRUE) re_start <- "[-=\u2500\u2550][ ]Session info[ ]" cand <- grep(re_start, lines)