diff --git a/core/utils.py b/core/utils.py index 36f0982..83e2c5b 100644 --- a/core/utils.py +++ b/core/utils.py @@ -102,7 +102,9 @@ def write_and_commit(files, message): These need refactoring """ + print(files) git = settings.GIT + commands = [] try: for filepath, content, encoding in files: cwd = filepath.parent @@ -126,10 +128,13 @@ def write_and_commit(files, message): raise WriteAndCommitError( f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this." ) - - cp_diff = subprocess.run([git, "diff", filename], cwd=cwd, capture_output=True, text=True) + cmd_diff = [git, "diff", filename] + cp_diff = subprocess.run(cmd_diff, cwd=cwd, capture_output=True, text=True) + commands.append(cmd_diff) 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: msgdata = ( "Ask a nerd to fix this.\n\n" @@ -145,18 +150,35 @@ def write_and_commit(files, message): ) else: print(f"No change {filepath}") - filenames = [filepath.name for filepath, content, encoding in files] - subprocess.run([git, "commit"] + filenames + ["-m", message], cwd=cwd, capture_output=True, text=True) - cp_status = subprocess.run([git, "status"] + filenames, 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 - if not cp_status.stdout or len(cp_status.stdout) < 2 or cp_status.stdout.split("\n")[-2] != "nothing to commit, working tree clean": + filepaths = [filepath for filepath, content, encoding in files] + cmd_commit = [git, "commit"] + filepaths + ["-m", message] + cm_status = subprocess.run(cmd_commit, cwd=cwd, capture_output=True, text=True) + commands.append(cmd_commit) + if cm_status == 0: msgdata = ( + "Commands: " + str(commands) + "Ask a nerd to fix this.\n\n" - + cp_status.stderr + + "Stderr: " + cp_status.stderr + "\n\n" - + cp_status.stdout - + "\n\nreturn code: " - + str(cp_status.returncode) + + "Stdout: " + cp_status.stdout + + "\n\nreturn code: " + 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( f"Error code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n"