t4203: demonstrate loss of uppercase characters in canonical email
[git/git.git] / mailmap.c
CommitLineData
7c1c6782 1#include "cache.h"
c455c87c 2#include "string-list.h"
fe5d30b6 3#include "mailmap.h"
7c1c6782 4
0925ce4d
MSO
5#define DEBUG_MAILMAP 0
6#if DEBUG_MAILMAP
7#define debug_mm(...) fprintf(stderr, __VA_ARGS__)
8#else
9static inline void debug_mm(const char *format, ...) {}
10#endif
11
d551a488 12const char *git_mailmap_file;
08610900 13const char *git_mailmap_blob;
0925ce4d
MSO
14
15struct mailmap_info {
16 char *name;
17 char *email;
18};
19
20struct mailmap_entry {
21 /* name and email for the simple mail-only case */
22 char *name;
23 char *email;
24
25 /* name and email for the complex mail and name matching case */
26 struct string_list namemap;
27};
28
29static void free_mailmap_info(void *p, const char *s)
30{
31 struct mailmap_info *mi = (struct mailmap_info *)p;
32 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
33 free(mi->name);
34 free(mi->email);
35}
36
37static void free_mailmap_entry(void *p, const char *s)
38{
39 struct mailmap_entry *me = (struct mailmap_entry *)p;
40 debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
41 debug_mm("mailmap: - simple: '%s' <%s>\n", me->name, me->email);
42 free(me->name);
43 free(me->email);
44
45 me->namemap.strdup_strings = 1;
46 string_list_clear_func(&me->namemap, free_mailmap_info);
47}
48
49static void add_mapping(struct string_list *map,
50 char *new_name, char *new_email, char *old_name, char *old_email)
51{
52 struct mailmap_entry *me;
53 int index;
bf637803
JS
54 char *p;
55
56 if (old_email)
57 for (p = old_email; *p; p++)
58 *p = tolower(*p);
59 if (new_email)
60 for (p = new_email; *p; p++)
61 *p = tolower(*p);
62
0925ce4d
MSO
63 if (old_email == NULL) {
64 old_email = new_email;
65 new_email = NULL;
66 }
67
68 if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
69 /* mailmap entry exists, invert index value */
70 index = -1 - index;
71 } else {
72 /* create mailmap entry */
aadceea6 73 struct string_list_item *item = string_list_insert_at_index(map, index, old_email);
74b531f6 74 item->util = xcalloc(1, sizeof(struct mailmap_entry));
0925ce4d
MSO
75 ((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
76 }
77 me = (struct mailmap_entry *)map->items[index].util;
78
79 if (old_name == NULL) {
80 debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
81 /* Replace current name and new email for simple entry */
d8d2eb7d
JM
82 if (new_name) {
83 free(me->name);
0925ce4d 84 me->name = xstrdup(new_name);
d8d2eb7d
JM
85 }
86 if (new_email) {
87 free(me->email);
0925ce4d 88 me->email = xstrdup(new_email);
d8d2eb7d 89 }
0925ce4d 90 } else {
74b531f6 91 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
0925ce4d
MSO
92 debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
93 if (new_name)
94 mi->name = xstrdup(new_name);
95 if (new_email)
96 mi->email = xstrdup(new_email);
78a395d3 97 string_list_insert(&me->namemap, old_name)->util = mi;
0925ce4d
MSO
98 }
99
100 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
101 old_name, old_email, new_name, new_email);
102}
103
5288dd58
BS
104static char *parse_name_and_email(char *buffer, char **name,
105 char **email, int allow_empty_email)
0925ce4d
MSO
106{
107 char *left, *right, *nstart, *nend;
2af202be 108 *name = *email = NULL;
0925ce4d
MSO
109
110 if ((left = strchr(buffer, '<')) == NULL)
111 return NULL;
112 if ((right = strchr(left+1, '>')) == NULL)
113 return NULL;
5288dd58 114 if (!allow_empty_email && (left+1 == right))
0925ce4d
MSO
115 return NULL;
116
117 /* remove whitespace from beginning and end of name */
118 nstart = buffer;
119 while (isspace(*nstart) && nstart < left)
120 ++nstart;
121 nend = left-1;
3174bc5c 122 while (nend > nstart && isspace(*nend))
0925ce4d
MSO
123 --nend;
124
8c381151 125 *name = (nstart <= nend ? nstart : NULL);
0925ce4d
MSO
126 *email = left+1;
127 *(nend+1) = '\0';
128 *right++ = '\0';
129
130 return (*right == '\0' ? NULL : right);
131}
132
7c8ce308
JK
133static void read_mailmap_line(struct string_list *map, char *buffer,
134 char **repo_abbrev)
135{
136 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
137 if (buffer[0] == '#') {
138 static const char abbrev[] = "# repo-abbrev:";
139 int abblen = sizeof(abbrev) - 1;
140 int len = strlen(buffer);
141
142 if (!repo_abbrev)
143 return;
144
145 if (len && buffer[len - 1] == '\n')
146 buffer[--len] = 0;
147 if (!strncmp(buffer, abbrev, abblen)) {
148 char *cp;
149
150 if (repo_abbrev)
151 free(*repo_abbrev);
152 *repo_abbrev = xmalloc(len);
153
154 for (cp = buffer + abblen; isspace(*cp); cp++)
155 ; /* nothing */
156 strcpy(*repo_abbrev, cp);
157 }
158 return;
159 }
160 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
161 parse_name_and_email(name2, &name2, &email2, 1);
162
163 if (email1)
164 add_mapping(map, name1, email1, name2, email2);
165}
166
167static int read_mailmap_file(struct string_list *map, const char *filename,
168 char **repo_abbrev)
7c1c6782
JH
169{
170 char buffer[1024];
938a60d6
JK
171 FILE *f;
172
173 if (!filename)
174 return 0;
175
176 f = fopen(filename, "r");
177 if (!f) {
178 if (errno == ENOENT)
179 return 0;
180 return error("unable to open mailmap at %s: %s",
181 filename, strerror(errno));
182 }
7c1c6782 183
7c8ce308
JK
184 while (fgets(buffer, sizeof(buffer), f) != NULL)
185 read_mailmap_line(map, buffer, repo_abbrev);
7c1c6782
JH
186 fclose(f);
187 return 0;
188}
189
08610900
JK
190static void read_mailmap_buf(struct string_list *map,
191 const char *buf, unsigned long len,
192 char **repo_abbrev)
193{
194 while (len) {
195 const char *end = strchrnul(buf, '\n');
196 unsigned long linelen = end - buf + 1;
197 char *line = xmemdupz(buf, linelen);
198
199 read_mailmap_line(map, line, repo_abbrev);
200
201 free(line);
202 buf += linelen;
203 len -= linelen;
204 }
205}
206
207static int read_mailmap_blob(struct string_list *map,
208 const char *name,
209 char **repo_abbrev)
210{
211 unsigned char sha1[20];
212 char *buf;
213 unsigned long size;
214 enum object_type type;
215
216 if (!name)
938a60d6 217 return 0;
08610900 218 if (get_sha1(name, sha1) < 0)
938a60d6 219 return 0;
08610900
JK
220
221 buf = read_sha1_file(sha1, &type, &size);
222 if (!buf)
938a60d6 223 return error("unable to read mailmap object at %s", name);
08610900 224 if (type != OBJ_BLOB)
938a60d6 225 return error("mailmap is not a blob: %s", name);
08610900
JK
226
227 read_mailmap_buf(map, buf, size, repo_abbrev);
228
229 free(buf);
230 return 0;
231}
232
d551a488
MSO
233int read_mailmap(struct string_list *map, char **repo_abbrev)
234{
938a60d6 235 int err = 0;
8c473cec 236
0925ce4d 237 map->strdup_strings = 1;
388c7f8a 238 map->cmp = strcasecmp;
8c473cec
JK
239
240 if (!git_mailmap_blob && is_bare_repository())
241 git_mailmap_blob = "HEAD:.mailmap";
242
938a60d6
JK
243 err |= read_mailmap_file(map, ".mailmap", repo_abbrev);
244 err |= read_mailmap_blob(map, git_mailmap_blob, repo_abbrev);
245 err |= read_mailmap_file(map, git_mailmap_file, repo_abbrev);
246 return err;
d551a488
MSO
247}
248
0925ce4d
MSO
249void clear_mailmap(struct string_list *map)
250{
251 debug_mm("mailmap: clearing %d entries...\n", map->nr);
252 map->strdup_strings = 1;
253 string_list_clear_func(map, free_mailmap_entry);
254 debug_mm("mailmap: cleared\n");
255}
256
388c7f8a
JH
257/*
258 * Look for an entry in map that match string[0:len]; string[len]
259 * does not have to be NUL (but it could be).
260 */
261static struct string_list_item *lookup_prefix(struct string_list *map,
262 const char *string, size_t len)
263{
264 int i = string_list_find_insert_index(map, string, 1);
265 if (i < 0) {
266 /* exact match */
267 i = -1 - i;
268 if (!string[len])
269 return &map->items[i];
270 /*
271 * that map entry matches exactly to the string, including
272 * the cruft at the end beyond "len". That is not a match
273 * with string[0:len] that we are looking for.
274 */
275 } else if (!string[len]) {
276 /*
277 * asked with the whole string, and got nothing. No
278 * matching entry can exist in the map.
279 */
280 return NULL;
281 }
282
283 /*
284 * i is at the exact match to an overlong key, or location the
285 * overlong key would be inserted, which must come after the
286 * real location of the key if one exists.
287 */
288 while (0 <= --i && i < map->nr) {
289 int cmp = strncasecmp(map->items[i].string, string, len);
290 if (cmp < 0)
291 /*
292 * "i" points at a key definitely below the prefix;
293 * the map does not have string[0:len] in it.
294 */
295 break;
296 else if (!cmp && !map->items[i].string[len])
297 /* found it */
298 return &map->items[i];
299 /*
300 * otherwise, the string at "i" may be string[0:len]
301 * followed by a string that sorts later than string[len:];
302 * keep trying.
303 */
304 }
305 return NULL;
306}
307
0925ce4d 308int map_user(struct string_list *map,
ea02ffa3
AP
309 const char **email, size_t *emaillen,
310 const char **name, size_t *namelen)
7c1c6782 311{
c455c87c 312 struct string_list_item *item;
0925ce4d 313 struct mailmap_entry *me;
388c7f8a 314
ea02ffa3
AP
315 debug_mm("map_user: map '%.*s' <%.*s>\n",
316 *name, *namelen, *emaillen, *email);
388c7f8a 317
ea02ffa3 318 item = lookup_prefix(map, *email, *emaillen);
0925ce4d
MSO
319 if (item != NULL) {
320 me = (struct mailmap_entry *)item->util;
321 if (me->namemap.nr) {
322 /* The item has multiple items, so we'll look up on name too */
323 /* If the name is not found, we choose the simple entry */
ea02ffa3
AP
324 struct string_list_item *subitem;
325 subitem = lookup_prefix(&me->namemap, *name, *namelen);
0925ce4d
MSO
326 if (subitem)
327 item = subitem;
328 }
329 }
7c1c6782 330 if (item != NULL) {
0925ce4d 331 struct mailmap_info *mi = (struct mailmap_info *)item->util;
ea02ffa3 332 if (mi->name == NULL && mi->email == NULL) {
0925ce4d
MSO
333 debug_mm("map_user: -- (no simple mapping)\n");
334 return 0;
335 }
ea02ffa3
AP
336 if (mi->email) {
337 *email = mi->email;
338 *emaillen = strlen(*email);
339 }
340 if (mi->name) {
341 *name = mi->name;
342 *namelen = strlen(*name);
343 }
344 debug_mm("map_user: to '%.*s' <.*%s>\n", *namelen, *name,
345 *emaillen, *email);
7c1c6782
JH
346 return 1;
347 }
0925ce4d 348 debug_mm("map_user: --\n");
7c1c6782
JH
349 return 0;
350}