1
+ import os
2
+ from typing import Union
3
+
1
4
from git import Repo
2
5
6
+ import file_utils
7
+
3
8
RENDERED_FRID_MESSAGE = "Changes related to Functional requirement ID (FRID): {}"
9
+ RENDER_ID_MESSAGE = "Render ID: {}"
4
10
BASE_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"
5
16
6
17
# The commit hash of the empty tree
7
18
EMPTY_TREE_COMMIT_HASH = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
8
19
9
20
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 )
13
33
14
34
return repo
15
35
16
36
17
- def is_dirty (repo_path ) :
37
+ def is_dirty (repo_path : Union [ str , os . PathLike ]) -> bool :
18
38
"""Checks if the repository is dirty."""
19
39
repo = Repo (repo_path )
20
40
return repo .is_dirty (untracked_files = True )
21
41
22
42
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 :
24
46
"""Adds all files to the git repository and commits them."""
25
47
repo = Repo (repo_path )
26
48
repo .git .add ("." )
27
49
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 )} "
29
59
30
60
# Check if there are any changes to commit
31
61
if not repo .is_dirty (untracked_files = True ):
@@ -36,15 +66,15 @@ def add_all_files_and_commit(repo_path, commit_message, frid):
36
66
return repo
37
67
38
68
39
- def revert_changes (repo_path ) :
69
+ def revert_changes (repo_path : Union [ str , os . PathLike ]) -> Repo :
40
70
"""Reverts all changes made since the last commit."""
41
71
repo = Repo (repo_path )
42
72
repo .git .reset ("--hard" )
43
73
repo .git .clean ("-xdf" )
44
74
return repo
45
75
46
76
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 :
48
78
"""Finds commit with given frid mentioned in the commit message and reverts the branch to it."""
49
79
repo = Repo (repo_path )
50
80
commit = _get_commit_with_frid (repo , frid )
@@ -53,15 +83,16 @@ def revert_to_commit_with_frid(repo_path, frid):
53
83
return repo
54
84
55
85
56
- def diff (repo_path , previous_frid = None ):
86
+ def diff (repo_path : Union [ str , os . PathLike ], previous_frid : str = None ) -> dict :
57
87
"""
58
88
Get the git diff between the current code state and the previous frid using git's native diff command.
59
89
If previous_frid is not provided, we try to find the commit related to the copy of the base folder.
60
90
Removes the 'diff --git' and 'index' lines to get clean unified diff format.
61
91
62
92
63
93
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
65
96
66
97
Returns:
67
98
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):
134
165
return diff_dict
135
166
136
167
137
- def _get_commit_with_frid (repo , frid ) :
168
+ def _get_commit_with_frid (repo : Repo , frid : str ) -> str :
138
169
"""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
+ )
140
174
if not commit :
141
175
raise Exception (f"No commit with frid { frid } found." )
142
176
return commit
143
177
144
178
145
- def _get_base_folder_commit (repo ) :
179
+ def _get_base_folder_commit (repo : Repo ) -> str :
146
180
"""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