| 47 | return modified_func_ids |
| 48 | |
| 49 | class CommitGraph(Processor): |
| 50 | |
| 51 | def __init__(self, repo_path, language_str): |
| 52 | super().__init__(repo_path) |
| 53 | language = Language[language_str] |
| 54 | if language == Language.CPP: |
| 55 | self.fname_filter = fname_filter_cpp |
| 56 | self.func_extractor = get_func_ranges_cpp |
| 57 | elif language == Language.RUBY: |
| 58 | self.fname_filter = fname_filter_ruby |
| 59 | self.func_extractor = get_func_ranges_ruby |
| 60 | else: |
| 61 | print("This language is not supported yet!") |
| 62 | |
| 63 | def start_process(self): |
| 64 | self.G = nx.DiGraph() |
| 65 | self.func_commit = {} |
| 66 | |
| 67 | def start_process_commit(self, commit): |
| 68 | self.G.add_node(commit.hexsha) |
| 69 | |
| 70 | def on_add(self, diff, commit): |
| 71 | fname = diff.b_blob.path |
| 72 | sha = commit.hexsha |
| 73 | if self.fname_filter(fname): |
| 74 | file_contents = get_contents(self.repo, commit, fname) |
| 75 | func_ids, _ = self.func_extractor(file_contents, fname) |
| 76 | for func_id in func_ids: |
| 77 | self.func_commit[func_id] = sha |
| 78 | |
| 79 | def on_delete(self, diff, commit): |
| 80 | fname = diff.a_blob.path |
| 81 | sha = commit.hexsha |
| 82 | if self.fname_filter(fname): |
| 83 | last_commit = commit.parents[0] |
| 84 | file_contents = get_contents(self.repo, last_commit, fname) |
| 85 | func_ids, _ = self.func_extractor(file_contents, fname) |
| 86 | for func_id in func_ids: |
| 87 | if func_id in self.func_commit: |
| 88 | add_edge(self.G, sha, self.func_commit[func_id], func_id) |
| 89 | del self.func_commit[func_id] |
| 90 | |
| 91 | def on_rename(self, diff, commit): |
| 92 | # when similarity is 100%, diff.a_blob and diff.b_blob are None, so don't use them |
| 93 | new_fname = diff.rename_to |
| 94 | old_fname = diff.rename_from |
| 95 | last_commit = commit.parents[0] |
| 96 | sha = commit.hexsha |
| 97 | |
| 98 | if self.fname_filter(new_fname) or self.fname_filter(old_fname): |
| 99 | file_contents = get_contents(self.repo, last_commit, old_fname) |
| 100 | func_ids, func_ranges = self.func_extractor(file_contents, old_fname) |
| 101 | try: |
| 102 | modified_intervals = parse_patch(diff.diff.decode("utf-8")) |
| 103 | except UnicodeDecodeError: |
| 104 | print("UnicodeDecodeError Found in change_type {}".format(diff.change_type)) |
| 105 | return -1 |
| 106 | modified_func_ids = get_modified_func_ids(func_ranges, modified_intervals, func_ids) |