blob: d62d7bf0e7f736f1d87c85d395b8a78078d20e3e [file] [log] [blame]
Junio C Hamano7c1c6782007-04-27 00:41:15 -07001#include "cache.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00002#include "environment.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01003#include "string-list.h"
Alex Riesenfe5d30b2007-05-01 00:31:52 +02004#include "mailmap.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07005#include "object-name.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07006#include "object-store.h"
Elijah Newrene38da482023-03-21 06:26:05 +00007#include "setup.h"
Junio C Hamano7c1c6782007-04-27 00:41:15 -07008
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +01009#define DEBUG_MAILMAP 0
10#if DEBUG_MAILMAP
11#define debug_mm(...) fprintf(stderr, __VA_ARGS__)
Eric Sunshinefbfba7a2013-07-15 02:54:12 -040012#define debug_str(X) ((X) ? (X) : "(none)")
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010013#else
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +020014__attribute__((format (printf, 1, 2)))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010015static inline void debug_mm(const char *format, ...) {}
Eric Sunshinefbfba7a2013-07-15 02:54:12 -040016static inline const char *debug_str(const char *s) { return s; }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010017#endif
18
Marius Storm-Olsend551a482009-02-08 15:34:27 +010019const char *git_mailmap_file;
Jeff King08610902012-12-12 06:04:04 -050020const char *git_mailmap_blob;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010021
22struct mailmap_info {
23 char *name;
24 char *email;
25};
26
27struct mailmap_entry {
28 /* name and email for the simple mail-only case */
29 char *name;
30 char *email;
31
32 /* name and email for the complex mail and name matching case */
33 struct string_list namemap;
34};
35
36static void free_mailmap_info(void *p, const char *s)
37{
38 struct mailmap_info *mi = (struct mailmap_info *)p;
Junio C Hamanobd237942013-07-15 02:54:13 -040039 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n",
40 s, debug_str(mi->name), debug_str(mi->email));
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010041 free(mi->name);
42 free(mi->email);
Ævar Arnfjörð Bjarmasonccdd5d12021-08-31 15:42:52 +020043 free(mi);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010044}
45
46static void free_mailmap_entry(void *p, const char *s)
47{
48 struct mailmap_entry *me = (struct mailmap_entry *)p;
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +010049 debug_mm("mailmap: removing entries for <%s>, with %"PRIuMAX" sub-entries\n",
50 s, (uintmax_t)me->namemap.nr);
Junio C Hamanobd237942013-07-15 02:54:13 -040051 debug_mm("mailmap: - simple: '%s' <%s>\n",
52 debug_str(me->name), debug_str(me->email));
Eric Sunshinefbfba7a2013-07-15 02:54:12 -040053
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010054 free(me->name);
55 free(me->email);
56
57 me->namemap.strdup_strings = 1;
58 string_list_clear_func(&me->namemap, free_mailmap_info);
Ævar Arnfjörð Bjarmasonccdd5d12021-08-31 15:42:52 +020059 free(me);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010060}
61
Junio C Hamanode2f95e2013-09-12 08:37:08 -070062/*
63 * On some systems (e.g. MinGW 4.0), string.h has _only_ inline
64 * definition of strcasecmp and no non-inline implementation is
65 * supplied anywhere, which is, eh, "unusual"; we cannot take an
66 * address of such a function to store it in namemap.cmp. This is
67 * here as a workaround---do not assign strcasecmp directly to
68 * namemap.cmp until we know no systems that matter have such an
69 * "unusual" string.h.
70 */
71static int namemap_cmp(const char *a, const char *b)
72{
73 return strcasecmp(a, b);
74}
75
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010076static void add_mapping(struct string_list *map,
Junio C Hamanobd237942013-07-15 02:54:13 -040077 char *new_name, char *new_email,
78 char *old_name, char *old_email)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010079{
80 struct mailmap_entry *me;
Stefan Beller63226212014-11-24 19:44:14 -080081 struct string_list_item *item;
Johannes Schindelinbf637802009-03-31 02:18:36 +020082
Junio C Hamanoafe8a902022-05-02 09:50:37 -070083 if (!old_email) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010084 old_email = new_email;
85 new_email = NULL;
86 }
87
Stefan Beller63226212014-11-24 19:44:14 -080088 item = string_list_insert(map, old_email);
89 if (item->util) {
90 me = (struct mailmap_entry *)item->util;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010091 } else {
René Scharfeca56dad2021-03-13 17:17:22 +010092 CALLOC_ARRAY(me, 1);
Junio C Hamano97e751b2013-07-15 02:54:08 -040093 me->namemap.strdup_strings = 1;
Junio C Hamanode2f95e2013-09-12 08:37:08 -070094 me->namemap.cmp = namemap_cmp;
Junio C Hamano97e751b2013-07-15 02:54:08 -040095 item->util = me;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010096 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010097
Junio C Hamanoafe8a902022-05-02 09:50:37 -070098 if (!old_name) {
Stefan Beller63226212014-11-24 19:44:14 -080099 debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email);
100
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100101 /* Replace current name and new email for simple entry */
Jim Meyeringd8d2eb72010-10-11 17:41:16 +0200102 if (new_name) {
103 free(me->name);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100104 me->name = xstrdup(new_name);
Jim Meyeringd8d2eb72010-10-11 17:41:16 +0200105 }
106 if (new_email) {
107 free(me->email);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100108 me->email = xstrdup(new_email);
Jim Meyeringd8d2eb72010-10-11 17:41:16 +0200109 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100110 } else {
Marc-André Lureau74b531f2011-11-17 02:25:06 +0100111 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
Stefan Beller63226212014-11-24 19:44:14 -0800112 debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email);
Junio C Hamano13092a92016-10-12 11:20:23 -0700113 mi->name = xstrdup_or_null(new_name);
114 mi->email = xstrdup_or_null(new_email);
Julian Phillips78a395d2010-06-26 00:41:35 +0100115 string_list_insert(&me->namemap, old_name)->util = mi;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100116 }
117
118 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
Junio C Hamanobd237942013-07-15 02:54:13 -0400119 debug_str(old_name), old_email,
120 debug_str(new_name), debug_str(new_email));
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100121}
122
Björn Steinbrink5288dd52009-03-31 17:30:39 +0200123static char *parse_name_and_email(char *buffer, char **name,
Junio C Hamanobd237942013-07-15 02:54:13 -0400124 char **email, int allow_empty_email)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100125{
126 char *left, *right, *nstart, *nend;
Linus Torvalds2af202b2009-06-18 10:28:43 -0700127 *name = *email = NULL;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100128
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700129 if (!(left = strchr(buffer, '<')))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100130 return NULL;
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700131 if (!(right = strchr(left + 1, '>')))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100132 return NULL;
Björn Steinbrink5288dd52009-03-31 17:30:39 +0200133 if (!allow_empty_email && (left+1 == right))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100134 return NULL;
135
136 /* remove whitespace from beginning and end of name */
137 nstart = buffer;
138 while (isspace(*nstart) && nstart < left)
139 ++nstart;
140 nend = left-1;
Romain Francoise3174bc52012-10-28 00:49:55 +0200141 while (nend > nstart && isspace(*nend))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100142 --nend;
143
Junio C Hamano8c381152013-07-15 02:54:06 -0400144 *name = (nstart <= nend ? nstart : NULL);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100145 *email = left+1;
146 *(nend+1) = '\0';
147 *right++ = '\0';
148
149 return (*right == '\0' ? NULL : right);
150}
151
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100152static void read_mailmap_line(struct string_list *map, char *buffer)
Jeff King7c8ce302012-12-12 05:59:45 -0500153{
154 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
Jeff King7c8ce302012-12-12 05:59:45 -0500155
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100156 if (buffer[0] == '#')
Jeff King7c8ce302012-12-12 05:59:45 -0500157 return;
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100158
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700159 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
Jeff King7c8ce302012-12-12 05:59:45 -0500160 parse_name_and_email(name2, &name2, &email2, 1);
161
162 if (email1)
163 add_mapping(map, name1, email1, name2, email2);
164}
165
Jeff Kingadcd9f52021-02-16 09:44:37 -0500166/* Flags for read_mailmap_file() */
167#define MAILMAP_NOFOLLOW (1<<0)
168
169static int read_mailmap_file(struct string_list *map, const char *filename,
170 unsigned flags)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700171{
172 char buffer[1024];
Jeff King938a60d2012-12-12 06:18:02 -0500173 FILE *f;
Jeff Kingadcd9f52021-02-16 09:44:37 -0500174 int fd;
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700175
Jeff King938a60d2012-12-12 06:18:02 -0500176 if (!filename)
177 return 0;
178
Jeff Kingadcd9f52021-02-16 09:44:37 -0500179 if (flags & MAILMAP_NOFOLLOW)
180 fd = open_nofollow(filename, O_RDONLY);
181 else
182 fd = open(filename, O_RDONLY);
183
184 if (fd < 0) {
Jeff King938a60d2012-12-12 06:18:02 -0500185 if (errno == ENOENT)
186 return 0;
Nguyễn Thái Ngọc Duy60901e42016-05-08 16:47:50 +0700187 return error_errno("unable to open mailmap at %s", filename);
Jeff King938a60d2012-12-12 06:18:02 -0500188 }
Jeff Kingadcd9f52021-02-16 09:44:37 -0500189 f = xfdopen(fd, "r");
Jeff King938a60d2012-12-12 06:18:02 -0500190
Jeff King7c8ce302012-12-12 05:59:45 -0500191 while (fgets(buffer, sizeof(buffer), f) != NULL)
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100192 read_mailmap_line(map, buffer);
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700193 fclose(f);
194 return 0;
195}
196
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100197static void read_mailmap_string(struct string_list *map, char *buf)
Jeff King08610902012-12-12 06:04:04 -0500198{
Jeff Kingf972a162013-08-27 21:41:39 -0400199 while (*buf) {
200 char *end = strchrnul(buf, '\n');
Jeff King08610902012-12-12 06:04:04 -0500201
Jeff Kingf972a162013-08-27 21:41:39 -0400202 if (*end)
203 *end++ = '\0';
Jeff King08610902012-12-12 06:04:04 -0500204
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100205 read_mailmap_line(map, buf);
Jeff Kingf972a162013-08-27 21:41:39 -0400206 buf = end;
Jeff King08610902012-12-12 06:04:04 -0500207 }
208}
209
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100210static int read_mailmap_blob(struct string_list *map, const char *name)
Jeff King08610902012-12-12 06:04:04 -0500211{
brian m. carlson15be4a52017-07-13 23:49:27 +0000212 struct object_id oid;
Jeff King08610902012-12-12 06:04:04 -0500213 char *buf;
214 unsigned long size;
215 enum object_type type;
216
217 if (!name)
Jeff King938a60d2012-12-12 06:18:02 -0500218 return 0;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200219 if (repo_get_oid(the_repository, name, &oid) < 0)
Jeff King938a60d2012-12-12 06:18:02 -0500220 return 0;
Jeff King08610902012-12-12 06:04:04 -0500221
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200222 buf = repo_read_object_file(the_repository, &oid, &type, &size);
Jeff King08610902012-12-12 06:04:04 -0500223 if (!buf)
Jeff King938a60d2012-12-12 06:18:02 -0500224 return error("unable to read mailmap object at %s", name);
Jeff King08610902012-12-12 06:04:04 -0500225 if (type != OBJ_BLOB)
Jeff King938a60d2012-12-12 06:18:02 -0500226 return error("mailmap is not a blob: %s", name);
Jeff King08610902012-12-12 06:04:04 -0500227
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100228 read_mailmap_string(map, buf);
Jeff King08610902012-12-12 06:04:04 -0500229
230 free(buf);
231 return 0;
232}
233
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100234int read_mailmap(struct string_list *map)
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100235{
Jeff King938a60d2012-12-12 06:18:02 -0500236 int err = 0;
Jeff King8c473ce2012-12-13 08:04:47 -0500237
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100238 map->strdup_strings = 1;
Junio C Hamanode2f95e2013-09-12 08:37:08 -0700239 map->cmp = namemap_cmp;
Jeff King8c473ce2012-12-13 08:04:47 -0500240
241 if (!git_mailmap_blob && is_bare_repository())
242 git_mailmap_blob = "HEAD:.mailmap";
243
Jeff Kinga38cb982021-02-10 15:34:33 -0500244 if (!startup_info->have_repository || !is_bare_repository())
Junio C Hamano204333b2021-03-22 14:00:22 -0700245 err |= read_mailmap_file(map, ".mailmap",
246 startup_info->have_repository ?
247 MAILMAP_NOFOLLOW : 0);
Jeff King5735dc52016-03-05 17:13:29 -0500248 if (startup_info->have_repository)
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100249 err |= read_mailmap_blob(map, git_mailmap_blob);
Jeff Kingadcd9f52021-02-16 09:44:37 -0500250 err |= read_mailmap_file(map, git_mailmap_file, 0);
Jeff King938a60d2012-12-12 06:18:02 -0500251 return err;
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100252}
253
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100254void clear_mailmap(struct string_list *map)
255{
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +0100256 debug_mm("mailmap: clearing %"PRIuMAX" entries...\n",
257 (uintmax_t)map->nr);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100258 map->strdup_strings = 1;
259 string_list_clear_func(map, free_mailmap_entry);
260 debug_mm("mailmap: cleared\n");
261}
262
Junio C Hamano388c7f82013-01-05 22:26:39 +0100263/*
264 * Look for an entry in map that match string[0:len]; string[len]
265 * does not have to be NUL (but it could be).
266 */
267static struct string_list_item *lookup_prefix(struct string_list *map,
268 const char *string, size_t len)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700269{
Junio C Hamano388c7f82013-01-05 22:26:39 +0100270 int i = string_list_find_insert_index(map, string, 1);
271 if (i < 0) {
272 /* exact match */
273 i = -1 - i;
274 if (!string[len])
275 return &map->items[i];
276 /*
277 * that map entry matches exactly to the string, including
278 * the cruft at the end beyond "len". That is not a match
279 * with string[0:len] that we are looking for.
280 */
281 } else if (!string[len]) {
282 /*
283 * asked with the whole string, and got nothing. No
284 * matching entry can exist in the map.
285 */
286 return NULL;
287 }
288
289 /*
290 * i is at the exact match to an overlong key, or location the
291 * overlong key would be inserted, which must come after the
292 * real location of the key if one exists.
293 */
294 while (0 <= --i && i < map->nr) {
295 int cmp = strncasecmp(map->items[i].string, string, len);
296 if (cmp < 0)
297 /*
298 * "i" points at a key definitely below the prefix;
299 * the map does not have string[0:len] in it.
300 */
301 break;
302 else if (!cmp && !map->items[i].string[len])
303 /* found it */
304 return &map->items[i];
305 /*
306 * otherwise, the string at "i" may be string[0:len]
307 * followed by a string that sorts later than string[len:];
308 * keep trying.
309 */
310 }
311 return NULL;
312}
313
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700314int map_user(struct string_list *map,
Junio C Hamanobd237942013-07-15 02:54:13 -0400315 const char **email, size_t *emaillen,
316 const char **name, size_t *namelen)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700317{
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700318 struct string_list_item *item;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100319 struct mailmap_entry *me;
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700320
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100321 debug_mm("map_user: map '%.*s' <%.*s>\n",
Junio C Hamanobd237942013-07-15 02:54:13 -0400322 (int)*namelen, debug_str(*name),
323 (int)*emaillen, debug_str(*email));
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700324
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100325 item = lookup_prefix(map, *email, *emaillen);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700326 if (item) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100327 me = (struct mailmap_entry *)item->util;
328 if (me->namemap.nr) {
Junio C Hamanobd237942013-07-15 02:54:13 -0400329 /*
330 * The item has multiple items, so we'll look up on
331 * name too. If the name is not found, we choose the
332 * simple entry.
333 */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100334 struct string_list_item *subitem;
335 subitem = lookup_prefix(&me->namemap, *name, *namelen);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100336 if (subitem)
337 item = subitem;
338 }
339 }
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700340 if (item) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100341 struct mailmap_info *mi = (struct mailmap_info *)item->util;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100342 if (mi->name == NULL && mi->email == NULL) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100343 debug_mm("map_user: -- (no simple mapping)\n");
344 return 0;
345 }
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100346 if (mi->email) {
347 *email = mi->email;
348 *emaillen = strlen(*email);
349 }
350 if (mi->name) {
351 *name = mi->name;
352 *namelen = strlen(*name);
353 }
Junio C Hamanobd237942013-07-15 02:54:13 -0400354 debug_mm("map_user: to '%.*s' <%.*s>\n",
355 (int)*namelen, debug_str(*name),
356 (int)*emaillen, debug_str(*email));
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700357 return 1;
358 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100359 debug_mm("map_user: --\n");
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700360 return 0;
361}