Skip to content

Commit f7f4602

Browse files
authored
fix: prevent unnecessary docs.json updates when content unchanged (#30)
- Compare actual data structures instead of string representations - Preserve existing file's trailing newline convention - Show friendly '✨ No changes needed' message when content is already up to date - Fixes #29: avoids creating PRs that only add newlines
1 parent 5bbbcf7 commit f7f4602

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/mdxify/navigation.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -381,29 +381,38 @@ def update_docs_json(
381381
return False
382382

383383
# Check if content has actually changed before writing
384-
# This prevents triggering pre-commit hooks when content is identical
385-
386-
# Use sort_keys=True for consistent JSON serialization
387-
new_content = json.dumps(docs_config, indent=2, sort_keys=True) + "\n"
388-
389-
# Read existing content for comparison
390-
# Re-parse and re-serialize to ensure consistent format for comparison
384+
# This prevents triggering unnecessary file changes
385+
386+
# Read existing content first to preserve its exact format
391387
try:
392388
with open(docs_json_path, "r") as f:
389+
existing_raw_content = f.read()
390+
# Also parse it to compare data structures
391+
f.seek(0)
393392
existing_data = json.load(f)
394-
existing_content = json.dumps(existing_data, indent=2, sort_keys=True) + "\n"
395393
except (FileNotFoundError, json.JSONDecodeError):
396-
existing_content = ""
397-
398-
# Only write if content has changed
399-
if new_content != existing_content:
400-
with open(docs_json_path, "w") as f:
401-
f.write(new_content)
402-
if isinstance(container_info, tuple) and container_info[1] is not None:
403-
print(f"Updated {docs_json_path} - replaced placeholder with {len(navigation_pages)} entries")
404-
else:
405-
print(f"Updated {docs_json_path} - {len(navigation_pages)} entries")
394+
existing_raw_content = ""
395+
existing_data = {}
396+
397+
# Compare the data structures (not the string representations)
398+
# This handles differences in formatting, whitespace, etc.
399+
if docs_config == existing_data:
400+
# No actual changes to the data
401+
print(f"✨ No changes needed for {docs_json_path} - content already up to date")
402+
return True
403+
404+
# Content has changed, so we need to write it
405+
# Preserve the existing file's trailing newline convention if it exists
406+
new_content = json.dumps(docs_config, indent=2, sort_keys=True)
407+
if existing_raw_content and existing_raw_content.endswith("\n"):
408+
new_content += "\n"
409+
410+
with open(docs_json_path, "w") as f:
411+
f.write(new_content)
412+
413+
if isinstance(container_info, tuple) and container_info[1] is not None:
414+
print(f"Updated {docs_json_path} - replaced placeholder with {len(navigation_pages)} entries")
406415
else:
407-
print(f"No changes needed for {docs_json_path} - {len(navigation_pages)} entries already up to date")
408-
416+
print(f"Updated {docs_json_path} - {len(navigation_pages)} entries")
417+
409418
return True

0 commit comments

Comments
 (0)