-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_data_builder.py
103 lines (90 loc) · 4.23 KB
/
check_data_builder.py
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
import subprocess
import os
import pandas as pd
from tqdm import tqdm
def read_file_in_commit(repo_storage, id, rev_path, commit_hash):
repo_name = id.split("__")[0]
repo_dir = os.path.join(repo_storage, repo_name)
cmd = (
f"git config --global --add safe.directory {repo_dir} && "
f"cd {repo_dir} && "
f"git cat-file -p {commit_hash}:{rev_path}"
)
try:
res = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError:
return None
return res.decode("utf-8")
def read_file_in_checkout(data_storage, id, rev_path, commit_hash):
repo_dir = os.path.join(data_storage, id)
versions = list(filter(lambda folder: "ver" in folder, os.listdir(repo_dir)))
if commit_hash in versions[0]:
file_path = os.path.join(repo_dir, versions[0], rev_path)
else:
file_path = os.path.join(repo_dir, versions[1], rev_path)
with open(file_path, "r", encoding="utf-8") as f:
code = f.read()
return code
def check_diff(code1, code2):
return code1.replace("\r\n", "\n") != code2.replace("\r\n", "\n")
def check_data_builder():
repo_storage = "/drive1/phatnt/zTrans/data/repos"
data_storage = "/drive1/thieulvd/code-migration"
df = pd.read_csv("/drive2/phatnt/zTrans/thieulvd/data/migrations_36_file.csv")
cnt_diff = 0
for index, row in tqdm(df.iterrows(), total=len(df)):
id = row["id"]
prev_commit = row["prev_commit"]
end_commit = row["end_commit"]
added = row["added"]
deleted = row["deleted"]
modified = row["modified"]
renamed_unchanged = row["renamed_unchanged"]
renamed_modified = row["renamed_modified"]
for file in eval(modified):
if file.endswith(".java"):
code1 = read_file_in_commit(repo_storage, id, file, prev_commit)
code2 = read_file_in_checkout(data_storage, id, file, prev_commit)
cnt_diff += check_diff(code1, code2)
code1 = read_file_in_commit(repo_storage, id, file, end_commit)
code2 = read_file_in_checkout(data_storage, id, file, end_commit)
cnt_diff += check_diff(code1, code2)
print(cnt_diff)
for file in eval(added):
if file.endswith(".java"):
code1 = read_file_in_commit(repo_storage, id, file, end_commit)
code2 = read_file_in_checkout(data_storage, id, file, end_commit)
cnt_diff += check_diff(code1, code2)
print(cnt_diff)
for file in eval(deleted):
if file.endswith(".java"):
code1 = read_file_in_commit(repo_storage, id, file, prev_commit)
code2 = read_file_in_checkout(data_storage, id, file, prev_commit)
cnt_diff += check_diff(code1, code2)
print(cnt_diff)
for item in eval(renamed_modified):
file1, file2, _ = item.values()
if file1.endswith(".java"):
code1 = read_file_in_commit(repo_storage, id, file1, prev_commit)
code2 = read_file_in_checkout(data_storage, id, file1, prev_commit)
cnt_diff += check_diff(code1, code2)
print(cnt_diff)
if file2.endswith(".java"):
code1 = read_file_in_commit(repo_storage, id, file2, end_commit)
code2 = read_file_in_checkout(data_storage, id, file2, end_commit)
cnt_diff += check_diff(code1, code2)
print(cnt_diff)
for item in eval(renamed_unchanged):
file1, file2 = item.values()
if file1.endswith(".java"):
code1 = read_file_in_commit(repo_storage, id, file1, prev_commit)
code2 = read_file_in_checkout(data_storage, id, file1, prev_commit)
cnt_diff += check_diff(code1, code2)
print(cnt_diff)
if file2.endswith(".java"):
code1 = read_file_in_commit(repo_storage, id, file2, end_commit)
code2 = read_file_in_checkout(data_storage, id, file2, end_commit)
cnt_diff += check_diff(code1, code2)
print(cnt_diff)
return cnt_diff
print(check_data_builder())