git comitting allowing files to be comitted in different directories.

This commit is contained in:
Martin Green
2023-07-05 21:10:05 +01:00
parent ffed6e3ba6
commit ad37a82713

View File

@@ -102,7 +102,9 @@ def write_and_commit(files, message):
These need refactoring These need refactoring
""" """
print(files)
git = settings.GIT git = settings.GIT
commands = []
try: try:
for filepath, content, encoding in files: for filepath, content, encoding in files:
cwd = filepath.parent cwd = filepath.parent
@@ -126,10 +128,13 @@ def write_and_commit(files, message):
raise WriteAndCommitError( raise WriteAndCommitError(
f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this." f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this."
) )
cmd_diff = [git, "diff", filename]
cp_diff = subprocess.run([git, "diff", filename], cwd=cwd, capture_output=True, text=True) cp_diff = subprocess.run(cmd_diff, cwd=cwd, capture_output=True, text=True)
commands.append(cmd_diff)
if cp_diff.returncode == 0: if cp_diff.returncode == 0:
cp_add = subprocess.run([git, "add", filename], cwd=cwd, capture_output=True, text=True) cmd_add = [git, "add", filename]
cp_add = subprocess.run(cmd_add, cwd=cwd, capture_output=True, text=True)
commands.append(cmd_add)
if cp_add.returncode != 0: if cp_add.returncode != 0:
msgdata = ( msgdata = (
"Ask a nerd to fix this.\n\n" "Ask a nerd to fix this.\n\n"
@@ -145,18 +150,35 @@ def write_and_commit(files, message):
) )
else: else:
print(f"No change {filepath}") print(f"No change {filepath}")
filenames = [filepath.name for filepath, content, encoding in files] filepaths = [filepath for filepath, content, encoding in files]
subprocess.run([git, "commit"] + filenames + ["-m", message], cwd=cwd, capture_output=True, text=True) cmd_commit = [git, "commit"] + filepaths + ["-m", message]
cp_status = subprocess.run([git, "status"] + filenames, cwd=cwd, capture_output=True, text=True) cm_status = subprocess.run(cmd_commit, cwd=cwd, capture_output=True, text=True)
#This produces return code = 1 if it commits OK, but when the repo still needs to be pushed to origin/expoweb commands.append(cmd_commit)
if not cp_status.stdout or len(cp_status.stdout) < 2 or cp_status.stdout.split("\n")[-2] != "nothing to commit, working tree clean": if cm_status == 0:
msgdata = ( msgdata = (
"Commands: " + str(commands) +
"Ask a nerd to fix this.\n\n" "Ask a nerd to fix this.\n\n"
+ cp_status.stderr + "Stderr: " + cp_status.stderr
+ "\n\n" + "\n\n"
+ cp_status.stdout + "Stdout: " + cp_status.stdout
+ "\n\nreturn code: " + "\n\nreturn code: " + str(cp_status.returncode)
+ str(cp_status.returncode) )
raise WriteAndCommitError(
f"Error committing. Edits saved, added to git, but NOT committed.\n\n"
+ msgdata
)
cmd_status = [git, "status"] + filepaths
cp_status = subprocess.run(cmd_status, cwd=cwd, capture_output=True, text=True)
commands.append(cp_status)
#This produces return code = 1 if it commits OK, but when the repo still needs to be pushed to origin/expoweb
if (not cp_status.stdout) or len(cp_status.stdout) < 2 or cp_status.stdout.split("\n")[-2] != "nothing to commit, working tree clean":
msgdata = (
str(commands) +
"Ask a nerd to fix this.\n\n"
+ "Stderr: " + cp_status.stderr
+ "\n\n"
+ "Stdout: " + cp_status.stdout
+ "\n\nreturn code: " + str(cp_status.returncode)
) )
raise WriteAndCommitError( raise WriteAndCommitError(
f"Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n" f"Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n"