Several scripts construct Git repository paths with os.path and pass
command strings through a shell.  On Windows, this can produce
backslash-separated paths in ChangeLog entries, while shell parsing
makes subprocess arguments platform-dependent.

Use posixpath for repository paths, pass argument lists directly to
subprocess functions, and run Python helpers with sys.executable.
Also remove unreachable code from find_changelog.

contrib/ChangeLog:

        * gcc-changelog/git_commit.py: Use posixpath for repository paths.
        * git-backport.py: Pass argument lists to subprocess.run and use
        sys.executable.
        * git-commit-mklog.py: Pass an argument list to subprocess.run.
        * git-fix-changelog.py: Pass argument lists to subprocess functions
        and use sys.executable.
        * mklog.py: Use posixpath for repository paths.  Pass argument lists
        to subprocess functions.  Remove unreachable code.

Signed-off-by: Oleg Tolmatcev <[email protected]>
---
 contrib/gcc-changelog/git_commit.py |  9 +++++----
 contrib/git-backport.py             |  5 +++--
 contrib/git-commit-mklog.py         |  6 +++---
 contrib/git-fix-changelog.py        | 15 ++++++++-------
 contrib/mklog.py                    | 18 ++++++++++--------
 5 files changed, 29 insertions(+), 24 deletions(-)

I don't have write access BTW.

diff --git a/contrib/gcc-changelog/git_commit.py 
b/contrib/gcc-changelog/git_commit.py
index 70732c8478..0d7e8f30ed 100755
--- a/contrib/gcc-changelog/git_commit.py
+++ b/contrib/gcc-changelog/git_commit.py
@@ -20,6 +20,7 @@
 
 import difflib
 import os
+import posixpath
 import re
 import sys
 from collections import defaultdict
@@ -620,7 +621,7 @@ class GitCommit:
     def check_file_patterns(self):
         for entry in self.changelog_entries:
             for pattern in entry.file_patterns:
-                name = os.path.join(entry.folder, pattern)
+                name = posixpath.join(entry.folder, pattern)
                 if not [name.startswith(pr) for pr in wildcard_prefixes]:
                     msg = 'unsupported wildcard prefix'
                     self.errors.append(Error(msg, name))
@@ -707,14 +708,14 @@ class GitCommit:
             assert not entry.folder.endswith('/')
             for file in entry.files:
                 if not self.is_changelog_filename(file):
-                    item = os.path.join(entry.folder, file)
+                    item = posixpath.join(entry.folder, file)
                     if item in mentioned_files:
                         msg = 'same file specified multiple times'
                         self.errors.append(Error(msg, file))
                     else:
                         mentioned_files.add(item)
             for pattern in entry.file_patterns:
-                mentioned_patterns.append(os.path.join(entry.folder, pattern))
+                mentioned_patterns.append(posixpath.join(entry.folder, 
pattern))
 
         cand = [x[0] for x in self.info.modified_files
                 if not self.is_changelog_filename(x[0])]
@@ -787,7 +788,7 @@ class GitCommit:
     def check_for_correct_changelog(self):
         for entry in self.changelog_entries:
             for file in entry.files:
-                full_path = os.path.join(entry.folder, file)
+                full_path = posixpath.join(entry.folder, file)
                 changelog_location = self.get_changelog_by_path(full_path)
                 if changelog_location != entry.folder:
                     msg = 'wrong ChangeLog location "%s", should be "%s"'
diff --git a/contrib/git-backport.py b/contrib/git-backport.py
index fd3be51c32..f4743df605 100755
--- a/contrib/git-backport.py
+++ b/contrib/git-backport.py
@@ -22,6 +22,7 @@
 import argparse
 import os
 import subprocess
+import sys
 
 script_folder = os.path.dirname(os.path.abspath(__file__))
 fixup_script = os.path.join(script_folder, 'git-fix-changelog.py')
@@ -31,5 +32,5 @@ if __name__ == '__main__':
     parser.add_argument('revision', help='Revision')
     args = parser.parse_args()
 
-    subprocess.run('git cherry-pick -x %s' % args.revision, shell=True)
-    subprocess.run(fixup_script, shell=True)
+    subprocess.run(['git', 'cherry-pick', '-x', args.revision])
+    subprocess.run([sys.executable, fixup_script])
diff --git a/contrib/git-commit-mklog.py b/contrib/git-commit-mklog.py
index e607979c43..bdbc3ea9e6 100755
--- a/contrib/git-commit-mklog.py
+++ b/contrib/git-commit-mklog.py
@@ -54,7 +54,7 @@ if __name__ == '__main__':
 
     if args.co:
         for author in args.co.split(','):
-            unknown_args.append(f'--trailer "Co-Authored-By: {author}"')
+            unknown_args += ['--trailer', f'Co-Authored-By: {author}']
 
-    commit_args = ' '.join(unknown_args)
-    subprocess.run(f'git commit {commit_args}', shell=True, env=myenv)
+    commit_args = ['git', 'commit', *unknown_args]
+    subprocess.run(commit_args, env=myenv)
diff --git a/contrib/git-fix-changelog.py b/contrib/git-fix-changelog.py
index 74fa18cc96..05718320c8 100755
--- a/contrib/git-fix-changelog.py
+++ b/contrib/git-fix-changelog.py
@@ -25,6 +25,7 @@
 import argparse
 import os
 import subprocess
+import sys
 import tempfile
 
 DESCRIPTION = 'Fix up ChangeLog of the current commit.'
@@ -54,13 +55,13 @@ if __name__ == '__main__':
     args = parser.parse_args()
 
     # Update commit message if change for a .cc file was taken
-    r = subprocess.run(f'{verify_script} HEAD', shell=True, encoding='utf8',
-                       stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    r = subprocess.run([sys.executable, verify_script, 'HEAD'],
+                       encoding='utf8', stdout=subprocess.PIPE,
+                       stderr=subprocess.PIPE)
     if r.returncode != 0:
         lines = r.stdout.splitlines()
-        cmd = 'git show -s --format=%B'
-        commit_message = subprocess.check_output(cmd, shell=True,
-                                                 encoding='utf8').strip()
+        commit_message = subprocess.check_output(
+            ['git', 'show', '-s', '--format=%B'], encoding='utf8').strip()
         commit_message = commit_message.splitlines()
 
         # Parse the following lines:
@@ -84,8 +85,8 @@ if __name__ == '__main__':
                                              delete=False) as w:
                 w.write('\n'.join(commit_message))
                 w.close()
-                subprocess.check_output(f'git commit --amend -F {w.name}',
-                                        shell=True, encoding='utf8')
+                subprocess.check_output(['git', 'commit', '--amend', '-F',
+                                         w.name], encoding='utf8')
                 os.unlink(w.name)
                 print(f'Commit message updated: {replaced} file(s) renamed.')
         else:
diff --git a/contrib/mklog.py b/contrib/mklog.py
index fa0b127386..ab1b8eba37 100755
--- a/contrib/mklog.py
+++ b/contrib/mklog.py
@@ -30,6 +30,7 @@ import argparse
 import datetime
 import json
 import os
+import posixpath
 import re
 import subprocess
 import sys
@@ -105,12 +106,11 @@ root = os.path.dirname(os.path.dirname(script_folder))
 def find_changelog(path):
     folder = os.path.split(path)[0]
     while True:
-        if os.path.exists(os.path.join(root, folder, 'ChangeLog')):
+        if os.path.exists(posixpath.join(root, folder, 'ChangeLog')):
             return folder
         folder = os.path.dirname(folder)
         if folder == '':
             return folder
-    raise AssertionError()
 
 
 def extract_function_name(line):
@@ -256,7 +256,7 @@ def generate_changelog(data, no_functions=False, 
fill_pr_titles=False,
     # sort ChangeLog so that 'testsuite' is at the end
     for changelog in sorted(changelog_list, key=lambda x: 'testsuite' in x):
         files = changelogs[changelog]
-        out += '%s:\n' % os.path.join(changelog, 'ChangeLog')
+        out += '%s:\n' % posixpath.join(changelog, 'ChangeLog')
         out += '\n'
         # new and deleted files should be at the end
         for file in sorted(files, key=sort_changelog_files):
@@ -336,16 +336,18 @@ def generate_changelog(data, no_functions=False, 
fill_pr_titles=False,
 
 def update_copyright(data):
     current_timestamp = datetime.datetime.now().strftime('%Y-%m-%d')
-    username = subprocess.check_output('git config user.name', shell=True,
-                                       encoding='utf8').strip()
-    email = subprocess.check_output('git config user.email', shell=True,
-                                    encoding='utf8').strip()
+    username = subprocess.check_output(
+        ['git', 'config', 'user.name'], encoding='utf8'
+    ).strip()
+    email = subprocess.check_output(
+        ['git', 'config', 'user.email'], encoding='utf8'
+    ).strip()
 
     changelogs = set()
     diff = PatchSet(data)
 
     for file in diff:
-        changelog = os.path.join(find_changelog(file.path), 'ChangeLog')
+        changelog = posixpath.join(find_changelog(file.path), 'ChangeLog')
         if changelog not in changelogs:
             changelogs.add(changelog)
             with open(changelog) as f:
-- 
2.56.0.windows.1

Reply via email to