nickderobertis/flexlate

Fix multiple iterations over files for git traverse

Opened this issue · 0 comments

Fix multiple iterations over files for git traverse

For now just using a set to keep it working, but should optimize

# TODO: Fix multiple iterations over files for git traverse

    branch.checkout()


def _get_initial_commit_sha(repo: Repo) -> str:
    return repo.git.rev_list("HEAD", max_parents=0)


def _get_initial_commit(repo: Repo) -> Commit:
    return repo.commit(_get_initial_commit_sha(repo))


def stage_and_commit_all(repo: Repo, commit_message: str):
    repo.git.add("-A")
    repo.git.commit("-m", commit_message)


def list_tracked_files(repo: Repo) -> Set[Path]:
    if repo.working_dir is None:
        raise ValueError("repo working dir should not be none")
    return _list_tracked_files(repo.tree(), Path(repo.working_dir))


def _list_tracked_files(tree: Tree, root_path: Path) -> Set[Path]:
    # TODO: Fix multiple iterations over files for git traverse
    #  For now just using a set to keep it working, but should optimize
    files: Set[Path] = set()
    for tree_or_blob in tree.traverse():
        if hasattr(tree_or_blob, "traverse"):
            # Got another tree
            tree = cast(Tree, tree_or_blob)
            files.update(_list_tracked_files(tree, root_path))
        else:
            # Got a blob
            blob = cast(Blob, tree_or_blob)
            files.add(root_path / Path(blob.path))
    return files


def delete_tracked_files_excluding_initial_commit(repo: Repo):
    if repo.working_dir is None:
        raise ValueError("repo working dir should not be none")
    initial_commit_files = _list_tracked_files(
        _get_initial_commit(repo).tree, Path(repo.working_dir)
    )
    for path in list_tracked_files(repo):
        if path not in initial_commit_files:
            os.remove(path)


def merge_branch_into_current(
    repo: Repo, branch_name: str, allow_conflicts: bool = True
):

9bca34bbb51859ded81a6573a232aa94b97381f2