From 7aad7f27d9f24e9b308156e9afa4337335235187 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 24 Jul 2026 03:34:59 +0200 Subject: [PATCH] Fix destructive file header detection (#835) Deleted: broad unanchored header matching that removed source content. --- actions/__init__.py | 2 +- actions/update_file_headers.py | 24 +++++++++--- tests/test_file_headers.py | 67 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 6 deletions(-) diff --git a/actions/__init__.py b/actions/__init__.py index 7e07189..0ade891 100644 --- a/actions/__init__.py +++ b/actions/__init__.py @@ -28,4 +28,4 @@ # ├── test_summarize_pr.py # └── ... -__version__ = "0.2.36" +__version__ = "0.2.37" diff --git a/actions/update_file_headers.py b/actions/update_file_headers.py index 52f7689..fec0f9a 100644 --- a/actions/update_file_headers.py +++ b/actions/update_file_headers.py @@ -120,18 +120,32 @@ def update_file(file_path, prefix, block_start, block_end, base_header): # Check for special first line special_line_index = -1 - if lines and (lines[0].startswith("#!") or lines[0].startswith(" 1 and re.match(encoding_cookie, lines[1]): + special_line_index = 1 + prefix_lines.append(lines[1]) start_idx = special_line_index + 1 if special_line_index >= 0 else 0 end_idx = min(start_idx + 5, len(lines)) # Look in first few lines - # Find existing header - header_index = next( - (i for i in range(start_idx, end_idx) if re.search(r"AGPL-3.0|CONFIDENTIAL|Ultralytics|©\s*\d{4}", lines[i])), - -1, + # An existing header must be the first non-empty line and use the file's comment syntax. + candidate_index = next((i for i in range(start_idx, end_idx) if lines[i].strip()), -1) + candidate = lines[candidate_index].strip() if candidate_index >= 0 else "" + comment_start = (prefix or block_start or "# ").strip() + known_header = candidate.startswith(comment_start) and re.search( + r"AGPL-3\.0 License|CONFIDENTIAL: Unauthorized use|©\s*2014[–-]\d{4}\s+Ultralytics Inc\.|" + r"Ultralytics Inc\..*Copyright ©\s*2014[–-]\d{4}\s*-\s*CONFIDENTIAL\s*-", + candidate, ) + header_index = candidate_index if candidate == formatted_header.strip() or known_header else -1 # Add the formatted header to prefix lines prefix_lines.append(formatted_header) diff --git a/tests/test_file_headers.py b/tests/test_file_headers.py index a73a28d..f306386 100644 --- a/tests/test_file_headers.py +++ b/tests/test_file_headers.py @@ -76,6 +76,73 @@ def test_update_file_with_shebang(): assert "print('Hello World')" in content +def test_update_file_preserves_early_ultralytics_source(): + """Test that Ultralytics references in source code are not mistaken for headers.""" + with TemporaryDirectory() as tmp_dir: + tmp_path = Path(tmp_dir) + docstring_file = tmp_path / "api.py" + docstring_file.write_text('"""Ultralytics Platform API client."""\n\nVALUE = 1\n') + import_file = tmp_path / "__init__.py" + import_file.write_text("from ultralytics import YOLO\n") + + for test_file, spacing in ((docstring_file, ""), (import_file, "\n")): + original = test_file.read_text() + assert update_file(test_file, "# ", None, None, "Ultralytics 🚀 Test Header") is True + assert test_file.read_text() == f"# Ultralytics 🚀 Test Header\n{spacing}{original}" + + +def test_update_file_with_lowercase_doctype(): + """Test that Prettier's lowercase HTML doctype remains first.""" + with TemporaryDirectory() as tmp_dir: + test_file = Path(tmp_dir) / "index.html" + test_file.write_text("\n\n") + + assert update_file(test_file, None, "", "Ultralytics 🚀 Test Header") is True + assert test_file.read_text().startswith("\n\n\n") + + +def test_update_file_with_bom_xml_declaration(): + """Test that a BOM-prefixed XML declaration remains first.""" + with TemporaryDirectory() as tmp_dir: + test_file = Path(tmp_dir) / "layout.xml" + test_file.write_text('\ufeff\n\n') + + assert update_file(test_file, None, "", "Ultralytics 🚀 Test Header") is True + assert test_file.read_text().startswith( + '\ufeff\n\n\n' + ) + + +def test_update_file_with_shebang_and_encoding_cookie(): + """Test that a Python encoding cookie remains on line two and the header stays idempotent.""" + with TemporaryDirectory() as tmp_dir: + test_file = Path(tmp_dir) / "script.py" + test_file.write_text( + "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n# Ultralytics 🚀 AGPL-3.0 License\n\nprint('hello')\n" + ) + + assert update_file(test_file, "# ", None, None, "Ultralytics 🚀 Test Header") is True + assert test_file.read_text().startswith( + "#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n# Ultralytics 🚀 Test Header\n\n" + ) + assert update_file(test_file, "# ", None, None, "Ultralytics 🚀 Test Header") is False + + +def test_update_file_replaces_legacy_private_header(): + """Test that the previously emitted private header is replaced rather than duplicated.""" + with TemporaryDirectory() as tmp_dir: + test_file = Path(tmp_dir) / "app.py" + test_file.write_text( + "# Ultralytics Inc. 🚀 Copyright © 2014-2025 - CONFIDENTIAL - " + "https://ultralytics.com - All Rights Reserved\n\n" + "print('hello')\n" + ) + + assert update_file(test_file, "# ", None, None, "© 2014-2026 Ultralytics Inc. CONFIDENTIAL") is True + assert test_file.read_text().startswith("# © 2014-2026 Ultralytics Inc. CONFIDENTIAL\n\n") + assert "Copyright" not in test_file.read_text() + + def test_update_file_no_changes(): """Test updating file with no changes needed.""" with TemporaryDirectory() as tmp_dir: