为什么 gitpython 结果与 github 网站不匹配?

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

我使用以下 python 代码来提取两次提交之间的差异(大块头)。

from git import Repo
!git clone https://github.com/apache/commons-math.git
repo = Repo("/content/commons-math")
file_path = 'commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java'
parent = 'd080f0d8251d58728024955764a5c0c75acf8277'
commit = '9d1741bfe4a7808cfa0c313891a717adf98a3087'
hunks = repo.git.diff(parent, commit, file_path, ignore_blank_lines=True, ignore_space_at_eol=True)

帅哥显示指定文件是新文件,添加689行创建:

diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java
new file mode 100644
index 000000000..dfdfdd946
--- /dev/null
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/distribution/EmpiricalDistributionTest.java
@@ -0,0 +1,689 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
.
.
.

但是,当我打开相应的GitHub 提交页面,并查看

EmpiricalDistributionTest.java
的详细信息时,它显示该文件已重命名(包含文件夹已更改)并更新了几行。我的第一个问题是为什么 GitPython 的结果与 GitHub 界面不匹配?第二个问题是如何配置 GitPython 以获得与 GitHub 网站界面相同的结果?

我发现当文件移动到另一个文件夹并且文件内容在该提交中发生更改时会发生此问题。在java项目中,当一个类的包含包发生变化时,文件夹名称和文件内容都会发生变化。但是,我不知道为什么 GitPython 无法将这种情况检测为对现有文件的更新。预先感谢您的帮助。

github gitpython
1个回答
0
投票

GitPython 的结果与 GitHub 界面不匹配的原因是 GitPython 只查看我指定的两个提交之间的差异,而 GitHub 界面显示文件的完整历史记录。当文件移动到不同的文件夹并更新其内容时,Git 将其存储为两个单独的更改:首先,文件从其旧位置删除,其次,在新位置创建一个包含更新内容的新文件.

要配置GitPython 得到与GitHub 网站界面相同的结果,在调用git.diff 命令时使用--follow 选项。此选项指示 Git 跟踪文件的历史记录,即使它已被移动或重命名。这是包含 --follow 选项的代码的更新版本:

hunks = repo.git.diff(parent, commit, file_path, ignore_blank_lines=True, ignore_space_at_eol=True, follow=True)

使用 --follow 选项,git.diff 命令将检测到文件已被移动和重命名,并显示文件的完整历史记录。

© www.soinside.com 2019 - 2024. All rights reserved.