How to Parse Git Diff -U Output Using Unidiff Parser

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

1 Answer

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.