我想用Python代码解析git diff,我有兴趣从diff解析器获取以下信息:
我正在使用unidiff 0.5.2来编写以下代码:
from unidiff import PatchSet
import git
import os
commit_sha1 = 'b4defafcb26ab86843bbe3464a4cf54cdc978696'
repo_directory_address = '/my/git/repo'
repository = git.Repo(repo_directory_address)
commit = repository.commit(commit_sha1)
diff_index = commit.diff(commit_sha1+'~1', create_patch=True)
diff_text = reduce(lambda x, y: str(x)+os.linesep+str(y), diff_index).split(os.linesep)
patch = PatchSet(diff_text)
print patch[0].is_added_file
我使用GitPython生成Git diff。我收到以下代码的以下错误:
current_file = PatchedFile(source_file, target_file,
UnboundLocalError: local variable 'source_file' referenced before assignment
如果您能帮助我解决此错误,我将不胜感激。
答案 0 :(得分:2)
<强>更新强>:
我发现我的旧答案不再适用了。这是新的解决方案:
对于此解决方案,您需要 git 和 unidiff 个包。
import git
from unidiff import PatchSet
from cStringIO import StringIO
commit_sha1 = 'commit_sha'
repo_directory_address = "your/repo/address"
repository = git.Repo(repo_directory_address)
commit = repository.commit(commit_sha1)
uni_diff_text = repository.git.diff(commit_sha1+ '~1', commit_sha1,
ignore_blank_lines=True,
ignore_space_at_eol=True)
patch_set = PatchSet(StringIO(uni_diff_text), encoding='utf-8')
change_list = [] # list of changes
# [(file_name, [row_number_of_deleted_line],
# [row_number_of_added_lines]), ... ]
for patched_file in patch_set:
file_path = patched_file.path # file name
print('file name :' + file_path)
del_line_no = [line.target_line_no
for hunk in patched_file for line in hunk
if line.is_added and
line.value.strip() != ''] # the row number of deleted lines
print('deleted lines : ' + str(del_line_no))
ad_line_no = [line.source_line_no for hunk in patched_file
for line in hunk if line.is_removed and
line.value.strip() != ''] # the row number of added liens
print('added lines : ' + str(ad_line_no))
change_list.append((file_path, del_line_no, ad_line_no))
旧解决方案(此解决方案可能不再有效)
最后,我找到了解决方案。 gitpython的输出与标准的git diff输出略有不同。在标准的git diff源文件中以 --- 开头,但gitpython的输出以 ------ 开头,正如您在运行的输出中看到的那样以下python代码(此示例使用elasticsearch repository生成):
import git
repo_directory_address = '/your/elasticsearch/repository/address'
revision = "ace83d9d2a97cfe8a8aa9bdd7b46ce71713fb494"
repository = git.Repo(repo_directory_address)
commit = repository.commit(rev=revision)
# Git ignore white space at the end of line, empty lines,
# renamed files and also copied files
diff_index = commit.diff(revision+'~1', create_patch=True, ignore_blank_lines=True,
ignore_space_at_eol=True, diff_filter='cr')
print reduce(lambda x, y: str(x)+str(y), diff_index)
部分输出将如下:
core/src/main/java/org/elasticsearch/action/index/IndexRequest.java
=======================================================
lhs: 100644 | f8b0ce6c13fd819a02b1df612adc929674749220
rhs: 100644 | b792241b56ce548e7dd12ac46068b0bcf4649195
------ a/core/src/main/java/org/elasticsearch/action/index/IndexRequest.java
+++ b/core/src/main/java/org/elasticsearch/action/index/IndexRequest.java
@@ -20,16 +20,18 @@
package org.elasticsearch.action.index;
import org.elasticsearch.ElasticsearchGenerationException;
+import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.DocumentRequest;
import org.elasticsearch.action.RoutingMissingException;
import org.elasticsearch.action.TimestampParsingException;
import org.elasticsearch.action.support.replication.ReplicationRequest;
import org.elasticsearch.client.Requests;
+import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.Nullable;
-import org.elasticsearch.common.UUIDs;
+import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
如您所见,源文件的第4行以 ------ 开头。要解决此问题,您需要编辑unidiff 0.5.2源文件中的正则表达式,该文件位于/unidiff/constants.py中:
RE_SOURCE_FILENAME = re.compile(
r'^--- (?P<filename>[^\t\n]+)(?:\t(?P<timestamp>[^\n]+))?')
到:
RE_SOURCE_FILENAME = re.compile(
r'^------ (?P<filename>[^\t\n]+)(?:\t(?P<timestamp>[^\n]+))?')
PS:如果重命名源文件,gitpython会以 --- 生成diff start。但它不会引发错误,因为我过滤了重命名文件的git diff( diff_filter =&#39; cr&#39; )。
答案 1 :(得分:0)
使用diff_index [i] .diff作为 tdihp 的推荐值,并将源文件和目标文件行添加到diff中,否则unidiff将抛出异常。这是我的工作代码示例:
diffs = []
diff_index = commit.diff(prev_commit, create_patch=True)
for diff in diff_index.iter_change_type('M'):
if diff.a_path[-3:] == ".js":
diffs.append(diff)
if diffs:
for d in diffs:
a_path = "--- " + d.a_rawpath.decode('utf-8')
b_path = "+++ " + d.b_rawpath.decode('utf-8')
# Get detailed info
patch = PatchSet(a_path + os.linesep + b_path + os.linesep + d.diff.decode('utf-8'))
for h in patch[0]:
for l in h:
print(" " + str(l.source_line_no) + " <-> " + str(l.target_line_no))
print("")