git-ident-filter.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. #ident "@(#)$Format:LocalFoodAI:app.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$"
  3. import sys
  4. import os
  5. import subprocess
  6. import re
  7. from datetime import datetime
  8. # Force LF-only line endings on Windows for stdin and stdout to prevent automatic CRLF translation
  9. if hasattr(sys.stdin, 'reconfigure'):
  10. sys.stdin.reconfigure(newline='\n')
  11. if hasattr(sys.stdout, 'reconfigure'):
  12. sys.stdout.reconfigure(newline='\n')
  13. # Detect execution mode (clean or smudge) passed by Git
  14. mode = sys.argv[1] if len(sys.argv) > 1 else "smudge"
  15. def get_git_info(file_path):
  16. """Retrieves commit metadata for the specific file using git log, or falls back to system context."""
  17. try:
  18. # 1. Query git log for the last commit details of the specific file
  19. cmd = [
  20. "git", "log", "-1",
  21. "--date=format:%Y/%m/%d %H:%M:%S",
  22. "--format=%an|%ae|%ad|%cn|%ce|%cd|%H|%D|%N",
  23. "--", file_path
  24. ]
  25. out = subprocess.check_output(cmd, stderr=subprocess.DEVNULL).decode('utf-8', errors='ignore').strip()
  26. if out:
  27. parts = out.split('|')
  28. if len(parts) == 9:
  29. return parts
  30. except Exception:
  31. pass
  32. # 2. Fallback: Query local Git configuration if file is not committed yet
  33. try:
  34. author_name = subprocess.check_output(["git", "config", "user.name"], stderr=subprocess.DEVNULL).decode('utf-8', errors='ignore').strip() or "system"
  35. author_email = subprocess.check_output(["git", "config", "user.email"], stderr=subprocess.DEVNULL).decode('utf-8', errors='ignore').strip() or "system@mail.com"
  36. except Exception:
  37. author_name = "system"
  38. author_email = "system@mail.com"
  39. now_str = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
  40. return [author_name, author_email, now_str, author_name, author_email, now_str, "Not Committed Yet", "local", "none"]
  41. if mode == "clean":
  42. # CLEAN Mode: Replaces any smudged $Format:LocalFoodAI:app.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$ tag back to the standard neutral git representation
  43. content = sys.stdin.read()
  44. # Non-greedy substitution to restore standard placeholder format for Git storage
  45. cleaned = re.sub(
  46. r'\$Format:LocalFoodAI:app.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$)?[^$]*?\$',
  47. r'$Format:LocalFoodAI:app.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$',
  48. content
  49. )
  50. sys.stdout.write(cleaned)
  51. else:
  52. # SMUDGE Mode: Dynamically injects actual project, file path, and commit metadata into the file
  53. try:
  54. # Get absolute path of repository to find project directory name
  55. toplevel = subprocess.check_output(["git", "rev-parse", "--show-toplevel"], stderr=subprocess.DEVNULL).decode().strip()
  56. project_name = os.path.basename(toplevel)
  57. # Get the relative path of the file being smudged
  58. file_name = sys.argv[2] if len(sys.argv) > 2 else "unknown_file"
  59. # Read the file content sent by Git on stdin
  60. content = sys.stdin.read()
  61. # Query git log metadata or local fallbacks
  62. info = get_git_info(file_name)
  63. # Format replacement string using LocalFoodAI and app.py
  64. replacement = f"$Format:LocalFoodAI:app.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$"
  65. # Regex replacement targeting the dynamic format placeholders
  66. smudged = re.sub(
  67. r'\$Format:LocalFoodAI:app.py:%an:%ae:%ad:%cn:%ce:%cd:%H:%D:%N$',
  68. replacement,
  69. content
  70. )
  71. sys.stdout.write(smudged)
  72. except Exception:
  73. # Security fallback: If executed outside Git repo, write stream unchanged
  74. sys.stdout.write(sys.stdin.read())