-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_repo_crawler.py
200 lines (123 loc) · 5.8 KB
/
run_repo_crawler.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
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
import argparse
import json
from tqdm import tqdm
from collections import namedtuple
from collections import OrderedDict
from pydriller import Repository, ModificationType
from code_diff.diff_utils import parse_hunks
from tssb_miner.diff_utils import iter_stmts
from tssb_miner.diff_utils import has_diff, diff_tokens
from tssb_miner.tokenizers import tokenize
LANG_EXTENSIONS = {
"python": [".py"],
"java": [".java"],
"javascript": [".js"]
}
SingleLineCommit = namedtuple("SingleLineCommit", ["project_url", "commit", "modfile", "lang"])
# Identify single line commits --------------------------------
def all_statements_common(lines, common_set, lang):
for i, line in lines.items():
if i in common_set: continue
if is_statement(line, lang): return False
return True
def is_single_line(parsed_diff, lang):
added, rms = OrderedDict(parsed_diff["added"]), OrderedDict(parsed_diff["deleted"])
common_lines = set.intersection(set(added.keys()), set(rms.keys()))
if len(common_lines) == 0: return False
# All non-common lines have to be comments!
if not all_statements_common(added, common_lines, lang): return False
if not all_statements_common(rms, common_lines, lang): return False
diff_lines = 0
for line_ix in common_lines:
add_line, rm_line = added[line_ix], rms[line_ix]
is_add_stmt = is_statement(add_line, lang)
is_rm_stmt = is_statement(rm_line, lang)
if not is_add_stmt and not is_rm_stmt: continue
if is_add_stmt != is_rm_stmt: return False # Here we uncomment things or remove a code (no code change)
if has_diff(add_line, rm_line, lang): diff_lines += 1
if diff_lines > 1: break
return diff_lines == 1
# Test if single token modification ---------------------------
def is_single_token_mod(diff, lang):
hunks = parse_hunks(diff)
if len(hunks) != 1: return False
hunk = hunks[0]
new_lines, old_lines = list(iter_stmts(hunk.after)), list(iter_stmts(hunk.before))
if len(new_lines) != len(old_lines): return False
if len(new_lines) == 0: return False
single_diffs = 0
for i in range(len(new_lines)):
diff = diff_tokens(new_lines[i], old_lines[i], lang)
if len(diff) > 1: return False
if len(diff) == 1: single_diffs += 1
return single_diffs == 1
def single_token_mod(diff, lang):
hunks = parse_hunks(diff)
single_diff = None
for hunk in hunks:
new_lines, old_lines = list(iter_stmts(hunk.after)), list(iter_stmts(hunk.before))
if len(new_lines) != len(old_lines): return "None"
if len(new_lines) == 0: return "None"
for i in range(len(new_lines)):
diff = diff_tokens(new_lines[i], old_lines[i], lang)
if len(diff) == 0: continue
if len(diff) > 1: return "None"
if single_diff is None:
single_diff = diff[0][0][1]
else:
return "None"
return single_diff if single_diff is not None else "None"
def is_likely_bug(commit_msg):
return any(t in commit_msg.lower() for t in ["error", "bug", "fix", "issue", "mistake",
"incorrect", "fault", "defect", "flaw", "type"])
def is_infunction(modfile):
return len(modfile.changed_methods) > 0
def create_result_entry(commit, modfile, lang = "python"):
return {
"project": commit.project_name,
"commit_sha": commit.hash,
"parent_sha": commit.parents[0],
"file_path" : modfile.old_path,
# Metrics useful for filtering the index
"likely_bug" : is_likely_bug(commit.msg),
"comodified" : len(commit.modified_files) > 1,
"in_function": is_infunction(modfile),
"single_token_mod": single_token_mod(modfile.diff, lang),
# Diff used to update the code
"diff": modfile.diff,
}
def parse_commit(commit, lang = "python"):
for modfile in commit.modified_files:
# Heuristics to exclude mod files
if modfile.change_type != ModificationType.MODIFY: continue
if not any(modfile.filename.endswith(ext) for ext in LANG_EXTENSIONS[lang]): continue
if not is_single_line(modfile.diff_parsed, lang): continue
yield modfile
def crawl_single_line_commits(repo_url, lang = "python"):
repo = Repository(repo_url, only_modifications_with_file_types=LANG_EXTENSIONS[lang])
T = tqdm(repo.traverse_commits())
for commit in T:
# Filter to exclude commits
if commit.merge: continue
if len(commit.parents) != 1: continue
for single_line_mod in parse_commit(commit, lang):
yield SingleLineCommit(repo_url, commit, single_line_mod, lang)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("repo_url")
parser.add_argument("output_file")
parser.add_argument("--lang", default="python")
args = parser.parse_args()
with open(args.output_file, "w") as o:
for slc in crawl_single_line_commits(args.repo_url, args.lang):
slc_info = create_result_entry(slc.commit, slc.modfile, slc.lang)
if slc_info is None: continue # We reject a single code change because an AST analysis found an incompatible type
slc_info["project_url"] = slc.project_url
o.write(json.dumps(slc_info) + "\n")
# Helpers ----------------------------------------------------------------
def is_statement(line, lang):
if len(line) == 0: return False
return any(tok_type != "comment" for _, tok_type in tokenize(line, lang))
# ------------------------------------------------------------------------
if __name__ == '__main__':
main()