@@ -66,14 +66,13 @@ def trim_diff(diff: str, max_chars: int, debug: bool = False) -> str:
6666 lines = diff .split ("\n " )
6767 result_lines : list [str ] = []
6868 current_length = 0
69- current_file = None
7069 in_hunk = False
7170
7271 # First, count the number of actual change lines (+ or -) to prioritize
7372 change_lines_count = 0
7473 for line in lines :
7574 stripped = line .lstrip ()
76- if (stripped .startswith ("+" ) or stripped .startswith ("-" )) and not stripped in (
75+ if (stripped .startswith ("+" ) or stripped .startswith ("-" )) and stripped not in (
7776 "+" ,
7877 "-" ,
7978 ):
@@ -96,7 +95,7 @@ def trim_diff(diff: str, max_chars: int, debug: bool = False) -> str:
9695 # Mark change lines and surrounding context
9796 if (
9897 stripped .startswith ("+" ) or stripped .startswith ("-" )
99- ) and not stripped in ("+" , "-" ):
98+ ) and stripped not in ("+" , "-" ):
10099 # Mark this line and surrounding context (3 lines before and after)
101100 for j in range (max (0 , i - 3 ), min (len (lines ), i + 4 )):
102101 important_indices .add (j )
@@ -123,7 +122,6 @@ def trim_diff(diff: str, max_chars: int, debug: bool = False) -> str:
123122 )
124123 else :
125124 break
126- current_file = line
127125 in_hunk = False
128126
129127 # Start of a new hunk
@@ -161,6 +159,65 @@ def trim_diff(diff: str, max_chars: int, debug: bool = False) -> str:
161159 return result
162160
163161
162+ def filter_diff (
163+ raw_diff : str , include_filenames : bool = True , debug : bool = False
164+ ) -> str :
165+ """Filter git diff to remove metadata and keep only meaningful changes.
166+
167+ Args:
168+ raw_diff: Raw git diff output
169+ include_filenames: Whether to keep filenames in the output
170+ debug: Whether to enable debug logging
171+
172+ Returns:
173+ Filtered diff with only relevant content
174+ """
175+ logger = logging .getLogger (__name__ )
176+ logger .debug ("Filtering git diff to remove metadata" )
177+
178+ if not raw_diff :
179+ return ""
180+
181+ filtered_lines = []
182+ current_file = None
183+
184+ for line in raw_diff .split ("\n " ):
185+ # Skip common metadata lines
186+ if line .startswith ("diff --git" ) or line .startswith ("index " ):
187+ continue
188+
189+ # Handle filename markers but keep the filename
190+ if line .startswith ("--- " ):
191+ continue
192+ if line .startswith ("+++ " ):
193+ if line .startswith ("+++ b/" ) and include_filenames :
194+ current_file = line [6 :] # Remove the "+++ b/" prefix
195+ continue
196+
197+ # Add filename header if we just found a new file
198+ if current_file and include_filenames :
199+ filtered_lines .append (f"File: { current_file } " )
200+ current_file = None
201+
202+ # Keep everything else: hunk headers, context lines, and actual changes
203+ filtered_lines .append (line )
204+
205+ filtered_diff = "\n " .join (filtered_lines )
206+
207+ if debug :
208+ logger .debug (
209+ f"Original diff: { len (raw_diff )} chars, Filtered: { len (filtered_diff )} chars"
210+ )
211+ logger .debug (f"Removed { len (raw_diff ) - len (filtered_diff )} chars of metadata" )
212+ logger .debug (
213+ "Filtered diff preview (first 500 chars):\n " + filtered_diff [:500 ]
214+ if filtered_diff
215+ else "(empty)"
216+ )
217+
218+ return filtered_diff
219+
220+
164221def query_ai_service (
165222 prompt : str ,
166223 service_type : str ,
@@ -266,65 +323,6 @@ def create_commit(message: str, debug: bool = False) -> bool:
266323 return False
267324
268325
269- def filter_diff (
270- raw_diff : str , include_filenames : bool = True , debug : bool = False
271- ) -> str :
272- """Filter git diff to remove metadata and keep only meaningful changes.
273-
274- Args:
275- raw_diff: Raw git diff output
276- include_filenames: Whether to keep filenames in the output
277- debug: Whether to enable debug logging
278-
279- Returns:
280- Filtered diff with only relevant content
281- """
282- logger = logging .getLogger (__name__ )
283- logger .debug ("Filtering git diff to remove metadata" )
284-
285- if not raw_diff :
286- return ""
287-
288- filtered_lines = []
289- current_file = None
290-
291- for line in raw_diff .split ("\n " ):
292- # Skip common metadata lines
293- if line .startswith ("diff --git" ) or line .startswith ("index " ):
294- continue
295-
296- # Handle filename markers but keep the filename
297- if line .startswith ("--- " ):
298- continue
299- if line .startswith ("+++ " ):
300- if line .startswith ("+++ b/" ) and include_filenames :
301- current_file = line [6 :] # Remove the "+++ b/" prefix
302- continue
303-
304- # Add filename header if we just found a new file
305- if current_file and include_filenames :
306- filtered_lines .append (f"File: { current_file } " )
307- current_file = None
308-
309- # Keep everything else: hunk headers, context lines, and actual changes
310- filtered_lines .append (line )
311-
312- filtered_diff = "\n " .join (filtered_lines )
313-
314- if debug :
315- logger .debug (
316- f"Original diff: { len (raw_diff )} chars, Filtered: { len (filtered_diff )} chars"
317- )
318- logger .debug (f"Removed { len (raw_diff ) - len (filtered_diff )} chars of metadata" )
319- logger .debug (
320- "Filtered diff preview (first 500 chars):\n " + filtered_diff [:500 ]
321- if filtered_diff
322- else "(empty)"
323- )
324-
325- return filtered_diff
326-
327-
328326def generate_commit_messages (
329327 diff : str ,
330328 max_chars : int = 200 ,
0 commit comments