1#include "cache.h"
2#include "string-list.h"
3#include "mailmap.h"
45
#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
1112
const char *git_mailmap_file;
1314
struct mailmap_info {
15char *name;
16char *email;
17};
1819
struct mailmap_entry {
20/* name and email for the simple mail-only case */
21char *name;
22char *email;
2324
/* name and email for the complex mail and name matching case */
25struct string_list namemap;
26};
2728
static void free_mailmap_info(void *p, const char *s)
29{
30struct mailmap_info *mi = (struct mailmap_info *)p;
31debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
32free(mi->name);
33free(mi->email);
34}
3536
static void free_mailmap_entry(void *p, const char *s)
37{
38struct mailmap_entry *me = (struct mailmap_entry *)p;
39debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
40debug_mm("mailmap: - simple: '%s' <%s>\n", me->name, me->email);
41free(me->name);
42free(me->email);
4344
me->namemap.strdup_strings = 1;
45string_list_clear_func(&me->namemap, free_mailmap_info);
46}
4748
static void add_mapping(struct string_list *map,
49char *new_name, char *new_email, char *old_name, char *old_email)
50{
51struct mailmap_entry *me;
52int index;
53char *p;
5455
if (old_email)
56for (p = old_email; *p; p++)
57*p = tolower(*p);
58if (new_email)
59for (p = new_email; *p; p++)
60*p = tolower(*p);
6162
if (old_email == NULL) {
63old_email = new_email;
64new_email = NULL;
65}
6667
if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
68/* mailmap entry exists, invert index value */
69index = -1 - index;
70} else {
71/* create mailmap entry */
72struct string_list_item *item = string_list_insert_at_index(index, old_email, map);
73item->util = xmalloc(sizeof(struct mailmap_entry));
74memset(item->util, 0, sizeof(struct mailmap_entry));
75((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
76}
77me = (struct mailmap_entry *)map->items[index].util;
7879
if (old_name == NULL) {
80debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
81/* Replace current name and new email for simple entry */
82free(me->name);
83free(me->email);
84if (new_name)
85me->name = xstrdup(new_name);
86if (new_email)
87me->email = xstrdup(new_email);
88} else {
89struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
90debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
91if (new_name)
92mi->name = xstrdup(new_name);
93if (new_email)
94mi->email = xstrdup(new_email);
95string_list_insert(old_name, &me->namemap)->util = mi;
96}
9798
debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
99old_name, old_email, new_name, new_email);
100}
101102
static char *parse_name_and_email(char *buffer, char **name,
103char **email, int allow_empty_email)
104{
105char *left, *right, *nstart, *nend;
106*name = *email = NULL;
107108
if ((left = strchr(buffer, '<')) == NULL)
109return NULL;
110if ((right = strchr(left+1, '>')) == NULL)
111return NULL;
112if (!allow_empty_email && (left+1 == right))
113return NULL;
114115
/* remove whitespace from beginning and end of name */
116nstart = buffer;
117while (isspace(*nstart) && nstart < left)
118++nstart;
119nend = left-1;
120while (isspace(*nend) && nend > nstart)
121--nend;
122123
*name = (nstart < nend ? nstart : NULL);
124*email = left+1;
125*(nend+1) = '\0';
126*right++ = '\0';
127128
return (*right == '\0' ? NULL : right);
129}
130131
static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
132{
133char buffer[1024];
134FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
135136
if (f == NULL)
137return 1;
138while (fgets(buffer, sizeof(buffer), f) != NULL) {
139char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
140if (buffer[0] == '#') {
141static const char abbrev[] = "# repo-abbrev:";
142int abblen = sizeof(abbrev) - 1;
143int len = strlen(buffer);
144145
if (!repo_abbrev)
146continue;
147148
if (len && buffer[len - 1] == '\n')
149buffer[--len] = 0;
150if (!strncmp(buffer, abbrev, abblen)) {
151char *cp;
152153
if (repo_abbrev)
154free(*repo_abbrev);
155*repo_abbrev = xmalloc(len);
156157
for (cp = buffer + abblen; isspace(*cp); cp++)
158; /* nothing */
159strcpy(*repo_abbrev, cp);
160}
161continue;
162}
163if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
164parse_name_and_email(name2, &name2, &email2, 1);
165166
if (email1)
167add_mapping(map, name1, email1, name2, email2);
168}
169fclose(f);
170return 0;
171}
172173
int read_mailmap(struct string_list *map, char **repo_abbrev)
174{
175map->strdup_strings = 1;
176/* each failure returns 1, so >1 means both calls failed */
177return read_single_mailmap(map, ".mailmap", repo_abbrev) +
178read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
179}
180181
void clear_mailmap(struct string_list *map)
182{
183debug_mm("mailmap: clearing %d entries...\n", map->nr);
184map->strdup_strings = 1;
185string_list_clear_func(map, free_mailmap_entry);
186debug_mm("mailmap: cleared\n");
187}
188189
int map_user(struct string_list *map,
190char *email, int maxlen_email, char *name, int maxlen_name)
191{
192char *p;
193struct string_list_item *item;
194struct mailmap_entry *me;
195char buf[1024], *mailbuf;
196int i;
197198
/* figure out space requirement for email */
199p = strchr(email, '>');
200if (!p) {
201/* email passed in might not be wrapped in <>, but end with a \0 */
202p = memchr(email, '\0', maxlen_email);
203if (!p)
204return 0;
205}
206if (p - email + 1 < sizeof(buf))
207mailbuf = buf;
208else
209mailbuf = xmalloc(p - email + 1);
210211
/* downcase the email address */
212for (i = 0; i < p - email; i++)
213mailbuf[i] = tolower(email[i]);
214mailbuf[i] = 0;
215216
debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
217item = string_list_lookup(mailbuf, map);
218if (item != NULL) {
219me = (struct mailmap_entry *)item->util;
220if (me->namemap.nr) {
221/* The item has multiple items, so we'll look up on name too */
222/* If the name is not found, we choose the simple entry */
223struct string_list_item *subitem = string_list_lookup(name, &me->namemap);
224if (subitem)
225item = subitem;
226}
227}
228if (mailbuf != buf)
229free(mailbuf);
230if (item != NULL) {
231struct mailmap_info *mi = (struct mailmap_info *)item->util;
232if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
233debug_mm("map_user: -- (no simple mapping)\n");
234return 0;
235}
236if (maxlen_email && mi->email)
237strlcpy(email, mi->email, maxlen_email);
238if (maxlen_name && mi->name)
239strlcpy(name, mi->name, maxlen_name);
240debug_mm("map_user: to '%s' <%s>\n", name, mi->email ? mi->email : "");
241return 1;
242}
243debug_mm("map_user: --\n");
244return 0;
245}
246247
int map_email(struct string_list *map, const char *email, char *name, int maxlen)
248{
249return map_user(map, (char *)email, 0, name, maxlen);
250}