Skip to content

Commit c69a52b

Browse files
Merge pull request #497 from lorenzwalthert/remove-git2r
Remove git2r
2 parents 474004b + fe15d80 commit c69a52b

File tree

8 files changed

+18
-125
lines changed

8 files changed

+18
-125
lines changed

R/roxygen2.R

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,12 @@
1-
extract_diff_one_line <- function(one_line) {
2-
if (one_line$old_lineno < 0 || one_line$new_lineno < 0) {
3-
one_line$content
4-
} else {
5-
NULL
6-
}
7-
}
8-
9-
extract_diff_lines <- function(lines) {
10-
purrr::map(lines, extract_diff_one_line)
11-
}
12-
13-
extract_diff_hunks <- function(hunks) {
14-
purrr::map(hunks, ~ extract_diff_lines(.x$lines))
15-
}
16-
17-
extract_diff_files <- function(files) {
18-
purrr::map(files, ~ extract_diff_hunks(.x$hunks))
19-
}
20-
21-
#' Extract old and new lines from `git diff --cached`
22-
#'
23-
#' This is useful to detect within a hook script if the core function from a
24-
#' hook such as [roxygen2::roxygenize()] must run at all or not.
25-
#' @param root The root of project.
26-
#' @keywords internal
27-
extract_diff_root <- function(root = ".") {
28-
assert_is_git_repo(root)
29-
repo <- git2r::repository(root)
30-
if (length(git2r::reflog(repo)) == 0) {
31-
# nothing committed yet
32-
all_files <- git2r::status()$staged
33-
34-
purrr::map(all_files[grepl("^R/.*\\.[Rr]$", all_files)], readLines) %>%
35-
unlist() %>%
36-
unname()
37-
} else {
38-
diff <- git2r::diff(repo, index = TRUE)
39-
extract_diff_files(diff$files) %>%
40-
unlist()
41-
}
42-
}
43-
441
#' Check if we should run roxygen.
452
#'
463
#' This is the case if a new or replaced/removed line contains a roxygen2
474
#' comment in a file that is staged.
485
#' This function is only exported for use in hook scripts, but it's not intended
496
#' to be called by the end-user directly.
7+
#' @param root The root of the git repo.
508
#' @return
519
#' A logical vector of length 1.
52-
#' @inheritParams extract_diff_root
5310
#' @keywords internal
5411
#' @family hook script helpers
5512
#' @examples
@@ -58,34 +15,18 @@ extract_diff_root <- function(root = ".") {
5815
#' }
5916
#' @export
6017
diff_requires_run_roxygenize <- function(root = ".") {
61-
if (rlang::with_handlers(withr::with_namespace("git2r", FALSE), error = function(...) TRUE)) {
62-
generic <- paste0(
63-
" Please add the package as a dependency to ",
64-
"`.pre-commit-config.yaml` -> `id: roxygenize` -> ",
65-
"`additional_dependencies` and try again. The package must be ",
66-
"specified so `renv::install()` understands it, e.g. like this:\n\n",
67-
" - id: roxygenize",
68-
"
69-
additional_dependencies:
70-
- git2r\n\n"
71-
)
72-
msg <- paste0(
73-
"The R package {git2r} must be available to benefit from caching of this hook.",
74-
generic
75-
)
76-
rlang::warn(msg)
77-
return(TRUE)
78-
}
79-
changed_lines_content <- extract_diff_root(root)
80-
is_roxygen <- grepl("^#'", changed_lines_content)
18+
git_raw_diff <- withr::with_dir(
19+
root, call_and_capture("git", c("diff", "--cached"))
20+
)$stdout
21+
is_roxygen <- grepl("^(\\+|-)#'", git_raw_diff)
8122
if (any(is_roxygen)) {
8223
return(TRUE)
8324
} else {
8425
# check if formals were changed
8526
# we invalidate the cache on formal change, even if it is not sure they are
8627
# documented with roxygen. This is easy, cheap and safe. Might give false
8728
# positive (invalidates in cases where it's not necessary).
88-
without_comments <- gsub("#.*", "", changed_lines_content)
29+
without_comments <- gsub("#.*", "", git_raw_diff)
8930
any(grep("function(", without_comments, fixed = TRUE))
9031
}
9132
}

R/utils.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ is_package <- function(root = here::here()) {
3838
magrittr::not()
3939
}
4040

41-
add_trailing_linebreak <- function(x) {
42-
paste0(x, "\n")
43-
}
44-
4541
#' Name the input
4642
#'
4743
#' @param x A vector.

inst/update-dependency-graph-existing-packages.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ hook_deps <- function(root) {
77
"pkgdown", "mockery",
88
"httr"
99
)
10-
out <- c(out, "roxygen2", "spelling", "styler", "pkgload", "lintr", "knitr", "git2r", "desc", "mockery")
10+
out <- c(out, "roxygen2", "spelling", "styler", "pkgload", "lintr", "knitr", "desc", "mockery")
1111
out <- setdiff(c(unique(c(out, deps[deps$type == "Imports", ]$package))), dont)
1212
out <- names(renv:::renv_package_dependencies(out))
1313
return(sort(out))

inst/update-existing-hook-dependencies.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ options(
22
repos = c(CRAN = "https://packagemanager.rstudio.com/all/latest"),
33
install.packages.compile.from.source = "never"
44
)
5-
install.packages("renv")
6-
install.packages("jsonlite")
5+
renv::install("renv")
6+
renv::install("jsonlite")
77
renv_deps <- names(jsonlite::read_json("renv.lock")$Packages)
88
renv::load()
99
renv::restore(prompt = FALSE)

man/diff_requires_run_roxygenize.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/extract_diff_root.Rd

Lines changed: 0 additions & 16 deletions
This file was deleted.

renv.lock

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,6 @@
236236
],
237237
"Hash": "47b5f30c720c23999b913a1a635cf0bb"
238238
},
239-
"git2r": {
240-
"Package": "git2r",
241-
"Version": "0.32.0",
242-
"Source": "Repository",
243-
"Repository": "RSPM",
244-
"Requirements": [
245-
"R",
246-
"graphics",
247-
"utils"
248-
],
249-
"Hash": "1882d7a76fd8c14b2322865f74c9a348"
250-
},
251239
"glue": {
252240
"Package": "glue",
253241
"Version": "1.6.2",
@@ -333,14 +321,9 @@
333321
},
334322
"lintr": {
335323
"Package": "lintr",
336-
"Version": "3.0.2.9000",
337-
"Source": "GitHub",
338-
"RemoteType": "github",
339-
"RemoteHost": "api.github.com",
340-
"RemoteUsername": "r-lib",
341-
"RemoteRepo": "lintr",
342-
"RemoteRef": "main",
343-
"RemoteSha": "4ef15f9689e7384e83a8e3250825006e95293bf5",
324+
"Version": "3.1.0",
325+
"Source": "Repository",
326+
"Repository": "RSPM",
344327
"Requirements": [
345328
"R",
346329
"backports",
@@ -355,7 +338,7 @@
355338
"xml2",
356339
"xmlparsedata"
357340
],
358-
"Hash": "fd19bcfedd9515297fdc318cbe7505d3"
341+
"Hash": "2b4b803af6017e93b67ddaf0eacba918"
359342
},
360343
"magrittr": {
361344
"Package": "magrittr",

tests/testthat/test-hook-roxygenize.R

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,37 @@ test_that("roxygen runs are done if necessary", {
55
text <- c("#' Roxygen comment", "#'", "#' more things", "NULL")
66
writeLines(text, "R/first.R")
77
writeLines(text, "R/second.R")
8-
expect_error(extract_diff_root("."), "is not a git repo")
98
git_init(".")
10-
expect_equal(extract_diff_root("."), NULL)
119
expect_false(diff_requires_run_roxygenize("."))
1210
git2r::add(".", "R/first.R")
1311
git2r::status()
1412

15-
expect_equal(extract_diff_root("."), text)
1613
expect_true(diff_requires_run_roxygenize("."))
1714
git2r::commit(".", "add roxgen2 file")
1815

1916
# when non roxygen lines are added
2017
text2 <- c("if (x) {", " TRUE", "} else {", " not_TRue(sinux(-i))", "}")
2118
writeLines(text2, "R/third.R")
22-
expect_equal(extract_diff_root("."), NULL)
19+
2320
git2r::add(".", "R/third.R")
24-
expect_equal(extract_diff_root("."), add_trailing_linebreak(text2))
2521
expect_false(diff_requires_run_roxygenize("."))
2622
git2r::commit(".", "add non-roxygen2 file")
2723

2824
# when roxygen line is replaced
2925
text[1] <- "# not roxygen, but replaced old "
3026
writeLines(text, "R/first.R")
3127
writeLines(text[1], "R/fourth.R")
32-
expect_equal(extract_diff_root("."), NULL)
28+
3329
git2r::add(".", c("R/first.R", "R/fourth.R"))
3430
git2r::status()
35-
expect_equal(length(extract_diff_root(".")), 3)
3631
expect_true(diff_requires_run_roxygenize("."))
3732
git2r::commit(".", "replaced")
3833

3934

4035
# when roxygen line is removed
4136
writeLines("#", "R/first.R")
4237

43-
expect_equal(extract_diff_root("."), NULL)
4438
git2r::add(".", "R/first.R")
45-
expect_equal(length(extract_diff_root(".")), 5)
4639
expect_true(diff_requires_run_roxygenize("."))
4740
git2r::commit(".", "when reomved")
4841
})
@@ -56,19 +49,15 @@ test_that("change in formals alone triggers invalidation", {
5649
# when new lines are added
5750
text <- c("#' Roxygen comment", "#'", "#' more things", "x <- function(a = 2) {", " a", "}")
5851
writeLines(text, "R/fifth.R")
59-
expect_equal(length(extract_diff_root(".")), 0)
6052
expect_false(diff_requires_run_roxygenize("."))
6153
git2r::add(".", "R/fifth.R")
6254

63-
expect_equal(extract_diff_root("."), text)
6455
expect_true(diff_requires_run_roxygenize("."))
6556
git2r::commit(".", "add file 5")
6657
# change signature
6758
text <- c("#' Roxygen comment", "#'", "#' more things", "x <- function(a = 3) {", " a", "}")
6859
writeLines(text, "R/fifth.R")
6960
git2r::add(".", "R/fifth.R")
70-
expect_equal(extract_diff_root("."), add_trailing_linebreak(c("x <- function(a = 2) {", "x <- function(a = 3) {")))
71-
expect_true(diff_requires_run_roxygenize("."))
7261
git2r::commit(".", "clear case 5")
7362
})
7463
})
@@ -139,7 +128,7 @@ test_that("fails gratefully when not installed package is required according to
139128
suppressWarnings(
140129
roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize"))
141130
),
142-
"The package .* is required"
131+
"The package .*required"
143132
)
144133
})
145134

@@ -167,6 +156,6 @@ test_that("warns if there is any other warning", {
167156

168157
expect_warning(
169158
roxygenize_with_cache(list(getwd()), dirs = dirs_R.cache("roxygenize")),
170-
"with @name"
159+
"(with|a) @name"
171160
)
172161
})

0 commit comments

Comments
 (0)