简体   繁体   English

如何使用 unidiff 解析器解析 git diff -U output

[英]How to parse git diff -U output using unidiff parser

I need hunks (lines added and deleted) with context so I used git diff -u to obtain a diff.我需要带有上下文的帅哥(添加和删除的行),所以我使用 git diff -u 来获取差异。 I can do line.is_added to to obtain lines_added but those line will not include lines of context.我可以做line.is_added来获得lines_added ,但这些行将不包括上下文行。 How do I parse this diff to get lines_added and lines deleted along with some lines of context.如何解析此差异以获取lines_added删除的行以及一些上下文行。 Right now I have现在我有

  Line 1
  ......
  Line 5 
- Line 6 
+ Line 7
  ......
  Line n

Essentially I want本质上我想要

lines_deleted = context lines, Line 6, more context lines and 
lines_added = context lines , Line 7, more context lines
lines_added = []
for hunk in patched_file:
         for line in hunk:
             if line.is_added or line .is_context and line.value.strip() != '':
                 lines_added.append(line.value)

and similarly for the lines_deleted.对于lines_deleted 也是如此。 This shall return:这将返回:

  Line 1
  ......
  Line 5  
+ Line 7
  ......
  Line n

assuming all lines other than Line 7 are context lines.假设除第 7 行之外的所有行都是上下文行。

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

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