diff --git a/src/git_ops.py b/src/git_ops.py index 492cca3..50be37f 100644 --- a/src/git_ops.py +++ b/src/git_ops.py @@ -33,4 +33,11 @@ def add_files_to_git(files_to_add_list): 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 + print(commit_output.stdout) + +def get_status(): + return subprocess.run(['git','status'],capture_output=True,text=True,check=True) + +def show_current_branch(): + current_branch_command = ['git','branch','--show-current'] + subprocess.run(current_branch_command,capture_output=True,text=True,check=True) \ No newline at end of file diff --git a/src/metadata_review_lib.py b/src/metadata_review_lib.py index 5744a55..ab4a4a0 100644 --- a/src/metadata_review_lib.py +++ b/src/metadata_review_lib.py @@ -10,14 +10,7 @@ import src.g5505_utils as utils import src.hdf5_vis as hdf5_vis import src.hdf5_lib as hdf5_lib import src.git_ops as git_ops -# TODO: incorporate lines 14-18 in git_ops module and refactor code where needed -current_branch_command = ['git','branch','--show-current'] -status_command = ['git','status'] -add_command = lambda add_list: ['git','add'] + add_list -rm_command = lambda add_list: ['git','add'] + add_list -commit_command = lambda message: ['git','commit','-m', message] -#import input_files.config_file as config_file import numpy as np @@ -97,8 +90,8 @@ def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs, restart = F #checkout_review_branch(branch_name) # Check you are working at the right branch - current_branch_command = ['git','branch','--show-current'] - curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True) + + curr_branch = git_ops.show_current_branch() if not branch_name in curr_branch.stdout: raise ValueError("Branch "+branch_name+" was not found. \nPlease open a Git Bash Terminal, and follow the below instructions: \n1. Change directory to your project's directory. \n2. Excecute the command: git checkout "+branch_name) @@ -120,7 +113,7 @@ def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs, restart = F f.write('under review') # Stage untracked review files and commit them to local repository - status = subprocess.run(status_command,capture_output=True,text=True,check=True) + status = git_ops.get_status() untracked_files = [] for line in status.stdout.splitlines(): #tmp = line.decode("utf-8") @@ -135,9 +128,9 @@ def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs, restart = F untracked_files.append(line.strip()) if untracked_files: - result = subprocess.run(add_command(untracked_files),capture_output=True,check=True) + result = subprocess.run(git_ops.add_files_to_git(untracked_files),capture_output=True,check=True) message = 'Initialized metadata review.' - commit_output = subprocess.run(commit_command(message),capture_output=True,check=True) + commit_output = subprocess.run(git_ops.commit_changes(message),capture_output=True,check=True) for line in commit_output.stdout.splitlines(): print(line.decode('utf-8')) @@ -184,12 +177,12 @@ def second_save_metadata_review(review_yaml_file_path, reviewer_attrs): #checkout_review_branch(repo_obj, branch_name) # Check you are working at the right branch - curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True) + curr_branch = git_ops.show_current_branch() if not branch_name in curr_branch.stdout: raise ValueError('Please checkout ' + branch_name + ' via Git Bash before submitting metadata review files. ') # Collect modified review files - status = subprocess.run(status_command,capture_output=True,check=True) + status = git_ops.get_status() modified_files = [] os.path.basename(review_yaml_file_path) for line in status.stdout.splitlines(): @@ -208,9 +201,9 @@ def second_save_metadata_review(review_yaml_file_path, reviewer_attrs): modified_files.append(review_status_file_path) - result = subprocess.run(add_command(modified_files),capture_output=True,check=True) + result = subprocess.run(git_ops.add_files_to_git(modified_files),capture_output=True,check=True) message = 'Submitted metadata review.' - commit_output = subprocess.run(commit_command(message),capture_output=True,check=True) + commit_output = subprocess.run(git_ops.commit_changes(message),capture_output=True,check=True) for line in commit_output.stdout.splitlines(): print(line.decode('utf-8')) @@ -283,7 +276,7 @@ def last_submit_metadata_review(reviewer_attrs): branch_name = '_'.join(['review',initials]) push_command = lambda repository,refspec: ['git','push',repository,refspec] - current_branch_command = ['git','branch','--show-current'] + list_branches_command = ['git','branch','--list'] branches = subprocess.run(list_branches_command,capture_output=True,text=True,check=True) @@ -292,7 +285,7 @@ def last_submit_metadata_review(reviewer_attrs): print('Make sure to run data owner review workflow from the beginning without missing any steps.') return - curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True) + curr_branch = git_ops.show_current_branch() if not branch_name in curr_branch.stdout: print('Complete metadata review could not be completed.\n') print('Make sure a data-owner workflow has already been started on branch '+branch_name+'\n')