Beispiel #1
0
    def work(self, namespace: argparse.Namespace, constants: Constants, state: State):
        if has_collisions_with_service_files(constants):
            return

        Path.mkdir(constants.mygit_path)
        Path.mkdir(constants.mygit_objects_path)
        Path.mkdir(constants.mygit_refs_path)
        Path.mkdir(constants.mygit_branches_path)
        Path.mkdir(constants.mygit_index_dir_path)
        default_branch_name = "master"
        with Path.open(constants.mygit_head_path, "w") as head:
            head.write(default_branch_name)

        Path.open(constants.mygit_branches_path / default_branch_name, 'w').close()
        Path.open(constants.mygit_index_path, "w").close()
        Path.open(constants.mygit_log_path, "w").close()

        with Path.open(constants.mygit_ignore_path, "w") as ignore:
            ignore.write(".mygit")

        index_object(constants.mygit_ignore_path, constants, state)
        state.load_cache(
            constants,
            get_compressed_file_content(constants.mygit_index_path),
            get_last_commit_index_content(constants))

        make_commit("init", constants, state)
        logging.info(Fore.GREEN + "new repository is created")
Beispiel #2
0
def index_file(file_path_absolute: Path, c: Constants, s: State):
    if not file_path_absolute.exists():
        s.current_indexed_paths[file_path_absolute] = "deleted"
        return

    with Path.open(file_path_absolute, "rb") as source:
        content = compress(source.read(), -1)
        checksum = sha1(content).hexdigest()
        blob_path = c.mygit_objects_path / checksum
        blob_index_path = c.mygit_index_dir_path / checksum

    if file_path_absolute in s.last_commit_indexed_path and s.last_commit_indexed_path[
            file_path_absolute] == checksum:
        return

    if file_path_absolute in s.current_indexed_paths:
        previous_indexed_checksum = s.current_indexed_paths[file_path_absolute]
        p_i_c_path = c.mygit_index_dir_path / previous_indexed_checksum
        if previous_indexed_checksum != checksum and p_i_c_path.exists():
            Path.unlink(p_i_c_path)

    s.current_indexed_paths[file_path_absolute] = checksum
    if not blob_index_path.exists() and not blob_path.exists():
        with Path.open(blob_index_path, "wb") as blob:
            blob.write(content)
Beispiel #3
0
def get_current_state(c: Constants) -> State:
    state = State()
    state.load_cache(
        c,
        backend.get_compressed_file_content(c.mygit_index_path),
        backend.get_last_commit_index_content(c))

    backend.check_status(c, state)

    return state
Beispiel #4
0
def handle_command(commands: dict, namespace: argparse.Namespace,
                   constants: Constants, state: State):
    state.load_cache(constants,
                     get_compressed_file_content(constants.mygit_index_path),
                     get_last_commit_index_content(constants))

    if namespace.command == "init":
        logging.warning(Fore.YELLOW +
                        "directory already contains the repository")
    else:
        commands[namespace.command].work(namespace, constants, state)
Beispiel #5
0
def main(workspace_path: Path, sys_args: list):
    colorama_init()

    parser = create_parser()
    subparsers = parser.add_subparsers(dest="command", title="mygit tools")
    commands = create_commands(subparsers)
    namespace = parser.parse_args(sys_args)
    constants = Constants(workspace_path)
    state = State()

    log_handlers = ([
        logging.FileHandler(constants.mygit_log_path),
        logging.StreamHandler()
    ] if is_init(constants) else [logging.StreamHandler()])

    logging.basicConfig(level=logging.NOTSET,
                        format='%(message)s',
                        handlers=log_handlers)

    if namespace.command is None:
        logging.warning(Fore.YELLOW +
                        "write command or use 'mygit -h' for help")
    else:
        if is_init(constants):
            handle_command(commands, namespace, constants, state)
        elif namespace.command == "init":
            commands[namespace.command].work(namespace, constants, state)
        else:
            logging.warning(
                Fore.YELLOW +
                "directory doesn't contain a repository. Use 'mygit init' to create new one"
            )

    colorama_deinit()
Beispiel #6
0
def index_deleted_files(s: State):
    for path in s.last_commit_indexed_path:
        if not path.exists() and path not in s.current_indexed_paths:
            s.current_indexed_paths[path] = "deleted"
Beispiel #7
0
def check_status(c: Constants, s: State):
    if not s.status_is_checked:
        check_tree(c.workspace_path, c, s)
        check_deleted_files(c, s)
        s.status_is_checked = True