Skip to content

Instantly share code, notes, and snippets.

@cwshu
Created April 25, 2015 18:50
Show Gist options
  • Save cwshu/7f06cbfd9b0bc24b2b56 to your computer and use it in GitHub Desktop.
Save cwshu/7f06cbfd9b0bc24b2b56 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
transform the diff program output format to "git diff --stat" style.
./diff_stat.py [diff_file]
"""
import sys
def get_fname(diff_data):
"""
format:
diff -r qemu/arch_init.c qemu-arm_fastvm/arch_init.c
diff_data_split_by_space[2]: qemu/arch_init.c
"""
diff_data_split_by_space = diff_data.split(' ')
name = diff_data_split_by_space[2]
return name
def raw_diff_to_stat(diff_filename):
f = open(diff_filename, 'r')
content_line = f.readlines()
stat_files = []
fname_max_len = 0
for line in content_line:
if line.startswith('diff'):
cur_file = get_fname(line)
fname_max_len = max(fname_max_len, len(cur_file))
cur_diff_descr = {'decre': 0, 'incre': 0}
stat_files.append((cur_file, cur_diff_descr))
elif line.startswith('<'):
stat_files[-1][1]['decre'] += 1
elif line.startswith('>'):
stat_files[-1][1]['incre'] += 1
return stat_files, fname_max_len
def print_diff_stat(stat_files, fname_max_len):
for file_ in stat_files:
name = file_[0] + (fname_max_len + 1 - len(file_[0]))*' '
incre = '+' + str(file_[1]['incre'])
decre = '-' + str(file_[1]['decre'])
print('{name:20}| {incre:>5}, {decre:>5}'.format(name=name, incre=incre, decre=decre))
if __name__ == '__main__':
if len(sys.argv) != 2:
print("./diff_stat.py [diff_file]")
diff_filename = sys.argv[1]
stat_files, fname_max_len = raw_diff_to_stat(diff_filename)
print_diff_stat(stat_files, fname_max_len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment