]> git.ipfire.org Git - thirdparty/git.git/blame - mailmap.c
mailmap: debug: avoid passing NULL to fprintf() '%s' conversion specification
[thirdparty/git.git] / mailmap.c
CommitLineData
7c1c6782 1#include "cache.h"
c455c87c 2#include "string-list.h"
fe5d30b6 3#include "mailmap.h"
7c1c6782 4
0925ce4d
MSO
5#define DEBUG_MAILMAP 0
6#if DEBUG_MAILMAP
7#define debug_mm(...) fprintf(stderr, __VA_ARGS__)
fbfba7ad 8#define debug_str(X) ((X) ? (X) : "(none)")
0925ce4d
MSO
9#else
10static inline void debug_mm(const char *format, ...) {}
fbfba7ad 11static inline const char *debug_str(const char *s) { return s; }
0925ce4d
MSO
12#endif
13
d551a488 14const char *git_mailmap_file;
08610900 15const char *git_mailmap_blob;
0925ce4d
MSO
16
17struct mailmap_info {
18 char *name;
19 char *email;
20};
21
22struct mailmap_entry {
23 /* name and email for the simple mail-only case */
24 char *name;
25 char *email;
26
27 /* name and email for the complex mail and name matching case */
28 struct string_list namemap;
29};
30
31static void free_mailmap_info(void *p, const char *s)
32{
33 struct mailmap_info *mi = (struct mailmap_info *)p;
fbfba7ad 34 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, debug_str(mi->name), debug_str(mi->email));
0925ce4d
MSO
35 free(mi->name);
36 free(mi->email);
37}
38
39static void free_mailmap_entry(void *p, const char *s)
40{
41 struct mailmap_entry *me = (struct mailmap_entry *)p;
42 debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
fbfba7ad
ES
43 debug_mm("mailmap: - simple: '%s' <%s>\n", debug_str(me->name), debug_str(me->email));
44
0925ce4d
MSO
45 free(me->name);
46 free(me->email);
47
48 me->namemap.strdup_strings = 1;
49 string_list_clear_func(&me->namemap, free_mailmap_info);
50}
51
52static void add_mapping(struct string_list *map,
53 char *new_name, char *new_email, char *old_name, char *old_email)
54{
55 struct mailmap_entry *me;
56 int index;
bf637803 57
0925ce4d
MSO
58 if (old_email == NULL) {
59 old_email = new_email;
60 new_email = NULL;
61 }
62
63 if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
64 /* mailmap entry exists, invert index value */
65 index = -1 - index;
97e751be 66 me = (struct mailmap_entry *)map->items[index].util;
0925ce4d
MSO
67 } else {
68 /* create mailmap entry */
97e751be
JH
69 struct string_list_item *item;
70
71 item = string_list_insert_at_index(map, index, old_email);
72 me = xcalloc(1, sizeof(struct mailmap_entry));
73 me->namemap.strdup_strings = 1;
74 me->namemap.cmp = strcasecmp;
75 item->util = me;
0925ce4d 76 }
0925ce4d
MSO
77
78 if (old_name == NULL) {
79 debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
80 /* Replace current name and new email for simple entry */
d8d2eb7d
JM
81 if (new_name) {
82 free(me->name);
0925ce4d 83 me->name = xstrdup(new_name);
d8d2eb7d
JM
84 }
85 if (new_email) {
86 free(me->email);
0925ce4d 87 me->email = xstrdup(new_email);
d8d2eb7d 88 }
0925ce4d 89 } else {
74b531f6 90 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
0925ce4d
MSO
91 debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
92 if (new_name)
93 mi->name = xstrdup(new_name);
94 if (new_email)
95 mi->email = xstrdup(new_email);
78a395d3 96 string_list_insert(&me->namemap, old_name)->util = mi;
0925ce4d
MSO
97 }
98
99 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
fbfba7ad 100 debug_str(old_name), old_email, debug_str(new_name), debug_str(new_email));
0925ce4d
MSO
101}
102
5288dd58
BS
103static char *parse_name_and_email(char *buffer, char **name,
104 char **email, int allow_empty_email)
0925ce4d
MSO
105{
106 char *left, *right, *nstart, *nend;
2af202be 107 *name = *email = NULL;
0925ce4d
MSO
108
109 if ((left = strchr(buffer, '<')) == NULL)
110 return NULL;
111 if ((right = strchr(left+1, '>')) == NULL)
112 return NULL;
5288dd58 113 if (!allow_empty_email && (left+1 == right))
0925ce4d
MSO
114 return NULL;
115
116 /* remove whitespace from beginning and end of name */
117 nstart = buffer;
118 while (isspace(*nstart) && nstart < left)
119 ++nstart;
120 nend = left-1;
3174bc5c 121 while (nend > nstart && isspace(*nend))
0925ce4d
MSO
122 --nend;
123
8c381151 124 *name = (nstart <= nend ? nstart : NULL);
0925ce4d
MSO
125 *email = left+1;
126 *(nend+1) = '\0';
127 *right++ = '\0';
128
129 return (*right == '\0' ? NULL : right);
130}
131
7c8ce308
JK
132static void read_mailmap_line(struct string_list *map, char *buffer,
133 char **repo_abbrev)
134{
135 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
136 if (buffer[0] == '#') {
137 static const char abbrev[] = "# repo-abbrev:";
138 int abblen = sizeof(abbrev) - 1;
139 int len = strlen(buffer);
140
141 if (!repo_abbrev)
142 return;
143
144 if (len && buffer[len - 1] == '\n')
145 buffer[--len] = 0;
146 if (!strncmp(buffer, abbrev, abblen)) {
147 char *cp;
148
149 if (repo_abbrev)
150 free(*repo_abbrev);
151 *repo_abbrev = xmalloc(len);
152
153 for (cp = buffer + abblen; isspace(*cp); cp++)
154 ; /* nothing */
155 strcpy(*repo_abbrev, cp);
156 }
157 return;
158 }
159 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
160 parse_name_and_email(name2, &name2, &email2, 1);
161
162 if (email1)
163 add_mapping(map, name1, email1, name2, email2);
164}
165
166static int read_mailmap_file(struct string_list *map, const char *filename,
167 char **repo_abbrev)
7c1c6782
JH
168{
169 char buffer[1024];
938a60d6
JK
170 FILE *f;
171
172 if (!filename)
173 return 0;
174
175 f = fopen(filename, "r");
176 if (!f) {
177 if (errno == ENOENT)
178 return 0;
179 return error("unable to open mailmap at %s: %s",
180 filename, strerror(errno));
181 }
7c1c6782 182
7c8ce308
JK
183 while (fgets(buffer, sizeof(buffer), f) != NULL)
184 read_mailmap_line(map, buffer, repo_abbrev);
7c1c6782
JH
185 fclose(f);
186 return 0;
187}
188
08610900
JK
189static void read_mailmap_buf(struct string_list *map,
190 const char *buf, unsigned long len,
191 char **repo_abbrev)
192{
193 while (len) {
194 const char *end = strchrnul(buf, '\n');
195 unsigned long linelen = end - buf + 1;
196 char *line = xmemdupz(buf, linelen);
197
198 read_mailmap_line(map, line, repo_abbrev);
199
200 free(line);
201 buf += linelen;
202 len -= linelen;
203 }
204}
205
206static int read_mailmap_blob(struct string_list *map,
207 const char *name,
208 char **repo_abbrev)
209{
210 unsigned char sha1[20];
211 char *buf;
212 unsigned long size;
213 enum object_type type;
214
215 if (!name)
938a60d6 216 return 0;
08610900 217 if (get_sha1(name, sha1) < 0)
938a60d6 218 return 0;
08610900
JK
219
220 buf = read_sha1_file(sha1, &type, &size);
221 if (!buf)
938a60d6 222 return error("unable to read mailmap object at %s", name);
08610900 223 if (type != OBJ_BLOB)
938a60d6 224 return error("mailmap is not a blob: %s", name);
08610900
JK
225
226 read_mailmap_buf(map, buf, size, repo_abbrev);
227
228 free(buf);
229 return 0;
230}
231
d551a488
MSO
232int read_mailmap(struct string_list *map, char **repo_abbrev)
233{
938a60d6 234 int err = 0;
8c473cec 235
0925ce4d 236 map->strdup_strings = 1;
388c7f8a 237 map->cmp = strcasecmp;
8c473cec
JK
238
239 if (!git_mailmap_blob && is_bare_repository())
240 git_mailmap_blob = "HEAD:.mailmap";
241
938a60d6
JK
242 err |= read_mailmap_file(map, ".mailmap", repo_abbrev);
243 err |= read_mailmap_blob(map, git_mailmap_blob, repo_abbrev);
244 err |= read_mailmap_file(map, git_mailmap_file, repo_abbrev);
245 return err;
d551a488
MSO
246}
247
0925ce4d
MSO
248void clear_mailmap(struct string_list *map)
249{
250 debug_mm("mailmap: clearing %d entries...\n", map->nr);
251 map->strdup_strings = 1;
252 string_list_clear_func(map, free_mailmap_entry);
253 debug_mm("mailmap: cleared\n");
254}
255
388c7f8a
JH
256/*
257 * Look for an entry in map that match string[0:len]; string[len]
258 * does not have to be NUL (but it could be).
259 */
260static struct string_list_item *lookup_prefix(struct string_list *map,
261 const char *string, size_t len)
262{
263 int i = string_list_find_insert_index(map, string, 1);
264 if (i < 0) {
265 /* exact match */
266 i = -1 - i;
267 if (!string[len])
268 return &map->items[i];
269 /*
270 * that map entry matches exactly to the string, including
271 * the cruft at the end beyond "len". That is not a match
272 * with string[0:len] that we are looking for.
273 */
274 } else if (!string[len]) {
275 /*
276 * asked with the whole string, and got nothing. No
277 * matching entry can exist in the map.
278 */
279 return NULL;
280 }
281
282 /*
283 * i is at the exact match to an overlong key, or location the
284 * overlong key would be inserted, which must come after the
285 * real location of the key if one exists.
286 */
287 while (0 <= --i && i < map->nr) {
288 int cmp = strncasecmp(map->items[i].string, string, len);
289 if (cmp < 0)
290 /*
291 * "i" points at a key definitely below the prefix;
292 * the map does not have string[0:len] in it.
293 */
294 break;
295 else if (!cmp && !map->items[i].string[len])
296 /* found it */
297 return &map->items[i];
298 /*
299 * otherwise, the string at "i" may be string[0:len]
300 * followed by a string that sorts later than string[len:];
301 * keep trying.
302 */
303 }
304 return NULL;
305}
306
0925ce4d 307int map_user(struct string_list *map,
ea02ffa3
AP
308 const char **email, size_t *emaillen,
309 const char **name, size_t *namelen)
7c1c6782 310{
c455c87c 311 struct string_list_item *item;
0925ce4d 312 struct mailmap_entry *me;
388c7f8a 313
ea02ffa3 314 debug_mm("map_user: map '%.*s' <%.*s>\n",
fbfba7ad 315 (int)*namelen, debug_str(*name), (int)*emaillen, debug_str(*email));
388c7f8a 316
ea02ffa3 317 item = lookup_prefix(map, *email, *emaillen);
0925ce4d
MSO
318 if (item != NULL) {
319 me = (struct mailmap_entry *)item->util;
320 if (me->namemap.nr) {
321 /* The item has multiple items, so we'll look up on name too */
322 /* If the name is not found, we choose the simple entry */
ea02ffa3
AP
323 struct string_list_item *subitem;
324 subitem = lookup_prefix(&me->namemap, *name, *namelen);
0925ce4d
MSO
325 if (subitem)
326 item = subitem;
327 }
328 }
7c1c6782 329 if (item != NULL) {
0925ce4d 330 struct mailmap_info *mi = (struct mailmap_info *)item->util;
ea02ffa3 331 if (mi->name == NULL && mi->email == NULL) {
0925ce4d
MSO
332 debug_mm("map_user: -- (no simple mapping)\n");
333 return 0;
334 }
ea02ffa3
AP
335 if (mi->email) {
336 *email = mi->email;
337 *emaillen = strlen(*email);
338 }
339 if (mi->name) {
340 *name = mi->name;
341 *namelen = strlen(*name);
342 }
fbfba7ad
ES
343 debug_mm("map_user: to '%.*s' <%.*s>\n", (int)*namelen, debug_str(*name),
344 (int)*emaillen, debug_str(*email));
7c1c6782
JH
345 return 1;
346 }
0925ce4d 347 debug_mm("map_user: --\n");
7c1c6782
JH
348 return 0;
349}