I want to set-up a pre-commit hook for devtools::document
and want to fail the commit if devtools::document
changes anything. I checked the devtools::document
source and it returns invisible()
so it's not possible to conditionally return an exit code.
Are there any clever ways to stop the commit from happening when devtools::document
does work?
Using the function capture.output
we can catch the messages devtools::documents
sends whenever it changes a file:
Writing getScore.Rd
So when we run:
results <- capture.output(devtools::document())
When a document is updated (for this example, I removed one of my .Rd files, results looks as follows:
> results
[1] "Writing getScore.Rd"
If nothing is written, results
is empty:
> results
character(0)
It is easy to go on from here I think :)
The OP, @BobJansen created the following script that runs the above method and returns a status = 0
if no files were edited, and a status = 100
if any where:
#!/usr/bin/Rscript
source("packrat/init.R")
lines <- capture.output(devtools::document(), type = 'output')
if (length(lines) > 0) {
print(lines)
q(save = 'no', status = 100)
} else {
q(save = 'no', status = 0)
}