def METHOD_NAME(worktree=False):
    git_repo_dir = tempfile.mkdtemp()
    to_rm = [git_repo_dir]
    try:
        subprocess.check_output(["git", "init", "."], cwd=git_repo_dir)
        with open(f"{git_repo_dir}/committed", "w") as committed_f:
            committed_f.write("normal committed file\n")
        subprocess.check_output(["git", "add", "committed"], cwd=git_repo_dir)
        with open(f"{git_repo_dir}/committed-ignored", "w") as gitignore_f:
            gitignore_f.write("this file is gitignored, but committed already")
        subprocess.check_output(["git", "add", "committed-ignored"], cwd=git_repo_dir)
        with open(f"{git_repo_dir}/.gitignore", "w") as gitignore_f:
            gitignore_f.write("ignored\n" "committed-ignored\n")
        subprocess.check_output(["git", "add", ".gitignore"], cwd=git_repo_dir)
        # NOTE: explicitly set the author so this test passes in the CI.
        subprocess.check_output(
            [
                "git",
                "-c",
                "user.name=Unit Test",
                "-c",
                "user.email=unit.test@testing.tvm.ai",
                "commit",
                "-m",
                "initial commit",
            ],
            cwd=git_repo_dir,
        )
        if worktree:
            worktree_dir = tempfile.mkdtemp()
            to_rm.append(worktree_dir)
            subprocess.check_output(["git", "worktree", "add", worktree_dir], cwd=git_repo_dir)
            git_repo_dir = worktree_dir
        with open(f"{git_repo_dir}/ignored", "w") as gitignore_f:
            gitignore_f.write("this file is gitignored")
        with open(f"{git_repo_dir}/added-to-index", "w") as added_f:
            added_f.write("only added to git index\n")
        subprocess.check_output(["git", "add", "added-to-index"], cwd=git_repo_dir)
        with open(f"{git_repo_dir}/ignored-added-to-index", "w") as ignored_f:
            ignored_f.write("this file is gitignored but in the index already\n")
        subprocess.check_output(["git", "add", "-f", "ignored-added-to-index"], cwd=git_repo_dir)
        with open(f"{git_repo_dir}/untracked", "w") as untracked_f:
            untracked_f.write("this file is untracked\n")
        os.mkdir(f"{git_repo_dir}/subdir")
        with open(f"{git_repo_dir}/subdir/untracked", "w") as untracked_f:
            untracked_f.write("this file is untracked\n")
        with open(f"{git_repo_dir}/subdir/untracked2", "w") as untracked_f:
            untracked_f.write("this file is also untracked\n")
        return git_repo_dir, to_rm
    except Exception:
        for rm_dir in to_rm:
            shutil.rmtree(rm_dir)
        raise