Fix accidental removal of shebangs when restoring a script

This commit is contained in:
2025-11-21 13:54:42 +00:00
parent 6dbadc476a
commit 1cb0bca865

View File

@@ -171,18 +171,31 @@ def main():
# read and strip header # read and strip header
raw = last.read_text(encoding="utf-8", errors="replace") raw = last.read_text(encoding="utf-8", errors="replace")
lines = raw.splitlines(keepends=True)
restored = None
# Detect a mirro header only if the file begins with the header line
if lines and lines[0].startswith(
"# ---------------------------------------------------"
):
restored = [] restored = []
skipping = True i = 0
for line in raw.splitlines(keepends=True): # Skip until the first blank line AFTER the header block
# header ends at first blank line after the dashed line block while i < len(lines):
if skipping: line = lines[i]
if line.strip() == "" and restored == []: i += 1
# allow only after header # the header ends when we hit a blank line
continue if line.strip() == "":
if line.startswith("#") or line.strip() == "": break
continue # i now points to the first real line of the original file
skipping = False restored = lines[i:]
restored.append(line) else:
# No header found — keep file verbatim
restored = lines
restored_text = "".join(restored)
target.write_text(restored_text, encoding="utf-8")
# if header wasn't present, restored = raw # if header wasn't present, restored = raw
if not restored: if not restored: