"""Collect lines whose last change (git blame, no -w) is by a given author, excluding commits whose subject suggests someone else's work or mechanical edits. Usage: blame_author.py repo tag author_regex out_file [path_regex]""" import subprocess, re, sys, collections repo, tag, auth, out = sys.argv[1:5]; pathre = re.compile(sys.argv[5] if len(sys.argv) > 5 else r'^src/.*\.(cpp|h)$') EXCL_PATH = re.compile(r'(^src/(leveldb|secp256k1|univalue|json|crypto/ctaes|minisketch|crc32c|test/data|qt/forms)/|/moc_|\.pb\.|qrc_|/locale/|bitcoinstrings)') BAD = re.compile(r'\b(patch(es)? (from|by)|from |by |thanks|merge|move[sd]?|renam|crlf|whitespace|format|copyright|indent|revert|cherry|backport|update .* to|license)\b', re.I) files = [f for f in subprocess.check_output(['git','-C',repo,'ls-tree','-r','--name-only',tag], text=True).split() if pathre.search(f) and not EXCL_PATH.search(f)] subj = {}; stats = collections.Counter(); fh = open(out, 'w') for f in files: try: b = subprocess.check_output(['git','-C',repo,'blame','--line-porcelain',tag,'--',f], text=True, errors='replace') except subprocess.CalledProcessError: continue cur = a = None; buf = [] for line in b.split('\n'): m = re.match(r'^([0-9a-f]{40}) ', line) if m: cur = m.group(1); continue if line.startswith('author '): a = line[7:]; continue if line.startswith('summary '): subj.setdefault(cur, line[8:]); continue if line.startswith('\t'): if re.search(auth, a or ''): stats['author_lines'] += 1 if not BAD.search(subj.get(cur, '')): buf.append(line[1:]); stats['kept'] += 1 continue if buf: fh.write('\n'.join(buf) + '\n\n'); buf = [] if buf: fh.write('\n'.join(buf) + '\n\n') stats['files'] += 1 print(dict(stats))