|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import json |
4 | 3 | import subprocess |
5 | 4 | import tempfile |
6 | 5 | import time |
|
9 | 8 | from typing import TYPE_CHECKING, Optional |
10 | 9 |
|
11 | 10 | import git |
12 | | -from filelock import FileLock |
13 | 11 |
|
14 | 12 | from codeflash.cli_cmds.console import logger |
15 | 13 | from codeflash.code_utils.compat import codeflash_cache_dir |
16 | 14 | from codeflash.code_utils.git_utils import check_running_in_git_repo, git_root_dir |
17 | 15 |
|
18 | 16 | if TYPE_CHECKING: |
19 | | - from typing import Any |
20 | | - |
21 | 17 | from git import Repo |
22 | 18 |
|
23 | 19 |
|
@@ -100,71 +96,24 @@ def get_patches_dir_for_project() -> Path: |
100 | 96 | return Path(patches_dir / project_id) |
101 | 97 |
|
102 | 98 |
|
103 | | -def get_patches_metadata() -> dict[str, Any]: |
104 | | - project_patches_dir = get_patches_dir_for_project() |
105 | | - meta_file = project_patches_dir / "metadata.json" |
106 | | - if meta_file.exists(): |
107 | | - with meta_file.open("r", encoding="utf-8") as f: |
108 | | - return json.load(f) |
109 | | - return {"id": get_git_project_id() or "", "patches": []} |
110 | | - |
111 | | - |
112 | | -def save_patches_metadata(patch_metadata: dict) -> dict: |
113 | | - project_patches_dir = get_patches_dir_for_project() |
114 | | - meta_file = project_patches_dir / "metadata.json" |
115 | | - lock_file = project_patches_dir / "metadata.json.lock" |
116 | | - |
117 | | - # we are not supporting multiple concurrent optimizations within the same process, but keep that in case we decide to do so in the future. |
118 | | - with FileLock(lock_file, timeout=10): |
119 | | - metadata = get_patches_metadata() |
120 | | - |
121 | | - patch_metadata["id"] = time.strftime("%Y%m%d-%H%M%S") |
122 | | - metadata["patches"].append(patch_metadata) |
123 | | - |
124 | | - meta_file.write_text(json.dumps(metadata, indent=2)) |
125 | | - |
126 | | - return patch_metadata |
127 | | - |
128 | | - |
129 | | -def overwrite_patch_metadata(patches: list[dict]) -> bool: |
130 | | - project_patches_dir = get_patches_dir_for_project() |
131 | | - meta_file = project_patches_dir / "metadata.json" |
132 | | - lock_file = project_patches_dir / "metadata.json.lock" |
133 | | - |
134 | | - with FileLock(lock_file, timeout=10): |
135 | | - metadata = get_patches_metadata() |
136 | | - metadata["patches"] = patches |
137 | | - meta_file.write_text(json.dumps(metadata, indent=2)) |
138 | | - return True |
139 | | - |
140 | | - |
141 | 99 | def create_diff_patch_from_worktree( |
142 | | - worktree_dir: Path, |
143 | | - files: list[str], |
144 | | - fto_name: Optional[str] = None, |
145 | | - metadata_input: Optional[dict[str, Any]] = None, |
146 | | -) -> dict[str, Any]: |
| 100 | + worktree_dir: Path, files: list[str], fto_name: Optional[str] = None |
| 101 | +) -> Optional[Path]: |
147 | 102 | repository = git.Repo(worktree_dir, search_parent_directories=True) |
148 | 103 | uni_diff_text = repository.git.diff(None, "HEAD", *files, ignore_blank_lines=True, ignore_space_at_eol=True) |
149 | 104 |
|
150 | 105 | if not uni_diff_text: |
151 | 106 | logger.warning("No changes found in worktree.") |
152 | | - return {} |
| 107 | + return None |
153 | 108 |
|
154 | 109 | if not uni_diff_text.endswith("\n"): |
155 | 110 | uni_diff_text += "\n" |
156 | 111 |
|
157 | 112 | project_patches_dir = get_patches_dir_for_project() |
158 | 113 | project_patches_dir.mkdir(parents=True, exist_ok=True) |
159 | 114 |
|
160 | | - final_function_name = fto_name or metadata_input.get("fto_name", "unknown") |
161 | | - patch_path = project_patches_dir / f"{worktree_dir.name}.{final_function_name}.patch" |
| 115 | + patch_path = project_patches_dir / f"{worktree_dir.name}.{fto_name}.patch" |
162 | 116 | with patch_path.open("w", encoding="utf8") as f: |
163 | 117 | f.write(uni_diff_text) |
164 | 118 |
|
165 | | - final_metadata = {"patch_path": str(patch_path)} |
166 | | - if metadata_input: |
167 | | - final_metadata.update(metadata_input) |
168 | | - final_metadata = save_patches_metadata(final_metadata) |
169 | | - |
170 | | - return final_metadata |
| 119 | + return patch_path |
0 commit comments