在 git post-commit 挂钩中,如何获取已更改的文件列表?

发布于 2024-10-02 13:41:01 字数 315 浏览 8 评论 0原文

尝试计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜柠檬 2024-10-09 13:41:01

在围绕 git 编写脚本时,您应该尝试坚持管道命令< /a> — 它们的格式不太可能改变并且更容易解析。这是一个输出在提交中更改的路径名称的命令:

git diff-tree -r --name-only --no-commit-id <tree-ish>

除此之外,您可能希望检查索引,因为它包含有关文件暂存时间的时间戳,这可能会给您带来额外的优势;但是,我不认为有办法访问此信息。

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.

决绝 2024-10-09 13:41:01

做了一些研究,发现 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 Python os.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.

带刺的爱情 2024-10-09 13:41:01

感谢 @JoshLee 他的回答

我尝试完成它:

git diff-tree -r --name-only --diff-filter=ACMRTUXB --no-commit-id HEAD

--diff-filter=ACMRTUXB 的解释:

ACMRTUXB 包含除 Delete 之外的所有更改。请参阅: https://git- scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203

Thank @JoshLee for his answer.

I tried to complete it:

git diff-tree -r --name-only --diff-filter=ACMRTUXB --no-commit-id HEAD

Explanation for --diff-filter=ACMRTUXB:

ACMRTUXB includes all changes except Delete. See: https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文