def status(): if is_father_dir_exist('.wit') is False: return wit_dir_path = get_folder_path('.wit') data_to_copy_path = pathlib.Path(wit_dir_path).parent commit_id = get_commit_id(wit_dir_path) if commit_id is None: logging.error('There was no commit yet. exiting program..') return staging_area_path = wit_dir_path + '\\staging_area' commit_id_path = wit_dir_path + '\\images\\' + commit_id files = [] changes_to_be_commited = get_files_to_be_commited(staging_area_path, commit_id_path, files.copy()) changes_not_staged_for_commit = get_changes_not_staged_for_commit(staging_area_path, data_to_copy_path, files.copy()) untracked_files = get_untraecked_files(data_to_copy_path, staging_area_path, files.copy()) print_status(commit_id, changes_to_be_commited, changes_not_staged_for_commit, untracked_files) return changes_to_be_commited, changes_not_staged_for_commit
def update_activated_branch_in_ref_file(new_commit_id): """Update the value of the activated branch in references file. according to head.""" activated_branch = get_activated_branch_name() wit_dir_path = get_folder_path('.wit') ref_file_path = os.path.join(wit_dir_path, 'references.txt') with open(ref_file_path, 'r') as ref_file: content = ref_file.readlines() i = 0 while i < len(content): content[i] = content[i].split('=') i += 1 dict_content = dict(content) if activated_branch != '': dict_content[activated_branch] = new_commit_id content ='' for k, v in dict_content.items(): content += k + '=' + v with open(ref_file_path, 'w') as ref_file: ref_file.write(content)
def get_commit_id_from_activated_branch(): activated_branch = get_activated_branch_name() if activated_branch == '': print('There is no activated branch.') return None wit_dir_path = get_folder_path('.wit') ref_file_path = os.path.join(wit_dir_path, 'references.txt') with open(ref_file_path, 'r') as ref_file: content = ref_file.readlines() i = 0 while i < len(content): content[i] = content[i].split('=') i += 1 dict_content = dict(content) try: commit_id_of_activated_branch = dict_content[activated_branch].strip() except KeyError: print('%s does not exist in the references file.', activated_branch) raise KeyError else: return commit_id_of_activated_branch
def is_ok_for_checkout(): """return False if 1.There are changes to be commited. 2.There are changes not staged for commit. this function prevent to do checkout in situations that might lead to information loss. return True otherwise. """ wit_dir_path = get_folder_path('.wit') data_to_copy_path = pathlib.Path(wit_dir_path).parent commit_id = get_commit_id(wit_dir_path) if commit_id is None: logging.error('There was no commit yet. exiting program..') return staging_area_path = wit_dir_path + '\\staging_area' commit_id_path = wit_dir_path + '\\images\\' + commit_id files = [] changes_to_be_commited = get_files_to_be_commited(staging_area_path, commit_id_path, files.copy()) changes_not_staged_for_commit = get_changes_not_staged_for_commit(staging_area_path, data_to_copy_path, files.copy()) return len(changes_to_be_commited) == 0 and len(changes_not_staged_for_commit) == 0
def branch(branch_name: str): if is_father_dir_exist('.wit') is False: return wit_dir_path = get_folder_path('.wit') add_name_to_ref_file(wit_dir_path, branch_name)
def get_activated_branch_name() -> str: """returns the name of the activated branch form the references file if not exist returns None.""" wit_dir_path = get_folder_path('.wit') activated_file_path = os.path.join(wit_dir_path, 'activated.txt') with open(activated_file_path, 'r') as activated_file: activated_name = activated_file.read() return activated_name
def checkout(parameter: str): COMMIT_ID_LENGTH = 40 if is_father_dir_exist('.wit') is False: return if is_ok_for_checkout() is False: logging.error(""" There are changes to be commited or there are changes_staged_for commit, checkout failed""") return wit_dir_path = get_folder_path('.wit') if parameter == 'master': parameter = get_commit_id(wit_dir_path) if len(parameter) == COMMIT_ID_LENGTH: # checkout commit_id update_head(wit_dir_path, parameter) erase_activated_file(wit_dir_path) else: # checkout branch_name if is_branch_name_exist(parameter): write_to_activated_file(parameter) # update_head(wit_dir_path, parameter) # branch_commit_id = get_commit_id_from_activated_branch() # if branch_commit_id != None: else: print('%s does not appear in references file.', parameter) temp = get_commit_id_from_activated_branch() if temp != None: parameter = temp update_original_dir_wit_according_to_checkout(wit_dir_path, parameter) change_staging_area_content(wit_dir_path, parameter) activated_branch_name = get_activated_branch_name() if activated_branch_name is None: logging.info('There is no activated branch right now.') return
def is_activated_branch_same_as_head(): """return true if branch point to same dir as head in references file.""" wit_dir_path = get_folder_path('.wit') activated_file_path = os.path.join(wit_dir_path, 'activated.txt') ref_file_path = os.path.join(wit_dir_path, 'references.txt') with open(activated_file_path, 'r') as activated_file: activated = activated_file.read() with open(ref_file_path, 'r') as ref_file: content = ref_file.readline().split('=') head = content[1].strip() return head == activated
def is_branch_name_exist(parameter: str) -> bool: wit_dir_path = get_folder_path('.wit') ref_file_path = os.path.join(wit_dir_path, 'references.txt') with open(ref_file_path, 'r') as ref_file: content = ref_file.readlines() i = 0 while i < len(content): content[i] = content[i].split('=') i += 1 content = dict(content) print(content) return parameter in content
def commit(message: str): """Creates a saving point to get back to""" if is_father_dir_exist('.wit'): logging.debug('.wit dir was found!') else: logging.error('.wit dir was not found. exit program..') return wit_dir_path = get_folder_path('.wit') commit_id = create_commit_id_dir(wit_dir_path) create_metadata_file(commit_id, message, wit_dir_path) copy_from_staging_area(wit_dir_path, commit_id) write_to_references_file(wit_dir_path, commit_id)
def graph(): if is_father_dir_exist('.wit') is False: return wit_dir_path = get_folder_path('.wit') cur_commit_id = get_commit_id(wit_dir_path) master_id = get_master_id(wit_dir_path) parent_commit_id = get_parent_commit_id(wit_dir_path, cur_commit_id) if cur_commit_id is None: logging.info('There was no commit yet.') return draw_graph(cur_commit_id, master_id, parent_commit_id)
import os from branch import branch from add import get_folder_path, is_father_dir_exist from commit_module import get_commit_id wit_dir_path = get_folder_path('.wit') def get_branch_commit_id(branch_name: str) -> str: ref_file_path = os.path.join(wit_dir_path, 'references.txt') with open(ref_file_path, 'r') as ref_file: content = ref_file.readlines() i = 0 while i < len(content): content[i] = content[i].split('=') i += 1 dict_content = dict(content) try: branch_commit_id = dict_content[branch_name].strip() except KeyError: print('%s does not exist in the references file.', branch_name) raise KeyError else: return branch_commit_id
def write_to_activated_file(activated_branch: str) -> None: wit_dir_path = get_folder_path('.wit') activated_file_path = os.path.join(wit_dir_path, 'activated.txt') with open(activated_file_path, 'w') as activated_file: activated_file.write(activated_branch)