1+ import os
2+ from typing import Union
3+
14from git import Repo
25
6+ import file_utils
7+
38RENDERED_FRID_MESSAGE = "Changes related to Functional requirement ID (FRID): {}"
9+ RENDER_ID_MESSAGE = "Render ID: {}"
410BASE_FOLDER_COMMIT_MESSAGE = "Initialize build with Base Folder content"
11+ REFACTORED_CODE_COMMIT_MESSAGE = "Refactored code after implementing {}"
12+ CONFORMANCE_TESTS_PASSED_COMMIT_MESSAGE = (
13+ "Fixed issues in the implementation code identified during conformance testing"
14+ )
15+ FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE = "Functional requirement ID (FRID): {} fully implemented"
516
617# The commit hash of the empty tree
718EMPTY_TREE_COMMIT_HASH = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
819
920
10- def init_clean_repo (repo_path ):
11- """Initializes a new git repository in the given path."""
12- repo = Repo .init (repo_path )
21+ def init_git_repo (path_to_repo : Union [str , os .PathLike ]) -> Repo :
22+ """
23+ Initializes a new git repository in the given path.
24+ If folder does not exist, it creates it.
25+ If the folder already exists, it deletes the content of the folder.
26+ """
27+ if os .path .isdir (path_to_repo ):
28+ file_utils .delete_files_and_subfolders (path_to_repo )
29+ else :
30+ os .makedirs (path_to_repo )
31+
32+ repo = Repo .init (path_to_repo )
1333
1434 return repo
1535
1636
17- def is_dirty (repo_path ) :
37+ def is_dirty (repo_path : Union [ str , os . PathLike ]) -> bool :
1838 """Checks if the repository is dirty."""
1939 repo = Repo (repo_path )
2040 return repo .is_dirty (untracked_files = True )
2141
2242
23- def add_all_files_and_commit (repo_path , commit_message , frid ):
43+ def add_all_files_and_commit (
44+ repo_path : Union [str , os .PathLike ], commit_message : str , frid : str = None , render_id : str = None
45+ ) -> Repo :
2446 """Adds all files to the git repository and commits them."""
2547 repo = Repo (repo_path )
2648 repo .git .add ("." )
2749
28- message = f"{ commit_message } \n \n { RENDERED_FRID_MESSAGE .format (frid )} " if frid else commit_message
50+ message = f"{ commit_message } "
51+
52+ if frid or render_id :
53+ message += "\n \n " + "-" * 80
54+
55+ if frid :
56+ message += f"\n \n { RENDERED_FRID_MESSAGE .format (frid )} "
57+ if render_id :
58+ message += f"\n \n { RENDER_ID_MESSAGE .format (render_id )} "
2959
3060 # Check if there are any changes to commit
3161 if not repo .is_dirty (untracked_files = True ):
@@ -36,15 +66,15 @@ def add_all_files_and_commit(repo_path, commit_message, frid):
3666 return repo
3767
3868
39- def revert_changes (repo_path ) :
69+ def revert_changes (repo_path : Union [ str , os . PathLike ]) -> Repo :
4070 """Reverts all changes made since the last commit."""
4171 repo = Repo (repo_path )
4272 repo .git .reset ("--hard" )
4373 repo .git .clean ("-xdf" )
4474 return repo
4575
4676
47- def revert_to_commit_with_frid (repo_path , frid ) :
77+ def revert_to_commit_with_frid (repo_path : Union [ str , os . PathLike ], frid : str ) -> Repo :
4878 """Finds commit with given frid mentioned in the commit message and reverts the branch to it."""
4979 repo = Repo (repo_path )
5080 commit = _get_commit_with_frid (repo , frid )
@@ -53,15 +83,16 @@ def revert_to_commit_with_frid(repo_path, frid):
5383 return repo
5484
5585
56- def diff (repo_path , previous_frid = None ):
86+ def diff (repo_path : Union [ str , os . PathLike ], previous_frid : str = None ) -> dict :
5787 """
5888 Get the git diff between the current code state and the previous frid using git's native diff command.
5989 If previous_frid is not provided, we try to find the commit related to the copy of the base folder.
6090 Removes the 'diff --git' and 'index' lines to get clean unified diff format.
6191
6292
6393 Args:
64- repo_path (str): Path to the git repository
94+ repo_path (str | os.PathLike): Path to the git repository
95+ previous_frid (str): Functional requirement ID (FRID) of the previous commit
6596
6697 Returns:
6798 dict: Dictionary with file names as keys and their clean diff strings as values
@@ -134,14 +165,18 @@ def diff(repo_path, previous_frid=None):
134165 return diff_dict
135166
136167
137- def _get_commit_with_frid (repo , frid ) :
168+ def _get_commit_with_frid (repo : Repo , frid : str ) -> str :
138169 """Finds commit with given frid mentioned in the commit message."""
139- commit = repo .git .rev_list ("main" , "--grep" , RENDERED_FRID_MESSAGE .format (frid ), "-n" , "1" )
170+ current_branch = repo .active_branch .name
171+ commit = repo .git .rev_list (
172+ current_branch , "--grep" , FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE .format (frid ), "-n" , "1"
173+ )
140174 if not commit :
141175 raise Exception (f"No commit with frid { frid } found." )
142176 return commit
143177
144178
145- def _get_base_folder_commit (repo ) :
179+ def _get_base_folder_commit (repo : Repo ) -> str :
146180 """Finds commit related to copy of the base folder."""
147- return repo .git .rev_list ("main" , "--grep" , BASE_FOLDER_COMMIT_MESSAGE , "-n" , "1" )
181+ current_branch = repo .active_branch .name
182+ return repo .git .rev_list (current_branch , "--grep" , BASE_FOLDER_COMMIT_MESSAGE , "-n" , "1" )
0 commit comments