mailmap: debug: avoid passing NULL to fprintf() '%s' conversion specification
[git] / mailmap.c
1 #include "cache.h"
2 #include "string-list.h"
3 #include "mailmap.h"
4
5 #define DEBUG_MAILMAP 0
6 #if DEBUG_MAILMAP
7 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
8 #define debug_str(X) ((X) ? (X) : "(none)")
9 #else
10 static inline void debug_mm(const char *format, ...) {}
11 static inline const char *debug_str(const char *s) { return s; }
12 #endif
13
14 const char *git_mailmap_file;
15 const char *git_mailmap_blob;
16
17 struct mailmap_info {
18         char *name;
19         char *email;
20 };
21
22 struct 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
31 static void free_mailmap_info(void *p, const char *s)
32 {
33         struct mailmap_info *mi = (struct mailmap_info *)p;
34         debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, debug_str(mi->name), debug_str(mi->email));
35         free(mi->name);
36         free(mi->email);
37 }
38
39 static 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);
43         debug_mm("mailmap: - simple: '%s' <%s>\n", debug_str(me->name), debug_str(me->email));
44
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
52 static 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;
57
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;
66                 me = (struct mailmap_entry *)map->items[index].util;
67         } else {
68                 /* create mailmap entry */
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;
76         }
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 */
81                 if (new_name) {
82                         free(me->name);
83                         me->name = xstrdup(new_name);
84                 }
85                 if (new_email) {
86                         free(me->email);
87                         me->email = xstrdup(new_email);
88                 }
89         } else {
90                 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
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);
96                 string_list_insert(&me->namemap, old_name)->util = mi;
97         }
98
99         debug_mm("mailmap:  '%s' <%s> -> '%s' <%s>\n",
100                  debug_str(old_name), old_email, debug_str(new_name), debug_str(new_email));
101 }
102
103 static char *parse_name_and_email(char *buffer, char **name,
104                 char **email, int allow_empty_email)
105 {
106         char *left, *right, *nstart, *nend;
107         *name = *email = NULL;
108
109         if ((left = strchr(buffer, '<')) == NULL)
110                 return NULL;
111         if ((right = strchr(left+1, '>')) == NULL)
112                 return NULL;
113         if (!allow_empty_email && (left+1 == right))
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;
121         while (nend > nstart && isspace(*nend))
122                 --nend;
123
124         *name = (nstart <= nend ? nstart : NULL);
125         *email = left+1;
126         *(nend+1) = '\0';
127         *right++ = '\0';
128
129         return (*right == '\0' ? NULL : right);
130 }
131
132 static 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
166 static int read_mailmap_file(struct string_list *map, const char *filename,
167                              char **repo_abbrev)
168 {
169         char buffer[1024];
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         }
182
183         while (fgets(buffer, sizeof(buffer), f) != NULL)
184                 read_mailmap_line(map, buffer, repo_abbrev);
185         fclose(f);
186         return 0;
187 }
188
189 static 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
206 static 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)
216                 return 0;
217         if (get_sha1(name, sha1) < 0)
218                 return 0;
219
220         buf = read_sha1_file(sha1, &type, &size);
221         if (!buf)
222                 return error("unable to read mailmap object at %s", name);
223         if (type != OBJ_BLOB)
224                 return error("mailmap is not a blob: %s", name);
225
226         read_mailmap_buf(map, buf, size, repo_abbrev);
227
228         free(buf);
229         return 0;
230 }
231
232 int read_mailmap(struct string_list *map, char **repo_abbrev)
233 {
234         int err = 0;
235
236         map->strdup_strings = 1;
237         map->cmp = strcasecmp;
238
239         if (!git_mailmap_blob && is_bare_repository())
240                 git_mailmap_blob = "HEAD:.mailmap";
241
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;
246 }
247
248 void 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
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  */
260 static 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
307 int map_user(struct string_list *map,
308                          const char **email, size_t *emaillen,
309                          const char **name, size_t *namelen)
310 {
311         struct string_list_item *item;
312         struct mailmap_entry *me;
313
314         debug_mm("map_user: map '%.*s' <%.*s>\n",
315                  (int)*namelen, debug_str(*name), (int)*emaillen, debug_str(*email));
316
317         item = lookup_prefix(map, *email, *emaillen);
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      */
323                         struct string_list_item *subitem;
324                         subitem = lookup_prefix(&me->namemap, *name, *namelen);
325                         if (subitem)
326                                 item = subitem;
327                 }
328         }
329         if (item != NULL) {
330                 struct mailmap_info *mi = (struct mailmap_info *)item->util;
331                 if (mi->name == NULL && mi->email == NULL) {
332                         debug_mm("map_user:  -- (no simple mapping)\n");
333                         return 0;
334                 }
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                 }
343                 debug_mm("map_user:  to '%.*s' <%.*s>\n", (int)*namelen, debug_str(*name),
344                                  (int)*emaillen, debug_str(*email));
345                 return 1;
346         }
347         debug_mm("map_user:  --\n");
348         return 0;
349 }