#!/usr/bin/env python3 """Отчёт о конфликтах слияния в git-репозитории.""" from __future__ import annotations import argparse import json import shutil import subprocess from pathlib import Path MARKERS = ("<<<<<<<", "=======", ">>>>>>>") def git(repo: Path, *args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( ["git", "-C", str(repo), *args], capture_output=True, text=True, encoding="utf-8", errors="replace", ) def count_markers(path: Path) -> dict[str, int]: counts = {marker: 0 for marker in MARKERS} try: with path.open("r", encoding="utf-8", errors="replace") as handle: for line in handle: stripped = line.lstrip() for marker in MARKERS: if stripped.startswith(marker): counts[marker] += 1 except OSError: pass return counts def main() -> int: parser = argparse.ArgumentParser(description="Показать конфликты слияния") parser.add_argument("--repo", default=".", help="Путь к репозиторию") parser.add_argument("--json", type=Path, help="Сохранить отчёт в JSON") args = parser.parse_args() if not shutil.which("git"): print("Не найден git.") return 2 repo = Path(args.repo).expanduser().resolve() if not repo.is_dir(): print(f"Папка не найдена: {repo}") return 2 check = git(repo, "rev-parse", "--is-inside-work-tree") if check.returncode != 0: print(f"Это не git-репозиторий: {repo}") return 2 result = git(repo, "diff", "--name-only", "--diff-filter=U") conflicts = [line.strip() for line in result.stdout.splitlines() if line.strip()] report = [] for name in conflicts: counts = count_markers(repo / name) report.append({"file": name, "markers": counts, "total": sum(counts.values())}) if args.json: payload = {"repo": str(repo), "conflicts": report} args.json.write_text( json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) if not report: print("Конфликтов не найдено.") return 0 print(f"Конфликтов: {len(report)}") for item in report: print(f" {item['file']} маркеров: {item['total']}") print("После ручного разрешения выполните git add и git commit.") return 0 if __name__ == "__main__": raise SystemExit(main())