Debian Codegraph @

R/lockfile-diff.R

raw
1
2 renv_lockfile_diff <- function(old, new, compare = NULL) {
3
4 compare <- compare %||% function(lhs, rhs) {
5 list(before = lhs, after = rhs)
6 }
7
8 # ensure both lists have the same names, inserting missing
9 # entries for those without any value
10 nms <- union(names(old), names(new)) %||% character()
11 if (length(nms)) {
12
13 nms <- sort(nms)
14 old[renv_vector_diff(nms, names(old))] <- list(NULL)
15 new[renv_vector_diff(nms, names(new))] <- list(NULL)
16
17 old <- old[nms]
18 new <- new[nms]
19
20 }
21
22 # ensure that these have the same length for comparison
23 if (is.list(old) && is.list(new))
24 length(old) <- length(new) <- max(length(old), length(new))
25
26 # check for differences
27 diffs <- mapply(
28 renv_lockfile_diff_impl, old, new,
29 MoreArgs = list(compare = compare),
30 SIMPLIFY = FALSE
31 )
32
33 # drop NULL entries
34 reject(diffs, empty)
35
36 }
37
38 renv_lockfile_diff_impl <- function(lhs, rhs, compare) {
39 case(
40 is.list(lhs) && empty(rhs) ~ renv_lockfile_diff(lhs, list(), compare),
41 empty(lhs) && is.list(rhs) ~ renv_lockfile_diff(list(), rhs, compare),
42 is.list(lhs) && is.list(rhs) ~ renv_lockfile_diff(lhs, rhs, compare),
43 !identical(c(lhs), c(rhs)) ~ compare(lhs, rhs),
44 NULL
45 )
46 }
47
48 renv_lockfile_diff_record <- function(before, after) {
49
50 before <- renv_record_normalize(before)
51 after <- renv_record_normalize(after)
52
53 # first, compare on version / record existence
54 type <- case(
55 is.null(before) ~ "install",
56 is.null(after) ~ "remove",
57 is.null(before$Version) || is.null(after$Version) ~ "crossgrade",
58 before$Version < after$Version ~ "upgrade",
59 before$Version > after$Version ~ "downgrade"
60 )
61
62 if (!is.null(type))
63 return(type)
64
65 # if we're running this as part of 'load()', and we're comparing
66 # packages with unknown sources, then just ignore those -- this
67 # is because we disable the 'guess repository' hack on startup,
68 # to avoid a potentially expensive query of package repositories
69 #
70 # https://github.com/rstudio/renv/issues/1683
71 if (the$load_running) {
72 unknown <-
73 identical(before$Source, "unknown") ||
74 identical(after$Source, "unknown")
75 if (unknown)
76 return(NULL)
77 }
78
79 # check for a crossgrade -- where the package version is the same,
80 # but details about the package's remotes have changed
81 if (!setequal(renv_record_names(before), renv_record_names(after)))
82 return("crossgrade")
83
84 nm <- union(renv_record_names(before), renv_record_names(after))
85 if (!identical(before[nm], after[nm]))
86 return("crossgrade")
87
88 NULL
89
90 }
91
92 renv_lockfile_diff_packages <- function(old, new) {
93
94 old <- renv_lockfile_records(old)
95 new <- renv_lockfile_records(new)
96
97 packages <- named(union(names(old), names(new)))
98 actions <- lapply(packages, function(package) {
99 before <- old[[package]]; after <- new[[package]]
100 renv_lockfile_diff_record(before, after)
101 })
102
103 Filter(Negate(is.null), actions)
104
105 }
106
107 renv_lockfile_override <- function(lockfile) {
108 records <- renv_lockfile_records(lockfile)
109 overrides <- renv_records_override(records)
110 renv_lockfile_records(lockfile) <- overrides
111 lockfile
112 }
113
114 renv_lockfile_repair <- function(lockfile) {
115
116 records <- renv_lockfile_records(lockfile)
117
118 # fix up records in lockfile
119 renv_lockfile_records(lockfile) <- enumerate(records, function(package, record) {
120
121 # if this package is from a repository, but doesn't specify an explicit
122 # version, then use the latest-available version of that package
123 source <- renv_record_source_normalize(record, record$Source)
124 if (identical(source, "Repository") && is.null(record$Version)) {
125 entry <- renv_available_packages_latest(package)
126 record$Version <- entry$Version
127 }
128
129 # return normalized record
130 record
131
132 })
133
134 lockfile
135
136 }