2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-03-30 08:39:50 +01:00

git-compatible editor field for updated page

This commit is contained in:
2024-12-27 16:02:38 +00:00
parent 5ee26af02a
commit 2b97c8f783
2 changed files with 37 additions and 5 deletions

View File

@@ -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: