diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2024-12-27 16:02:38 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2024-12-27 16:02:38 +0000 |
commit | 2b97c8f7839e127f80d367810a2578cb920c5c29 (patch) | |
tree | cb35192d680f7cb68b7588ce06b4c856e1f26ce7 /core/utils.py | |
parent | 5ee26af02ad53bbb5ddcd2fa7f7c9fca29bb0f72 (diff) | |
download | troggle-2b97c8f7839e127f80d367810a2578cb920c5c29.tar.gz troggle-2b97c8f7839e127f80d367810a2578cb920c5c29.tar.bz2 troggle-2b97c8f7839e127f80d367810a2578cb920c5c29.zip |
git-compatible editor field for updated page
Diffstat (limited to 'core/utils.py')
-rw-r--r-- | core/utils.py | 28 |
1 files changed, 25 insertions, 3 deletions
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 <john.doe@example.com>" + + 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 <automaton@potatohut.expo>" 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: |