Skip to content

Commit 3c78def

Browse files
Merge pull request #312 from lorenzwalthert/issue-311
- Don't block on hook installation by default (#312)
2 parents aa1a2ed + 43ad5f3 commit 3c78def

File tree

8 files changed

+46
-12
lines changed

8 files changed

+46
-12
lines changed

NEWS.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ includes switching to R based hook for `readme-rmd-render`, avoiding the
55
{usethis} dependency, integration tests via GitHub Actions, auto-updates for
66
used packages, roxygen snippet generation and more. In addition:
77

8+
* New option `precommit.block_install_hooks` (defaults to `FALSE`) governs
9+
if `use_precommit(..., install_hooks = TRUE)` should be blocking until
10+
hooks are installed or not.
811
* Always sort `inst/WORDLIST` (#303).
912
* rename default branch to *main* (#307).
10-
11-
1213
* `style-files` hook gains an argument `--cache-root` that is passed to
1314
`options(styler.cache_root = ...)` (#305).
1415

16+
1517
# precommit v0.1.3.9012
1618

1719
This is a pre-release for `v0.2.0` and imposes a minimal version requirement

R/call.R

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
#' @param command The command to issue. A character string of length one.
33
#' @param args The command line arguments.
44
#' @param ... Arguments passed to [system2()].
5+
#' @param wait Passed to [system2()].
56
#' @return
67
#' A list with:
78
#' * content of stderr
89
#' * content of stdout
910
#' * exit status
1011
#' @keywords internal
11-
call_and_capture <- function(command, args, ...) {
12+
call_and_capture <- function(command, args, ..., wait = TRUE) {
1213
if (length(command) != 1 | !inherits(command, "character")) {
1314
rlang::abort("The command must be a character string of length 1.")
1415
}
@@ -23,8 +24,17 @@ call_and_capture <- function(command, args, ...) {
2324
))
2425
}
2526
exit_status <- suppressWarnings(
26-
system2(command, args, ..., stdout = stdout, stderr = stderr)
27+
system2(command, args, ..., wait = wait, stdout = stdout, stderr = stderr)
2728
)
29+
# on Windows, there is no output when wait = FALSE
30+
if (is_windows() && !wait) {
31+
out <- list(
32+
stdout = "[cannot recover stdout for Windows when R option `precommit.block_install_hooks` is FALSE]",
33+
stderr = "[cannot recover stderr for Windows when R option `precommit.block_install_hooks` is FALSE]",
34+
exit_status = exit_status
35+
)
36+
return(out)
37+
}
2838
stderr <- readLines(stderr)
2939
if (isTRUE(any(grepl("error", stderr, ignore.case = TRUE)))) {
3040
# conda run has exit status 0 but stderr with ERROR, we need to set exit
@@ -53,15 +63,17 @@ call_and_capture <- function(command, args, ...) {
5363
#' ensure an executable to runs successfully) or, if the installation method was
5464
#' not conda, as a plain bash command.
5565
#' @param ... Arguments passed to the command line call `pre-commit`.
66+
#' @param wait Passed to [base::system2()].
5667
#' @keywords internal
57-
call_precommit <- function(...) {
68+
call_precommit <- function(..., wait = TRUE) {
5869
if (is_conda_installation()) {
5970
call_and_capture(
6071
reticulate::conda_binary(),
61-
c("run", "-n", "r-precommit", path_precommit_exec(), ...)
72+
c("run", "-n", "r-precommit", path_precommit_exec(), ...),
73+
wait = wait
6274
)
6375
} else {
64-
call_and_capture(path_precommit_exec(), c(...))
76+
call_and_capture(path_precommit_exec(), c(...), wait = wait)
6577
}
6678
}
6779

R/install.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ update_impl <- function() {
7373
}
7474

7575
install_repo <- function(root, install_hooks, legacy_hooks) {
76+
wait_for_hook_installation <- getOption(
77+
"precommit.block_install_hooks", FALSE
78+
)
7679
withr::with_dir(root, {
7780
remove_usethis_readme_hook()
7881
out <- call_precommit(
7982
"install",
8083
if (install_hooks) "--install-hooks",
81-
if (legacy_hooks == "remove") "--overwrite"
84+
if (legacy_hooks == "remove") "--overwrite",
85+
wait = wait_for_hook_installation
8286
)
8387
if (out$exit_status == 0) {
8488
if (any(grepl("Use -f to use only pre-commit.", out$stdout, fixed = TRUE))) {
@@ -102,7 +106,15 @@ install_repo <- function(root, install_hooks, legacy_hooks) {
102106
))
103107
}
104108
} else {
105-
cli::cli_alert_success("Sucessfully installed pre-commit for repo.")
109+
if (wait_for_hook_installation) {
110+
cli::cli_alert_success("Sucessfully installed pre-commit for repo.")
111+
} else {
112+
cli::cli_alert_info(paste0(
113+
"Installing hooks in non-blocking background process.",
114+
" If you experience problems or prefer a blocking process, use ",
115+
'{.code options("precommit.block_install_hooks" = TRUE)}.'
116+
))
117+
}
106118
}
107119
} else {
108120
cli::cli_alert_danger("Failed to install pre-commit for repo.")

R/zzz.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.onLoad <- function(libname, pkgname) {
22
op <- options()
33
op.precommit <- list(
4-
precommit.executable = path_derive_precommit_exec()
4+
precommit.executable = path_derive_precommit_exec(),
5+
precommit.block_install_hooks = FALSE
56
)
67
toset <- !(names(op.precommit) %in% names(op))
78
if (any(toset)) options(op.precommit[toset])

man/call_and_capture.Rd

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

man/call_precommit.Rd

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

test.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x

tests/testthat/test-conda.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ if (!on_cran()) {
5454
git2r::init()
5555
usethis::proj_set(".")
5656
usethis::use_readme_rmd(open = FALSE)
57+
withr::local_options(precommit.block_install_hooks = TRUE)
5758

5859
# usethis hook is removed without error
5960
expect_message(
@@ -150,6 +151,7 @@ if (!on_cran()) {
150151
expect_message(install_precommit(), "already installed")
151152
}
152153
tempdir <- local_test_setup(use_precommit = FALSE, quiet = FALSE, install_hooks = FALSE)
154+
withr::local_options(precommit.block_install_hooks = TRUE)
153155
withr::with_dir(
154156
tempdir,
155157
{

0 commit comments

Comments
 (0)