"""Lines added by an author in their own non-merge commits (git log -p), for C/C++ files under src/, excluding subtrees and mechanical commits. Usage: added_lines.py repo author_regex out_file [until]""" import subprocess, re, sys, collections repo, auth, out = sys.argv[1:4]; until = sys.argv[4] if len(sys.argv) > 4 else 'HEAD' EXCL = 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)|thanks|merge|move[sd]?|renam|crlf|whitespace|format|copyright|indent|revert|cherry|backport|license)\b', re.I) log = subprocess.check_output(['git','-C',repo,'log',until,'--no-merges','--perl-regexp','--author='+auth,'--format=@@COMMIT %H %s','-p','--unified=0','--','src/'], text=True, errors='replace') st = collections.Counter(); fh = open(out,'w'); ok = False; fok = False; buf = [] for line in log.split('\n'): if line.startswith('@@COMMIT '): ok = not BAD.search(line.split(' ',2)[2] if line.count(' ')>=2 else ''); st['commits'] += 1; st['commits_kept'] += ok; continue if line.startswith('+++ '): p = line[6:] if line.startswith('+++ b/') else '' fok = ok and bool(re.search(r'\.(cpp|h)$', p)) and p.startswith('src/') and not EXCL.search(p); continue if line.startswith('@@'): if buf: fh.write('\n'.join(buf)+'\n\n'); buf = [] continue if fok and line.startswith('+') and not line.startswith('+++'): buf.append(line[1:]); st['lines'] += 1 if buf: fh.write('\n'.join(buf)+'\n') print(dict(st))