@@ -21,47 +21,51 @@ def parse_metrics(content):
2121def parse_diff_file (diff_path ):
2222 """
2323 Parses a unified diff file and categorizes changes into added, removed, and modified metrics.
24+ Also captures the raw diff sections for each metric.
2425 """
2526 changes = {
2627 'added' : defaultdict (list ),
2728 'removed' : defaultdict (list ),
2829 'modified' : defaultdict (list )
2930 }
3031
31- current_block = []
32- current_change = None
32+ # Store raw diff sections for each metric - just collect all lines related to each metric
33+ raw_diff_sections = defaultdict ( list )
3334
3435 with open (diff_path , 'r' ) as f :
35- for line in f :
36- line = line .strip ()
37- # Skip diff headers
38- if line .startswith ('+++' ) or line .startswith ('---' ):
39- continue
40-
41- # Start new block for changes
42- if line .startswith ('+' ) or line .startswith ('-' ):
43- if current_block and current_change :
44- metric_name = extract_metric_name (current_block [0 ])
45- if metric_name :
46- changes [current_change ][metric_name ].append ('\n ' .join (current_block ))
47- current_block = [line [1 :].strip ()] # Remove +/-
48- current_change = 'added' if line .startswith ('+' ) else 'removed'
49- elif line .startswith (' ' ):
50- if current_block :
51- current_block .append (line .strip ())
36+ lines = f .readlines ()
37+
38+ current_metric = None
39+
40+ for line in lines :
41+ original_line = line .rstrip ()
42+ stripped = line .strip ()
43+
44+ # Skip diff headers
45+ if stripped .startswith ('+++' ) or stripped .startswith ('---' ):
46+ continue
47+
48+ # Check if this line contains a metric change
49+ if stripped .startswith ('+' ) or stripped .startswith ('-' ):
50+ metric_name = extract_metric_name (stripped [1 :].strip ())
51+ if metric_name :
52+ # Track the change type
53+ change_type = 'added' if stripped .startswith ('+' ) else 'removed'
54+ changes [change_type ][metric_name ].append (stripped [1 :].strip ())
55+
56+ # Always add to raw diff sections regardless of change type
57+ raw_diff_sections [metric_name ].append (original_line )
58+ current_metric = metric_name
5259 else :
53- if current_block and current_change :
54- metric_name = extract_metric_name (current_block [0 ])
55- if metric_name :
56- changes [current_change ][metric_name ].append ('\n ' .join (current_block ))
57- current_block = []
58- current_change = None
59-
60- # Process any remaining block
61- if current_block and current_change :
62- metric_name = extract_metric_name (current_block [0 ])
63- if metric_name :
64- changes [current_change ][metric_name ].append ('\n ' .join (current_block ))
60+ # If we're in a metric section, keep adding lines
61+ if current_metric :
62+ raw_diff_sections [current_metric ].append (original_line )
63+ elif stripped .startswith (' ' ) and current_metric :
64+ # Context line - add to current metric's raw section
65+ raw_diff_sections [current_metric ].append (original_line )
66+ else :
67+ # End of current metric section
68+ current_metric = None
6569
6670 # Identify modified metrics (same metric name with both additions and removals)
6771 common_metrics = set (changes ['added' ].keys ()) & set (changes ['removed' ].keys ())
@@ -71,17 +75,31 @@ def parse_diff_file(diff_path):
7175 'removed' : changes ['removed' ].pop (metric )
7276 }
7377
74- return changes
78+ return changes , raw_diff_sections
7579
7680def extract_metric_name (line ):
7781 """Extracts metric name from a metric line, matching the diff generation format"""
7882 if '{' in line :
7983 return line .split ('{' )[0 ].strip ()
8084 return line .strip ()
8185
82- def generate_diff_summary ( changes ):
86+ def get_raw_diff_sample ( raw_lines , max_lines = 7 ):
8387 """
84- Generates a markdown summary from the parsed diff changes.
88+ Get sample raw diff lines, preserving original diff formatting.
89+ """
90+ if not raw_lines :
91+ return []
92+
93+ # Take up to max_lines
94+ sample_lines = raw_lines [:max_lines ]
95+ if len (raw_lines ) > max_lines :
96+ sample_lines .append ("..." )
97+
98+ return sample_lines
99+
100+ def generate_diff_summary (changes , raw_diff_sections ):
101+ """
102+ Generates a markdown summary from the parsed diff changes with raw diff samples.
85103 """
86104 summary = ["## 📊 Metrics Diff Summary\n " ]
87105
@@ -100,12 +118,30 @@ def generate_diff_summary(changes):
100118 summary .append ("\n ### 🆕 Added Metrics" )
101119 for metric , samples in changes ['added' ].items ():
102120 summary .append (f"- `{ metric } ` ({ len (samples )} variants)" )
121+ raw_samples = get_raw_diff_sample (raw_diff_sections .get (metric , []))
122+ if raw_samples :
123+ summary .append ("<details>" )
124+ summary .append ("<summary>View diff sample</summary>" )
125+ summary .append ("" )
126+ summary .append ("```diff" )
127+ summary .extend (raw_samples )
128+ summary .append ("```" )
129+ summary .append ("</details>" )
103130
104131 # Removed metrics
105132 if changes ['removed' ]:
106133 summary .append ("\n ### ❌ Removed Metrics" )
107134 for metric , samples in changes ['removed' ].items ():
108135 summary .append (f"- `{ metric } ` ({ len (samples )} variants)" )
136+ raw_samples = get_raw_diff_sample (raw_diff_sections .get (metric , []))
137+ if raw_samples :
138+ summary .append ("<details>" )
139+ summary .append ("<summary>View diff sample</summary>" )
140+ summary .append ("" )
141+ summary .append ("```diff" )
142+ summary .extend (raw_samples )
143+ summary .append ("```" )
144+ summary .append ("</details>" )
109145
110146 # Modified metrics
111147 if changes ['modified' ]:
@@ -115,6 +151,16 @@ def generate_diff_summary(changes):
115151 summary .append (f" - Added variants: { len (versions ['added' ])} " )
116152 summary .append (f" - Removed variants: { len (versions ['removed' ])} " )
117153
154+ raw_samples = get_raw_diff_sample (raw_diff_sections .get (metric , []))
155+ if raw_samples :
156+ summary .append (" <details>" )
157+ summary .append (" <summary>View diff sample</summary>" )
158+ summary .append ("" )
159+ summary .append (" ```diff" )
160+ summary .extend ([f" { line } " for line in raw_samples ])
161+ summary .append (" ```" )
162+ summary .append (" </details>" )
163+
118164 return "\n " .join (summary )
119165
120166def main ():
@@ -124,8 +170,8 @@ def main():
124170
125171 args = parser .parse_args ()
126172
127- changes = parse_diff_file (args .diff )
128- summary = generate_diff_summary (changes )
173+ changes , raw_diff_sections = parse_diff_file (args .diff )
174+ summary = generate_diff_summary (changes , raw_diff_sections )
129175
130176 with open (args .output , 'w' ) as f :
131177 f .write (summary )
0 commit comments