diff options
author | Martin Green <martin.speleo@gmail.com> | 2023-07-05 21:10:05 +0100 |
---|---|---|
committer | Martin Green <martin.speleo@gmail.com> | 2023-07-05 21:10:05 +0100 |
commit | ad37a8271303867ace4e350516b6db9c91e0396e (patch) | |
tree | d278d1b49fdc0567bbe2f35ec590cbf3c2b8e88c /core/utils.py | |
parent | ffed6e3ba60a8908b3bf4d0c12d1587406528462 (diff) | |
download | troggle-ad37a8271303867ace4e350516b6db9c91e0396e.tar.gz troggle-ad37a8271303867ace4e350516b6db9c91e0396e.tar.bz2 troggle-ad37a8271303867ace4e350516b6db9c91e0396e.zip |
git comitting allowing files to be comitted in different directories.
Diffstat (limited to 'core/utils.py')
-rw-r--r-- | core/utils.py | 44 |
1 files changed, 33 insertions, 11 deletions
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) + 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" + + "Stderr: " + cp_status.stderr + + "\n\n" + + "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": + 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" - + 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 code with git on server for this file {filename}. Edits saved, added to git, but NOT committed.\n\n" |