|
8 | 8 |
|
9 | 9 | from codeclash.agents.utils import GameContext |
10 | 10 | from codeclash.constants import GH_ORG |
11 | | -from codeclash.tournaments.utils.git_utils import filter_git_diff |
| 11 | +from codeclash.tournaments.utils.git_utils import extract_modified_code_file_paths_from_diff, filter_git_diff |
12 | 12 | from codeclash.utils.environment import assert_zero_exit_code, create_file_in_container |
13 | 13 | from codeclash.utils.log import get_logger |
14 | 14 |
|
@@ -40,6 +40,7 @@ def __init__( |
40 | 40 | "player_unique_id": self._player_unique_id, |
41 | 41 | "diff": {0: ""}, # mapping round -> diff |
42 | 42 | "incremental_diff": {0: ""}, # mapping round -> diff |
| 43 | + "modified_files": {0: {}}, # mapping round -> {file_path: file_content} |
43 | 44 | "created_timestamp": int(time.time()), |
44 | 45 | "config": self.config, |
45 | 46 | "initial_commit_hash": self._get_commit_hash(), |
@@ -67,8 +68,11 @@ def pre_run_hook(self, *, new_round: int) -> None: |
67 | 68 | def post_run_hook(self, *, round: int) -> None: |
68 | 69 | """Should be called after we called the run method.""" |
69 | 70 | self._commit() |
70 | | - self._metadata["diff"][round] = self._get_round_diff(round) |
| 71 | + raw_diff = self._get_round_diff(round) |
| 72 | + filtered_diff = filter_git_diff(raw_diff) |
| 73 | + self._metadata["diff"][round] = raw_diff |
71 | 74 | self._metadata["incremental_diff"][round] = self._get_round_diff(round, incremental=True) |
| 75 | + self._metadata["modified_files"][round] = self._extract_modified_files_from_diff(filtered_diff) |
72 | 76 | if self.push: |
73 | 77 | for cmd in [ |
74 | 78 | f"git push origin {self._branch_name}", |
@@ -151,6 +155,23 @@ def _commit(self) -> None: |
151 | 155 | self._tag_round(r) |
152 | 156 | self.logger.info(f"Committed changes for {self.name} for round {r}") |
153 | 157 |
|
| 158 | + def _extract_modified_files_from_diff(self, diff: str) -> dict[str, str]: |
| 159 | + """Extract modified file paths from a git diff and get their full content. |
| 160 | + Returns a dict mapping file path to full file content. |
| 161 | + Only includes common code file extensions. |
| 162 | + """ |
| 163 | + file_paths = extract_modified_code_file_paths_from_diff(diff) |
| 164 | + |
| 165 | + file_contents = {} |
| 166 | + for file_path in file_paths: |
| 167 | + out = assert_zero_exit_code( |
| 168 | + self.environment.execute(f"cat '{file_path}'"), |
| 169 | + logger=self.logger, |
| 170 | + ) |
| 171 | + file_contents[file_path] = out["output"] |
| 172 | + |
| 173 | + return file_contents |
| 174 | + |
154 | 175 | def _get_round_diff(self, round: int, *, incremental: bool = False) -> str: |
155 | 176 | """Get the diff between the round and initial version (round 0). |
156 | 177 | If incremental is True, get the diff between the round and the previous round. |
|
0 commit comments