Exemplo n.º 1
0
def _delete_tag(dataset_client: DatasetClientType, tbrn_info: TBRN) -> None:
    if not tbrn_info.revision:
        error(f'To delete a tag, "{tbrn_info.get_tbrn()}" must have a tag name')

    dataset_client.delete_tag(tbrn_info.revision)
    tag_tbrn = TBRN(dataset_client.name, revision=tbrn_info.revision).get_colored_tbrn()
    click.echo(f'Successfully deleted tag "{tag_tbrn}"')
Exemplo n.º 2
0
def _create_branch(dataset_client: DatasetClientType, name: str) -> None:
    if dataset_client.status.is_draft:
        error("Branch cannot be created from a draft")

    dataset_client.create_branch(name)
    branch_tbrn = TBRN(dataset_client.name, revision=name).get_colored_tbrn()
    click.echo(f'Successfully created branch "{branch_tbrn}"')
Exemplo n.º 3
0
def _delete_branch(dataset_client: DatasetClientType, tbrn_info: TBRN) -> None:
    if not tbrn_info.revision:
        error(
            f'To delete a branch, "{tbrn_info.get_tbrn()}" must have a branch name'
        )

    dataset_client._delete_branch(tbrn_info.revision)  # pylint: disable=protected-access
    click.echo(f'Successfully deleted branch "{tbrn_info.get_colored_tbrn()}"')
Exemplo n.º 4
0
def _create_tag(dataset_client: DatasetClientType, name: str) -> None:
    if dataset_client.status.is_draft:
        error(f'To create a tag, "{dataset_client.name}" cannot be in draft status')

    if not dataset_client.status.commit_id:
        error(f'To create a tag, "{dataset_client.name}" should have commit history')

    dataset_client.create_tag(name=name)
    tag_tbrn = TBRN(dataset_client.name, revision=name).get_colored_tbrn()
    click.echo(f'Successfully created tag "{tag_tbrn}"')
Exemplo n.º 5
0
    def __init__(
        self,
        dataset_client: DatasetClientType,
        revisions: List[Optional[str]],
        commit_id_to_branches: Dict[str, List[str]],
        oneline: bool,
        *,
        show_drafts: bool,
    ):
        all_commit_logs = list(map(dataset_client.list_commits, revisions))
        all_drafts: List[Draft] = []
        error_message = f'Dataset "{dataset_client.name}" has no commit history'
        if show_drafts:
            error_message += " or open drafts"
            for revision in revisions:
                all_drafts.extend(dataset_client.list_drafts(branch_name=revision))

        if not all_commit_logs[0]:
            if not all_drafts:
                error(error_message)
            self._sorted_commit_logs = []
        else:
            # Sort logs from different branches by the date of the latest commit of each branch.
            self._sorted_commit_logs = sorted(all_commit_logs, key=lambda x: x[0].committer.date)

        self._commit_id_to_branches = commit_id_to_branches
        self._commit_printer, self._draft_printer = (
            (_get_oneline_commit_message, _get_oneline_draft_message)
            if oneline
            else (_get_full_commit_message, _get_full_draft_message)
        )

        self._sorted_drafts = sorted(all_drafts, key=lambda x: x.updated_at)
        self._keys = [log[0].committer.date for log in self._sorted_commit_logs]
Exemplo n.º 6
0
def _echo_draft(
    dataset_client: DatasetClientType,
    title: str = "",
    description: str = "",
    branch_name: Optional[str] = None,
) -> None:
    if not branch_name:
        error("Draft should be created based on a branch.")

    branch = dataset_client.get_branch(branch_name)

    if branch.commit_id != ROOT_COMMIT_ID:
        commit_id = f"({branch.commit_id})"
    else:
        commit_id = ""

    if not title:
        title = "<no title>"
    if description:
        description = f"\n\n{indent(description, INDENT)}"
    draft_message = f"{title}{description}"
    click.echo(
        _FULL_DRAFT_MESSAGE.format(
            branch_name,
            commit_id,
            draft_message,
        ))
Exemplo n.º 7
0
    def _build_commit_tree(
            dataset_client: DatasetClientType,
            revisions: List[Optional[str]]) -> List[_CommitNode]:
        commit_to_node: Dict[str, _CommitNode] = {}
        leaves: Dict[str, _CommitNode] = {}
        for revision in revisions:
            commits = dataset_client.list_commits(revision)
            child_node: Optional[_CommitNode] = None
            for index, commit in enumerate(commits):
                commit_id = commit.commit_id
                current_node = commit_to_node.get(commit_id,
                                                  _CommitNode(commit))
                if child_node:
                    current_node.add_child(child_node)
                # Commit already exists in the tree.
                if commit_id in commit_to_node:
                    break

                # Save leaf node to leaf set.
                if index == 0:
                    leaves[commit_id] = current_node
                # Save commit to commit dict.
                commit_to_node[commit_id] = current_node
                child_node = current_node

        # Check the correction of leaf set.
        for commit_id in tuple(leaves):
            if leaves[commit_id].available_child_num != 0:
                del leaves[commit_id]
        return sorted(leaves.values(),
                      key=lambda x: x.commit.committer.date,
                      reverse=True)
Exemplo n.º 8
0
def _list_drafts(dataset_client: DatasetClientType, tbrn_info: TBRN) -> None:
    if tbrn_info.revision:
        error(
            f'list drafts based on given revision "{tbrn_info.get_tbrn()}" is not supported'
        )

    if tbrn_info.is_draft:
        draft = dataset_client.get_draft(tbrn_info.draft_number)
        click.echo(f"Draft: {tbrn_info.get_tbrn()}")
        _echo_draft(dataset_client, draft.title, draft.description,
                    draft.branch_name)
    else:
        for draft in dataset_client.list_drafts():
            click.echo(
                f"Draft: {TBRN(tbrn_info.dataset_name, draft_number=draft.number).get_tbrn()}"
            )
            _echo_draft(dataset_client, draft.title, draft.description,
                        draft.branch_name)
Exemplo n.º 9
0
def _edit_draft(
    dataset_client: DatasetClientType,
    tbrn_info: TBRN,
    message: Tuple[str, ...],
    config_parser: ConfigParser,
) -> None:
    if not tbrn_info.is_draft:
        error("Draft number is required when editing draft")

    draft = dataset_client.get_draft()
    hint_message = format_hint(draft.title, draft.description, _DRAFT_HINT)
    title, description = edit_message(message, hint_message, config_parser)
    if not title:
        error("Aborting updating draft due to empty draft message")

    dataset_client.update_draft(title=title, description=description)
    click.echo(f'Successfully updated draft "{tbrn_info.get_colored_tbrn()}"')
    _echo_draft(dataset_client, title, description,
                dataset_client.status.branch_name)
Exemplo n.º 10
0
def _list_branches(dataset_client: DatasetClientType, verbose: bool) -> None:
    branches = dataset_client.list_branches()
    if not verbose:
        for branch in branches:
            click.echo(branch.name)
    else:
        name_length = max(len(branch.name) for branch in branches)
        for branch in branches:
            click.echo(
                f"{branch.name:{name_length}} {shorten(branch.commit_id)} {branch.title}"
            )
Exemplo n.º 11
0
    def _build_tree(
        self,
        dataset_client: DatasetClientType,
        revisions: List[Optional[str]],
        show_drafts: bool,
    ) -> List[_T]:
        commit_to_node: Dict[str, _RootCommitNode] = {}
        leaves: Dict[str, _T] = {}
        for revision in revisions:
            child_node: Optional[_RootCommitNode] = None
            for commit in dataset_client.list_commits(revision):
                commit_id = commit.commit_id
                current_node = commit_to_node.get(commit_id, _CommitNode(commit))
                if child_node:
                    current_node.add_child(child_node)
                # Commit already exists in the tree.
                if commit_id in commit_to_node:
                    break

                # Save leaf node to leaf set.
                if not child_node:
                    leaves[commit_id] = current_node
                # Save commit to commit dict.
                commit_to_node[commit_id] = current_node
                child_node = current_node
            if show_drafts:
                for draft in dataset_client.list_drafts(branch_name=revision):
                    draft_node = _DraftNode(draft)
                    leaves[draft_node.key] = draft_node
                    self._key_to_branches[draft_node.key] = [draft.branch_name]
                    parent_commit_id = draft.parent_commit_id
                    if parent_commit_id in commit_to_node:
                        parent_node = commit_to_node[parent_commit_id]
                    else:
                        parent_node = _RootCommitNode()
                        commit_to_node[parent_commit_id] = parent_node
                        if child_node:
                            parent_node.add_child(child_node)
                    parent_node.add_child(draft_node)

        return self._check_and_sort_leaves(leaves)
Exemplo n.º 12
0
def _create_draft(
    dataset_client: DatasetClientType,
    tbrn_info: TBRN,
    message: Tuple[str, ...],
    config_parser: ConfigParser,
) -> None:
    if tbrn_info.is_draft:
        error(
            f'Create a draft in draft status "{tbrn_info.get_tbrn()}" is not permitted'
        )

    title, description = edit_message(message, _DRAFT_HINT, config_parser)
    if not title:
        error("Aborting creating draft due to empty draft message")

    dataset_client.create_draft(title=title, description=description)
    status = dataset_client.status
    draft_tbrn = TBRN(tbrn_info.dataset_name,
                      draft_number=status.draft_number).get_colored_tbrn()
    click.echo(f'Successfully created draft "{draft_tbrn}"')
    _echo_draft(dataset_client, title, description, status.branch_name)
Exemplo n.º 13
0
def _list_tags(dataset_client: DatasetClientType, sort_key: str) -> None:
    for tag in sort_branches_or_tags(sort_key, dataset_client.list_tags()):
        click.echo(tag.name)
Exemplo n.º 14
0
def _list_tags(dataset_client: DatasetClientType) -> None:
    for tag in dataset_client.list_tags():
        click.echo(tag.name)
Exemplo n.º 15
0
def _close_draft(dataset_client: DatasetClientType, tbrn_info: TBRN) -> None:
    if not tbrn_info.draft_number:
        error("Draft number is required when editing draft")

    dataset_client._close_draft(tbrn_info.draft_number)  # pylint: disable=protected-access
    click.echo(f'Successfully closed draft "{tbrn_info.get_colored_tbrn()}"')