def merge(branch: str, wit_path: str): """Merges two commits in to a new commit.""" if _can_checkout: if is_branch(wit_path, branch): branch_on_head = _get_references_data(wit_path)["HEAD"] branch_id = _get_references_data(wit_path)[branch] if _get_head(wit_path) == branch_on_head: commit_id = commit( f'A merge between "{branch_on_head}" and "{branch}"', wit_path, branch_id) else: common_parent = _get_common_parent(wit_path, _get_head(wit_path), branch_id) _move_files(wit_path, common_parent, branch_id) commit_id = commit( f'A merge between "{branch_on_head}" and "{branch}"', wit_path, branch_id) checkout(branch_on_head, wit_path) logging.info( f'"{branch}" was successfully merged with {branch_on_head}.') print(commit_id) else: logging.error( f'"{branch}" is not a branch, try "branch" function to create a new branch.' )
def _update_commit_references(wit_path, commit_id): """Updates commit references.""" active_branch_name = _get_activated(wit_path) active_branch_id = _get_references_data(wit_path)[active_branch_name] head_val = _get_references_data(wit_path)['HEAD'] if (head_val == active_branch_id) or (_get_head(wit_path) == active_branch_id): _edit_references(wit_path, active_branch_name, commit_id) _edit_references(wit_path, 'HEAD', active_branch_name) else: _edit_references(wit_path, 'HEAD', commit_id)
def branch(name, wit_path): """Creates a new branch.""" if name != 'None': if len(name) < 30: head = _get_head(wit_path) _add_branch(wit_path, name, head) else: logging.error(f'branch name is too long "{name}" (max 30 digits).') else: logging.error(f'branch name is not valid {name}.')
def _move_files(wit_path, common_parent, branch): """Copies all files from the common dir and up.""" staging_area_path = os.path.join(wit_path, '.wit', 'staging_area') images_path = os.path.join(wit_path, '.wit', 'images') common_parent_path = os.path.join(images_path, os.path.relpath(common_parent, wit_path)) _delete_dir_content(staging_area_path) branch_parent = list(_get_all_parents(branch, wit_path, common_parent)) + [[branch]] head_parent = list( _get_all_parents(_get_head(wit_path), wit_path, common_parent)) + [[_get_head(wit_path)]] _copy_to_staging_area(wit_path, common_parent_path) for parents in branch_parent: for parent in parents: _copy_to_staging_area(wit_path, os.path.join(images_path, parent)) for parents in head_parent: for parent in parents: _copy_to_staging_area(wit_path, os.path.join(images_path, parent))
def status(wit_path: str) -> str: """Prints a status report.""" current_id = _get_head(wit_path) head_name = _get_references_data(wit_path)['HEAD'] untracked_files = '\n'.join(_get_untracked_files(wit_path)) changes_to_be_committed = _return_as_string(_get_changes_to_be_committed, wit_path, current_id) changes_not_staged_for_commit = _return_as_string( _get_changes_not_staged_for_commit, wit_path) return ('\nstatus report:\n' + '-' * 14 + _get_full_id(current_id, head_name) + 'Changes to be committed:\n' + f'{changes_to_be_committed}\n\n' + 'Changes not staged for commit:\n' + f'{changes_not_staged_for_commit}\n\n' + 'Untracked files:\n' + f'{untracked_files}\n\n')
def commit(message: str, wit_path: str, second_parent=None) -> str: """Creates save of all wanted files.""" if len(message) < 100: commit_id = _create_id() commit_path = os.path.join(wit_path, '.wit', 'images', commit_id) _copy_dir( src=os.path.join(wit_path, '.wit', 'staging_area'), dst=os.path.join(wit_path, '.wit', 'images', commit_path), ) parents = _get_head(wit_path) if second_parent is not None: parents = f'{parents}, {second_parent}' _create_commit_data_file(commit_path, parents, message) _update_commit_references(wit_path, commit_id) logging.info('your project was successfully saved.') return commit_id else: logging.error('Message is too long (max 50 letters).')
def graph(wit_path): """Shows a graph with all of HEAD's parents.""" head = _get_head(wit_path) fig, ax = plt.subplots() _set_config(ax) x = 0.5 y = 1 indexes = _get_indexes({head: (x, y)}, head, (x, y), wit_path) for commit_id, point in indexes.items(): x, y = point for parent in _get_parents(commit_id, wit_path): end = indexes[parent] distance = _get_arrow_distance(point, end) _create_arrow(ax, point, distance) if commit_id == head: plt.text(x, y - 0.035, '(HEAD)', size=8, ha='center', va='center') _create_circle(ax, (x, y), commit_id) plt.show()