blob: da2589b08229813963347115a001d8ce6f45ae32 [file] [log] [blame]
Junio C Hamano7c1c6782007-04-27 00:41:15 -07001#include "cache.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01002#include "string-list.h"
Alex Riesenfe5d30b2007-05-01 00:31:52 +02003#include "mailmap.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Junio C Hamano7c1c6782007-04-27 00:41:15 -07005
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +01006#define DEBUG_MAILMAP 0
7#if DEBUG_MAILMAP
8#define debug_mm(...) fprintf(stderr, __VA_ARGS__)
Eric Sunshinefbfba7a2013-07-15 02:54:12 -04009#define debug_str(X) ((X) ? (X) : "(none)")
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010010#else
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +020011__attribute__((format (printf, 1, 2)))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010012static inline void debug_mm(const char *format, ...) {}
Eric Sunshinefbfba7a2013-07-15 02:54:12 -040013static inline const char *debug_str(const char *s) { return s; }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010014#endif
15
Marius Storm-Olsend551a482009-02-08 15:34:27 +010016const char *git_mailmap_file;
Jeff King08610902012-12-12 06:04:04 -050017const char *git_mailmap_blob;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010018
19struct mailmap_info {
20 char *name;
21 char *email;
22};
23
24struct mailmap_entry {
25 /* name and email for the simple mail-only case */
26 char *name;
27 char *email;
28
29 /* name and email for the complex mail and name matching case */
30 struct string_list namemap;
31};
32
33static void free_mailmap_info(void *p, const char *s)
34{
35 struct mailmap_info *mi = (struct mailmap_info *)p;
Junio C Hamanobd237942013-07-15 02:54:13 -040036 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n",
37 s, debug_str(mi->name), debug_str(mi->email));
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010038 free(mi->name);
39 free(mi->email);
Ævar Arnfjörð Bjarmasonccdd5d12021-08-31 15:42:52 +020040 free(mi);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010041}
42
43static void free_mailmap_entry(void *p, const char *s)
44{
45 struct mailmap_entry *me = (struct mailmap_entry *)p;
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +010046 debug_mm("mailmap: removing entries for <%s>, with %"PRIuMAX" sub-entries\n",
47 s, (uintmax_t)me->namemap.nr);
Junio C Hamanobd237942013-07-15 02:54:13 -040048 debug_mm("mailmap: - simple: '%s' <%s>\n",
49 debug_str(me->name), debug_str(me->email));
Eric Sunshinefbfba7a2013-07-15 02:54:12 -040050
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010051 free(me->name);
52 free(me->email);
53
54 me->namemap.strdup_strings = 1;
55 string_list_clear_func(&me->namemap, free_mailmap_info);
Ævar Arnfjörð Bjarmasonccdd5d12021-08-31 15:42:52 +020056 free(me);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010057}
58
Junio C Hamanode2f95e2013-09-12 08:37:08 -070059/*
60 * On some systems (e.g. MinGW 4.0), string.h has _only_ inline
61 * definition of strcasecmp and no non-inline implementation is
62 * supplied anywhere, which is, eh, "unusual"; we cannot take an
63 * address of such a function to store it in namemap.cmp. This is
64 * here as a workaround---do not assign strcasecmp directly to
65 * namemap.cmp until we know no systems that matter have such an
66 * "unusual" string.h.
67 */
68static int namemap_cmp(const char *a, const char *b)
69{
70 return strcasecmp(a, b);
71}
72
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010073static void add_mapping(struct string_list *map,
Junio C Hamanobd237942013-07-15 02:54:13 -040074 char *new_name, char *new_email,
75 char *old_name, char *old_email)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010076{
77 struct mailmap_entry *me;
Stefan Beller63226212014-11-24 19:44:14 -080078 struct string_list_item *item;
Johannes Schindelinbf637802009-03-31 02:18:36 +020079
Junio C Hamanoafe8a902022-05-02 09:50:37 -070080 if (!old_email) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010081 old_email = new_email;
82 new_email = NULL;
83 }
84
Stefan Beller63226212014-11-24 19:44:14 -080085 item = string_list_insert(map, old_email);
86 if (item->util) {
87 me = (struct mailmap_entry *)item->util;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010088 } else {
René Scharfeca56dad2021-03-13 17:17:22 +010089 CALLOC_ARRAY(me, 1);
Junio C Hamano97e751b2013-07-15 02:54:08 -040090 me->namemap.strdup_strings = 1;
Junio C Hamanode2f95e2013-09-12 08:37:08 -070091 me->namemap.cmp = namemap_cmp;
Junio C Hamano97e751b2013-07-15 02:54:08 -040092 item->util = me;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010093 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010094
Junio C Hamanoafe8a902022-05-02 09:50:37 -070095 if (!old_name) {
Stefan Beller63226212014-11-24 19:44:14 -080096 debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email);
97
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010098 /* Replace current name and new email for simple entry */
Jim Meyeringd8d2eb72010-10-11 17:41:16 +020099 if (new_name) {
100 free(me->name);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100101 me->name = xstrdup(new_name);
Jim Meyeringd8d2eb72010-10-11 17:41:16 +0200102 }
103 if (new_email) {
104 free(me->email);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100105 me->email = xstrdup(new_email);
Jim Meyeringd8d2eb72010-10-11 17:41:16 +0200106 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100107 } else {
Marc-André Lureau74b531f2011-11-17 02:25:06 +0100108 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
Stefan Beller63226212014-11-24 19:44:14 -0800109 debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email);
Junio C Hamano13092a92016-10-12 11:20:23 -0700110 mi->name = xstrdup_or_null(new_name);
111 mi->email = xstrdup_or_null(new_email);
Julian Phillips78a395d2010-06-26 00:41:35 +0100112 string_list_insert(&me->namemap, old_name)->util = mi;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100113 }
114
115 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
Junio C Hamanobd237942013-07-15 02:54:13 -0400116 debug_str(old_name), old_email,
117 debug_str(new_name), debug_str(new_email));
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100118}
119
Björn Steinbrink5288dd52009-03-31 17:30:39 +0200120static char *parse_name_and_email(char *buffer, char **name,
Junio C Hamanobd237942013-07-15 02:54:13 -0400121 char **email, int allow_empty_email)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100122{
123 char *left, *right, *nstart, *nend;
Linus Torvalds2af202b2009-06-18 10:28:43 -0700124 *name = *email = NULL;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100125
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700126 if (!(left = strchr(buffer, '<')))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100127 return NULL;
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700128 if (!(right = strchr(left + 1, '>')))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100129 return NULL;
Björn Steinbrink5288dd52009-03-31 17:30:39 +0200130 if (!allow_empty_email && (left+1 == right))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100131 return NULL;
132
133 /* remove whitespace from beginning and end of name */
134 nstart = buffer;
135 while (isspace(*nstart) && nstart < left)
136 ++nstart;
137 nend = left-1;
Romain Francoise3174bc52012-10-28 00:49:55 +0200138 while (nend > nstart && isspace(*nend))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100139 --nend;
140
Junio C Hamano8c381152013-07-15 02:54:06 -0400141 *name = (nstart <= nend ? nstart : NULL);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100142 *email = left+1;
143 *(nend+1) = '\0';
144 *right++ = '\0';
145
146 return (*right == '\0' ? NULL : right);
147}
148
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100149static void read_mailmap_line(struct string_list *map, char *buffer)
Jeff King7c8ce302012-12-12 05:59:45 -0500150{
151 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
Jeff King7c8ce302012-12-12 05:59:45 -0500152
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100153 if (buffer[0] == '#')
Jeff King7c8ce302012-12-12 05:59:45 -0500154 return;
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100155
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700156 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
Jeff King7c8ce302012-12-12 05:59:45 -0500157 parse_name_and_email(name2, &name2, &email2, 1);
158
159 if (email1)
160 add_mapping(map, name1, email1, name2, email2);
161}
162
Jeff Kingadcd9f52021-02-16 09:44:37 -0500163/* Flags for read_mailmap_file() */
164#define MAILMAP_NOFOLLOW (1<<0)
165
166static int read_mailmap_file(struct string_list *map, const char *filename,
167 unsigned flags)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700168{
169 char buffer[1024];
Jeff King938a60d2012-12-12 06:18:02 -0500170 FILE *f;
Jeff Kingadcd9f52021-02-16 09:44:37 -0500171 int fd;
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700172
Jeff King938a60d2012-12-12 06:18:02 -0500173 if (!filename)
174 return 0;
175
Jeff Kingadcd9f52021-02-16 09:44:37 -0500176 if (flags & MAILMAP_NOFOLLOW)
177 fd = open_nofollow(filename, O_RDONLY);
178 else
179 fd = open(filename, O_RDONLY);
180
181 if (fd < 0) {
Jeff King938a60d2012-12-12 06:18:02 -0500182 if (errno == ENOENT)
183 return 0;
Nguyễn Thái Ngọc Duy60901e42016-05-08 16:47:50 +0700184 return error_errno("unable to open mailmap at %s", filename);
Jeff King938a60d2012-12-12 06:18:02 -0500185 }
Jeff Kingadcd9f52021-02-16 09:44:37 -0500186 f = xfdopen(fd, "r");
Jeff King938a60d2012-12-12 06:18:02 -0500187
Jeff King7c8ce302012-12-12 05:59:45 -0500188 while (fgets(buffer, sizeof(buffer), f) != NULL)
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100189 read_mailmap_line(map, buffer);
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700190 fclose(f);
191 return 0;
192}
193
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100194static void read_mailmap_string(struct string_list *map, char *buf)
Jeff King08610902012-12-12 06:04:04 -0500195{
Jeff Kingf972a162013-08-27 21:41:39 -0400196 while (*buf) {
197 char *end = strchrnul(buf, '\n');
Jeff King08610902012-12-12 06:04:04 -0500198
Jeff Kingf972a162013-08-27 21:41:39 -0400199 if (*end)
200 *end++ = '\0';
Jeff King08610902012-12-12 06:04:04 -0500201
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100202 read_mailmap_line(map, buf);
Jeff Kingf972a162013-08-27 21:41:39 -0400203 buf = end;
Jeff King08610902012-12-12 06:04:04 -0500204 }
205}
206
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100207static int read_mailmap_blob(struct string_list *map, const char *name)
Jeff King08610902012-12-12 06:04:04 -0500208{
brian m. carlson15be4a52017-07-13 23:49:27 +0000209 struct object_id oid;
Jeff King08610902012-12-12 06:04:04 -0500210 char *buf;
211 unsigned long size;
212 enum object_type type;
213
214 if (!name)
Jeff King938a60d2012-12-12 06:18:02 -0500215 return 0;
brian m. carlson15be4a52017-07-13 23:49:27 +0000216 if (get_oid(name, &oid) < 0)
Jeff King938a60d2012-12-12 06:18:02 -0500217 return 0;
Jeff King08610902012-12-12 06:04:04 -0500218
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000219 buf = read_object_file(&oid, &type, &size);
Jeff King08610902012-12-12 06:04:04 -0500220 if (!buf)
Jeff King938a60d2012-12-12 06:18:02 -0500221 return error("unable to read mailmap object at %s", name);
Jeff King08610902012-12-12 06:04:04 -0500222 if (type != OBJ_BLOB)
Jeff King938a60d2012-12-12 06:18:02 -0500223 return error("mailmap is not a blob: %s", name);
Jeff King08610902012-12-12 06:04:04 -0500224
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100225 read_mailmap_string(map, buf);
Jeff King08610902012-12-12 06:04:04 -0500226
227 free(buf);
228 return 0;
229}
230
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100231int read_mailmap(struct string_list *map)
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100232{
Jeff King938a60d2012-12-12 06:18:02 -0500233 int err = 0;
Jeff King8c473ce2012-12-13 08:04:47 -0500234
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100235 map->strdup_strings = 1;
Junio C Hamanode2f95e2013-09-12 08:37:08 -0700236 map->cmp = namemap_cmp;
Jeff King8c473ce2012-12-13 08:04:47 -0500237
238 if (!git_mailmap_blob && is_bare_repository())
239 git_mailmap_blob = "HEAD:.mailmap";
240
Jeff Kinga38cb982021-02-10 15:34:33 -0500241 if (!startup_info->have_repository || !is_bare_repository())
Junio C Hamano204333b2021-03-22 14:00:22 -0700242 err |= read_mailmap_file(map, ".mailmap",
243 startup_info->have_repository ?
244 MAILMAP_NOFOLLOW : 0);
Jeff King5735dc52016-03-05 17:13:29 -0500245 if (startup_info->have_repository)
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100246 err |= read_mailmap_blob(map, git_mailmap_blob);
Jeff Kingadcd9f52021-02-16 09:44:37 -0500247 err |= read_mailmap_file(map, git_mailmap_file, 0);
Jeff King938a60d2012-12-12 06:18:02 -0500248 return err;
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100249}
250
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100251void clear_mailmap(struct string_list *map)
252{
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +0100253 debug_mm("mailmap: clearing %"PRIuMAX" entries...\n",
254 (uintmax_t)map->nr);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100255 map->strdup_strings = 1;
256 string_list_clear_func(map, free_mailmap_entry);
257 debug_mm("mailmap: cleared\n");
258}
259
Junio C Hamano388c7f82013-01-05 22:26:39 +0100260/*
261 * Look for an entry in map that match string[0:len]; string[len]
262 * does not have to be NUL (but it could be).
263 */
264static struct string_list_item *lookup_prefix(struct string_list *map,
265 const char *string, size_t len)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700266{
Junio C Hamano388c7f82013-01-05 22:26:39 +0100267 int i = string_list_find_insert_index(map, string, 1);
268 if (i < 0) {
269 /* exact match */
270 i = -1 - i;
271 if (!string[len])
272 return &map->items[i];
273 /*
274 * that map entry matches exactly to the string, including
275 * the cruft at the end beyond "len". That is not a match
276 * with string[0:len] that we are looking for.
277 */
278 } else if (!string[len]) {
279 /*
280 * asked with the whole string, and got nothing. No
281 * matching entry can exist in the map.
282 */
283 return NULL;
284 }
285
286 /*
287 * i is at the exact match to an overlong key, or location the
288 * overlong key would be inserted, which must come after the
289 * real location of the key if one exists.
290 */
291 while (0 <= --i && i < map->nr) {
292 int cmp = strncasecmp(map->items[i].string, string, len);
293 if (cmp < 0)
294 /*
295 * "i" points at a key definitely below the prefix;
296 * the map does not have string[0:len] in it.
297 */
298 break;
299 else if (!cmp && !map->items[i].string[len])
300 /* found it */
301 return &map->items[i];
302 /*
303 * otherwise, the string at "i" may be string[0:len]
304 * followed by a string that sorts later than string[len:];
305 * keep trying.
306 */
307 }
308 return NULL;
309}
310
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700311int map_user(struct string_list *map,
Junio C Hamanobd237942013-07-15 02:54:13 -0400312 const char **email, size_t *emaillen,
313 const char **name, size_t *namelen)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700314{
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700315 struct string_list_item *item;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100316 struct mailmap_entry *me;
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700317
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100318 debug_mm("map_user: map '%.*s' <%.*s>\n",
Junio C Hamanobd237942013-07-15 02:54:13 -0400319 (int)*namelen, debug_str(*name),
320 (int)*emaillen, debug_str(*email));
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700321
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100322 item = lookup_prefix(map, *email, *emaillen);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700323 if (item) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100324 me = (struct mailmap_entry *)item->util;
325 if (me->namemap.nr) {
Junio C Hamanobd237942013-07-15 02:54:13 -0400326 /*
327 * The item has multiple items, so we'll look up on
328 * name too. If the name is not found, we choose the
329 * simple entry.
330 */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100331 struct string_list_item *subitem;
332 subitem = lookup_prefix(&me->namemap, *name, *namelen);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100333 if (subitem)
334 item = subitem;
335 }
336 }
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700337 if (item) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100338 struct mailmap_info *mi = (struct mailmap_info *)item->util;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100339 if (mi->name == NULL && mi->email == NULL) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100340 debug_mm("map_user: -- (no simple mapping)\n");
341 return 0;
342 }
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100343 if (mi->email) {
344 *email = mi->email;
345 *emaillen = strlen(*email);
346 }
347 if (mi->name) {
348 *name = mi->name;
349 *namelen = strlen(*name);
350 }
Junio C Hamanobd237942013-07-15 02:54:13 -0400351 debug_mm("map_user: to '%.*s' <%.*s>\n",
352 (int)*namelen, debug_str(*name),
353 (int)*emaillen, debug_str(*email));
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700354 return 1;
355 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100356 debug_mm("map_user: --\n");
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700357 return 0;
358}