'How to get sha of current git commit from R

Is there a way to get SHA of current GIT commit from R? I need to access it via a function call (not as a hard string).

I have adapted GIT as version control system for analysis, and want to print the SHA on footnote of my intermediate reports (my working drafts, in pdf format, get a life of their own and it is not immediately obvious by looking at them at what moment they were generated; this creates a reproducibility issue).

For reference: I am using R 3.4.1 via R Studio and creating reports via r markdown.



Solution 1:[1]

Install git2r package and then:

> library(git2r)
> r = revparse_single(path,"HEAD")
> sha(r)
[1] "f5bb1f115e9db0c732840298465488e8dfea5032"

It also gives you some other info about the commit too, in other members of the returned object:

> r$author
name:  Barry Rowlingson
email: [email protected]
when:  2022-01-12 18:37:14 GMT

These are documented in help(commit), but only sha seems to have an accessor function for it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1