Skip to content

Commit b3ca490

Browse files
ousamabenyounessafishamsi
authored andcommitted
fix(report): use a portable basename in the GRAPH_REPORT header (#2682)
1 parent 5d8d611 commit b3ca490

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎graphify/report.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,34 @@
22
from __future__ import annotations
33
import re
44
from datetime import date
5+
from pathlib import Path
56
import networkx as nx
67

78

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+
833
def _safe_community_name(label: str) -> str:
934
"""Mirrors export.safe_name so community hub filenames and report wikilinks always agree."""
1035
cleaned = re.sub(r'[\\/*?:"<>|#^[\]]', "", label.replace("\r\n", " ").replace("\r", " ").replace("\n", " ")).strip()
@@ -101,7 +126,7 @@ def generate(
101126
inf_avg = round(sum(inf_scores) / len(inf_scores), 2) if inf_scores else None
102127

103128
lines = [
104-
f"# Graph Report - {root} ({today})",
129+
f"# Graph Report - {_portable_root_label(root)} ({today})",
105130
"",
106131
"## Corpus Check",
107132
]

‎tests/test_report.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ def test_report_shows_raw_cohesion_scores():
6363
assert "⚠" not in report
6464

6565

66+
def test_report_header_does_not_embed_host_absolute_path():
67+
"""#2628 / #2598: the header must not bake the generator host absolute path
68+
into GRAPH_REPORT.md — it labels with the project directory basename so the
69+
same graph produces the same bytes on any machine."""
70+
G, communities, cohesion, labels, gods, surprises, detection, tokens = make_inputs()
71+
report = generate(G, communities, cohesion, labels, gods, surprises, detection,
72+
tokens, "/Users/mike/dev/apps/secretproj")
73+
header = report.splitlines()[0]
74+
assert "/Users/mike" not in header
75+
assert "secretproj" in header
76+
77+
78+
def test_portable_root_label():
79+
from graphify.report import _portable_root_label
80+
# Absolute paths collapse to the basename on both POSIX and Windows.
81+
assert _portable_root_label("/Users/mike/dev/apps/proj") == "proj"
82+
assert _portable_root_label(r"C:\Users\mike\dev\proj") == "proj"
83+
# A trailing slash still yields the directory name, not an empty label.
84+
assert _portable_root_label("/Users/mike/dev/proj/") == "proj"
85+
# Relative names pass through unchanged.
86+
assert _portable_root_label("./project") == "project"
87+
assert _portable_root_label("project") == "project"
88+
89+
6690
# --- work-memory lessons section ----------------------------------------------
6791

6892
def test_report_work_memory_section_present_with_overlay_and_dead_ends():

0 commit comments

Comments
 (0)