blob: 3d6a5e9400f4c8195d0f58ab58f3f9a4dfa0acba [file] [log] [blame]
Elijah Newrene93fc5d2023-04-11 00:41:50 -07001#include "git-compat-util.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"
Elijah Newrena034e912023-05-16 06:34:06 +00006#include "object-store-ll.h"
Elijah Newrene38da482023-03-21 06:26:05 +00007#include "setup.h"
Junio C Hamano7c1c6782007-04-27 00:41:15 -07008
Marius Storm-Olsend551a482009-02-08 15:34:27 +01009const char *git_mailmap_file;
Jeff King08610902012-12-12 06:04:04 -050010const char *git_mailmap_blob;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010011
12struct mailmap_info {
13 char *name;
14 char *email;
15};
16
17struct mailmap_entry {
18 /* name and email for the simple mail-only case */
19 char *name;
20 char *email;
21
22 /* name and email for the complex mail and name matching case */
23 struct string_list namemap;
24};
25
Jeff King910d07a2023-03-17 15:16:26 -040026static void free_mailmap_info(void *p, const char *s UNUSED)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010027{
28 struct mailmap_info *mi = (struct mailmap_info *)p;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010029 free(mi->name);
30 free(mi->email);
Ævar Arnfjörð Bjarmasonccdd5d12021-08-31 15:42:52 +020031 free(mi);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010032}
33
Jeff King910d07a2023-03-17 15:16:26 -040034static void free_mailmap_entry(void *p, const char *s UNUSED)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010035{
36 struct mailmap_entry *me = (struct mailmap_entry *)p;
Eric Sunshinefbfba7a2013-07-15 02:54:12 -040037
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010038 free(me->name);
39 free(me->email);
40
41 me->namemap.strdup_strings = 1;
42 string_list_clear_func(&me->namemap, free_mailmap_info);
Ævar Arnfjörð Bjarmasonccdd5d12021-08-31 15:42:52 +020043 free(me);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010044}
45
Junio C Hamanode2f95e2013-09-12 08:37:08 -070046/*
47 * On some systems (e.g. MinGW 4.0), string.h has _only_ inline
48 * definition of strcasecmp and no non-inline implementation is
49 * supplied anywhere, which is, eh, "unusual"; we cannot take an
50 * address of such a function to store it in namemap.cmp. This is
51 * here as a workaround---do not assign strcasecmp directly to
52 * namemap.cmp until we know no systems that matter have such an
53 * "unusual" string.h.
54 */
55static int namemap_cmp(const char *a, const char *b)
56{
57 return strcasecmp(a, b);
58}
59
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010060static void add_mapping(struct string_list *map,
Junio C Hamanobd237942013-07-15 02:54:13 -040061 char *new_name, char *new_email,
62 char *old_name, char *old_email)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010063{
64 struct mailmap_entry *me;
Stefan Beller63226212014-11-24 19:44:14 -080065 struct string_list_item *item;
Johannes Schindelinbf637802009-03-31 02:18:36 +020066
Junio C Hamanoafe8a902022-05-02 09:50:37 -070067 if (!old_email) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010068 old_email = new_email;
69 new_email = NULL;
70 }
71
Stefan Beller63226212014-11-24 19:44:14 -080072 item = string_list_insert(map, old_email);
73 if (item->util) {
74 me = (struct mailmap_entry *)item->util;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010075 } else {
René Scharfeca56dad2021-03-13 17:17:22 +010076 CALLOC_ARRAY(me, 1);
Junio C Hamano97e751b2013-07-15 02:54:08 -040077 me->namemap.strdup_strings = 1;
Junio C Hamanode2f95e2013-09-12 08:37:08 -070078 me->namemap.cmp = namemap_cmp;
Junio C Hamano97e751b2013-07-15 02:54:08 -040079 item->util = me;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010080 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010081
Junio C Hamanoafe8a902022-05-02 09:50:37 -070082 if (!old_name) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010083 /* Replace current name and new email for simple entry */
Jim Meyeringd8d2eb72010-10-11 17:41:16 +020084 if (new_name) {
85 free(me->name);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010086 me->name = xstrdup(new_name);
Jim Meyeringd8d2eb72010-10-11 17:41:16 +020087 }
88 if (new_email) {
89 free(me->email);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010090 me->email = xstrdup(new_email);
Jim Meyeringd8d2eb72010-10-11 17:41:16 +020091 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010092 } else {
Marc-André Lureau74b531f2011-11-17 02:25:06 +010093 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
Junio C Hamano13092a92016-10-12 11:20:23 -070094 mi->name = xstrdup_or_null(new_name);
95 mi->email = xstrdup_or_null(new_email);
Julian Phillips78a395d2010-06-26 00:41:35 +010096 string_list_insert(&me->namemap, old_name)->util = mi;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010097 }
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +010098}
99
Björn Steinbrink5288dd52009-03-31 17:30:39 +0200100static char *parse_name_and_email(char *buffer, char **name,
Junio C Hamanobd237942013-07-15 02:54:13 -0400101 char **email, int allow_empty_email)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100102{
103 char *left, *right, *nstart, *nend;
Linus Torvalds2af202b2009-06-18 10:28:43 -0700104 *name = *email = NULL;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100105
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700106 if (!(left = strchr(buffer, '<')))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100107 return NULL;
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700108 if (!(right = strchr(left + 1, '>')))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100109 return NULL;
Björn Steinbrink5288dd52009-03-31 17:30:39 +0200110 if (!allow_empty_email && (left+1 == right))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100111 return NULL;
112
113 /* remove whitespace from beginning and end of name */
114 nstart = buffer;
115 while (isspace(*nstart) && nstart < left)
116 ++nstart;
117 nend = left-1;
Romain Francoise3174bc52012-10-28 00:49:55 +0200118 while (nend > nstart && isspace(*nend))
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100119 --nend;
120
Junio C Hamano8c381152013-07-15 02:54:06 -0400121 *name = (nstart <= nend ? nstart : NULL);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100122 *email = left+1;
123 *(nend+1) = '\0';
124 *right++ = '\0';
125
126 return (*right == '\0' ? NULL : right);
127}
128
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100129static void read_mailmap_line(struct string_list *map, char *buffer)
Jeff King7c8ce302012-12-12 05:59:45 -0500130{
131 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
Jeff King7c8ce302012-12-12 05:59:45 -0500132
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100133 if (buffer[0] == '#')
Jeff King7c8ce302012-12-12 05:59:45 -0500134 return;
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100135
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700136 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
Jeff King7c8ce302012-12-12 05:59:45 -0500137 parse_name_and_email(name2, &name2, &email2, 1);
138
139 if (email1)
140 add_mapping(map, name1, email1, name2, email2);
141}
142
Jeff Kingadcd9f52021-02-16 09:44:37 -0500143/* Flags for read_mailmap_file() */
144#define MAILMAP_NOFOLLOW (1<<0)
145
146static int read_mailmap_file(struct string_list *map, const char *filename,
147 unsigned flags)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700148{
149 char buffer[1024];
Jeff King938a60d2012-12-12 06:18:02 -0500150 FILE *f;
Jeff Kingadcd9f52021-02-16 09:44:37 -0500151 int fd;
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700152
Jeff King938a60d2012-12-12 06:18:02 -0500153 if (!filename)
154 return 0;
155
Jeff Kingadcd9f52021-02-16 09:44:37 -0500156 if (flags & MAILMAP_NOFOLLOW)
157 fd = open_nofollow(filename, O_RDONLY);
158 else
159 fd = open(filename, O_RDONLY);
160
161 if (fd < 0) {
Jeff King938a60d2012-12-12 06:18:02 -0500162 if (errno == ENOENT)
163 return 0;
Nguyễn Thái Ngọc Duy60901e42016-05-08 16:47:50 +0700164 return error_errno("unable to open mailmap at %s", filename);
Jeff King938a60d2012-12-12 06:18:02 -0500165 }
Jeff Kingadcd9f52021-02-16 09:44:37 -0500166 f = xfdopen(fd, "r");
Jeff King938a60d2012-12-12 06:18:02 -0500167
Jeff King7c8ce302012-12-12 05:59:45 -0500168 while (fgets(buffer, sizeof(buffer), f) != NULL)
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100169 read_mailmap_line(map, buffer);
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700170 fclose(f);
171 return 0;
172}
173
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100174static void read_mailmap_string(struct string_list *map, char *buf)
Jeff King08610902012-12-12 06:04:04 -0500175{
Jeff Kingf972a162013-08-27 21:41:39 -0400176 while (*buf) {
177 char *end = strchrnul(buf, '\n');
Jeff King08610902012-12-12 06:04:04 -0500178
Jeff Kingf972a162013-08-27 21:41:39 -0400179 if (*end)
180 *end++ = '\0';
Jeff King08610902012-12-12 06:04:04 -0500181
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100182 read_mailmap_line(map, buf);
Jeff Kingf972a162013-08-27 21:41:39 -0400183 buf = end;
Jeff King08610902012-12-12 06:04:04 -0500184 }
185}
186
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100187static int read_mailmap_blob(struct string_list *map, const char *name)
Jeff King08610902012-12-12 06:04:04 -0500188{
brian m. carlson15be4a52017-07-13 23:49:27 +0000189 struct object_id oid;
Jeff King08610902012-12-12 06:04:04 -0500190 char *buf;
191 unsigned long size;
192 enum object_type type;
193
194 if (!name)
Jeff King938a60d2012-12-12 06:18:02 -0500195 return 0;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200196 if (repo_get_oid(the_repository, name, &oid) < 0)
Jeff King938a60d2012-12-12 06:18:02 -0500197 return 0;
Jeff King08610902012-12-12 06:04:04 -0500198
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200199 buf = repo_read_object_file(the_repository, &oid, &type, &size);
Jeff King08610902012-12-12 06:04:04 -0500200 if (!buf)
Jeff King938a60d2012-12-12 06:18:02 -0500201 return error("unable to read mailmap object at %s", name);
Jeff King08610902012-12-12 06:04:04 -0500202 if (type != OBJ_BLOB)
Jeff King938a60d2012-12-12 06:18:02 -0500203 return error("mailmap is not a blob: %s", name);
Jeff King08610902012-12-12 06:04:04 -0500204
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100205 read_mailmap_string(map, buf);
Jeff King08610902012-12-12 06:04:04 -0500206
207 free(buf);
208 return 0;
209}
210
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100211int read_mailmap(struct string_list *map)
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100212{
Jeff King938a60d2012-12-12 06:18:02 -0500213 int err = 0;
Jeff King8c473ce2012-12-13 08:04:47 -0500214
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100215 map->strdup_strings = 1;
Junio C Hamanode2f95e2013-09-12 08:37:08 -0700216 map->cmp = namemap_cmp;
Jeff King8c473ce2012-12-13 08:04:47 -0500217
218 if (!git_mailmap_blob && is_bare_repository())
219 git_mailmap_blob = "HEAD:.mailmap";
220
Jeff Kinga38cb982021-02-10 15:34:33 -0500221 if (!startup_info->have_repository || !is_bare_repository())
Junio C Hamano204333b2021-03-22 14:00:22 -0700222 err |= read_mailmap_file(map, ".mailmap",
223 startup_info->have_repository ?
224 MAILMAP_NOFOLLOW : 0);
Jeff King5735dc52016-03-05 17:13:29 -0500225 if (startup_info->have_repository)
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100226 err |= read_mailmap_blob(map, git_mailmap_blob);
Jeff Kingadcd9f52021-02-16 09:44:37 -0500227 err |= read_mailmap_file(map, git_mailmap_file, 0);
Jeff King938a60d2012-12-12 06:18:02 -0500228 return err;
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100229}
230
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100231void clear_mailmap(struct string_list *map)
232{
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100233 map->strdup_strings = 1;
234 string_list_clear_func(map, free_mailmap_entry);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100235}
236
Junio C Hamano388c7f82013-01-05 22:26:39 +0100237/*
238 * Look for an entry in map that match string[0:len]; string[len]
239 * does not have to be NUL (but it could be).
240 */
241static struct string_list_item *lookup_prefix(struct string_list *map,
242 const char *string, size_t len)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700243{
Junio C Hamano388c7f82013-01-05 22:26:39 +0100244 int i = string_list_find_insert_index(map, string, 1);
245 if (i < 0) {
246 /* exact match */
247 i = -1 - i;
248 if (!string[len])
249 return &map->items[i];
250 /*
251 * that map entry matches exactly to the string, including
252 * the cruft at the end beyond "len". That is not a match
253 * with string[0:len] that we are looking for.
254 */
255 } else if (!string[len]) {
256 /*
257 * asked with the whole string, and got nothing. No
258 * matching entry can exist in the map.
259 */
260 return NULL;
261 }
262
263 /*
264 * i is at the exact match to an overlong key, or location the
265 * overlong key would be inserted, which must come after the
266 * real location of the key if one exists.
267 */
268 while (0 <= --i && i < map->nr) {
269 int cmp = strncasecmp(map->items[i].string, string, len);
270 if (cmp < 0)
271 /*
272 * "i" points at a key definitely below the prefix;
273 * the map does not have string[0:len] in it.
274 */
275 break;
276 else if (!cmp && !map->items[i].string[len])
277 /* found it */
278 return &map->items[i];
279 /*
280 * otherwise, the string at "i" may be string[0:len]
281 * followed by a string that sorts later than string[len:];
282 * keep trying.
283 */
284 }
285 return NULL;
286}
287
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700288int map_user(struct string_list *map,
Junio C Hamanobd237942013-07-15 02:54:13 -0400289 const char **email, size_t *emaillen,
290 const char **name, size_t *namelen)
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700291{
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700292 struct string_list_item *item;
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100293 struct mailmap_entry *me;
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700294
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100295 item = lookup_prefix(map, *email, *emaillen);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700296 if (item) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100297 me = (struct mailmap_entry *)item->util;
298 if (me->namemap.nr) {
Junio C Hamanobd237942013-07-15 02:54:13 -0400299 /*
300 * The item has multiple items, so we'll look up on
301 * name too. If the name is not found, we choose the
302 * simple entry.
303 */
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100304 struct string_list_item *subitem;
305 subitem = lookup_prefix(&me->namemap, *name, *namelen);
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100306 if (subitem)
307 item = subitem;
308 }
309 }
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700310 if (item) {
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100311 struct mailmap_info *mi = (struct mailmap_info *)item->util;
Jeff King910d07a2023-03-17 15:16:26 -0400312 if (mi->name == NULL && mi->email == NULL)
Marius Storm-Olsen0925ce42009-02-08 15:34:29 +0100313 return 0;
Antoine Pelisseea02ffa2013-01-05 22:26:40 +0100314 if (mi->email) {
315 *email = mi->email;
316 *emaillen = strlen(*email);
317 }
318 if (mi->name) {
319 *name = mi->name;
320 *namelen = strlen(*name);
321 }
Junio C Hamano7c1c6782007-04-27 00:41:15 -0700322 return 1;
323 }
324 return 0;
325}