-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwho.cc
281 lines (216 loc) · 5.17 KB
/
who.cc
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
/*
* Copyright (c) 2017 SUSE Linux GmbH
*
* Licensed under the GNU General Public License Version 2
* as published by the Free Software Foundation.
*
* See http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* for details.
*
* Author: Joerg Roedel <[email protected]>
*/
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <git2.h>
#include "who.h"
int git_who::load_path_map(std::string filename)
{
std::ifstream file;
std::string line;
file.open(filename.c_str());
if (!file.is_open()) {
std::cerr << "Can't open path-map file: " << filename << std::endl;
return 1;
}
while (getline(file, line)) {
struct people people;
auto pos = line.find_first_of(";");
if (pos == std::string::npos)
continue;
auto path = line.substr(0, pos);
line = line.substr(pos + 1);
while (line.length() > 0) {
std::string token;
pos = line.find_first_of(";");
if (pos != std::string::npos) {
token = line.substr(0, pos);
line = line.substr(pos + 1);
} else {
token = line;
line = "";
}
pos = token.find_first_of(":");
if (pos == std::string::npos)
continue;
auto name = token.substr(0, pos);
auto count = token.substr(pos + 1);
struct person p;
p.name = name;
p.count = atoi(count.c_str());
people.add_one(p);
}
path_map[path] = people;
}
file.close();
return 0;
}
static int diff_file_cb(const git_diff_delta *delta, float progess, void *data)
{
git_who *w = static_cast<git_who *>(data);
w->add_path(delta->new_file.path);
return 0;
}
void git_who::add_path(std::string path)
{
paths.emplace(std::move(path));
}
bool git_who::get_paths_from_commit(git_commit *commit, size_t idx)
{
git_commit *parent;
git_tree *a, *b;
git_diff *diff;
int error;
bool ret;
ret = false;
error = git_commit_parent(&parent, commit, 0);
if (error)
goto out;
error = git_commit_tree(&a, parent);
if (error)
goto out_parent;
error = git_commit_tree(&b, commit);
if (error)
goto out_tree_a;
error = git_diff_tree_to_tree(&diff, git_commit_owner(commit), a, b, NULL);
if (error)
goto out_tree_b;
#if LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR < 23
error = git_diff_foreach(diff, diff_file_cb, NULL, NULL, this);
#else
error = git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, this);
#endif
if (error)
goto out_diff;
ret = true;
out_diff:
git_diff_free(diff);
out_tree_b:
git_tree_free(b);
out_tree_a:
git_tree_free(a);
out_parent:
git_commit_free(parent);
out:
return ret;
}
static int treewalk_cb(const char *root, const git_tree_entry *e, void *data)
{
git_who *w = static_cast<git_who *>(data);
std::string path = root;
path += git_tree_entry_name(e);
w->add_path(std::move(path));
return 0;
}
bool git_who::get_paths_from_revision(git_repository *repo, std::string rev)
{
unsigned int parents;
git_commit *commit;
git_object *obj;
int error;
bool ret;
error = git_revparse_single(&obj, repo, rev.c_str());
if (error)
return false;
ret = false;
error = git_commit_lookup(&commit, repo, git_object_id(obj));
if (error)
goto out_obj_free;
parents = git_commit_parentcount(commit);
if (parents == 0) {
git_tree *tree;
error = git_commit_tree(&tree, commit);
if (error)
goto out_commit_free;
error = git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_cb, this);
git_tree_free(tree);
if (error)
goto out_commit_free;
} else {
for (unsigned int i = 0; i < parents; ++i) {
ret = get_paths_from_commit(commit, i);
if (ret)
goto out_commit_free;
}
}
ret = true;
out_commit_free:
git_commit_free(commit);
out_obj_free:
git_object_free(obj);
return ret;
}
static bool is_prefix(std::string prefix, std::string value)
{
auto len1 = prefix.length();
auto len2 = value.length();
if (len1 > len2)
return false;
return (value.substr(0, len1) == prefix);
}
void git_who::match_paths(struct people &results)
{
std::set<std::string> prefix_paths, new_paths;
// First build a prefix map for unknown paths
for (auto path : paths) {
auto pos = path_map.find(path);
// We care only about prefixes for unknown paths
if (pos != path_map.end())
continue;
while (path != "" && pos == path_map.end()) {
auto slash = path.find_last_of("/");
if (slash == std::string::npos)
path = "";
else
path = path.substr(0, slash);
pos = path_map.find(path);
}
if (path != "" && pos != path_map.end())
prefix_paths.insert(path);
}
// Copy the prefixes to new_paths
new_paths = prefix_paths;
// Eliminate paths that have a prefix already stored
for (auto path : paths) {
bool store = true;
for (auto prefix : prefix_paths) {
if (is_prefix(prefix, path)) {
store = false;
break;
}
}
if (!store)
continue;
auto pos = path_map.find(path);
if (pos != path_map.end())
new_paths.insert(path);
}
// Do the matching
for (auto path : new_paths) {
auto pos = path_map.find(path);
if (pos == path_map.end()) {
std::cerr << "BUG: Unknown path in new_path set" << std::endl;
continue;
}
results = results + pos->second;
}
// Sort the results
std::sort(results.persons.rbegin(), results.persons.rend());
}
void git_who::reset(void)
{
paths.clear();
}