-
Notifications
You must be signed in to change notification settings - Fork 5
/
git-suse.cc
736 lines (578 loc) · 14.3 KB
/
git-suse.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
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
/*
* Copyright (c) 2016 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 <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <getopt.h>
#include <git2.h>
using namespace std;
/* Options */
string path_blacklist_file;
string revision = "HEAD";
string repo_path = ".";
bool diff_mode = false;
string blacklist_file;
string path_map_file;
bool std_out = false;
bool append = false;
string file_name;
string base_rev;
string base_file;
map<string, map<string, int> > path_map;
typedef map<string, git_oid> oid_map_t;
oid_map_t file_oid_map;
struct patch_info {
string context;
string path;
};
using results_type = map<string, patch_info>;
static bool is_hex(const string &s)
{
for (string::const_iterator c = s.begin(); c != s.end(); ++c) {
if (!isxdigit(*c))
return false;
}
return true;
}
static string to_lower(string s)
{
transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}
string trim(const string &line)
{
static const char *spaces = " \n\t\r";
size_t pos1, pos2;
pos1 = line.find_first_not_of(spaces);
pos2 = line.find_last_not_of(spaces);
if (pos1 == string::npos)
return string("");
return line.substr(pos1, pos2-pos1+1);
}
static int split_trim(vector<string> &items, const char *delim,
string buffer, unsigned splits)
{
unsigned num;
string item;
size_t pos;
buffer = trim(buffer);
num = 0;
pos = 0;
while (pos != std::string::npos) {
pos = buffer.find_first_of(delim, 0);
if (pos == string::npos) {
item = buffer;
} else {
item = buffer.substr(0, pos);
buffer = buffer.substr(pos + 1);
}
num += 1;
items.push_back(trim(item));
if (splits && num == splits)
break;
}
return num;
}
static const char *domains[] = {
"suse.de",
"suse.cz",
"suse.com",
"novell.com",
0,
};
static bool is_suse_email(const string &email)
{
size_t pos = email.find_first_of("@");
if (pos == string::npos)
return false;
string domain = email.substr(pos + 1);
for (const char **c = domains; *c; ++c) {
if (to_lower(domain) == *c)
return true;
}
return false;
}
static int blob_content(string& content, const string &path,
git_repository *repo, git_tree *tree)
{
git_blob *blob;
int error;
content.clear();
auto oid_it = file_oid_map.find(path);
if (oid_it == file_oid_map.end()) {
giterr_set_str(GIT_ENOTFOUND, "Path not found");
return GIT_ENOTFOUND;
}
error = git_blob_lookup(&blob, repo, &oid_it->second);
if (error)
return error;
content = (const char *)git_blob_rawcontent(blob);
git_blob_free(blob);
return error;
}
static void parse_patch(const string &path,
git_repository *repo, git_tree *tree,
results_type &results,
set<string> &blacklist)
{
string committer = "Unknown";
vector<string> commit_ids;
bool in_patch = false;
string content;
int error;
error = blob_content(content, path, repo, tree);
if (error)
return;
istringstream is(content);
string line;
while (getline(is, line)) {
size_t len, pos;
string token;
if (line == "---") {
in_patch = true;
if (path_map_file == "")
break;
}
if (in_patch) {
if (line.length() < 5)
continue;
string prefix = line.substr(0, 4);
if (prefix != "+++ ")
continue;
string path = line.substr(4);
auto pos = path.find_first_of("/");
if (pos != string::npos)
path = path.substr(pos + 1);
pos = path.find_first_of(" ");
if (pos != string::npos)
path = path.substr(0, pos);
while (true) {
path_map[path][committer] += 1;
pos = path.find_last_of("/");
if (pos == string::npos)
break;
path = path.substr(0, pos);
}
continue;
}
len = line.length();
pos = line.find_first_of(":");
if (pos == string::npos || pos + 1 >= len)
continue;
token = to_lower(line.substr(0, pos));
if (token == "git-commit" || token == "no-fix" ||
token == "alt-commit") {
string id;
pos = line.find_first_not_of(" ", pos + 1);
if (pos == string::npos)
continue;
line = to_lower(line);
auto pos2 = line.find_first_not_of("0123456789abcdef",
pos);
if (pos2 == string::npos)
pos2 = line.length();
if (pos2 - pos != 40)
continue;
id = line.substr(pos, pos2 - pos);
if (token == "git-commit" || token == "alt-commit")
commit_ids.emplace_back(id);
else
blacklist.emplace(id);
} else if (token == "signed-off-by" || token == "acked-by" ||
token == "reviewed-by") {
vector<string> items;
split_trim(items, " \t", line, 0);
for (vector<string>::iterator it = items.begin();
it != items.end();
++it) {
string email;
pos = it->find_first_of("@", 0);
len = it->length();
if (!len || pos == string::npos)
continue;
email = *it;
if (email[0] == '<') {
email = email.substr(1);
len -= 1;
}
if (!len)
continue;
if (email[len - 1] == '>')
email = email.substr(0, len - 1);
if (is_suse_email(email))
committer = email;
}
}
}
auto pos = path.find_first_of("/");
if (pos != std::string::npos) {
auto directory = path.substr(0, pos);
// Count patches in "patches.kernel.org" directory
// as Base fixes
if (directory == "patches.kernel.org")
committer = "Base";
}
for (auto &it : commit_ids) {
results[it].context = committer;
results[it].path = path;
}
}
static void parse_blacklist(string &content,
set<string> &blacklist,
vector<string> &path_blacklist)
{
istringstream is(content);
string line;
while (getline(is, line)) {
line = trim(line);
auto pos = line.find_first_of("# \n\t\r");
if (pos != string::npos)
line = line.substr(0, pos);
if (line.length() == 40 && is_hex(line))
blacklist.emplace(to_lower(line));
else if (line.length() > 0)
path_blacklist.emplace_back(line);
}
}
static void parse_series(const string& series,
git_repository *repo, git_tree *tree,
results_type &results,
set<string> &blacklist)
{
istringstream is(series);
string line;
while (getline(is, line)) {
vector<string> items;
string path;
size_t pos;
pos = line.find_first_of("#", 0);
if (pos != string::npos)
line = line.substr(0, pos);
split_trim(items, " \t", line, 0);
for (vector<string>::iterator it = items.begin();
it != items.end();
++it) {
if (it->find_first_of("/", 0) != string::npos) {
path = *it;
break;
}
}
if (!path.length())
continue;
parse_patch(path, repo, tree, results, blacklist);
}
}
static int fill_file_oid_map(const char *root, const git_tree_entry *entry,
void *payload)
{
if (git_tree_entry_type(entry) == GIT_OBJ_BLOB) {
string path(root);
path += git_tree_entry_name(entry);
oid_map_t &map = *(oid_map_t*)payload;
map[path] = *git_tree_entry_id(entry);
}
return 0;
}
static int handle_revision(git_repository *repo, const char *revision,
const string& outfile,
results_type &results,
set<string> &blacklist,
vector<string> &path_blacklist)
{
git_commit *commit;
git_object *obj;
git_tree *tree;
string series;
string blist;
int error;
error = git_revparse_single(&obj, repo, revision);
if (error < 0)
goto out;
error = git_commit_lookup(&commit, repo, git_object_id(obj));
if (error)
goto out_obj_free;
error = git_commit_tree(&tree, commit);
if (error)
goto out_commit_free;
file_oid_map.clear();
error = git_tree_walk(tree, GIT_TREEWALK_PRE,
fill_file_oid_map, &file_oid_map);
if (error)
goto out_free_tree;
error = blob_content(blist, "blacklist.conf", repo, tree);
if (!error)
parse_blacklist(blist, blacklist, path_blacklist);
error = blob_content(series, "series.conf", repo, tree);
if (error)
goto out_free_tree;
parse_series(series, repo, tree, results, blacklist);
out_free_tree:
git_tree_free(tree);
out_commit_free:
git_commit_free(commit);
out_obj_free:
git_object_free(obj);
out:
return error;
}
static string base_name(const string &s)
{
size_t pos = s.find_last_of("/");
if (pos == string::npos)
return s;
return s.substr(pos + 1);
}
enum {
OPTION_HELP,
OPTION_REPO,
OPTION_FILE,
OPTION_BASE,
OPTION_APPEND,
OPTION_STDOUT,
OPTION_BLACKLIST,
OPTION_PATH_BLACKLIST,
OPTION_PATH_MAP,
OPTION_BASE_FILE,
};
static struct option options[] = {
{ "help", no_argument, 0, OPTION_HELP },
{ "repo", required_argument, 0, OPTION_REPO },
{ "file", required_argument, 0, OPTION_FILE },
{ "base", required_argument, 0, OPTION_BASE },
{ "append", no_argument, 0, OPTION_APPEND },
{ "stdout", no_argument, 0, OPTION_STDOUT },
{ "blacklist", required_argument, 0, OPTION_BLACKLIST },
{ "path-blacklist", required_argument, 0, OPTION_PATH_BLACKLIST },
{ "path-map", required_argument, 0, OPTION_PATH_MAP },
{ "base-file", required_argument, 0, OPTION_BASE_FILE },
{ 0, 0, 0, 0 }
};
static void usage(const char *prg)
{
printf("Usage: %s [Options] Revision\n", base_name(prg).c_str());
printf("Options:\n");
printf(" --help, -h Print this message end exit\n");
printf(" --repo, -r Path to git repository\n");
printf(" --file, -f Write output to specified file\n");
printf(" --blacklist Write blacklist to specified file\n");
printf(" --path-blacklist Write path-blacklist to specified file\n");
printf(" --path-map Write path-map to specified file\n");
printf(" --base, -b Show only commits not in given base version\n");
printf(" --base-file File to store commit-list from the base-kernel\n");
printf(" (Only used when --base is specified)\n");
printf(" --append Open output file in append mode\n");
printf(" --stdout, -c Write output to stdout\n");
}
static void parse_options(int argc, char **argv)
{
int c;
if (argc < 2) {
usage(argv[0]);
exit(1);
}
while (true) {
int opt_idx;
c = getopt_long(argc, argv, "hr:f:b:c", options, &opt_idx);
if (c == -1)
break;
switch (c) {
case OPTION_HELP:
case 'h':
usage(argv[0]);
exit(0);
break;
case OPTION_REPO:
case 'r':
repo_path = optarg;
break;
case OPTION_FILE:
case 'f':
file_name = optarg;
break;
case OPTION_BASE:
case 'b':
base_rev = optarg;
diff_mode = true;
break;
case OPTION_BASE_FILE:
base_file = optarg;
break;
case OPTION_APPEND:
append = true;
break;
case OPTION_STDOUT:
case 'c':
std_out = true;
break;
case OPTION_BLACKLIST:
blacklist_file = optarg;
break;
case OPTION_PATH_BLACKLIST:
path_blacklist_file = optarg;
break;
case OPTION_PATH_MAP:
path_map_file = optarg;
break;
default:
usage(argv[0]);
exit(1);
}
}
if (optind < argc)
revision = argv[optind++];
}
static void write_blacklist(set<string> &blacklist)
{
ofstream file;
if (blacklist_file == "")
return;
file.open(blacklist_file.c_str());
if (!file.is_open()) {
fprintf(stderr, "Can't open blacklist file for writing\n");
return;
}
for (auto &c : blacklist)
file << c << std::endl;
cout << "Wrote " << blacklist.size() << " blacklisted commits to " << blacklist_file << endl;
file.close();
}
static void write_path_blacklist(vector<string> &path_blacklist)
{
ofstream file;
if (path_blacklist_file == "")
return;
file.open(path_blacklist_file.c_str());
if (!file.is_open()) {
fprintf(stderr, "Can't open path-blacklist file for writing\n");
return;
}
for (auto &path : path_blacklist)
file << path << std::endl;
cout << "Wrote " << path_blacklist.size() << " blacklisted paths to "
<< path_blacklist_file << endl;
file.close();
}
static void write_results(ostream &os, results_type &results)
{
for (auto &it : results) {
os << it.first << ',' << it.second.context;
if (it.second.path.length() > 0)
os << ',' << it.second.path;
os << endl;
}
}
static void write_path_map(string filename)
{
ofstream file;
if (filename == "")
return;
file.open(filename.c_str());
if (!file.is_open()) {
fprintf(stderr, "Can't open path-map file for writing\n");
return;
}
for (auto &path : path_map) {
file << path.first;
for (auto &m : path.second)
file << ';' << m.first << ":" << m.second;
file << endl;
}
file.close();
}
static void do_diff(results_type &result,
const results_type &base,
const results_type &branch)
{
for (const auto &it : branch) {
if (base.find(it.first) == base.end())
result[it.first] = it.second;
}
}
int main(int argc, char **argv)
{
ios_base::openmode file_mode = ios_base::out;
results_type results, base;
vector<string> path_blacklist;
git_repository *repo = NULL;
set<string> blacklist;
const git_error *e;
ofstream of;
ostream *os;
int error;
parse_options(argc, argv);
if (file_name == "")
file_name = base_name(revision) + ".list";
if (append)
file_mode |= ofstream::app;
if (!std_out) {
of.open(file_name.c_str(), file_mode);
if (!of.is_open()) {
cerr << "Can't open output file " << file_name << endl;
error = 1;
goto out;
}
os = &of;
} else {
os = &cout;
}
git_libgit2_init();
error = git_repository_open(&repo, repo_path.c_str());
if (error < 0)
goto error;
if (diff_mode) {
vector<string> ignored2;
set<string> ignored;
error = handle_revision(repo, base_rev.c_str(), file_name, base,
ignored, ignored2);
if (error)
goto error;
}
error = handle_revision(repo, revision.c_str(), file_name,
results, blacklist, path_blacklist);
if (error)
goto error;
if (diff_mode) {
results_type r;
do_diff(r, base, results);
results = r;
if (base_file != "") {
ofstream bof(base_file);
if (bof.is_open()) {
write_results(bof, base);
cout << "Wrote " << base.size() << " commits to " << base_file << endl;
} else {
cerr << "Can't open " << base_file << " for writing" << endl;
}
}
}
write_results(*os, results);
if (!std_out)
cout << "Wrote " << results.size() << " commits to " << file_name << endl;
write_blacklist(blacklist);
write_path_blacklist(path_blacklist);
write_path_map(path_map_file);
out:
if (of.is_open())
of.close();
git_repository_free(repo);
git_libgit2_shutdown();
return error;
error:
e = giterr_last();
cout << "Error: " << e->message << endl;
goto out;
}