在git post-commit hook中,如何获取已更改文件的列表? [英] In a git post-commit hook how do I get a list of the files that were changed?
问题描述
我有一个提交后提交的git钩子,它提交了一个提交后的git钩子,有关提交的API信息。我想要做的是弄清楚在提交上花了多长时间。大致。
我的假设是,可以通过查找所涉及文件的所有创建时间和修改时间的最小值来计算出一个粗略的值,并与最大创建 - 和修改时间。
我可以在Python脚本中轻松完成此操作。如果有人告诉我这是文件foo.txt,bar.txt和path / bla.txt,我可以在基于这些文件的脚本中快速进行一些算术运算。
所以,在一个git post-commit hook中,如何获得已更改文件的列表?
当在git中编写脚本时,应该尝试坚持 plumbing命令 - 它们的格式不太可能更改并且更易于解析。这是一个命令,用于输出在提交中更改的路径的名称:
git diff-tree -r --name -only --no-commit-id< tree-ish>除此之外,您可能希望检查索引,因为它包含有关文件上传时间的时间戳,这可能会给你一个额外的优势;但是,我不相信有一种方法可以访问这些信息。
Trying to figure out how long time was spent in a commit in a Git post-commit hook.
I've got a post-commit git hook that submits information over an API about the commit. What I want to do is figure out how long time was spent on the commit. Roughly.
My assumption is that a rough value can be figured out by finding the minimum of all the creation-time and modification-time of the files involved and compare with the maximum creation- and modification-time.
I can easily do this in a Python script. If someone tells me it was the files "foo.txt", "bar.txt" and "path/bla.txt" I can quickly do some arithmetic in a script based on these files.
So, In a git post-commit hook how do I get a list of the files that were changed?
解决方案 When writing scripts around git, you should try to stick to the plumbing commands — their format is less likely to change and easier to parse. Here is a command which outputs the names of the paths which changed in a commit:
git diff-tree -r --name-only --no-commit-id <tree-ish>
That aside, you may wish to examine the index, as it contains timestamps about when the files were staged, which might give you an extra edge; however, I don’t believe there is a way to access this information.
这篇关于在git post-commit hook中,如何获取已更改文件的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!