在 git post-commit 挂钩中,如何获取已更改的文件列表?
尝试计算 Git 提交后挂钩中的提交花费了多长时间。
我有一个提交后 git 挂钩,它通过 API 提交有关提交的信息。我想做的是弄清楚提交花费了多长时间。大致。
我的假设是,可以通过找到所涉及文件的所有创建时间和修改时间的最小值并将其与最大创建时间和修改时间进行比较来计算出粗略值。
我可以在 Python 脚本中轻松完成此操作。如果有人告诉我这是文件“foo.txt”、“bar.txt”和“path/bla.txt”,我可以在基于这些文件的脚本中快速进行一些算术运算。
那么,在 git post-commit 挂钩中,如何获取已更改的文件列表?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在围绕 git 编写脚本时,您应该尝试坚持管道命令< /a> — 它们的格式不太可能改变并且更容易解析。这是一个输出在提交中更改的路径名称的命令:
除此之外,您可能希望检查索引,因为它包含有关文件暂存时间的时间戳,这可能会给您带来额外的优势;但是,我不认为有办法访问此信息。
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:
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 log --name-only -n1 是最好的方法。获得最小值并不难。和最大。通过进行一些字符串匹配并使用 Python os.stat 模块来从文件中获取时间戳。
作为通用解决方案,它仍然不是很好,因为文件的修改时间并不能真正反映实际花费的时间的实际情况。
Been doing some research and found that
git log --name-only -n1
is the best approach. It's not difficult to get the min. and max. timestamps out of the files by doing a bit of string matching and using the Pythonos.stat
module.As a general solution it still isn't great because the modification times on the files doesn't really reflect the reality of the time spent actually.
感谢 @JoshLee 他的回答。
我尝试完成它:
--diff-filter=ACMRTUXB
的解释:Thank @JoshLee for his answer.
I tried to complete it:
Explanation for
--diff-filter=ACMRTUXB
: