Fix accidental removal of shebangs when restoring a script
This commit is contained in:
@@ -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")
|
||||||
restored = []
|
lines = raw.splitlines(keepends=True)
|
||||||
skipping = True
|
|
||||||
for line in raw.splitlines(keepends=True):
|
restored = None
|
||||||
# header ends at first blank line after the dashed line block
|
|
||||||
if skipping:
|
# Detect a mirro header only if the file begins with the header line
|
||||||
if line.strip() == "" and restored == []:
|
if lines and lines[0].startswith(
|
||||||
# allow only after header
|
"# ---------------------------------------------------"
|
||||||
continue
|
):
|
||||||
if line.startswith("#") or line.strip() == "":
|
restored = []
|
||||||
continue
|
i = 0
|
||||||
skipping = False
|
# Skip until the first blank line AFTER the header block
|
||||||
restored.append(line)
|
while i < len(lines):
|
||||||
|
line = lines[i]
|
||||||
|
i += 1
|
||||||
|
# the header ends when we hit a blank line
|
||||||
|
if line.strip() == "":
|
||||||
|
break
|
||||||
|
# i now points to the first real line of the original file
|
||||||
|
restored = lines[i:]
|
||||||
|
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:
|
||||||
|
|||||||
Reference in New Issue
Block a user