<commit_before>from funcy import group_by

from dvc.ignore import CleanTree
from dvc.scm.tree import WorkingTree


def brancher(  # noqa: E302
    self, all_branches=False, all_tags=False, all_commits=False
):
    """Generator that iterates over specified revisions.

    Args:
        branches (list): a list of branches to iterate over.
        all_branches (bool): iterate over all available branches.
        tags (list): a list of tags to iterate over.
        all_tags (bool): iterate over all available tags.

    Yields:
        str: the display name for the currently selected tree, it could be:
            - a git revision identifier
            - empty string it there is no branches to iterate over
            - "Working Tree" if there are uncommitted changes in the SCM repo
    """
    if not any([all_branches, all_tags, all_commits]):
        yield ""
        return

    saved_tree = self.tree
    revs = []

    scm = self.scm

    self.tree = CleanTree(WorkingTree(self.root_dir))
    yield "working tree"

    if all_commits:
        revs = scm.list_all_commits()
    else:
        if all_branches:
            revs.extend(scm.list_branches())

        if all_tags:
            revs.extend(scm.list_tags())

    # NOTE: it might be a good idea to wrap this loop in try/finally block
    # to don't leave the tree on some unexpected branch after the
    # `brancher()`, but this could cause problems on exception handling
    # code which might expect the tree on which exception was raised to
    # stay in place. This behavior is a subject to change.
    for sha, names in group_by(scm.resolve_rev, revs).items():
        self.tree = CleanTree(scm.get_tree(sha))
        yield ", ".join(names)

    self.tree = saved_tree<commit_msg>dvc: safely return to saved tree in brancher<commit_after>from funcy import group_by

from dvc.ignore import CleanTree
from dvc.scm.tree import WorkingTree


def brancher(  # noqa: E302
    self, all_branches=False, all_tags=False, all_commits=False
):
    """Generator that iterates over specified revisions.

    Args:
        branches (list): a list of branches to iterate over.
        all_branches (bool): iterate over all available branches.
        tags (list): a list of tags to iterate over.
        all_tags (bool): iterate over all available tags.

    Yields:
        str: the display name for the currently selected tree, it could be:
            - a git revision identifier
            - empty string it there is no branches to iterate over
            - "Working Tree" if there are uncommitted changes in the SCM repo
    """
    if not any([all_branches, all_tags, all_commits]):
        yield ""
        return

    saved_tree = self.tree
    revs = []

    scm = self.scm

    self.tree = CleanTree(WorkingTree(self.root_dir))
    yield "working tree"

    if all_commits:
        revs = scm.list_all_commits()
    else:
        if all_branches:
            revs.extend(scm.list_branches())

        if all_tags:
            revs.extend(scm.list_tags())

    try:
        for sha, names in group_by(scm.resolve_rev, revs).items():
            self.tree = CleanTree(scm.get_tree(sha))
            yield ", ".join(names)
    finally:
        self.tree = saved_tree
{'fraction_non_alphanumeric': 0.07911001236093942, 'fraction_numerical': 0.001854140914709518, 'mean_word_length': 2.476906552094522, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5407432', 'n_tokens_mistral': 981, 'n_tokens_neox': 940, 'n_words': 338}