|
2 | 2 | from __future__ import annotations |
3 | 3 | import re |
4 | 4 | from datetime import date |
| 5 | +from pathlib import Path |
5 | 6 | import networkx as nx |
6 | 7 |
|
7 | 8 |
|
| 9 | +def _portable_root_label(root: str) -> str: |
| 10 | + """Portable label for the report header — the project directory basename. |
| 11 | +
|
| 12 | + GRAPH_REPORT.md is a tracked artifact in practice, so its header must not |
| 13 | + bake the generator host's absolute path into the file: the same graph would |
| 14 | + otherwise produce different bytes on different machines and leak the build |
| 15 | + machine's directory layout into git history (#2628, same class as #2598). |
| 16 | +
|
| 17 | + Taking the basename strips any leading absolute path without touching the |
| 18 | + filesystem, and makes `graphify update .`, `graphify update ./proj`, and |
| 19 | + `graphify update /abs/path/proj` all label the header `proj`. Only the |
| 20 | + degenerate `.`/``/`..` cases need a cwd resolve to recover the real name; |
| 21 | + if even that fails, fall back to the raw value. |
| 22 | + """ |
| 23 | + raw = str(root).replace("\\", "/") |
| 24 | + name = Path(raw).name |
| 25 | + if name in ("", ".", ".."): |
| 26 | + try: |
| 27 | + name = Path(raw).resolve().name |
| 28 | + except (OSError, RuntimeError): |
| 29 | + name = "" |
| 30 | + return name or raw |
| 31 | + |
| 32 | + |
8 | 33 | def _safe_community_name(label: str) -> str: |
9 | 34 | """Mirrors export.safe_name so community hub filenames and report wikilinks always agree.""" |
10 | 35 | cleaned = re.sub(r'[\\/*?:"<>|#^[\]]', "", label.replace("\r\n", " ").replace("\r", " ").replace("\n", " ")).strip() |
@@ -101,7 +126,7 @@ def generate( |
101 | 126 | inf_avg = round(sum(inf_scores) / len(inf_scores), 2) if inf_scores else None |
102 | 127 |
|
103 | 128 | lines = [ |
104 | | - f"# Graph Report - {root} ({today})", |
| 129 | + f"# Graph Report - {_portable_root_label(root)} ({today})", |
105 | 130 | "", |
106 | 131 | "## Corpus Check", |
107 | 132 | ] |
|
0 commit comments