diff --git a/dvc/repo/brancher.py b/dvc/repo/brancher.py index c1ebff3cb..ca6dfe84f 100644 --- a/dvc/repo/brancher.py +++ b/dvc/repo/brancher.py @@ -42,13 +42,9 @@ def brancher( # noqa: E302 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 + 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