Skip to content

Commit dee786e

Browse files
authored
Prevent duplication and modification of shebang in shell scripts (#1208)
* Fix: Prevent duplication and modification of shebang This changes the behavior when adding a header to a file which previously had no copyright/license header. If the first line of the file starts with a shebang (`#!`), the header is put below the first line instead of above. The previous approach was to include the shebang in the template, but this caused the shebang to be duplicated as the template is prepended without modifying the rest of the file. Since the template was chosen based on the file extension, this could also cause a script with a `.sh` extension and a `#!/bin/bash` shebang to now have a `#!/bin/sh` shebang, potentially causing the script to be executed using a different shell. * Change: Remove shebang from templates Remove workaround of having the shebang in the template for shell scripts as this is now handled within `pontos/updateheader/updateheader.py`. * Add: new test case for files starting with shebang
1 parent 896db7f commit dee786e

8 files changed

Lines changed: 40 additions & 9 deletions

File tree

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/bin/bash
21
# SPDX-FileCopyrightText: <year> <company>
32
#
43
# SPDX-License-Identifier: AGPL-3.0-or-later
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/bin/sh
21
# SPDX-FileCopyrightText: <year> <company>
32
#
43
# SPDX-License-Identifier: AGPL-3.0-or-later
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/bin/bash
21
# SPDX-FileCopyrightText: <year> <company>
32
#
43
# SPDX-License-Identifier: GPL-2.0-or-later
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/bin/sh
21
# SPDX-FileCopyrightText: <year> <company>
32
#
43
# SPDX-License-Identifier: GPL-2.0-or-later
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/bin/bash
21
# SPDX-FileCopyrightText: <year> <company>
32
#
43
# SPDX-License-Identifier: GPL-3.0-or-later
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/bin/sh
21
# SPDX-FileCopyrightText: <year> <company>
32
#
43
# SPDX-License-Identifier: GPL-3.0-or-later

pontos/updateheader/updateheader.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,18 @@ def update_file(
191191
)
192192
if header:
193193
fp.seek(0) # back to beginning of file
194-
rest_of_file = fp.read()
195-
fp.seek(0)
196-
fp.write(header + "\n" + rest_of_file)
194+
first_line = fp.readline()
195+
196+
# first line is a shebang, leave it
197+
if first_line.startswith("#!"):
198+
rest_of_file = fp.read()
199+
fp.seek(0)
200+
fp.write(first_line + header + "\n" + rest_of_file)
201+
else:
202+
rest_of_file = first_line + fp.read()
203+
fp.seek(0)
204+
fp.write(header + "\n" + rest_of_file)
205+
197206
print(f"{file}: Added license header.")
198207
return
199208

tests/updateheader/test_header.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,34 @@ def test_cleanup_file_changed_company(self):
567567
new_content = tmp.read_text(encoding="utf-8")
568568
self.assertEqual(expected_content, new_content)
569569

570+
def test_handle_file_with_shebang(self):
571+
test_content = """#!/bin/bash
572+
""" # noqa: E501
573+
574+
expected_content = f"""#!/bin/bash
575+
# SPDX-FileCopyrightText: {str(datetime.datetime.now().year)} Greenbone AG
576+
#
577+
# SPDX-License-Identifier: GPL-3.0-or-later
578+
579+
""" # noqa: E501
580+
581+
company = "Greenbone AG"
582+
year = str(datetime.datetime.now().year)
583+
license_id = "GPL-3.0-or-later"
584+
585+
with temp_file(content=test_content, name="foo.sh") as tmp:
586+
587+
update_file(
588+
tmp,
589+
year,
590+
license_id,
591+
company,
592+
cleanup=True,
593+
)
594+
595+
new_content = tmp.read_text(encoding="utf-8")
596+
self.assertEqual(expected_content, new_content)
597+
570598

571599
class ParseArgsTestCase(TestCase):
572600
def test_argparser_files(self):

0 commit comments

Comments
 (0)