-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_modifications.py
More file actions
1206 lines (996 loc) · 50 KB
/
Copy pathcollect_modifications.py
File metadata and controls
1206 lines (996 loc) · 50 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import subprocess
import re
import os
import sys
import json
import openai
import tempfile
from openai import OpenAI
from typing import List, Dict
import time
import json
import random
import subprocess
from datetime import datetime, timedelta
api_key = os.getenv("OPENAI_API_KEY", "Place_your_API_key_here")
if api_key == "Place_your_API_key_here":
print("⚠️ Error: OpenAI API Key is missing. Please provide it via environment variable.")
def run_git_command(cmd, cwd):
result = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=cwd, encoding='utf-8', errors='ignore')
return result.stdout.strip().split('\n') if result.stdout else []
def extract_reverted_commit(message):
"""Extract commit hash from revert message"""
patterns = [
r'[Rr]evert\s+"?commit\s+([0-9a-f]{6,40})',
r'[Rr]evert\s+"?([0-9a-f]{6,40})\s+commit',
r'[Rr]evert\s+commit\s+([0-9a-f]{6,40})',
r'[Rr]evert\s+([0-9a-f]{6,40})\b',
r'[Rr]evert\s+"?(?:commit\s+)?([0-9a-f]{6,40})',
r'[Rr]everts?\s+([0-9a-f]{6,40})',
r'[Tt]his\s+reverts?\s+commit\s+([0-9a-f]{6,40})',
]
for pattern in patterns:
match = re.search(pattern, message)
if match:
return match.group(1)
return None
def save_revert_pairs(repo_name):
"""
Scans git logs for revert commits to extract their referenced original hashes,
filtering for single-file C/C++ edits to establish initial defective candidates.
"""
repo_path = os.path.join(os.path.dirname(__file__), repo_name)
if not os.path.exists(repo_path):
print(f"Repository {repo_name} not found at {repo_path}")
return
# Revert commits with referenced commits
print(f"Collecting revert commits from {repo_name}...")
cmd = 'git log --all --grep="revert" -i --format="%H|%s"'
reverts = run_git_command(cmd, repo_path)
print(len(reverts))
cnt = 0
revert_data = []
for ind, line in enumerate(reverts):
if ind%20==0:
print(ind)
if '|' in line:
commit_hash, subject = line.split('|', 1)
# Get full commit message
cmd = f'git log -1 --format="%B" {commit_hash}'
full_msg = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore').stdout.strip()
# Check if contains commit reference
reverted_hash = extract_reverted_commit(full_msg)
if reverted_hash:
# Verify and get reverted commit info
cmd = f'git log -1 --format="%s" {reverted_hash}'
result = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
if result.returncode == 0:
# Check if revert commit only modified one file
cmd = f'git show --name-only --pretty="" {commit_hash}'
files_modified = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8',
errors='ignore').stdout.strip().split('\n')
files_modified = [f for f in files_modified if f] # Remove empty strings
if len(files_modified) == 1:
file_path = files_modified[0]
# Skip header files and non-C/C++ files
if file_path.endswith(('.c', '.cpp', '.cc', '.cxx', '.c++')):
reverted_msg = result.stdout.strip()
# Get full message for reverted commit
cmd = f'git log -1 --format="%B" {reverted_hash}'
reverted_full_msg = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8',
errors='ignore').stdout.strip()
# Get date for reverted commit
cmd = f'git log -1 --format="%ai" {reverted_hash}'
date_result = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
date_str = date_result.stdout.strip()
if date_result.returncode != 0 or not date_str:
print(ind, "wrong", commit_hash)
continue
else:
date = date_str.split()[0] # YYYY-MM-DD
revert_data.append({
"date": date,
"defective_modification": 1,
"project": repo_name,
"file_path": file_path,
"commit": reverted_hash,
"commit_message": reverted_full_msg,
"revert_commit": commit_hash,
"revert_message": full_msg
})
cnt+=1
if cnt%20==0:
print("Collected: ", cnt)
revert_data.sort(key=lambda x: x['date'])
cutoff_date = "2025-02-28"
revert_data = [item for item in revert_data if item['date'] < cutoff_date]
# Save as JSONL
with open(f'{repo_name}_defective_1.jsonl', 'w', encoding='utf-8') as f:
for item in revert_data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
print(f"Found {len(revert_data)} revert pairs")
print(f"Saved to {repo_name}_defective_1.jsonl")
def extract_single_function_commits(jsonl_file, repo_name):
"""
Parses git diffs and uses ctags to isolate modifications confined to exactly one function,
extracting the raw "before" and "after" code states for analysis.
"""
cnt=0
repo_path = os.path.join(os.path.dirname(__file__), repo_name)
output_data = []
reverted_commits_set = set()
function_set = set()
with open(jsonl_file, 'r', encoding='utf-8') as f:
for line in f:
data = json.loads(line)
commit_hash = data['commit']
file_path = data['file_path']
if commit_hash in reverted_commits_set:
continue
# Get the diff of the revert commit
cmd = ['git', 'diff', f'{commit_hash}^', commit_hash, '--', file_path]
diff_result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
if diff_result.returncode != 0:
print("Error getting diff")
continue
# print(diff_result)
if not diff_result.stdout:
print("Empty diff")
continue
# Parse diff to find modified line numbers
modified_lines = {'added': [], 'deleted': []}
current_line_old = 0
current_line_new = 0
for line in diff_result.stdout.split('\n'):
if line.startswith('@@'):
match = re.search(r'@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@', line)
if match:
current_line_old = int(match.group(1))
current_line_new = int(match.group(2))
elif line.startswith('+') and not line.startswith('+++'):
modified_lines['added'].append(current_line_new)
current_line_new += 1
elif line.startswith('-') and not line.startswith('---'):
modified_lines['deleted'].append(current_line_old)
current_line_old += 1
else:
current_line_old += 1
current_line_new += 1
if not modified_lines['added'] and not modified_lines['deleted']:
print("No modified lines found")
continue
print(f"Modified lines: {modified_lines['added'][:10]}{'...' if len(modified_lines['added']) > 10 else ''}")
print(f"Modified lines: {modified_lines['deleted'][:10]}{'...' if len(modified_lines['deleted']) > 10 else ''}")
parent_functions=[]
current_functions=[]
if modified_lines['deleted']:
parent_functions = get_function_names_at_lines(
f"{commit_hash}^", file_path, modified_lines['deleted'], repo_path
)
# For added lines - use current commit
if modified_lines['added']:
current_functions = get_function_names_at_lines(
commit_hash, file_path, modified_lines['added'], repo_path
)
# Combine both
function_names = set(parent_functions + current_functions)
if len(function_names)==1:
print(function_names)
func_name = list(function_names)[0]
# Get before version (at reverted_commit^)
cmd = ['git', 'show', f'{commit_hash}^:{file_path}']
before_content = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore').stdout
# Get after version (at reverted_commit)
cmd = ['git', 'show', f'{commit_hash}:{file_path}']
after_content = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore').stdout
# Extract function from both versions
before_func = extract_function_by_name(before_content, func_name)
after_func = extract_function_by_name(after_content, func_name)
if before_func==after_func:
print("Identical Functions")
continue
if cnt%100==0:
print("before: \n", before_func)
print("after: \n",after_func)
if before_func and after_func and before_func not in function_set:
output_data.append({
"date": data["date"],
"defective_modification": data["defective_modification"],
"project": data["project"],
"file_path": data["file_path"],
"function_name":func_name,
"function_before": before_func,
"function_after": after_func,
"commit": data['commit'],
"commit_message": data['commit_message'],
"revert_commit": data['revert_commit'],
"revert_message": data['revert_message']
})
reverted_commits_set.add(data['commit'])
function_set.add(before_func)
cnt+=1
print(cnt)
# Save results
with open(f'{repo_name}_defective_2.jsonl', 'w', encoding='utf-8') as f:
for item in output_data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
print(f"Extracted {len(output_data)} function pairs")
def extract_function_by_name(file_content, func_name):
"""
Locates a target function's start line using ctags and determines its exact boundaries
via brace-matching logic to extract the complete function body from the file content.
"""
if not file_content or not func_name:
return None
# Write to temp file
with tempfile.NamedTemporaryFile(mode='w', suffix='.c', delete=False, encoding='utf-8') as tmp:
tmp.write(file_content)
tmp_path = tmp.name
try:
# Run ctags with same options as original
ctags_cmd = ['ctags', '-x', '--c-kinds=f', tmp_path]
ctags_result = subprocess.run(ctags_cmd, capture_output=True, text=True)
if ctags_result.returncode != 0:
print(f"ctags run failed: {ctags_result.stderr}")
return None
# Parse ctags output
functions_info = []
for line in ctags_result.stdout.split('\n'):
if line:
parts = line.split()
if len(parts) >= 3:
name = parts[0]
line_no = int(parts[2])
functions_info.append((name, line_no))
# Find target function
target_func_info = None
for name, start_line in functions_info:
if name == func_name:
target_func_info = (name, start_line)
break
if target_func_info is None:
return None
# Get function using brace matching for accurate boundaries
lines = file_content.split('\n')
start_line = target_func_info[1] - 1 # Convert to 0-based index
# Find function boundaries using brace counting
brace_count = 0
func_started = False
end_line = start_line
in_string = False
in_char = False
in_comment = False
in_multiline_comment = False
for i in range(start_line, len(lines)):
line = lines[i]
j = 0
while j < len(line):
# Handle multi-line comments
if j < len(line) - 1 and line[j:j + 2] == '/*' and not in_string and not in_char:
in_multiline_comment = True
j += 2
continue
elif j < len(line) - 1 and line[j:j + 2] == '*/' and in_multiline_comment:
in_multiline_comment = False
j += 2
continue
# Skip if in multi-line comment
if in_multiline_comment:
j += 1
continue
# Handle single-line comments
if j < len(line) - 1 and line[j:j + 2] == '//' and not in_string and not in_char:
break # Skip rest of line
char = line[j]
# Handle string literals
if char == '"' and not in_char and (j == 0 or line[j - 1] != '\\'):
in_string = not in_string
# Handle character literals
elif char == "'" and not in_string and (j == 0 or line[j - 1] != '\\'):
in_char = not in_char
# Count braces only if not in string/char/comment
elif not in_string and not in_char:
if char == '{':
brace_count += 1
func_started = True
elif char == '}':
brace_count -= 1
j += 1
# Check if function is complete
if func_started and brace_count == 0:
end_line = i
break
# If brace matching failed, fall back to next function or end of file
if brace_count != 0:
# Sort functions by line number
functions_info.sort(key=lambda x: x[1])
# Find next function
target_index = None
for i, (name, line_no) in enumerate(functions_info):
if name == func_name:
target_index = i
break
if target_index is not None and target_index + 1 < len(functions_info):
end_line = functions_info[target_index + 1][1] - 2 # Line before next function
else:
end_line = len(lines) - 1
# Extract function content
function_lines = lines[start_line:end_line + 1]
# Remove trailing empty lines
while function_lines and not function_lines[-1].strip():
function_lines.pop()
return '\n'.join(function_lines)
except subprocess.CalledProcessError as e:
print(f"ctags run failed: {e}")
return None
except Exception as e:
print(f"Error during function extraction: {e}")
return None
finally:
# Clean up temp file
if os.path.exists(tmp_path):
os.unlink(tmp_path)
def get_function_names_at_lines(commit_hash, file_path, line_numbers, repo_path):
"""
Retrieves the file content at a specific commit and uses ctags to identify
which unique function names encompass the provided list of modified line numbers.
"""
# Get file content
cmd = ['git', 'show', f'{commit_hash}:{file_path}']
file_content = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore').stdout
# Write to temp file
with tempfile.NamedTemporaryFile(mode='w', suffix='.c', delete=False, encoding='utf-8') as tmp:
tmp.write(file_content)
tmp_path = tmp.name
# Run ctags
ctags_cmd = ['ctags', '-x', '--c-kinds=f', tmp_path]
ctags_result = subprocess.run(ctags_cmd, capture_output=True, text=True)
functions_info = []
for line in ctags_result.stdout.split('\n'):
if line:
parts = line.split()
if len(parts) >= 3:
func_name = parts[0]
line_no = int(parts[2])
functions_info.append((func_name, line_no))
functions_info.sort(key=lambda x: x[1])
# Clean up
os.unlink(tmp_path)
# Find functions containing modified lines
lines = file_content.split('\n')
found_functions = set()
lines_in_functions = 0
for mod_line in line_numbers:
for i, (name, start) in enumerate(functions_info):
end = functions_info[i + 1][1] if i + 1 < len(functions_info) else len(lines)
if start <= mod_line < end:
found_functions.add(name)
lines_in_functions += 1
break
if lines_in_functions == 0:
return []
return list(found_functions)
def filter_only_bug_related(jsonl_file, repo_name, api_key=None, gpt_model="gpt-4o"):
"""
Employs GPT-4o under a unanimous 3-vote triage rubric to filter out non-defect reverts,
retaining only high-confidence, genuinely bug-related modifications.
"""
# API key setup
if api_key:
client = OpenAI(api_key=api_key)
else:
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
if not client.api_key:
raise ValueError("OpenAI API key not found")
def is_likely_bug_related2(messages: List[str], num_runs=3, threshold=3) -> List[bool]:
"""Batch processing with GPT-4o"""
prompt = """You are a code review expert analyzing reverted commits.
For each case below, you'll see:
1. ORIGINAL: The commit that was later reverted (the commit we're evaluating)
2. REVERT: The commit message explaining why it was reverted
Your task: Classify this revert case into one of the following categories.
Return "NEW" if this introduces or exposes bugs:
- The ORIGINAL commit was NOT primarily a bug fix (e.g., new features, refactoring, optimization, code cleanup)
- BUT it was reverted because it introduced new bugs, exposed existing bugs, system crashed or caused functional regressions
Return "YES" if this is a FAILED bug fix attempt:
- The ORIGINAL commit was attempting to fix a bug (crashes, errors, memory issues, logic errors, security vulnerabilities)
- AND it was reverted because the bug fix failed or was incomplete (e.g., test flaky, breaks compile, better solution found)
Return "NO" for ALL other cases, including:
- Insufficient information in revert message
- "Not needed anymore" or "better solution found"
- Simple feature changes without clear technical problems
- Any case where you need to make assumptions about the reason
IMPORTANT: When in doubt, choose "NO". Only use "NEW" and "YES" when there's explicit evidence of technical problems.
For each case:
- Start your answer with either NEW,YES, or NO, followed by a brief explanation of your reasoning.
- After each case, add this exact separator on a new line: ------------------------------------------------
Cases to analyze:
"""
# Build batch prompt
for i, msg in enumerate(messages):
prompt += f"\n{i + 1}. {msg}"
# Store votes for each message across runs
votes1 = [0] * len(messages) # Count of YES votes for each message
votes2 = [0] * len(messages) # Count of YES votes for each message
expl = [{"NEW": [], "YES": [], "NO": []} for _ in range(len(messages))] # Store explanations by vote type
for run in range(num_runs):
try:
cnt=0
while True:
response = client.chat.completions.create(
model=gpt_model,
messages=[{"role": "user", "content": prompt}],
temperature=0.2,
max_tokens=1000
)
# Parse responses
raw_text = response.choices[0].message.content.strip()
blocks = [b.strip() for b in re.split(r'-{5,}\s*\n', raw_text) if b.strip()]
if len(blocks) < len(messages):
number_pattern = r'(?:^|\n)(?=\d+[\.\)]\s*(?:\*\*)?(?:YES|NO|NEW))'
alt_blocks = re.split(number_pattern, gpt_response)
alt_blocks = [b.strip() for b in alt_blocks if b.strip()]
if len(alt_blocks) > len(blocks):
blocks = alt_blocks
print(f"[INFO] Used number pattern to split response (found {len(blocks)} blocks)")
parsed_answers = []
for block in blocks:
match = re.search(r'\b(YES|NO|NEW)\b', block, re.IGNORECASE)
if match:
label = match.group(1).upper()
clean_block = re.sub(r'```', '', block)
clean_block = re.sub(r'^\d+\.\s*', '', clean_block)
parsed_answers.append((label, clean_block.strip()))
else:
print(f"[WARN] No label found in case:\n{block}\n")
filtered_answers = []
filtered_explanations = []
for i, (label, explanation) in enumerate(parsed_answers, 1):
print(f"Case {i}: {label}")
print(explanation)
print("------")
filtered_answers.append(label)
filtered_explanations.append(explanation)
print(filtered_answers)
# Validation check
cnt += 1
if len(filtered_answers) == len(messages):
# Count votes and store explanations
for i, (ans, explanation) in enumerate(zip(filtered_answers, filtered_explanations)):
if i < len(messages): # Safety check
ans_upper = ans.strip().upper()
if ans_upper == "NEW":
votes1[i] += 1
expl[i]["NEW"].append(explanation)
elif ans_upper == "YES":
votes2[i] += 1
expl[i]["YES"].append(explanation)
else: # NO
expl[i]["NO"].append(explanation)
break
else:
print("retry")
if cnt == 5:
print(f"Retry failed. Logging problematic prompt.")
# Save problematic prompt to file
with open('problematic_prompts.txt', 'a', encoding='utf-8') as f:
f.write(f"\n{'=' * 80}\n")
f.write(f"Timestamp: {datetime.now()}\n")
f.write(f"Run: {run + 1}, Expected: {len(messages)} responses\n")
f.write(f"Got: {len(filtered_answers)} responses\n")
f.write(f"Raw response:\n{response.choices[0].message.content}\n")
f.write(f"Prompt:\n{prompt}\n")
f.write(f"{'=' * 80}\n")
break
except Exception as e:
print(f"GPT API error on run {run + 1}: {e}")
continue
results = []
for i, (vote1, vote2) in enumerate(zip(votes1, votes2)):
total_votes = num_runs
bug_votes = vote1 + vote2
if vote1 >= threshold and vote2 == 0:
# NEW majority
label = "NEW"
ratio = f"{vote1}/{total_votes}"
# Save one of NEW explanations
explanation = expl[i]["NEW"][0] if expl[i]["NEW"] else "No explanation available"
elif vote2 >= threshold and vote1 == 0:
# YES majority
label = "YES"
ratio = f"{vote2}/{total_votes}"
# Save one of YES explanations
explanation = expl[i]["YES"][0] if expl[i]["YES"] else "No explanation available"
elif bug_votes >= threshold:
# MIX - Pick the explanation from major votes
label = "MIX"
ratio = f"{vote1}/{total_votes}+{vote2}/{total_votes}"
if vote1 > vote2:
explanation = expl[i]["NEW"][0] if expl[i]["NEW"] else "No explanation available"
elif vote2 > vote1:
explanation = expl[i]["YES"][0] if expl[i]["YES"] else "No explanation available"
else:
explanation = expl[i]["NEW"][0] if expl[i]["NEW"] else (
expl[i]["YES"][0] if expl[i]["YES"] else "No explanation available")
else:
label = "NO"
ratio = f"{bug_votes}/{total_votes}"
explanation = expl[i]["NO"][0] if expl[i]["NO"] else "No explanation available"
results.append((label, ratio, explanation))
return results
# Process data
filtered_data = []
batch_size = 5 # Process 5 at a time
with open(jsonl_file, 'r', encoding='utf-8') as f:
all_data = [json.loads(line) for line in f]
print(f"Processing {len(all_data)} commits...")
for i in range(0, len(all_data), batch_size):
batch = all_data[i:i + batch_size]
# Prepare messages for GPT
messages = []
for ind,item in enumerate(batch):
# Combine both commit messages for context
combined_msg = f"{ind}.\nORIGINAL:\n {item['commit_message'][:1000]}\n\nREVERT:\n {item['revert_message'][:1000]} \n"
messages.append(combined_msg)
decisions = is_likely_bug_related2(messages)
# Filter based on decisions
for item, (decision, confidence, expl) in zip(batch, decisions):
if decision != "NO":
if decision=="NEW":
item['commit_type'] = "CLEAN_TO_DEFECTIVE"
elif decision=="YES":
item['commit_type'] = "DEFECTIVE_TO_DEFECTIVE"
else:
item['commit_type'] = "MIX"
item['confidence'] = confidence
item['explanation'] = expl
print(decision, confidence, expl)
filtered_data.append(item)
print("--------------")
# Rate limiting
time.sleep(0.5)
if (i + batch_size) % 5 == 0:
print(f"Processed {min(i + batch_size, len(all_data))}/{len(all_data)}")
# Save filtered results
output_file = f'{repo_name}_defective_3.jsonl'
with open(output_file, 'w', encoding='utf-8') as f:
for item in filtered_data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
print(f"\nFiltering complete:")
print(f"Total commits: {len(all_data)}")
print(f"Bug-related commits: {len(filtered_data)} ({len(filtered_data) / len(all_data) * 100:.1f}%)")
print(f"Saved to: {output_file}")
return filtered_data
def collect_clean_commits(repo_name, defective_file):
"""
Gathers contemporary single-function commits and inspects up to 5 subsequent changes
via post-hoc history checks to eliminate potential false-clean labels.
"""
repo_path = os.path.join(os.path.dirname(__file__), repo_name)
defective_dates = []
seen_commits = set()
with open(defective_file, 'r', encoding='utf-8') as f:
for line in f:
data = json.loads(line)
defective_dates.append(data['date'])
seen_commits.add(data['commit'])
defective_dates.sort()
def_idx = 0 # Start from the earliest defective date
# Set cutoff date
cutoff_date = datetime(2025, 2, 28)
start_date = datetime(2020, 4, 3)
# Get all commits before cutoff date
cmd = ['git', 'log', f'--after={start_date.strftime("%Y-%m-%d")}', f'--before={cutoff_date.strftime("%Y-%m-%d")}', '--format=%H|%ad', '--date=short']
result = subprocess.run(cmd, capture_output=True, text=True, cwd=repo_path, encoding='utf-8', errors='ignore')
if result.returncode != 0:
print(f"Git error: {result.stderr}")
return
all_commits = [
parts for line in result.stdout.strip().split('\n') if line
for parts in [line.split('|')]
if len(parts) == 2
]
all_commits.reverse()
print(f"Number of commits: {len(all_commits)}")
# Define problematic keywords
problematic_keywords = [
'revert', 'rollback', 'undo', 'back out',
'incomplete', 'partial', 'temporary',
'fix bug', 'bug fix', 'hotfix',
'broke', 'broken', 'regression'
]
clean_candidates = []
collected_per_date = {}
seen_functions = set()
processed_combinations = set()
for commit_hash, commit_date in all_commits:
# Find which defective date range this belongs to
if def_idx >= len(defective_dates):
break
target_date = defective_dates[def_idx]
# Skip if this commit is older than current target_date
if commit_date < target_date:
continue
# If 4 already collected for this date, move to the next
if collected_per_date.get(target_date, 0) >= 4:
def_idx += 1
# Re-check this commit against the next target_date in next iteration
continue
# Single function check
result = is_single_function_modification(repo_path, commit_hash)
if not result[0]:
continue
_, file_path, func_name = result
combination_key = f"{file_path}::{func_name}"
if combination_key in processed_combinations:
print(f"Skipping already processed: {combination_key}")
continue
processed_combinations.add(combination_key)
# Get the function content at this commit
cmd = ['git', 'show', f'{commit_hash}^:{file_path}']
result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
function_before = extract_function_by_name(result.stdout, func_name)
cmd = ['git', 'show', f'{commit_hash}:{file_path}']
result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
function_target = extract_function_by_name(result.stdout, func_name)
if not function_target or not function_before:
continue
if function_before==function_target:
print("Identical result")
continue
# Get all subsequent commits that modify this file
cmd = ['git', 'log', f'{commit_hash}..HEAD', '--pretty=format:%H%n%B%n---END---', '--', file_path]
result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
if result.returncode != 0:
continue
commits_data = result.stdout.strip().split('---END---')
subsequent_commits = []
for commit_data in commits_data:
if not commit_data.strip():
continue
lines = commit_data.strip().split('\n')
if len(lines) >= 1:
commit_hash_temp = lines[0]
commit_message = '\n'.join(lines[1:]).strip()
subsequent_commits.append((commit_hash_temp, commit_message))
# Check up to 5 subsequent commits for function modifications
is_problematic = False
checked_count = 0
for sub_commit_hash, commit_message in subsequent_commits:
if checked_count >= 5:
break
# Get the function content at this subsequent commit
cmd = ['git', 'show', f'{sub_commit_hash}:{file_path}']
result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
function_after = extract_function_by_name(result.stdout, func_name)
# If function disappeared (renamed or deleted), stop checking
if not function_after:
is_problematic = True
break
# If function was modified
if function_target != function_after:
checked_count += 1
# Check for problematic keywords in commit message
commit_message_lower = commit_message.lower()
func_name_lower = func_name.lower()
print("Check", checked_count, sub_commit_hash, commit_message_lower[:20])
for keyword in problematic_keywords:
if keyword in commit_message_lower:
is_problematic = True
print("Buggy keyword found")
break
if is_problematic:
break
# Update function_target for next comparison
function_target = function_after
# If no problems found (either checked 10 modifications or function disappeared without issues)
if is_problematic:
print("Skipping", len(subsequent_commits), commit_date)
continue
# Dead code identification
if checked_count == 0:
print("No modifications found - skipping")
continue
# Get commit details
commit_details = get_commit_details(repo_path, commit_hash, file_path, func_name)
if commit_details is None:
continue # Skip this commit if details couldn't be retrieved
commit_details['project']=repo_name
commit_hash = commit_details["commit"]
function_code = commit_details["function_before"]
if commit_hash in seen_commits or function_code in seen_functions:
continue # Skip if either is already seen
seen_commits.add(commit_hash)
seen_functions.add(function_code)
print(commit_details["date"], commit_details["commit"])
clean_candidates.append(commit_details)
collected_per_date[target_date] = collected_per_date.get(target_date, 0) + 1
print(f"[{commit_date}] Collected {len(clean_candidates)} candidates")
# Save to file
repo_name = os.path.basename(repo_path)
with open(f'{repo_name}_clean_1.jsonl', 'w', encoding='utf_8') as f:
for candidate in clean_candidates:
f.write(json.dumps(candidate) + '\n')
print(f"Collected {len(clean_candidates)} clean commit candidates")
def get_commit_details(repo_path, commit_hash, file_path, func_name):
"""Get detailed information about a commit."""
msg_cmd = ['git', 'show', '-s', '--format=%B', commit_hash]
msg_result = subprocess.run(msg_cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
commit_message = msg_result.stdout.strip()
# Get commit date separately
date_cmd = ['git', 'show', '-s', '--format=%ai', commit_hash]
date_result = subprocess.run(date_cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
date = date_result.stdout.strip().split()[0] # 'YYYY-MM-DD'
cmd = ['git', 'show', f'{commit_hash}^:{file_path}']
result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
function_before = extract_function_by_name(result.stdout, func_name)
cmd = ['git', 'show', f'{commit_hash}:{file_path}']
result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
function_after = extract_function_by_name(result.stdout, func_name)
if function_before == function_after:
print(function_before)
print("=-----------------",commit_hash)
print(function_after)
print("Identical Functions")
return None
return {
"date": date,
"defective_modification": 0,
"project": repo_path,
"file_path": file_path,
"function_name": func_name,
"function_before": function_before,
"function_after": function_after,
"commit": commit_hash,
"commit_message": commit_message
}
def is_single_function_modification(repo_path, commit_hash):
"""Check if commit modifies only a single function."""
# Check single file modification
cmd = f'git show --name-only --pretty="" {commit_hash}'
files = subprocess.run(cmd, shell=True, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore').stdout.strip().split('\n')
files = [f for f in files if f]
if len(files) != 1 or not files[0].endswith(('.c', '.cpp', '.cc', '.cxx', '.c++')):
return False, False
file_path = files[0]
# Check single function modification
cmd = ['git', 'diff', f'{commit_hash}^', commit_hash, '--', file_path]
diff_result = subprocess.run(cmd, capture_output=True, text=True,
cwd=repo_path, encoding='utf-8', errors='ignore')
if not diff_result.stdout:
return False, False
# Parse diff to find modified line numbers
modified_lines = {'added': [], 'deleted': []}
current_line_old = 0
current_line_new = 0
for line in diff_result.stdout.split('\n'):
if line.startswith('@@'):
match = re.search(r'@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@', line)
if match:
current_line_old = int(match.group(1))
current_line_new = int(match.group(2))
elif line.startswith('+') and not line.startswith('+++'):
modified_lines['added'].append(current_line_new)
current_line_new += 1
elif line.startswith('-') and not line.startswith('---'):
modified_lines['deleted'].append(current_line_old)
current_line_old += 1
else:
current_line_old += 1
current_line_new += 1
if not modified_lines['added'] and not modified_lines['deleted']:
return False, False
parent_functions = []
current_functions = []
if modified_lines['deleted']:
parent_functions = get_function_names_at_lines(
f"{commit_hash}^", file_path, modified_lines['deleted'], repo_path
)
if modified_lines['added']:
current_functions = get_function_names_at_lines(
commit_hash, file_path, modified_lines['added'], repo_path
)
function_names = set(parent_functions + current_functions)
return len(function_names) == 1, file_path, list(function_names)[0] if len(function_names) == 1 else None
def classify_clean_commits(repo_name, clean_file, gpt_model="gpt-4o"):
"""
Utilizes GPT-4o to categorize clean candidates into bug fixes or feature improvements,
ensuring rigorous data sorting through a mandatory unanimous vote criteria.
"""
repo_path = os.path.join(os.path.dirname(__file__), repo_name)
# API key setup
if api_key:
client = OpenAI(api_key=api_key)
else:
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))