Account for "Git: ... no available repositories" in `rstudioapi::executeCommand("vcsRefresh") `
Reported in private beta: https://github.com/posit-dev/positron-beta/discussions/63
Apparently, rstudioapi::executeCommand("vcsRefresh") is a silent no-op in RStudio if the current project is not a Git repo.
However, in Positron, this creates a pop-up (which can just be dismissed, but it's still distracting/confusing):
usethis calls its internal rstudio_git_tickle() often to force RStudio to refresh its git pane. This was done in reaction to confusion if "Git facts" have changed on the ground, but the RStudio UI didn't reflect that.
It seems like vcsRefresh might require a bit more wrapping in our rstudioapi shim, i.e. do some sort of pre-check.
https://github.com/r-lib/usethis/blob/9e64daf13ac1636187d59e6446d9526a414d8ba6/R/rstudio.R#L184-L189
I've looked into this a bit more and I suspect @hadley's hypothesis of a race condition is correct:
https://github.com/r-lib/usethis/pull/2011#issue-2380380721
Changing the definition of usethis:::rstudio_git_tickle() to add uses_git() to the guard does not eliminate the pop-up for plain old usethis::use_git():
rstudio_git_tickle <- function() {
if (uses_git() && rstudioapi::hasFun("executeCommand")) {
rstudioapi::executeCommand("vcsRefresh")
}
invisible()
}
So now I think I may be wrong about this having (only?) to do with rstudio_git_tickle() and vcsRefresh. With use_git(), it's starting to look like the pop-up comes from some other git operation, such as gert::git_status(). In any case, I can apply a bandaid in usethis by adding a wee bit of sleep after initializing the repo and adding the guard above (to address the case when we truly aren't in a git repo).
This comes up, as described in #4002, when you run devtools::test() on an R package that is not a git repo.
To verify this, there are a couple of options but probably the most direct/easy is to run rstudioapi::executeCommand("vcsRefresh") in a workspace that is (git pane will refresh) and is not (nothing happens, including no popup) a git repo.
Verified Fixed
Positron Version(s) : 2024.07.0-125
OS Version : OSX
Test scenario(s)
rstudioapi::executeCommand("vcsRefresh") in a git repo and a plain folder.
Link(s) to TestRail test cases run or created: N/A