From ef7c6c9efb3296fedc7a077aba537a76cae90c24 Mon Sep 17 00:00:00 2001 From: Florez Ospina Juan Felipe Date: Wed, 29 May 2024 15:17:09 +0200 Subject: [PATCH] Implemented a git operations module for automated git ops, based on subprocess. --- src/git_ops.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/git_ops.py diff --git a/src/git_ops.py b/src/git_ops.py new file mode 100644 index 0000000..492cca3 --- /dev/null +++ b/src/git_ops.py @@ -0,0 +1,36 @@ +import subprocess + + +def perform_git_operations(hdf5_upload): + status_command = ['git', 'status'] + status = subprocess.run(status_command, capture_output=True, check=True) + + if hdf5_upload: + upload_ext = ['.h5', '.yaml'] + else: + upload_ext = ['.yaml'] + + files_to_add_list = extract_files_to_add(status.stdout, upload_ext) + if files_to_add_list: + add_files_to_git(files_to_add_list) + commit_changes('Updated hdf5 file with yaml review file.') + else: + print("There were no found h5 and yaml files, needing to be saved. This action will not have effect on the review process' commit history.") + +def extract_files_to_add(git_status_output, upload_ext): + files_to_add_list = [] + for line in git_status_output.splitlines(): + tmp = line.decode("utf-8") + if 'modified' in tmp: + if any(ext in tmp for ext in upload_ext): + files_to_add_list.append(tmp.split()[1]) + return files_to_add_list + +def add_files_to_git(files_to_add_list): + add_command = ['git', 'add'] + files_to_add_list + subprocess.run(add_command, capture_output=True, check=True) + +def commit_changes(message): + commit_command = ['git', 'commit', '-m', message] + commit_output = subprocess.run(commit_command, capture_output=True, check=True) + print(commit_output.stdout) \ No newline at end of file