Debian Codegraph @

R/hash.R

raw
1
2 renv_hash_text <- function(text) {
3 renv_bootstrap_hash_text(text)
4 }
5
6 renv_hash_fields <- function(dcf) {
7 c(
8 renv_hash_fields_default(),
9 renv_hash_fields_remotes(dcf)
10 )
11 }
12
13 renv_hash_fields_default <- function() {
14 c(
15 "Package", "Version", "Title",
16 "Author", "Maintainer", "Description",
17 "Depends", "Imports", "Suggests", "LinkingTo"
18 )
19 }
20
21 renv_hash_fields_remotes <- function(dcf) {
22
23 # if this seems to be a cran-like record, only keep remotes
24 # when RemoteSha appears to be a hash (e.g. for r-universe)
25 # note that RemoteSha may be a package version when installed
26 # by e.g. pak
27 if (renv_record_cranlike(dcf)) {
28 sha <- dcf[["RemoteSha"]]
29 if (is.null(sha) || nchar(sha) < 40L)
30 return(character())
31 }
32
33 # grab the relevant remotes
34 remotes <- grep("^Remote", names(dcf), perl = TRUE, value = TRUE)
35
36 # don't include 'RemoteRef' if it's a non-informative remote
37 if (identical(dcf[["RemoteRef"]], "HEAD"))
38 remotes <- setdiff(remotes, "RemoteRef")
39
40 remotes
41
42 }
43
44 renv_hash_description <- function(path) {
45 filebacked(
46 context = "renv_hash_description",
47 path = path,
48 callback = renv_hash_description_impl
49 )
50 }
51
52 renv_hash_description_impl <- function(path) {
53 record <- renv_description_read(path)
54 renv_hash_record(record)
55 }
56
57 renv_hash_record <- function(record) {
58
59 # find relevant fields for hashing
60 fields <- renv_hash_fields(record)
61
62 # collapse vector / list dependency fields
63 depfields <- c("Depends", "Imports", "Suggests", "LinkingTo", "Enhances")
64 for (depfield in depfields) {
65 if (!is.null(record[[depfield]])) {
66 value <- unlist(record[[depfield]])
67 record[[depfield]] <- paste(value, collapse = ", ")
68 }
69 }
70
71 # retrieve these fields
72 subsetted <- record[renv_vector_intersect(fields, names(record))]
73
74 # sort names (use C locale to ensure consistent ordering)
75 ordered <- subsetted[csort(names(subsetted))]
76
77 # paste together into single string
78 contents <- paste(names(ordered), ordered, sep = ": ", collapse = "\n")
79
80 # remove whitespace -- it's possible that tools (e.g. Packrat) that
81 # mutate a package's DESCRIPTION file may also inadvertently change
82 # the structure of whitespace within some fields; that whitespace is
83 # normally not semantically meaningful so we remove that so such
84 # DESCRIPTIONS can obtain the same hash value. (this ultimately
85 # arises as 'write.dcf()' allows both 'indent' and 'width' to be
86 # configured based on the 'width' option)
87 contents <- gsub("[[:space:]]", "", contents)
88
89 # compute the hash
90 invisible(md5(contents))
91
92 }