Skip to content

Commit 07e184a

Browse files
make use_precommit() non-blocking for long-lasting hook installation
1 parent 4aa9f08 commit 07e184a

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-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: 8 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,7 +24,7 @@ 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
)
2829
stderr <- readLines(stderr)
2930
if (isTRUE(any(grepl("error", stderr, ignore.case = TRUE)))) {
@@ -53,15 +54,17 @@ call_and_capture <- function(command, args, ...) {
5354
#' ensure an executable to runs successfully) or, if the installation method was
5455
#' not conda, as a plain bash command.
5556
#' @param ... Arguments passed to the command line call `pre-commit`.
57+
#' @param wait Passed to [base::system2()].
5658
#' @keywords internal
57-
call_precommit <- function(...) {
59+
call_precommit <- function(..., wait = TRUE) {
5860
if (is_conda_installation()) {
5961
call_and_capture(
6062
reticulate::conda_binary(),
61-
c("run", "-n", "r-precommit", path_precommit_exec(), ...)
63+
c("run", "-n", "r-precommit", path_precommit_exec(), ...),
64+
wait = wait
6265
)
6366
} else {
64-
call_and_capture(path_precommit_exec(), c(...))
67+
call_and_capture(path_precommit_exec(), c(...), wait = wait)
6568
}
6669
}
6770

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.

0 commit comments

Comments
 (0)