Debian Codegraph @

R/history.R

raw
1
2 #' View and revert to a historical lockfile
3 #'
4 #' @description
5 #' `history()` uses your version control system to show prior versions of the
6 #' lockfile and `revert()` allows you to restore one of them.
7 #'
8 #' These functions are currently only implemented for projects that use git.
9 #'
10 #' @inherit renv-params
11 #'
12 #' @export
13 #'
14 #' @return `history()` returns a `data.frame` summarizing the commits in which
15 #' `renv.lock` has been changed. `revert()` is usually called for its
16 #' side-effect but also invisibly returns the `commit` used.
17 #'
18 #' @examples
19 #' \dontrun{
20 #'
21 #' # get history of previous versions of renv.lock in VCS
22 #' db <- renv::history()
23 #'
24 #' # choose an older commit
25 #' commit <- db$commit[5]
26 #'
27 #' # revert to that version of the lockfile
28 #' renv::revert(commit = commit)
29 #'
30 #' }
31 history <- function(project = NULL) {
32
33 renv_scope_error_handler()
34
35 project <- renv_project_resolve(project)
36 renv_project_lock(project = project)
37
38 lockpath <- renv_lockfile_path(project)
39 if (!file.exists(lockpath))
40 return(data_frame())
41
42 renv_git_preflight()
43
44 renv_scope_wd(project)
45
46 args <- c("log", "--pretty=format:%H\031%at\031%ct\031%s", renv_shell_path(lockpath))
47 data <- renv_system_exec("git", args, action = "retrieving git log")
48
49 parts <- strsplit(data, "\031", fixed = TRUE)
50 tbl <- bind(parts, names = c("commit", "author_date", "committer_date", "subject"))
51 tbl$author_date <- as.POSIXct(as.numeric(tbl$author_date), origin = "1970-01-01")
52 tbl$committer_date <- as.POSIXct(as.numeric(tbl$committer_date), origin = "1970-01-01")
53
54 tbl
55
56 }
57
58 #' @param commit The commit associated with a prior version of the lockfile.
59 #' @param ... Optional arguments; currently unused.
60 #' @export
61 #' @rdname history
62 revert <- function(commit = "HEAD", ..., project = NULL) {
63
64 renv_scope_error_handler()
65 renv_dots_check(...)
66
67 project <- renv_project_resolve(project)
68 renv_project_lock(project = project)
69
70 renv_git_preflight()
71
72 renv_scope_wd(project)
73
74 lockpath <- renv_lockfile_path(project = project)
75 system2("git", c("checkout", commit, "--", renv_shell_path(lockpath)))
76 system2("git", c("reset", "HEAD", renv_shell_path(lockpath)), stdout = FALSE, stderr = FALSE)
77 system2("git", c("diff", "--", renv_shell_path(lockpath)))
78
79 writef("- renv.lock from commit %s has been checked out.", commit)
80 invisible(commit)
81
82 }