Skip to content

Commit 241b50f

Browse files
authored
Merge branch 'main' into vsc/lang_change_26_sept
2 parents 4b1aa00 + f26df43 commit 241b50f

File tree

4 files changed

+108
-182
lines changed

4 files changed

+108
-182
lines changed

codeflash/code_utils/git_worktree_utils.py

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import json
43
import subprocess
54
import tempfile
65
import time
@@ -9,15 +8,12 @@
98
from typing import TYPE_CHECKING, Optional
109

1110
import git
12-
from filelock import FileLock
1311

1412
from codeflash.cli_cmds.console import logger
1513
from codeflash.code_utils.compat import codeflash_cache_dir
1614
from codeflash.code_utils.git_utils import check_running_in_git_repo, git_root_dir
1715

1816
if TYPE_CHECKING:
19-
from typing import Any
20-
2117
from git import Repo
2218

2319

@@ -100,71 +96,24 @@ def get_patches_dir_for_project() -> Path:
10096
return Path(patches_dir / project_id)
10197

10298

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-
14199
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]:
147102
repository = git.Repo(worktree_dir, search_parent_directories=True)
148103
uni_diff_text = repository.git.diff(None, "HEAD", *files, ignore_blank_lines=True, ignore_space_at_eol=True)
149104

150105
if not uni_diff_text:
151106
logger.warning("No changes found in worktree.")
152-
return {}
107+
return None
153108

154109
if not uni_diff_text.endswith("\n"):
155110
uni_diff_text += "\n"
156111

157112
project_patches_dir = get_patches_dir_for_project()
158113
project_patches_dir.mkdir(parents=True, exist_ok=True)
159114

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"
162116
with patch_path.open("w", encoding="utf8") as f:
163117
f.write(uni_diff_text)
164118

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

Comments
 (0)