110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Conta linhas de codigo ADICIONADAS por autor de commit, num repositorio git, numa janela de tempo.
|
|
|
|
Uso:
|
|
python3 count_loc.py <path-do-repo> [--since "3 months ago"]
|
|
|
|
Requisitos:
|
|
- Repositorio local com fetch atualizado (ver skill SKILL.md, secao "Autenticar fetch sem prompt").
|
|
- Python 3, git no PATH.
|
|
|
|
Saida (stdout): uma linha por autor, "<linhas>\t<email>", ordenado desc.
|
|
Saida (stderr): quantidade de commits de revert excluidos.
|
|
|
|
Ver .context/skills/git-devops-productivity/SKILL.md para a lista de exclusoes e o porque de cada uma
|
|
antes de estender EXCLUDE_PATTERNS — cada padrao aqui existe por um outlier real encontrado em produção.
|
|
"""
|
|
import subprocess
|
|
import sys
|
|
import re
|
|
import argparse
|
|
from collections import defaultdict
|
|
|
|
EXCLUDE_PATTERNS = [
|
|
r'package-lock\.json$', r'yarn\.lock$', r'pnpm-lock\.yaml$',
|
|
r'\.min\.js$', r'\.min\.css$',
|
|
r'/dist/', r'/node_modules/', r'/bin/', r'/obj/',
|
|
r'\.csproj\.nuget\.',
|
|
r'packages\.lock\.json$',
|
|
r'\.designer\.cs$',
|
|
r'/Migrations/.*\.cs$', # EF/FluentMigrator: migration autogerada, nao escrita a mao
|
|
r'\.snap$',
|
|
r'poetry\.lock$', r'Cargo\.lock$',
|
|
r'graphify-out/', # indice de codigo gerado (ver skill code-index) — nunca conta
|
|
r'\.generated\.',
|
|
r'knowledge_base\.json$', # conteudo/dados de RAG, nao codigo
|
|
r'\.jsonl$',
|
|
]
|
|
excl_re = [re.compile(p) for p in EXCLUDE_PATTERNS]
|
|
|
|
REVERT_RE = re.compile(r'^revert\b', re.IGNORECASE)
|
|
|
|
|
|
def excluded(path: str) -> bool:
|
|
return any(r.search(path) for r in excl_re)
|
|
|
|
|
|
def get_commits(repo: str, since: str):
|
|
out = subprocess.run(
|
|
["git", "-C", repo, "log", "--all", f"--since={since}", "--no-merges", "--format=%H|%ae|%s"],
|
|
capture_output=True, text=True
|
|
).stdout.strip().split("\n")
|
|
result = []
|
|
for line in out:
|
|
if not line:
|
|
continue
|
|
parts = line.split("|", 2)
|
|
if len(parts) == 3:
|
|
result.append(parts)
|
|
return result
|
|
|
|
|
|
def get_added_lines(repo: str, commit_hash: str) -> int:
|
|
out = subprocess.run(
|
|
["git", "-C", repo, "show", "--numstat", "--format=", commit_hash],
|
|
capture_output=True, text=True
|
|
).stdout.strip().split("\n")
|
|
added = 0
|
|
for line in out:
|
|
if not line.strip():
|
|
continue
|
|
parts = line.split("\t")
|
|
if len(parts) != 3:
|
|
continue
|
|
add, _rem, path = parts
|
|
if excluded(path):
|
|
continue
|
|
if add == "-": # binario, git nao reporta numstat
|
|
continue
|
|
try:
|
|
added += int(add)
|
|
except ValueError:
|
|
pass
|
|
return added
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("repo", help="Path do repositorio git local")
|
|
parser.add_argument("--since", default="3 months ago", help='Janela, formato git (default: "3 months ago")')
|
|
args = parser.parse_args()
|
|
|
|
commits = get_commits(args.repo, args.since)
|
|
by_author = defaultdict(int)
|
|
skipped_reverts = 0
|
|
for commit_hash, email, subject in commits:
|
|
if REVERT_RE.match(subject.strip()):
|
|
skipped_reverts += 1
|
|
continue
|
|
by_author[email] += get_added_lines(args.repo, commit_hash)
|
|
|
|
for email, total in sorted(by_author.items(), key=lambda x: -x[1]):
|
|
print(f"{total}\t{email}")
|
|
if skipped_reverts:
|
|
print(f"(excluidos {skipped_reverts} commits de revert)", file=sys.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|