mailmap: remove email copy and length limitation
[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;
0925ce4d
MSO
13
14struct mailmap_info {
15 char *name;
16 char *email;
17};
18
19struct mailmap_entry {
20 /* name and email for the simple mail-only case */
21 char *name;
22 char *email;
23
24 /* name and email for the complex mail and name matching case */
25 struct string_list namemap;
26};
27
28static void free_mailmap_info(void *p, const char *s)
29{
30 struct mailmap_info *mi = (struct mailmap_info *)p;
31 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
32 free(mi->name);
33 free(mi->email);
34}
35
36static void free_mailmap_entry(void *p, const char *s)
37{
38 struct mailmap_entry *me = (struct mailmap_entry *)p;
39 debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
40 debug_mm("mailmap: - simple: '%s' <%s>\n", me->name, me->email);
41 free(me->name);
42 free(me->email);
43
44 me->namemap.strdup_strings = 1;
45 string_list_clear_func(&me->namemap, free_mailmap_info);
46}
47
48static void add_mapping(struct string_list *map,
49 char *new_name, char *new_email, char *old_name, char *old_email)
50{
51 struct mailmap_entry *me;
52 int index;
bf637803
JS
53 char *p;
54
55 if (old_email)
56 for (p = old_email; *p; p++)
57 *p = tolower(*p);
58 if (new_email)
59 for (p = new_email; *p; p++)
60 *p = tolower(*p);
61
0925ce4d
MSO
62 if (old_email == NULL) {
63 old_email = new_email;
64 new_email = NULL;
65 }
66
67 if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
68 /* mailmap entry exists, invert index value */
69 index = -1 - index;
70 } else {
71 /* create mailmap entry */
aadceea6 72 struct string_list_item *item = string_list_insert_at_index(map, index, old_email);
74b531f6 73 item->util = xcalloc(1, sizeof(struct mailmap_entry));
0925ce4d
MSO
74 ((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
75 }
76 me = (struct mailmap_entry *)map->items[index].util;
77
78 if (old_name == NULL) {
79 debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
80 /* Replace current name and new email for simple entry */
d8d2eb7d
JM
81 if (new_name) {
82 free(me->name);
0925ce4d 83 me->name = xstrdup(new_name);
d8d2eb7d
JM
84 }
85 if (new_email) {
86 free(me->email);
0925ce4d 87 me->email = xstrdup(new_email);
d8d2eb7d 88 }
0925ce4d 89 } else {
74b531f6 90 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
0925ce4d
MSO
91 debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
92 if (new_name)
93 mi->name = xstrdup(new_name);
94 if (new_email)
95 mi->email = xstrdup(new_email);
78a395d3 96 string_list_insert(&me->namemap, old_name)->util = mi;
0925ce4d
MSO
97 }
98
99 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
100 old_name, old_email, new_name, new_email);
101}
102
5288dd58
BS
103static char *parse_name_and_email(char *buffer, char **name,
104 char **email, int allow_empty_email)
0925ce4d
MSO
105{
106 char *left, *right, *nstart, *nend;
2af202be 107 *name = *email = NULL;
0925ce4d
MSO
108
109 if ((left = strchr(buffer, '<')) == NULL)
110 return NULL;
111 if ((right = strchr(left+1, '>')) == NULL)
112 return NULL;
5288dd58 113 if (!allow_empty_email && (left+1 == right))
0925ce4d
MSO
114 return NULL;
115
116 /* remove whitespace from beginning and end of name */
117 nstart = buffer;
118 while (isspace(*nstart) && nstart < left)
119 ++nstart;
120 nend = left-1;
3174bc5c 121 while (nend > nstart && isspace(*nend))
0925ce4d
MSO
122 --nend;
123
124 *name = (nstart < nend ? nstart : NULL);
125 *email = left+1;
126 *(nend+1) = '\0';
127 *right++ = '\0';
128
129 return (*right == '\0' ? NULL : right);
130}
131
d551a488 132static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
7c1c6782
JH
133{
134 char buffer[1024];
d551a488 135 FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
7c1c6782
JH
136
137 if (f == NULL)
138 return 1;
139 while (fgets(buffer, sizeof(buffer), f) != NULL) {
2af202be 140 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
7c1c6782
JH
141 if (buffer[0] == '#') {
142 static const char abbrev[] = "# repo-abbrev:";
143 int abblen = sizeof(abbrev) - 1;
144 int len = strlen(buffer);
145
8503ee43
AR
146 if (!repo_abbrev)
147 continue;
148
7c1c6782
JH
149 if (len && buffer[len - 1] == '\n')
150 buffer[--len] = 0;
151 if (!strncmp(buffer, abbrev, abblen)) {
152 char *cp;
153
154 if (repo_abbrev)
155 free(*repo_abbrev);
156 *repo_abbrev = xmalloc(len);
157
158 for (cp = buffer + abblen; isspace(*cp); cp++)
159 ; /* nothing */
160 strcpy(*repo_abbrev, cp);
161 }
162 continue;
163 }
5288dd58
BS
164 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
165 parse_name_and_email(name2, &name2, &email2, 1);
0925ce4d
MSO
166
167 if (email1)
168 add_mapping(map, name1, email1, name2, email2);
7c1c6782
JH
169 }
170 fclose(f);
171 return 0;
172}
173
d551a488
MSO
174int read_mailmap(struct string_list *map, char **repo_abbrev)
175{
0925ce4d 176 map->strdup_strings = 1;
388c7f8a 177 map->cmp = strcasecmp;
d551a488
MSO
178 /* each failure returns 1, so >1 means both calls failed */
179 return read_single_mailmap(map, ".mailmap", repo_abbrev) +
180 read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
181}
182
0925ce4d
MSO
183void clear_mailmap(struct string_list *map)
184{
185 debug_mm("mailmap: clearing %d entries...\n", map->nr);
186 map->strdup_strings = 1;
187 string_list_clear_func(map, free_mailmap_entry);
188 debug_mm("mailmap: cleared\n");
189}
190
388c7f8a
JH
191/*
192 * Look for an entry in map that match string[0:len]; string[len]
193 * does not have to be NUL (but it could be).
194 */
195static struct string_list_item *lookup_prefix(struct string_list *map,
196 const char *string, size_t len)
197{
198 int i = string_list_find_insert_index(map, string, 1);
199 if (i < 0) {
200 /* exact match */
201 i = -1 - i;
202 if (!string[len])
203 return &map->items[i];
204 /*
205 * that map entry matches exactly to the string, including
206 * the cruft at the end beyond "len". That is not a match
207 * with string[0:len] that we are looking for.
208 */
209 } else if (!string[len]) {
210 /*
211 * asked with the whole string, and got nothing. No
212 * matching entry can exist in the map.
213 */
214 return NULL;
215 }
216
217 /*
218 * i is at the exact match to an overlong key, or location the
219 * overlong key would be inserted, which must come after the
220 * real location of the key if one exists.
221 */
222 while (0 <= --i && i < map->nr) {
223 int cmp = strncasecmp(map->items[i].string, string, len);
224 if (cmp < 0)
225 /*
226 * "i" points at a key definitely below the prefix;
227 * the map does not have string[0:len] in it.
228 */
229 break;
230 else if (!cmp && !map->items[i].string[len])
231 /* found it */
232 return &map->items[i];
233 /*
234 * otherwise, the string at "i" may be string[0:len]
235 * followed by a string that sorts later than string[len:];
236 * keep trying.
237 */
238 }
239 return NULL;
240}
241
0925ce4d
MSO
242int map_user(struct string_list *map,
243 char *email, int maxlen_email, char *name, int maxlen_name)
7c1c6782 244{
f026358e 245 char *end_of_email;
c455c87c 246 struct string_list_item *item;
0925ce4d 247 struct mailmap_entry *me;
388c7f8a 248 size_t maillen;
7c1c6782 249
0925ce4d 250 /* figure out space requirement for email */
f026358e
JH
251 end_of_email = strchr(email, '>');
252 if (!end_of_email) {
0925ce4d 253 /* email passed in might not be wrapped in <>, but end with a \0 */
f026358e
JH
254 end_of_email = memchr(email, '\0', maxlen_email);
255 if (!end_of_email)
0925ce4d
MSO
256 return 0;
257 }
388c7f8a
JH
258
259 maillen = end_of_email - email;
260
261 debug_mm("map_user: map '%s' <%.*s>\n", name, maillen, email);
262
263 item = lookup_prefix(map, email, maillen);
0925ce4d
MSO
264 if (item != NULL) {
265 me = (struct mailmap_entry *)item->util;
266 if (me->namemap.nr) {
267 /* The item has multiple items, so we'll look up on name too */
268 /* If the name is not found, we choose the simple entry */
e8c8b713 269 struct string_list_item *subitem = string_list_lookup(&me->namemap, name);
0925ce4d
MSO
270 if (subitem)
271 item = subitem;
272 }
273 }
7c1c6782 274 if (item != NULL) {
0925ce4d
MSO
275 struct mailmap_info *mi = (struct mailmap_info *)item->util;
276 if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
277 debug_mm("map_user: -- (no simple mapping)\n");
278 return 0;
279 }
280 if (maxlen_email && mi->email)
281 strlcpy(email, mi->email, maxlen_email);
f026358e
JH
282 else
283 *end_of_email = '\0';
0925ce4d
MSO
284 if (maxlen_name && mi->name)
285 strlcpy(name, mi->name, maxlen_name);
286 debug_mm("map_user: to '%s' <%s>\n", name, mi->email ? mi->email : "");
7c1c6782
JH
287 return 1;
288 }
0925ce4d 289 debug_mm("map_user: --\n");
7c1c6782
JH
290 return 0;
291}