mailmap: work around implementations with pure inline strcasecmp
[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",
35                  s, debug_str(mi->name), debug_str(mi->email));
36         free(mi->name);
37         free(mi->email);
38 }
39
40 static void free_mailmap_entry(void *p, const char *s)
41 {
42         struct mailmap_entry *me = (struct mailmap_entry *)p;
43         debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n",
44                  s, me->namemap.nr);
45         debug_mm("mailmap: - simple: '%s' <%s>\n",
46                  debug_str(me->name), debug_str(me->email));
47
48         free(me->name);
49         free(me->email);
50
51         me->namemap.strdup_strings = 1;
52         string_list_clear_func(&me->namemap, free_mailmap_info);
53 }
54
55 /*
56  * On some systems (e.g. MinGW 4.0), string.h has _only_ inline
57  * definition of strcasecmp and no non-inline implementation is
58  * supplied anywhere, which is, eh, "unusual"; we cannot take an
59  * address of such a function to store it in namemap.cmp.  This is
60  * here as a workaround---do not assign strcasecmp directly to
61  * namemap.cmp until we know no systems that matter have such an
62  * "unusual" string.h.
63  */
64 static int namemap_cmp(const char *a, const char *b)
65 {
66         return strcasecmp(a, b);
67 }
68
69 static void add_mapping(struct string_list *map,
70                         char *new_name, char *new_email,
71                         char *old_name, char *old_email)
72 {
73         struct mailmap_entry *me;
74         int index;
75
76         if (old_email == NULL) {
77                 old_email = new_email;
78                 new_email = NULL;
79         }
80
81         if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
82                 /* mailmap entry exists, invert index value */
83                 index = -1 - index;
84                 me = (struct mailmap_entry *)map->items[index].util;
85         } else {
86                 /* create mailmap entry */
87                 struct string_list_item *item;
88
89                 item = string_list_insert_at_index(map, index, old_email);
90                 me = xcalloc(1, sizeof(struct mailmap_entry));
91                 me->namemap.strdup_strings = 1;
92                 me->namemap.cmp = namemap_cmp;
93                 item->util = me;
94         }
95
96         if (old_name == NULL) {
97                 debug_mm("mailmap: adding (simple) entry for %s at index %d\n",
98                          old_email, index);
99                 /* Replace current name and new email for simple entry */
100                 if (new_name) {
101                         free(me->name);
102                         me->name = xstrdup(new_name);
103                 }
104                 if (new_email) {
105                         free(me->email);
106                         me->email = xstrdup(new_email);
107                 }
108         } else {
109                 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
110                 debug_mm("mailmap: adding (complex) entry for %s at index %d\n",
111                          old_email, index);
112                 if (new_name)
113                         mi->name = xstrdup(new_name);
114                 if (new_email)
115                         mi->email = xstrdup(new_email);
116                 string_list_insert(&me->namemap, old_name)->util = mi;
117         }
118
119         debug_mm("mailmap:  '%s' <%s> -> '%s' <%s>\n",
120                  debug_str(old_name), old_email,
121                  debug_str(new_name), debug_str(new_email));
122 }
123
124 static char *parse_name_and_email(char *buffer, char **name,
125                                   char **email, int allow_empty_email)
126 {
127         char *left, *right, *nstart, *nend;
128         *name = *email = NULL;
129
130         if ((left = strchr(buffer, '<')) == NULL)
131                 return NULL;
132         if ((right = strchr(left+1, '>')) == NULL)
133                 return NULL;
134         if (!allow_empty_email && (left+1 == right))
135                 return NULL;
136
137         /* remove whitespace from beginning and end of name */
138         nstart = buffer;
139         while (isspace(*nstart) && nstart < left)
140                 ++nstart;
141         nend = left-1;
142         while (nend > nstart && isspace(*nend))
143                 --nend;
144
145         *name = (nstart <= nend ? nstart : NULL);
146         *email = left+1;
147         *(nend+1) = '\0';
148         *right++ = '\0';
149
150         return (*right == '\0' ? NULL : right);
151 }
152
153 static void read_mailmap_line(struct string_list *map, char *buffer,
154                               char **repo_abbrev)
155 {
156         char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
157         if (buffer[0] == '#') {
158                 static const char abbrev[] = "# repo-abbrev:";
159                 int abblen = sizeof(abbrev) - 1;
160                 int len = strlen(buffer);
161
162                 if (!repo_abbrev)
163                         return;
164
165                 if (len && buffer[len - 1] == '\n')
166                         buffer[--len] = 0;
167                 if (!strncmp(buffer, abbrev, abblen)) {
168                         char *cp;
169
170                         if (repo_abbrev)
171                                 free(*repo_abbrev);
172                         *repo_abbrev = xmalloc(len);
173
174                         for (cp = buffer + abblen; isspace(*cp); cp++)
175                                 ; /* nothing */
176                         strcpy(*repo_abbrev, cp);
177                 }
178                 return;
179         }
180         if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
181                 parse_name_and_email(name2, &name2, &email2, 1);
182
183         if (email1)
184                 add_mapping(map, name1, email1, name2, email2);
185 }
186
187 static int read_mailmap_file(struct string_list *map, const char *filename,
188                              char **repo_abbrev)
189 {
190         char buffer[1024];
191         FILE *f;
192
193         if (!filename)
194                 return 0;
195
196         f = fopen(filename, "r");
197         if (!f) {
198                 if (errno == ENOENT)
199                         return 0;
200                 return error("unable to open mailmap at %s: %s",
201                              filename, strerror(errno));
202         }
203
204         while (fgets(buffer, sizeof(buffer), f) != NULL)
205                 read_mailmap_line(map, buffer, repo_abbrev);
206         fclose(f);
207         return 0;
208 }
209
210 static void read_mailmap_buf(struct string_list *map,
211                              const char *buf, unsigned long len,
212                              char **repo_abbrev)
213 {
214         while (len) {
215                 const char *end = strchrnul(buf, '\n');
216                 unsigned long linelen = end - buf + 1;
217                 char *line = xmemdupz(buf, linelen);
218
219                 read_mailmap_line(map, line, repo_abbrev);
220
221                 free(line);
222                 buf += linelen;
223                 len -= linelen;
224         }
225 }
226
227 static int read_mailmap_blob(struct string_list *map,
228                              const char *name,
229                              char **repo_abbrev)
230 {
231         unsigned char sha1[20];
232         char *buf;
233         unsigned long size;
234         enum object_type type;
235
236         if (!name)
237                 return 0;
238         if (get_sha1(name, sha1) < 0)
239                 return 0;
240
241         buf = read_sha1_file(sha1, &type, &size);
242         if (!buf)
243                 return error("unable to read mailmap object at %s", name);
244         if (type != OBJ_BLOB)
245                 return error("mailmap is not a blob: %s", name);
246
247         read_mailmap_buf(map, buf, size, repo_abbrev);
248
249         free(buf);
250         return 0;
251 }
252
253 int read_mailmap(struct string_list *map, char **repo_abbrev)
254 {
255         int err = 0;
256
257         map->strdup_strings = 1;
258         map->cmp = namemap_cmp;
259
260         if (!git_mailmap_blob && is_bare_repository())
261                 git_mailmap_blob = "HEAD:.mailmap";
262
263         err |= read_mailmap_file(map, ".mailmap", repo_abbrev);
264         err |= read_mailmap_blob(map, git_mailmap_blob, repo_abbrev);
265         err |= read_mailmap_file(map, git_mailmap_file, repo_abbrev);
266         return err;
267 }
268
269 void clear_mailmap(struct string_list *map)
270 {
271         debug_mm("mailmap: clearing %d entries...\n", map->nr);
272         map->strdup_strings = 1;
273         string_list_clear_func(map, free_mailmap_entry);
274         debug_mm("mailmap: cleared\n");
275 }
276
277 /*
278  * Look for an entry in map that match string[0:len]; string[len]
279  * does not have to be NUL (but it could be).
280  */
281 static struct string_list_item *lookup_prefix(struct string_list *map,
282                                               const char *string, size_t len)
283 {
284         int i = string_list_find_insert_index(map, string, 1);
285         if (i < 0) {
286                 /* exact match */
287                 i = -1 - i;
288                 if (!string[len])
289                         return &map->items[i];
290                 /*
291                  * that map entry matches exactly to the string, including
292                  * the cruft at the end beyond "len".  That is not a match
293                  * with string[0:len] that we are looking for.
294                  */
295         } else if (!string[len]) {
296                 /*
297                  * asked with the whole string, and got nothing.  No
298                  * matching entry can exist in the map.
299                  */
300                 return NULL;
301         }
302
303         /*
304          * i is at the exact match to an overlong key, or location the
305          * overlong key would be inserted, which must come after the
306          * real location of the key if one exists.
307          */
308         while (0 <= --i && i < map->nr) {
309                 int cmp = strncasecmp(map->items[i].string, string, len);
310                 if (cmp < 0)
311                         /*
312                          * "i" points at a key definitely below the prefix;
313                          * the map does not have string[0:len] in it.
314                          */
315                         break;
316                 else if (!cmp && !map->items[i].string[len])
317                         /* found it */
318                         return &map->items[i];
319                 /*
320                  * otherwise, the string at "i" may be string[0:len]
321                  * followed by a string that sorts later than string[len:];
322                  * keep trying.
323                  */
324         }
325         return NULL;
326 }
327
328 int map_user(struct string_list *map,
329              const char **email, size_t *emaillen,
330              const char **name, size_t *namelen)
331 {
332         struct string_list_item *item;
333         struct mailmap_entry *me;
334
335         debug_mm("map_user: map '%.*s' <%.*s>\n",
336                  (int)*namelen, debug_str(*name),
337                  (int)*emaillen, debug_str(*email));
338
339         item = lookup_prefix(map, *email, *emaillen);
340         if (item != NULL) {
341                 me = (struct mailmap_entry *)item->util;
342                 if (me->namemap.nr) {
343                         /*
344                          * The item has multiple items, so we'll look up on
345                          * name too. If the name is not found, we choose the
346                          * simple entry.
347                          */
348                         struct string_list_item *subitem;
349                         subitem = lookup_prefix(&me->namemap, *name, *namelen);
350                         if (subitem)
351                                 item = subitem;
352                 }
353         }
354         if (item != NULL) {
355                 struct mailmap_info *mi = (struct mailmap_info *)item->util;
356                 if (mi->name == NULL && mi->email == NULL) {
357                         debug_mm("map_user:  -- (no simple mapping)\n");
358                         return 0;
359                 }
360                 if (mi->email) {
361                                 *email = mi->email;
362                                 *emaillen = strlen(*email);
363                 }
364                 if (mi->name) {
365                                 *name = mi->name;
366                                 *namelen = strlen(*name);
367                 }
368                 debug_mm("map_user:  to '%.*s' <%.*s>\n",
369                          (int)*namelen, debug_str(*name),
370                          (int)*emaillen, debug_str(*email));
371                 return 1;
372         }
373         debug_mm("map_user:  --\n");
374         return 0;
375 }