如何在rmarkdown中打印git历史记录?

问题描述 投票:0回答:3

我正在使用 rmarkdown 编写分析报告,并希望有一个“文档版本”部分,其中我将指出文档的不同版本以及所做的更改。

我没有手动写下来,而是考虑使用 git History 并将其自动插入到 markdown 文档中(将其格式化为表格)。

我怎样才能做到这一点?可以吗?

r git knitr r-markdown
3个回答
5
投票

安装

git2r
https://github.com/ropensci/git2r然后你可以做类似的事情:

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

现在我有了这个存储库上所有提交的列表。您可以根据您的需要从每次提交和格式中获取信息到您的文档中。

> 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

因此,如果您只想要时间和提交消息,那么您可以从对象中获取它。

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

我不知道是否有适当的访问器函数,而不是使用@符号来获取插槽。需要多读一点文档...

您可以通过这种方式从您的提交中创建一个数据框:

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

它将日期转换为 POSIXct 对象,这很好。从数据框创建 Markdown 表应该很简单!


3
投票

您可以使用

pretty=format
[1]

手动将 git log 转换为 markdown

类似的东西

git log --reverse --pretty=format:'| %H | %s |'

这将输出类似这样的内容:

| a8d5defb511f1e44ddea21b42aec9b03ee768253 | initial commit |
| fdd9865e9cf01bd53c4f1dc106ee603b0a730f48 | fix tests |
| 10b58e8dd9cf0b9bebbb520408f0b342df613627 | add Dockerfile |
| d039004e8073a20b5d6eab1979c1afa213b78fa3 | update README.md |

1:https://git-scm.com/docs/pretty-formats


0
投票
git log --pretty=format:"## %h %n- **Author:** %an %n- **Date:** %ad %n- **Message:** %s%n" --date=short
© www.soinside.com 2019 - 2024. All rights reserved.