-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgit-who.cc
294 lines (232 loc) · 6.08 KB
/
git-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
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* 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 <set>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <git2.h>
#include "who.h"
static std::string path_map_file;
static std::string db;
static std::vector<std::string> ignore_params;
static std::vector<std::string> params;
static std::map<std::string, bool> ignore;
std::string repo_path = ".";
static std::string trim(const std::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 == std::string::npos)
return std::string("");
return line.substr(pos1, pos2-pos1+1);
}
static bool ignore_from_file(std::string filename)
{
std::ifstream file;
std::string line;
file.open(filename.c_str());
if (!file.is_open())
return false;
while (getline(file, line)) {
line = trim(line);
auto pos = line.find_first_of("#");
if (pos != std::string::npos)
line = trim(line.substr(0, pos));
if (line == "")
continue;
ignore[line] = true;
}
file.close();
return true;
}
enum {
OPTION_HELP,
OPTION_PATH_MAP,
OPTION_REPO,
OPTION_IGNORE,
OPTION_DB,
};
static struct option options[] = {
{ "help", no_argument, 0, OPTION_HELP },
{ "path-map", required_argument, 0, OPTION_PATH_MAP },
{ "repo", required_argument, 0, OPTION_REPO },
{ "ignore", required_argument, 0, OPTION_IGNORE },
{ "database", required_argument, 0, OPTION_DB },
{ 0, 0, 0, 0 }
};
static void usage(const char *prg)
{
std::cout << "Usage " << prg << " [OPTIONS] [REVISION/PATH ...]" << std::endl;
std::cout << "Options" << std::endl;
std::cout << " --help, -h Print this help message" << std::endl;
std::cout << " --path-map, -p <file> File containing the path-map data" << std::endl;
std::cout << " --repo, -r <path> Path to git repository" << std::endl;
std::cout << " --ignore, -i <user/file> Email address to ignore (if possible)" << std::endl;
std::cout << " If the parameter is a file, the email addresses" << std::endl;
std::cout << " are read from there" << std::endl;
std::cout << " --database, -d <name> Select database (set fixes.<name>.pathmap and " << std::endl;
std::cout << " fixes.<name>.ignore)" << std::endl;
}
static bool parse_options(int argc, char **argv)
{
int c;
while (true) {
int opt_idx;
c = getopt_long(argc, argv, "hp:r:i:d:", options, &opt_idx);
if (c == -1)
break;
switch (c) {
case OPTION_HELP:
case 'h':
usage(argv[0]);
exit(0);
break;
case OPTION_PATH_MAP:
case 'p':
path_map_file = optarg;
break;
case OPTION_REPO:
case 'r':
repo_path = optarg;
break;
case OPTION_IGNORE:
case 'i':
ignore_params.emplace_back(optarg);
break;
case OPTION_DB:
case 'd':
db = optarg;
break;
default:
usage(argv[0]);
return false;
}
}
while (optind < argc)
params.emplace_back(argv[optind++]);
return true;
}
static std::string config_get_string_nofail(git_config *cfg, const char *name)
{
#if LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR < 23
const git_config_entry *entry;
#else
git_config_entry *entry;
#endif
std::string ret;
if (git_config_get_entry(&entry, cfg, name))
return ret;
ret = entry->value;
#if LIBGIT2_VER_MAJOR > 0 || LIBGIT2_VER_MINOR >= 23
git_config_entry_free(entry);
#endif
return ret;
}
static std::string config_get_path_nofail(git_config *cfg, const char *name)
{
std::string path;
path = config_get_string_nofail(cfg, name);
auto len = path.length();
if (!len || path[0] != '~')
return path;
if (len > 1 && path[1] != '/')
return path;
return std::string(getenv("HOME")) + path.substr(1);
}
void load_git_config(git_repository *repo)
{
git_config *repo_cfg;
std::string key, val;
int error;
error = git_repository_config(&repo_cfg, repo);
if (error)
return;
key = "fixes." + db + ".pathmap";
val = config_get_path_nofail(repo_cfg, key.c_str());
if (path_map_file == "" && val != "")
path_map_file = val;
key = "fixes." + db + ".ignore";
val = config_get_path_nofail(repo_cfg, key.c_str());
if (val != "")
ignore_params.emplace_back(val);
git_config_free(repo_cfg);
}
static void print_results(struct people &results)
{
bool do_ignore = false;
// Check if there are unignored people in the list
for (auto &p : results.persons) {
auto pos = ignore.find(p.name);
if (pos == ignore.end()) {
do_ignore = true;
break;
}
}
// Print results
for (auto &p : results.persons) {
if (do_ignore) {
auto pos = ignore.find(p.name);
if (pos != ignore.end())
continue;
}
std::cout << p.name << " (" << p.count << ")" << std::endl;
}
}
int main(int argc, char **argv)
{
git_repository *repo = NULL;
struct people results;
int ret, error;
git_who who;
ret = 1;
if (!parse_options(argc, argv))
goto out;
git_libgit2_init();
error = git_repository_open(&repo, repo_path.c_str());
if (error < 0)
goto error;
if (db != "")
load_git_config(repo);
ret = who.load_path_map(path_map_file);
if (ret)
goto out_repo;
for (auto &p : params) {
if (!who.get_paths_from_revision(repo, p))
// param is not a revision, treat as path
who.add_path(p);
}
for (auto &i : ignore_params) {
if (!ignore_from_file(i))
ignore[i] = true;
}
who.match_paths(results);
print_results(results);
ret = 0;
out_repo:
git_repository_free(repo);
out:
git_libgit2_shutdown();
return ret;
error:
auto e = giterr_last();
std::cerr << "Error: " << e->message << std::endl;
ret = 1;
goto out;
}