简体   繁体   English

从 Git diff 文件计算更改行

[英]Calculating lines of change from a Git diff file

The goal目标

I am building a git stats script in Python that can only access the historical git diff patches, so basically files like this我正在用 Python 构建一个 git stats 脚本,它只能访问历史 git diff 补丁,所以基本上是这样的文件

diff --git a/README b/README
index 980a0d5f..fef29374 100644
--- a/README
+++ b/README
@@ -1 +1,3 @@
 Hello World!
+
+Hello planet! - DD
\ No newline at end of file

What do I want exactly?我到底想要什么?

  1. Take a list of git diff files as input将 git diff 文件列表作为输入
  2. Calculate how many lines were changed (optional), added and removed in each diff file计算在每个 diff 文件中更改、添加和删除的行数(可选)
  3. Sum it all up总结一下
  4. Print "total lines added = X, total lines removed = Y" etc.打印“添加的总行数 = X,删除的总行数 = Y”等。

Constraints约束

The system running this script does not have access to the git repository, nor does it have git installed - introducing an interesting complication.运行此脚本的系统无法访问 git 存储库,也没有安装 git - 引入了一个有趣的复杂性。 🙂 🙂

I have no issues with accessing the API through Python or writing code to manually calculate things.我对通过 Python 访问 API 或编写代码来手动计算事物没有任何问题。 The only issue I have is what I mentioned above.我唯一的问题是我上面提到的。

Other sources其他来源

I checked, and so I know there were many similar questions on this topic.我查了一下,所以我知道关于这个话题有很多类似的问题。 I'm just having trouble finding what I'm looking for in Python, without using git in the repository directly... (happy to close this if someone can point me to a solution)我只是无法在 Python 中找到我要查找的内容,而无需直接在存储库中使用git ...... (如果有人能指出我的解决方案,很高兴关闭它)


So then, any ideas?那么,有什么想法吗? I assume I can just manually parse each of the diff files and sum it up, but I'm hoping for a silver bullet from a git magician!我假设我可以手动解析每个 diff 文件并将其总结,但我希望来自 git 魔术师的灵丹妙药!

It turns out that GitHub API already offers these diff stats without the need to have git installed or repository checked out locally.事实证明,GitHub API 已经提供了这些差异统计信息,而无需安装git或在本地签出存储库。

GitHub Docs GitHub 文档

Since this is good enough for my case, I'm going to use it.由于这对我的情况来说已经足够好了,所以我将使用它。 New answers are still welcome though.不过,仍然欢迎新的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM