gitparsingdiffunified-diff

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

Solution

  • 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.