13
13
from mindflow .utils .prompt_builders import build_context_prompt
14
14
from mindflow .utils .prompts import GIT_DIFF_PROMPT_PREFIX
15
15
16
+ from mindflow .utils .diff_parser import parse_git_diff , IGNORE_FILE_EXTENSIONS
17
+
16
18
17
19
def run_diff (args : Tuple [str ]) -> str :
18
20
"""
@@ -25,12 +27,16 @@ def run_diff(args: Tuple[str]) -> str:
25
27
26
28
# Execute the git diff command and retrieve the output as a string
27
29
diff_result = subprocess .check_output (command ).decode ("utf-8" )
28
-
29
30
if diff_result .strip () == "" :
30
31
return "No staged changes."
31
32
33
+ diff_dict , excluded_filenames = parse_git_diff (diff_result )
34
+
35
+ if len (diff_dict ) <= 0 :
36
+ return "No staged changes."
37
+
32
38
batched_parsed_diff_result = batch_git_diffs (
33
- parse_git_diff ( diff_result ) , token_limit = completion_model .hard_token_limit
39
+ diff_dict , token_limit = completion_model .hard_token_limit
34
40
)
35
41
36
42
response : str = ""
@@ -58,37 +64,22 @@ def run_diff(args: Tuple[str]) -> str:
58
64
for future in concurrent .futures .as_completed (futures ):
59
65
response += future .result ()
60
66
67
+ if len (excluded_filenames ) > 0 :
68
+ response += f"\n \n NOTE: The following files were excluded from the diff: { ', ' .join (excluded_filenames )} "
69
+
61
70
return response
62
71
63
72
64
73
import re
65
74
66
75
67
- def parse_git_diff (diff_output : str ) -> List [Tuple [str , str ]]:
68
- file_diffs : List [Dict [str , List [str ]]] = []
69
- current_diff : Optional [Dict [str , List [str ]]] = None
70
- for line in diff_output .split ("\n " ):
71
- if line .startswith ("diff --git" ):
72
- if current_diff is not None :
73
- file_diffs .append (current_diff )
74
- current_diff = {"file_name" : None , "content" : []} # type: ignore
75
- match = re .match (r"^diff --git a/(.+?) b/.+?$" , line )
76
- if match :
77
- current_diff ["file_name" ] = match .group (1 ) # type: ignore
78
- if current_diff is not None :
79
- current_diff ["content" ].append (line )
80
- if current_diff is not None :
81
- file_diffs .append (current_diff )
82
- return [(diff ["file_name" ], "\n " .join (diff ["content" ])) for diff in file_diffs ] # type: ignore
83
-
84
-
85
76
def batch_git_diffs (
86
- file_diffs : List [ Tuple [ str , str ] ], token_limit : int
77
+ file_diffs : Dict [ str , str ], token_limit : int
87
78
) -> List [List [Tuple [str , str ]]]:
88
79
batches = []
89
80
current_batch : List = []
90
81
current_batch_size = 0
91
- for file_name , diff_content in file_diffs :
82
+ for file_name , diff_content in file_diffs . items () :
92
83
if len (diff_content ) > token_limit :
93
84
chunks = [
94
85
diff_content [i : i + token_limit ]
0 commit comments