Skip to content

Commit cf2b1f5

Browse files
git2r should not be a dependency for user side, e.g. when using use_precommit()
1 parent 5da9a1b commit cf2b1f5

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ editor_options:
99
- Ensure sorting for `spell-check` hook is case-insensitive on all
1010
operating systems. This may re-order your whole `inst/WORDLIST`
1111
(#405).
12+
- {git2r} is no longer needed for user side (#417).
1213
- Fix bug that prevented pkg load outside directories with precommit
1314
files (@mpadge, #413).
1415
- hook `forbid-to-commit` in template `.pre-commit-config.yaml` has

R/assert.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
assert_is_git_repo <- function(root) {
2-
if (is.null(git2r::discover_repository(root))) {
2+
if (!is_git_repo(root = root)) {
33
rlang::abort(paste0(
44
"The directory ", root, " is not a git repo. Please navigate to ",
55
root, " and init git in ",

R/utils.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,21 @@ rev_as_pkg_version <- function(rev) {
108108
has_git <- function() {
109109
nzchar(Sys.which("git"))
110110
}
111+
112+
is_git_repo <- function(root = here::here()) {
113+
withr::local_dir(root)
114+
if (has_git()) {
115+
rlang::with_handlers(
116+
{
117+
output <- call_and_capture(
118+
"git",
119+
c("rev-parse", "--is-inside-work-tree")
120+
)
121+
output$exit_status == 0 && as.logical(toupper(output$stdout))
122+
},
123+
erorr = function(...) FALSE
124+
)
125+
} else {
126+
dir.exists(".git")
127+
}
128+
}

man/git_init.Rd

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

tests/testthat/test-utils.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ test_that("call capturing for pass", {
1616
)
1717
})
1818

19+
test_that("git repo status can be evaluated", {
20+
tmpdir <- local_test_setup(git = FALSE)
21+
expect_false(is_git_repo(root = tmpdir))
22+
git_init(tmpdir)
23+
expect_true(is_git_repo(root = tmpdir))
24+
})
25+
1926
test_that("call capturing for error (command that does not exist)", {
2027
expect_silent(captured <- call_and_capture("j23lkjsdi", "1"))
2128
expect_true(

0 commit comments

Comments
 (0)