update_contributors.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. import subprocess
  3. from collections import defaultdict
  4. from pathlib import Path
  5. # Path to CONTRIBUTORS.md (in repository root)
  6. contributors_file = Path(__file__).parent.parent / "CONTRIBUTORS.md"
  7. def run_git(command):
  8. """Run a git command and return the output as a list of lines."""
  9. try:
  10. result = subprocess.run(
  11. ["git"] + command, capture_output=True, text=True, check=True
  12. )
  13. return result.stdout.strip().split("\n")
  14. except subprocess.CalledProcessError as e:
  15. print(f"❌ Error: Git command failed. Make sure you're in a Git repository.")
  16. print(f" Command: git {' '.join(command)}")
  17. print(f" Error: {e.stderr}")
  18. raise SystemExit(1)
  19. except FileNotFoundError:
  20. print("❌ Error: Git is not installed or not found in PATH.")
  21. raise SystemExit(1)
  22. def parse_existing_references():
  23. """Parse existing CONTRIBUTORS.md to extract manual Reference values."""
  24. references = {}
  25. if not contributors_file.exists():
  26. return references
  27. try:
  28. content = contributors_file.read_text(encoding="utf-8")
  29. lines = content.split("\n")
  30. for line in lines:
  31. # Skip header, separator, and empty lines
  32. if not line.strip() or line.startswith("#") or "---" in line or line.startswith("This file"):
  33. continue
  34. # Check if it's a table row (starts with |)
  35. if line.startswith("|") and line.count("|") >= 6:
  36. parts = [p.strip() for p in line.split("|")]
  37. # parts[0] is empty (before first |), parts[1] is Name, parts[6] is Reference
  38. if len(parts) >= 7:
  39. name = parts[1]
  40. reference = parts[6]
  41. # Only store non-empty references
  42. if name and reference:
  43. references[name] = reference
  44. except Exception:
  45. # If parsing fails, just return empty dict
  46. pass
  47. return references
  48. def get_contributors():
  49. """Extract contributors from git log."""
  50. log_lines = run_git(["log", "--format=%aN|%aE|%ad", "--date=short"])
  51. contributors = defaultdict(lambda: {"emails": set(), "first": None, "last": None, "count": 0})
  52. for line in log_lines:
  53. if "|" not in line:
  54. continue
  55. name, email, date = line.split("|")
  56. name, email = name.strip(), email.strip()
  57. info = contributors[name]
  58. info["emails"].add(email)
  59. info["count"] += 1
  60. if not info["first"] or date < info["first"]:
  61. info["first"] = date
  62. if not info["last"] or date > info["last"]:
  63. info["last"] = date
  64. return contributors
  65. def generate_table(contributors, existing_references):
  66. """Generate markdown table, preserving manual Reference values."""
  67. header = [
  68. "# 🌍 Project Contributors",
  69. "",
  70. "This file lists all contributors automatically extracted from Git commit history.",
  71. "Do not edit manually — run `python scripts/update_contributors.py` to refresh.",
  72. "",
  73. "| Name | Email | Contributions | First Commit | Last Commit | Reference |",
  74. "|------|--------|----------------|---------------|--------------|-----------|",
  75. ]
  76. rows = []
  77. for name, info in sorted(contributors.items(), key=lambda x: x[0].lower()):
  78. emails = ", ".join(sorted(info["emails"]))
  79. # Preserve existing reference if it exists, otherwise leave empty
  80. reference = existing_references.get(name, "")
  81. # Use proper pluralization for commits
  82. commit_text = f"{info['count']} commit" if info['count'] == 1 else f"{info['count']} commits"
  83. rows.append(f"| {name} | {emails} | {commit_text} | {info['first']} | {info['last']} | {reference} |")
  84. return "\n".join(header + rows) + "\n"
  85. def main():
  86. existing_references = parse_existing_references()
  87. contributors = get_contributors()
  88. md = generate_table(contributors, existing_references)
  89. contributors_file.write_text(md, encoding="utf-8")
  90. print(f"✅ Updated {contributors_file} with {len(contributors)} contributors.")
  91. if __name__ == "__main__":
  92. main()