0

我需要带有上下文的帅哥(添加和删除的行),所以我使用 git diff -u 来获取差异。我可以做line.is_added来获得lines_added,但这些行将不包括上下文行。如何解析此差异以获取lines_added删除的行以及一些上下文行。现在我有

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

本质上我想要

lines_deleted = context lines, Line 6, more context lines and 
lines_added = context lines , Line 7, more context lines
4

1 回答 1

0
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)

对于lines_deleted 也是如此。这将返回:

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

假设除第 7 行之外的所有行都是上下文行。

于 2020-05-03T00:17:49.817 回答