Skip to content

Commit 4a883cd

Browse files
author
Tjaz Erzen
committed
Handle filtering by FRID
1 parent cb4543b commit 4a883cd

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

git_utils.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,49 @@ def _get_commit(repo: Repo, frid: Optional[str]) -> str:
259259
return initial_commit
260260

261261

262-
def _get_commit_with_frid(repo: Repo, frid: str) -> str:
263-
"""Finds commit with given frid mentioned in the commit message."""
264-
return _get_commit_with_message(repo, FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE.format(frid))
262+
def _get_commit_with_frid(
263+
repo: Repo, frid: str, module_name: Optional[str] = None
264+
) -> str:
265+
"""
266+
Finds commit with given frid mentioned in the commit message.
267+
268+
Args:
269+
repo (Repo): Git repository object
270+
frid (str): Functional requirement ID
271+
module_name (Optional[str]): Module name to filter by. If provided, only returns
272+
commits that have both the FRID and module name.
273+
274+
Returns:
275+
str: Commit SHA if found, empty string otherwise
276+
"""
277+
commit_message_pattern = FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE.format(frid)
278+
279+
# If no module name filtering is needed, use the original logic
280+
if not module_name:
281+
return _get_commit_with_message(repo, commit_message_pattern)
282+
283+
# Use multiple grep patterns with --all-match for AND condition
284+
escaped_frid_message = commit_message_pattern.replace("[", "\\[").replace(
285+
"]", "\\]"
286+
)
287+
module_name_pattern = MODULE_NAME_MESSAGE.format(module_name)
288+
escaped_module_message = module_name_pattern.replace("[", "\\[").replace("]", "\\]")
289+
290+
return repo.git.rev_list(
291+
repo.active_branch.name,
292+
"--grep",
293+
escaped_frid_message,
294+
"--grep",
295+
escaped_module_message,
296+
"--all-match",
297+
"-n",
298+
"1",
299+
)
265300

266301

267-
def has_commit_for_frid(repo_path: Union[str, os.PathLike], frid: str) -> bool:
302+
def has_commit_for_frid(
303+
repo_path: Union[str, os.PathLike], frid: str, module_name: Optional[str] = None
304+
) -> bool:
268305
"""
269306
Check if a commit exists for the given FRID in the repository.
270307
@@ -276,7 +313,7 @@ def has_commit_for_frid(repo_path: Union[str, os.PathLike], frid: str) -> bool:
276313
bool: True if the commit exists, False otherwise
277314
"""
278315
repo = Repo(repo_path)
279-
commit_with_frid = _get_commit_with_frid(repo, frid)
316+
commit_with_frid = _get_commit_with_frid(repo, frid, module_name)
280317
if not commit_with_frid:
281318
return False
282319
return True

module_renderer.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,29 @@ def _validate_previous_frid_commits_exist(
7373

7474
# Check if commits exist for all previous FRIDs
7575
build_folder_path = os.path.join(self.args.build_folder, module_name)
76-
conformance_tests_path = os.path.join(self.args.conformance_tests_folder, module_name)
76+
conformance_tests_path = os.path.join(
77+
self.args.conformance_tests_folder, module_name
78+
)
79+
80+
if not os.path.exists(build_folder_path):
81+
raise MissingPreviousFridCommitsError(
82+
f"Cannot render from FRID {first_render_frid}: "
83+
f"Folder {build_folder_path} for module {module_name} does not exist. "
84+
f"Please render all previous FRIDs for module {module_name} first."
85+
)
86+
87+
if not os.path.exists(conformance_tests_path):
88+
raise MissingPreviousFridCommitsError(
89+
f"Cannot render from FRID {first_render_frid}: "
90+
f"Folder {conformance_tests_path} for module {module_name} does not exist. "
91+
f"Please render all previous FRIDs for module {module_name} first."
92+
)
7793

7894
for prev_frid in previous_frids:
7995
# Check in build folder
80-
if not git_utils.has_commit_for_frid(build_folder_path, prev_frid):
96+
if not git_utils.has_commit_for_frid(
97+
build_folder_path, prev_frid, module_name
98+
):
8199
raise MissingPreviousFridCommitsError(
82100
f"Cannot render from FRID {first_render_frid}: "
83101
f"Missing commit for previous FRID {prev_frid} in {build_folder_path}. "
@@ -86,7 +104,9 @@ def _validate_previous_frid_commits_exist(
86104

87105
# Check in conformance tests folder (only if conformance tests are enabled)
88106
if self.args.render_conformance_tests:
89-
if not git_utils.has_commit_for_frid(conformance_tests_path, prev_frid):
107+
if not git_utils.has_commit_for_frid(
108+
conformance_tests_path, prev_frid, module_name
109+
):
90110
raise MissingPreviousFridCommitsError(
91111
f"Cannot render from FRID {first_render_frid}: "
92112
f"Missing commit for previous FRID {prev_frid} in {conformance_tests_path}. "

plain2code.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import plain_file
1515
import plain_spec
1616
from event_bus import EventBus
17-
from module_renderer import ModuleRenderer
17+
from module_renderer import MissingPreviousFridCommitsError, ModuleRenderer
1818
from plain2code_arguments import parse_arguments
1919
from plain2code_console import console
2020
from plain2code_exceptions import InvalidFridArgument, MissingAPIKey, PlainSyntaxError
@@ -247,6 +247,10 @@ def main():
247247
console.error(f"Error rendering plain code: {str(e)}\n")
248248
console.debug(f"Render ID: {run_state.render_id}")
249249
dump_crash_logs(args)
250+
except MissingPreviousFridCommitsError as e:
251+
console.error(f"Error rendering plain code: {str(e)}\n")
252+
console.debug(f"Render ID: {run_state.render_id}")
253+
dump_crash_logs(args)
250254
except MissingAPIKey as e:
251255
console.error(f"Missing API key: {str(e)}\n")
252256
except Exception as e:

0 commit comments

Comments
 (0)