Skip to content

Instantly share code, notes, and snippets.

@alldne
Last active January 5, 2020 04:08
Show Gist options
  • Select an option

  • Save alldne/2a95a4839a7c5a3c6112414f40469081 to your computer and use it in GitHub Desktop.

Select an option

Save alldne/2a95a4839a7c5a3c6112414f40469081 to your computer and use it in GitHub Desktop.
parse-git-log-numstat.py
#!/usr/bin/env python3
import sys
import os
import fileinput
from collections import Counter
def main():
n = None
if len(sys.argv) > 1:
n = int(sys.argv[1])
parse_stdin_and_show_most_common(n)
def parse_stdin_and_show_most_common(n):
chars = { digit for digit in map(str, range(0, 11)) }
# For binary files, outputs two - instead of saying 0 0. (git log --help)
chars.add("-")
def contains_path(line):
return line[0] in chars
def extract_filename(fullpath):
fullpath = fullpath.split("\t")[2]
return os.path.basename(fullpath.strip())
lines = (line for line in sys.stdin)
paths = filter(contains_path, lines)
filenames = map(extract_filename, paths)
counter = Counter(filenames)
print_result(counter, n)
def print_result(counter, n):
for filename, count in counter.most_common(n):
print(f"{count}\t{filename}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment