Maybe extract this processing chunk into a function called: process_diff_output that would go through the git diff output and build the list of tuples you are generating (resulting).

Then have a format_diff function that would receive the list of tuples you generated and return a list of formatted lines.