python在线diff工具在哪_Python Git diff解析器

最后,我找到了解决方案. 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行以——开头.要解决此问题,您需要编辑0700的源文件中的正则表达式,该文件位于/unidiff/constants.py中:

RE_SOURCE_FILENAME = re.compile(

r'^--- (?P[^\t\n]+)(?:\t(?P[^\n]+))?')

至:

RE_SOURCE_FILENAME = re.compile(

r'^------ (?P[^\t\n]+)(?:\t(?P[^\n]+))?')

PS:如果源文件重命名,gitpython会生成带有—的diff开头.但它不会抛出错误,因为我过滤了重命名文件的git diff(diff_filter =’cr’).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值