Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 3 additions & 27 deletions codeflash/code_utils/git_worktree_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,18 @@
import subprocess
import tempfile
import time
from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING, Optional
from typing import Optional

import git

from codeflash.cli_cmds.console import logger
from codeflash.code_utils.compat import codeflash_cache_dir
from codeflash.code_utils.git_utils import check_running_in_git_repo, git_root_dir

if TYPE_CHECKING:
from git import Repo


worktree_dirs = codeflash_cache_dir / "worktrees"
patches_dir = codeflash_cache_dir / "patches"

if TYPE_CHECKING:
from git import Repo


@lru_cache(maxsize=1)
def get_git_project_id() -> str:
"""Return the first commit sha of the repo."""
repo: Repo = git.Repo(search_parent_directories=True)
root_commits = list(repo.iter_commits(rev="HEAD", max_parents=0))
return root_commits[0].hexsha


def create_worktree_snapshot_commit(worktree_dir: Path, commit_message: str) -> None:
repository = git.Repo(worktree_dir, search_parent_directories=True)
Expand Down Expand Up @@ -96,12 +80,6 @@ def remove_worktree(worktree_dir: Path) -> None:
logger.exception(f"Failed to remove worktree: {worktree_dir}")


@lru_cache(maxsize=1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What benefit we were having with 1 object in cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before I used to store patch files inside a directory that has the name of the first commit sha of the repo (to know later what directory the patches are located for each project)

so caching here to prevent getting the first commit sha on every run

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Saga4
we don't need all of this now, since we are using the vscode workspace state
to store data for each workspace into the disk easily

def get_patches_dir_for_project() -> Path:
project_id = get_git_project_id() or ""
return Path(patches_dir / project_id)


def create_diff_patch_from_worktree(
worktree_dir: Path, files: list[str], fto_name: Optional[str] = None
) -> Optional[Path]:
Expand All @@ -115,10 +93,8 @@ def create_diff_patch_from_worktree(
if not uni_diff_text.endswith("\n"):
uni_diff_text += "\n"

project_patches_dir = get_patches_dir_for_project()
project_patches_dir.mkdir(parents=True, exist_ok=True)

patch_path = project_patches_dir / f"{worktree_dir.name}.{fto_name}.patch"
patches_dir.mkdir(parents=True, exist_ok=True)
patch_path = Path(patches_dir / f"{worktree_dir.name}.{fto_name}.patch")
with patch_path.open("w", encoding="utf8") as f:
f.write(uni_diff_text)

Expand Down
Loading