Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print git history in rmarkdown? [closed]

I am writing an analysis report with rmarkdown and would like to have a "document versions" section in which I would indicate the different versions of the document and the changes made.

Instead of writing it down manually, I was thinking about using git history and inserting it automatically in the markdown document (formatting it in a table).

How can I do that? Is it possible?

like image 430
Ben Avatar asked Feb 04 '17 08:02

Ben


1 Answers

Install git2r, https://github.com/ropensci/git2r then you can do stuff like:

> r = repository(".")
> cs = commits(r)
> cs[[1]]
[02cf9a0] 2017-02-02: uses Rcpp attributes instead of inline

So now I have a list of all the commits on this repo. You can get the info out of each commit and format as per your desire into your document.

> summary(cs[[1]])
Commit:  02cf9a0ff92d3f925b68853374640596530c90b5
Author:  barryrowlingson <[email protected]>
When:    2017-02-02 23:03:17

     uses Rcpp attributes instead of inline

11 files changed, 308 insertions, 151 deletions
DESCRIPTION           | -  0 +  2  in 2 hunks
NAMESPACE             | -  0 +  2  in 1 hunk
R/RcppExports.R       | -  0 + 23  in 1 hunk
R/auxfunctions.R      | -  1 +  1  in 1 hunk
R/skewt.r             | -  0 +  3  in 1 hunk
R/update_params.R     | -  1 +  1  in 1 hunk
R/update_params_cpp.R | -149 +  4  in 2 hunks
src/.gitignore        | -  0 +  3  in 1 hunk
src/RcppExports.cpp   | -  0 + 76  in 1 hunk
src/hello_world.cpp   | -  0 + 13  in 1 hunk
src/update_params.cpp | -  0 +180  in 1 hunk

So if you just want the time and the commit message then you can grab it out of the object.

> cs[[3]]@message
[1] "fix imports etc\n"
> cs[[3]]@committer@when
2017-01-20 23:26:20

I don't know if there's proper accessor functions for these rather than using @-notation to get slots. Need to read the docs a bit more...

You can make a data frame from your commits this way:

> do.call(rbind,lapply(cs,function(cs){as(cs,"data.frame")}))

which converts the dates to POSIXct objects, which is nice. Creating a markdown table from the data frame should be trivial!

like image 153
Spacedman Avatar answered Sep 19 '22 00:09

Spacedman