简体   繁体   中英

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. I can do line.is_added to to obtain lines_added but those line will not include lines of context. How do I parse this diff to get lines_added and lines deleted along with some lines of context. 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. This shall return:

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

assuming all lines other than Line 7 are context lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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