From 2b97c8f7839e127f80d367810a2578cb920c5c29 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Fri, 27 Dec 2024 16:02:38 +0000 Subject: git-compatible editor field for updated page --- core/utils.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'core/utils.py') diff --git a/core/utils.py b/core/utils.py index a958d36..50746eb 100644 --- a/core/utils.py +++ b/core/utils.py @@ -233,14 +233,35 @@ nothing to commit, working tree clean f"CANNOT git COMMIT on server for this file {filename}. Subprocess error. Edits not saved.\nAsk a nerd to fix this." ) +def g_string(author_string): -def write_and_commit(files, message): + # Regular expression for a git-compatible author string + # valid example "John Doe " + + author_regex = re.compile(r'^[a-zA-Z][\w\s\.\-]* <[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}>$') + + if author_regex.match(author_string): + print(f"Valid git-compatible author string: {author_string}") + return author_string + else: + editor = author_string.replace("@","_at_") + editor = re.sub('[^0-9a-zA-Z_]+', '*', editor) + editor += f" <{editor}@potatohut.expo>" + print(f"Not git-compatible author string, replacing as '{editor}'") + return editor + +def write_and_commit(files, message, editor=None): """Writes the content to the filepath and adds and commits the file to git. If this fails, a WriteAndCommitError is raised. These need refactoring """ git = settings.GIT commands = [] + if editor: + editor = g_string(editor) + else: + # cannot happen as form verification has this as an obligatory field + editor = "Automatic " try: for filepath, content, encoding in files: cwd = filepath.parent @@ -296,8 +317,9 @@ def write_and_commit(files, message): else: print(f"No change {filepath}") filepaths = [filepath for filepath, content, encoding in files] - message = message + " " + str(filepaths) - cmd_commit = [git, "commit", "-m", message] + # message = message + " " + str(filepaths) + print([git, "commit", "-m", message, "--author", f"{editor}"]) + cmd_commit = [git, "commit", "-m", message, "--author", f"{editor}"] cm_status = subprocess.run(cmd_commit, cwd=cwd, capture_output=True, text=True) commands.append(cmd_commit) if cm_status.returncode != 0: -- cgit v1.2.3