|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import json |
4 | 3 | import os |
5 | 4 | import shutil |
6 | 5 | import subprocess |
7 | 6 | import sys |
8 | 7 | import tempfile |
9 | 8 | import time |
10 | | -from functools import cache, lru_cache |
| 9 | +from functools import cache |
11 | 10 | from io import StringIO |
12 | 11 | from pathlib import Path |
13 | | -from typing import TYPE_CHECKING, Optional |
| 12 | +from typing import TYPE_CHECKING |
14 | 13 |
|
15 | 14 | import git |
16 | | -from filelock import FileLock |
17 | 15 | from rich.prompt import Confirm |
18 | 16 | from unidiff import PatchSet |
19 | 17 |
|
20 | 18 | from codeflash.cli_cmds.console import logger |
21 | | -from codeflash.code_utils.compat import codeflash_cache_dir |
22 | 19 | from codeflash.code_utils.config_consts import N_CANDIDATES |
23 | 20 |
|
24 | 21 | if TYPE_CHECKING: |
25 | | - from typing import Any |
26 | | - |
27 | 22 | from git import Repo |
28 | 23 |
|
29 | 24 |
|
@@ -197,148 +192,3 @@ def get_last_commit_author_if_pr_exists(repo: Repo | None = None) -> str | None: |
197 | 192 | return None |
198 | 193 | else: |
199 | 194 | return last_commit.author.name |
200 | | - |
201 | | - |
202 | | -worktree_dirs = codeflash_cache_dir / "worktrees" |
203 | | -patches_dir = codeflash_cache_dir / "patches" |
204 | | - |
205 | | - |
206 | | -@lru_cache(maxsize=1) |
207 | | -def get_git_project_id() -> str: |
208 | | - """Return the first commit sha of the repo.""" |
209 | | - repo: Repo = git.Repo(search_parent_directories=True) |
210 | | - root_commits = list(repo.iter_commits(rev="HEAD", max_parents=0)) |
211 | | - return root_commits[0].hexsha |
212 | | - |
213 | | - |
214 | | -def create_worktree_snapshot_commit(worktree_dir: Path, commit_message: str) -> None: |
215 | | - repository = git.Repo(worktree_dir, search_parent_directories=True) |
216 | | - repository.git.add(".") |
217 | | - repository.git.commit("-m", commit_message, "--no-verify") |
218 | | - |
219 | | - |
220 | | -def create_detached_worktree(module_root: Path) -> Optional[Path]: |
221 | | - if not check_running_in_git_repo(module_root): |
222 | | - logger.warning("Module is not in a git repository. Skipping worktree creation.") |
223 | | - return None |
224 | | - git_root = git_root_dir() |
225 | | - current_time_str = time.strftime("%Y%m%d-%H%M%S") |
226 | | - worktree_dir = worktree_dirs / f"{git_root.name}-{current_time_str}" |
227 | | - |
228 | | - repository = git.Repo(git_root, search_parent_directories=True) |
229 | | - |
230 | | - repository.git.worktree("add", "-d", str(worktree_dir)) |
231 | | - |
232 | | - # Get uncommitted diff from the original repo |
233 | | - repository.git.add("-N", ".") # add the index for untracked files to be included in the diff |
234 | | - exclude_binary_files = [":!*.pyc", ":!*.pyo", ":!*.pyd", ":!*.so", ":!*.dll", ":!*.whl", ":!*.egg", ":!*.egg-info", ":!*.pyz", ":!*.pkl", ":!*.pickle", ":!*.joblib", ":!*.npy", ":!*.npz", ":!*.h5", ":!*.hdf5", ":!*.pth", ":!*.pt", ":!*.pb", ":!*.onnx", ":!*.db", ":!*.sqlite", ":!*.sqlite3", ":!*.feather", ":!*.parquet", ":!*.jpg", ":!*.jpeg", ":!*.png", ":!*.gif", ":!*.bmp", ":!*.tiff", ":!*.webp", ":!*.wav", ":!*.mp3", ":!*.ogg", ":!*.flac", ":!*.mp4", ":!*.avi", ":!*.mov", ":!*.mkv", ":!*.pdf", ":!*.doc", ":!*.docx", ":!*.xls", ":!*.xlsx", ":!*.ppt", ":!*.pptx", ":!*.zip", ":!*.rar", ":!*.tar", ":!*.tar.gz", ":!*.tgz", ":!*.bz2", ":!*.xz"] # fmt: off |
235 | | - uni_diff_text = repository.git.diff( |
236 | | - None, "HEAD", "--", *exclude_binary_files, ignore_blank_lines=True, ignore_space_at_eol=True |
237 | | - ) |
238 | | - |
239 | | - if not uni_diff_text.strip(): |
240 | | - logger.info("No uncommitted changes to copy to worktree.") |
241 | | - return worktree_dir |
242 | | - |
243 | | - # Write the diff to a temporary file |
244 | | - with tempfile.NamedTemporaryFile(mode="w", suffix=".codeflash.patch", delete=False) as tmp_patch_file: |
245 | | - tmp_patch_file.write(uni_diff_text + "\n") # the new line here is a must otherwise the last hunk won't be valid |
246 | | - tmp_patch_file.flush() |
247 | | - |
248 | | - patch_path = Path(tmp_patch_file.name).resolve() |
249 | | - |
250 | | - # Apply the patch inside the worktree |
251 | | - try: |
252 | | - subprocess.run( |
253 | | - ["git", "apply", "--ignore-space-change", "--ignore-whitespace", "--whitespace=nowarn", patch_path], |
254 | | - cwd=worktree_dir, |
255 | | - check=True, |
256 | | - ) |
257 | | - create_worktree_snapshot_commit(worktree_dir, "Initial Snapshot") |
258 | | - except subprocess.CalledProcessError as e: |
259 | | - logger.error(f"Failed to apply patch to worktree: {e}") |
260 | | - |
261 | | - return worktree_dir |
262 | | - |
263 | | - |
264 | | -def remove_worktree(worktree_dir: Path) -> None: |
265 | | - try: |
266 | | - repository = git.Repo(worktree_dir, search_parent_directories=True) |
267 | | - repository.git.worktree("remove", "--force", worktree_dir) |
268 | | - except Exception: |
269 | | - logger.exception(f"Failed to remove worktree: {worktree_dir}") |
270 | | - |
271 | | - |
272 | | -def get_patches_dir_for_project() -> Path: |
273 | | - project_id = get_git_project_id() |
274 | | - return patches_dir / project_id |
275 | | - |
276 | | - |
277 | | -def get_patches_metadata() -> dict[str, Any]: |
278 | | - project_patches_dir = get_patches_dir_for_project() |
279 | | - meta_file = project_patches_dir / "metadata.json" |
280 | | - if meta_file.exists(): |
281 | | - with meta_file.open("r", encoding="utf-8") as f: |
282 | | - return json.load(f) |
283 | | - return {"id": get_git_project_id() or "", "patches": []} |
284 | | - |
285 | | - |
286 | | -def save_patches_metadata(patch_metadata: dict) -> dict: |
287 | | - project_patches_dir = get_patches_dir_for_project() |
288 | | - meta_file = project_patches_dir / "metadata.json" |
289 | | - lock_file = project_patches_dir / "metadata.json.lock" |
290 | | - |
291 | | - # we are not supporting multiple concurrent optimizations within the same process, but keep that in case we decide to do so in the future. |
292 | | - with FileLock(lock_file, timeout=10): |
293 | | - metadata = get_patches_metadata() |
294 | | - |
295 | | - patch_metadata["id"] = time.strftime("%Y%m%d-%H%M%S") |
296 | | - metadata["patches"].append(patch_metadata) |
297 | | - |
298 | | - meta_file.write_text(json.dumps(metadata, indent=2)) |
299 | | - |
300 | | - return patch_metadata |
301 | | - |
302 | | - |
303 | | -def overwrite_patch_metadata(patches: list[dict]) -> bool: |
304 | | - project_patches_dir = get_patches_dir_for_project() |
305 | | - meta_file = project_patches_dir / "metadata.json" |
306 | | - lock_file = project_patches_dir / "metadata.json.lock" |
307 | | - |
308 | | - with FileLock(lock_file, timeout=10): |
309 | | - metadata = get_patches_metadata() |
310 | | - metadata["patches"] = patches |
311 | | - meta_file.write_text(json.dumps(metadata, indent=2)) |
312 | | - return True |
313 | | - |
314 | | - |
315 | | -def create_diff_patch_from_worktree( |
316 | | - worktree_dir: Path, |
317 | | - files: list[str], |
318 | | - fto_name: Optional[str] = None, |
319 | | - metadata_input: Optional[dict[str, Any]] = None, |
320 | | -) -> dict[str, Any]: |
321 | | - repository = git.Repo(worktree_dir, search_parent_directories=True) |
322 | | - uni_diff_text = repository.git.diff(None, "HEAD", *files, ignore_blank_lines=True, ignore_space_at_eol=True) |
323 | | - |
324 | | - if not uni_diff_text: |
325 | | - logger.warning("No changes found in worktree.") |
326 | | - return {} |
327 | | - |
328 | | - if not uni_diff_text.endswith("\n"): |
329 | | - uni_diff_text += "\n" |
330 | | - |
331 | | - project_patches_dir = get_patches_dir_for_project() |
332 | | - project_patches_dir.mkdir(parents=True, exist_ok=True) |
333 | | - |
334 | | - final_function_name = fto_name or metadata_input.get("fto_name", "unknown") |
335 | | - patch_path = project_patches_dir / f"{worktree_dir.name}.{final_function_name}.patch" |
336 | | - with patch_path.open("w", encoding="utf8") as f: |
337 | | - f.write(uni_diff_text) |
338 | | - |
339 | | - final_metadata = {"patch_path": str(patch_path)} |
340 | | - if metadata_input: |
341 | | - final_metadata.update(metadata_input) |
342 | | - final_metadata = save_patches_metadata(final_metadata) |
343 | | - |
344 | | - return final_metadata |
0 commit comments